Skip to content

Commit

Permalink
fix(amdTranspiler): Ensure unique class names
Browse files Browse the repository at this point in the history
If the determined class name is equal to name of the variable used in
the extends call, later analysis of the same will fail.

This change attempts to ensure a unique name for the generated class so
that it can be analyzed properly.

Resolves #184
  • Loading branch information
RandomByte committed Jun 28, 2024
1 parent eacea1e commit b70698c
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/linter/ui5Types/amdTranspiler/rewriteExtendCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function rewriteExtendCall(nodeFactory: ts.NodeFactory,
throw new UnsupportedExtendCall(`Not a UI5 Class#extends call ${toPosStr(callExp.expression)}`);
}

const className = getClassNameFromArguments(callExp.arguments);
const className = nodeFactory.createUniqueName(getClassNameFromArguments(callExp.arguments));
const body = getClassBodyFromArguments(nodeFactory, callExp.arguments);
return nodeFactory.createClassDeclaration(modifiers,
className,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Inheriting from sap/fe/core/AppComponent (implements IAsyncContentCreation interface), no redundant async flags in manifest
sap.ui.define(["sap/fe/core/AppComponent"], function (AppComponent) {
sap.ui.define(["sap/fe/core/AppComponent"], function (Component) {
"use strict";

return AppComponent.extend("mycomp.Component", {
return Component.extend("mycomp.Component", {
metadata: {
manifest: "json",
},
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/transpiler/amd/NameConflict.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sap.ui.define(["sap/ui/core/UIComponent", `sap/ui/Device`], function (UIComponent) {
"use strict";

return UIComponent.extend("com.ui5.troublesome.app.UIComponent", {});
});
79 changes: 51 additions & 28 deletions test/lib/linter/amdTranspiler/snapshots/transpiler.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ Generated by [AVA](https://avajs.dev).
`import UIComponent from "sap/ui/core/UIComponent";␊
import Device from "sap/ui/Device";␊
export default class Component extends UIComponent {␊
export default class Component_1 extends UIComponent {␊
}␊
//# sourceMappingURL=Dependencies.js.map`

> Snapshot 2
{
file: 'Dependencies.js',
mappings: 'OAAsE,WAAW,MAAlE,yBAAyB;OAA2C,MAAM,MAA/C;uCAGlC,WAAW',
mappings: 'OAAsE,WAAW,MAAlE,yBAAyB;OAA2C,MAAM,MAA/C;yCAGlC,WAAW',
names: [],
sourceRoot: '',
sources: [
Expand Down Expand Up @@ -60,15 +60,15 @@ Generated by [AVA](https://avajs.dev).
`import UIComponent from "sap/ui/core/UIComponent";␊
import Device from "sap/ui/Device";␊
import models from "./model/models";␊
export default class Component extends UIComponent {␊
export default class Component_1 extends UIComponent {␊
}␊
//# sourceMappingURL=Dependencies_LocalImport.js.map`

> Snapshot 2
{
file: 'Dependencies_LocalImport.js',
mappings: 'OAAwF,WAAW,MAApF,yBAAyB;OAA6D,MAAM,MAAjE,eAAe;OAAoD,MAAM,MAAxD,gBAAgB;uCAGnE,WAAW',
mappings: 'OAAwF,WAAW,MAApF,yBAAyB;OAA6D,MAAM,MAAjE,eAAe;OAAoD,MAAM,MAAxD,gBAAgB;yCAGnE,WAAW',
names: [],
sourceRoot: '',
sources: [
Expand Down Expand Up @@ -108,15 +108,15 @@ Generated by [AVA](https://avajs.dev).
`import UIComponent from "sap/ui/core/UIComponent";␊
import Device from "sap/ui/Device";␊
import models from "./model/models";␊
export default class Component extends UIComponent {␊
export default class Component_1 extends UIComponent {␊
}␊
//# sourceMappingURL=Dependencies_Var.js.map`

> Snapshot 2
{
file: 'Dependencies_Var.js',
mappings: 'OAC8B,WAAW,MAD3B,yBAAyB;OACI,MAAM,MADR,eAAe;OACL,MAAM,MADC,gBAAgB;uCAIlE,WAAW',
mappings: 'OAC8B,WAAW,MAD3B,yBAAyB;OACI,MAAM,MADR,eAAe;OACL,MAAM,MADC,gBAAgB;yCAIlE,WAAW',
names: [],
sourceRoot: '',
sources: [
Expand All @@ -130,15 +130,15 @@ Generated by [AVA](https://avajs.dev).
> Snapshot 1
`import ControllerExtension from "sap/ui/core/mvc/ControllerExtension";␊
export default class ReuseExtension extends ControllerExtension {␊
export default class ReuseExtension_1 extends ControllerExtension {␊
}␊
//# sourceMappingURL=ExportFlag.js.map`

> Snapshot 2
{
file: 'ExportFlag.js',
mappings: 'OAAgE,mBAAmB,MAApE,qCAAqC;4CAC5C,mBAAmB',
mappings: 'OAAgE,mBAAmB,MAApE,qCAAqC;8CAC5C,mBAAmB',
names: [],
sourceRoot: '',
sources: [
Expand Down Expand Up @@ -183,15 +183,15 @@ Generated by [AVA](https://avajs.dev).
`import Controller from "sap/ui/core/mvc/Controller";␊
import "sap/ui/core/UIComponent";␊
import "sap/ui/core/routing/History";␊
export default class MyController extends Controller {␊
export default class MyController_1 extends Controller {␊
}␊
//# sourceMappingURL=Factory_ArrowFunction.js.map`

> Snapshot 2
{
file: 'Factory_ArrowFunction.js',
mappings: 'OAAiB,UAAU,MAMzB,4BAA4B;OAC5B,yBAAyB;OACzB,6BAA6B;0CAPvB,UAAU',
mappings: 'OAAiB,UAAU,MAMzB,4BAA4B;OAC5B,yBAAyB;OACzB,6BAA6B;4CAPvB,UAAU',
names: [],
sourceRoot: '',
sources: [
Expand All @@ -205,15 +205,15 @@ Generated by [AVA](https://avajs.dev).
> Snapshot 1
`import Controller from "sap/ui/core/mvc/Controller";␊
export default class MyController extends Controller {␊
export default class MyController_1 extends Controller {␊
}␊
//# sourceMappingURL=Factory_ArrowFunctionExpr.js.map`

> Snapshot 2
{
file: 'Factory_ArrowFunctionExpr.js',
mappings: 'OAA+C,UAAU,MAA1C,4BAA4B;0CAAoB,UAAU',
mappings: 'OAA+C,UAAU,MAA1C,4BAA4B;4CAAoB,UAAU',
names: [],
sourceRoot: '',
sources: [
Expand All @@ -230,7 +230,7 @@ Generated by [AVA](https://avajs.dev).
import UIComponent from "sap/ui/core/UIComponent";␊
import History from "sap/ui/core/routing/History";␊
import Button from "sap/m/Button";␊
export default class BaseController extends Controller {␊
export default class BaseController_1 extends Controller {␊
createButton() {␊
var btn = new Button({␊
blocked: true␊
Expand All @@ -247,7 +247,7 @@ Generated by [AVA](https://avajs.dev).
{
file: 'Factory_BasicModule.js',
mappings: 'OACW,UAAU,MADN,4BAA4B;OACpB,WAAW,MADW,yBAAyB;OAClC,OAAO,MAD6B,6BAA6B;OACxD,MAAM,MADoD,cAAc;4CAI7G,UAAU;IAEhB,YAAY;QACX,IAAI,GAAG,GAAG,IAAI,MAAM,CAAC;YACpB,OAAO,EAAE,IAAI;SACb,CAAC,CAAC;QACH,GAAG,CAAC,SAAS,CAAC;YACb,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QACvB,CAAC,CAAC,CAAC;QACH,OAAO,GAAG,CAAC;IACZ,CAAC',
mappings: 'OACW,UAAU,MADN,4BAA4B;OACpB,WAAW,MADW,yBAAyB;OAClC,OAAO,MAD6B,6BAA6B;OACxD,MAAM,MADoD,cAAc;8CAI7G,UAAU;IAEhB,YAAY;QACX,IAAI,GAAG,GAAG,IAAI,MAAM,CAAC;YACpB,OAAO,EAAE,IAAI;SACb,CAAC,CAAC;QACH,GAAG,CAAC,SAAS,CAAC;YACb,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QACvB,CAAC,CAAC,CAAC;QACH,OAAO,GAAG,CAAC;IACZ,CAAC',
names: [],
sourceRoot: '',
sources: [
Expand Down Expand Up @@ -325,7 +325,7 @@ Generated by [AVA](https://avajs.dev).
import Button from "sap/m/Button";␊
const someValue1 = "foo";␊
const someValue2 = new Button();␊
export default class BaseController extends Controller {␊
export default class BaseController_1 extends Controller {␊
static metadata = {␊
interfaces: [␊
library.IAsyncContentCreation␊
Expand Down Expand Up @@ -356,7 +356,7 @@ Generated by [AVA](https://avajs.dev).
{
file: 'Factory_ExtendCall.js',
mappings: 'OACW,UAAU,MADN,4BAA4B;OACpB,WAAW,MADW,yBAAyB;OAClC,OAAO,MAD6B,6BAA6B;OACxD,MAAM,MADoD,cAAc;AAGpH,MAAM,UAAU,GA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KAAK,GAAE,IAAI,MAAM,EAAE;IACnB,KAAK,GAAE,OAAO,CAAC,WAAW,EAAE;IAC5B,KAAK,GAAE,QAAQ,CAAC,KAAK,CAAC;IACtB,KAAK,GAAE,OAAO,CAAC,cAAc,CAAC;IAC9B,KAAK,GAAE,UAAU;IACjB,KAAK,GAAE,GAAG,UAAU,EAAE;IACtB,MAAM,GAAE,UAAU;IAClB,OAAO,CAAC,KAAK;QACZ,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IACpB,CAAC',
names: [],
sourceRoot: '',
sources: [
Expand All @@ -372,15 +372,15 @@ Generated by [AVA](https://avajs.dev).
`import Controller from "sap/ui/core/mvc/Controller";␊
import "sap/ui/core/UIComponent";␊
import "sap/ui/core/routing/History";␊
export default class MyController extends Controller {␊
export default class MyController_1 extends Controller {␊
}␊
//# sourceMappingURL=Factory_FunctionVarDeclaration.js.map`

> Snapshot 2
{
file: 'Factory_FunctionVarDeclaration.js',
mappings: 'OAA0B,UAAU,MAMlC,4BAA4B;OAC5B,yBAAyB;OACzB,6BAA6B;0CAPvB,UAAU',
mappings: 'OAA0B,UAAU,MAMlC,4BAA4B;OAC5B,yBAAyB;OACzB,6BAA6B;4CAPvB,UAAU',
names: [],
sourceRoot: '',
sources: [
Expand All @@ -396,15 +396,15 @@ Generated by [AVA](https://avajs.dev).
`import Controller from "sap/ui/core/mvc/Controller";␊
import "sap/ui/core/UIComponent";␊
import "sap/ui/core/routing/History";␊
export default class MyController extends Controller {␊
export default class MyController_1 extends Controller {␊
}␊
//# sourceMappingURL=Factory_FunctionVarExpression.js.map`

> Snapshot 2
{
file: 'Factory_FunctionVarExpression.js',
mappings: 'OAAiB,UAAU,MAMzB,4BAA4B;OAC5B,yBAAyB;OACzB,6BAA6B;0CAPvB,UAAU',
mappings: 'OAAiB,UAAU,MAMzB,4BAA4B;OAC5B,yBAAyB;OACzB,6BAA6B;4CAPvB,UAAU',
names: [],
sourceRoot: '',
sources: [
Expand Down Expand Up @@ -531,15 +531,15 @@ Generated by [AVA](https://avajs.dev).
`import Controller from "sap/ui/core/mvc/Controller";␊
import "sap/ui/core/UIComponent";␊
import "sap/ui/core/routing/History";␊
export default class MyController extends Controller {␊
export default class MyController_1 extends Controller {␊
}␊
//# sourceMappingURL=Factory_cond_iife.js.map`

> Snapshot 2
{
file: 'Factory_cond_iife.js',
mappings: 'OAWa,UAAU,MAPnB,4BAA4B;OAC5B,yBAAyB;OACzB,6BAA6B;0CAMzB,UAAU',
mappings: 'OAWa,UAAU,MAPnB,4BAA4B;OAC5B,yBAAyB;OACzB,6BAA6B;4CAMzB,UAAU',
names: [],
sourceRoot: '',
sources: [
Expand All @@ -555,15 +555,15 @@ Generated by [AVA](https://avajs.dev).
`import Controller from "sap/ui/core/mvc/Controller";␊
import "sap/ui/core/UIComponent";␊
import "sap/ui/core/routing/History";␊
export default class MyController extends Controller {␊
export default class MyController_1 extends Controller {␊
}␊
//# sourceMappingURL=Factory_cond_iife_2.js.map`

> Snapshot 2
{
file: 'Factory_cond_iife_2.js',
mappings: 'OAWa,UAAU,MAPnB,4BAA4B;OAC5B,yBAAyB;OACzB,6BAA6B;0CAMzB,UAAU',
mappings: 'OAWa,UAAU,MAPnB,4BAA4B;OAC5B,yBAAyB;OACzB,6BAA6B;4CAMzB,UAAU',
names: [],
sourceRoot: '',
sources: [
Expand All @@ -579,15 +579,15 @@ Generated by [AVA](https://avajs.dev).
`import Controller from "sap/ui/core/mvc/Controller";␊
import "sap/ui/core/UIComponent";␊
import "sap/ui/core/routing/History";␊
export default class MyController extends Controller {␊
export default class MyController_1 extends Controller {␊
}␊
//# sourceMappingURL=Factory_hoisted_function.js.map`

> Snapshot 2
{
file: 'Factory_hoisted_function.js',
mappings: 'OASiB,UAAU,MAPzB,4BAA4B;OAC5B,yBAAyB;OACzB,6BAA6B;0CAMvB,UAAU',
mappings: 'OASiB,UAAU,MAPzB,4BAA4B;OAC5B,yBAAyB;OACzB,6BAA6B;4CAMvB,UAAU',
names: [],
sourceRoot: '',
sources: [
Expand All @@ -603,15 +603,15 @@ Generated by [AVA](https://avajs.dev).
`import Controller from "sap/ui/core/mvc/Controller";␊
import "sap/ui/core/UIComponent";␊
import "sap/ui/core/routing/History";␊
export default class MyController extends Controller {␊
export default class MyController_1 extends Controller {␊
}␊
//# sourceMappingURL=Factory_iife.js.map`

> Snapshot 2
{
file: 'Factory_iife.js',
mappings: 'OASa,UAAU,MANpB,4BAA4B;OAC5B,yBAAyB;OACzB,6BAA6B;0CAKxB,UAAU',
mappings: 'OASa,UAAU,MANpB,4BAA4B;OAC5B,yBAAyB;OACzB,6BAA6B;4CAKxB,UAAU',
names: [],
sourceRoot: '',
sources: [
Expand Down Expand Up @@ -711,6 +711,29 @@ Generated by [AVA](https://avajs.dev).
version: 3,
}

## Transpile NameConflict.js

> Snapshot 1
`import UIComponent from "sap/ui/core/UIComponent";␊
import "sap/ui/Device";␊
export default class UIComponent_1 extends UIComponent {␊
}␊
//# sourceMappingURL=NameConflict.js.map`

> Snapshot 2
{
file: 'NameConflict.js',
mappings: 'OAAsE,WAAW,MAAlE,yBAAyB;OAAE;2CAGlC,WAAW',
names: [],
sourceRoot: '',
sources: [
'NameConflict.js',
],
version: 3,
}

## Transpile Noop_DynamicImport.js

> Snapshot 1
Expand Down
Binary file modified test/lib/linter/amdTranspiler/snapshots/transpiler.ts.snap
Binary file not shown.

0 comments on commit b70698c

Please sign in to comment.