diff --git a/src/linter/ui5Types/amdTranspiler/moduleDeclarationToDefinition.ts b/src/linter/ui5Types/amdTranspiler/moduleDeclarationToDefinition.ts index 9f1fed33..836ff4fd 100644 --- a/src/linter/ui5Types/amdTranspiler/moduleDeclarationToDefinition.ts +++ b/src/linter/ui5Types/amdTranspiler/moduleDeclarationToDefinition.ts @@ -225,6 +225,12 @@ function getModuleBody( nodeFactory.createToken(ts.SyntaxKind.DefaultKeyword), ]); body = [classDeclaration]; + + if (ts.isArrowFunction(moduleDeclaration.factory)) { + // TODO: Comment in equalsGreaterThanToken is trailing, but it should be + // moved to classDeclaration as leading comment + moveComments.push([moduleDeclaration.factory.equalsGreaterThanToken, classDeclaration]); + } } catch (err) { if (err instanceof UnsupportedExtendCall) { log.verbose(`Failed to transform extend call: ${err.message}`); @@ -235,15 +241,20 @@ function getModuleBody( } } else { // Export expression directly - body = [createDefaultExport(nodeFactory, moduleDeclaration.factory.body)]; + const defaultExport = createDefaultExport(nodeFactory, moduleDeclaration.factory.body); + body = [defaultExport]; + moveComments.push([moduleDeclaration.factory.body, defaultExport]); } } else if (ts.isClassDeclaration(moduleDeclaration.factory) || ts.isLiteralExpression(moduleDeclaration.factory) || ts.isArrayLiteralExpression(moduleDeclaration.factory) || ts.isObjectLiteralExpression(moduleDeclaration.factory) || - ts.isPropertyAccessExpression(moduleDeclaration.factory)) { + ts.isPropertyAccessExpression(moduleDeclaration.factory) || + ts.isIdentifier(moduleDeclaration.factory)) { // Use factory directly - body = [createDefaultExport(nodeFactory, moduleDeclaration.factory)]; + const defaultExport = createDefaultExport(nodeFactory, moduleDeclaration.factory); + body = [defaultExport]; + moveComments.push([moduleDeclaration.factory, defaultExport]); } else { // Identifier throw new Error(`FIXME: Unsupported factory type ${ts.SyntaxKind[moduleDeclaration.factory.kind]} at ` + toPosStr(moduleDeclaration.factory)); diff --git a/src/linter/ui5Types/amdTranspiler/parseModuleDeclaration.ts b/src/linter/ui5Types/amdTranspiler/parseModuleDeclaration.ts index 5ed36e7a..84900aab 100644 --- a/src/linter/ui5Types/amdTranspiler/parseModuleDeclaration.ts +++ b/src/linter/ui5Types/amdTranspiler/parseModuleDeclaration.ts @@ -46,8 +46,13 @@ export default function parseModuleDeclaration( const sym = checker.getSymbolAtLocation(arg); if (sym?.declarations) { for (const decl of sym.declarations) { - if (ts.isVariableDeclaration(decl)) { - if (decl.initializer) { + if ( + ts.isVariableDeclaration(decl) && decl.initializer + ) { + // Do not use the initializer of a let declaration, as the value might be changed later + if (ts.isVariableDeclarationList(decl.parent) && decl.parent.flags & ts.NodeFlags.Let) { + return arg; + } else { return decl.initializer; } } else if (ts.isParameter(decl)) { @@ -99,6 +104,7 @@ function assertSupportedTypes(args: (ts.Expression | ts.Declaration)[]): DefineC case SyntaxKind.ClassDeclaration: case SyntaxKind.NoSubstitutionTemplateLiteral: case SyntaxKind.PropertyAccessExpression: + case SyntaxKind.Identifier: return; default: throw new UnsupportedModuleError( diff --git a/test/fixtures/transpiler/amd/Comments.js b/test/fixtures/transpiler/amd/Comments.js new file mode 100644 index 00000000..57b5f919 --- /dev/null +++ b/test/fixtures/transpiler/amd/Comments.js @@ -0,0 +1,5 @@ +// This comment should be at the beginning of the file after transpiling +sap.ui.define(["sap/ui/core/Control"], function(Control) { + // This comment should be after the Control import statement after transpiling +}); +// This comment should be at the end of the file after transpiling diff --git a/test/fixtures/transpiler/amd/Factory_ArrowFunction.js b/test/fixtures/transpiler/amd/Factory_ArrowFunction.js index 2dd8e795..459913f3 100644 --- a/test/fixtures/transpiler/amd/Factory_ArrowFunction.js +++ b/test/fixtures/transpiler/amd/Factory_ArrowFunction.js @@ -1,5 +1,7 @@ const factory = (Controller) => { + // This comment should be above the "class" statement of MyController after transpiling return Controller.extend("MyController", {}); + // This comment should be below the "class" statement of MyController after transpiling }; sap.ui.define( diff --git a/test/fixtures/transpiler/amd/Factory_ArrowFunctionExpr.js b/test/fixtures/transpiler/amd/Factory_ArrowFunctionExpr.js index 7b8a92d0..d5855406 100644 --- a/test/fixtures/transpiler/amd/Factory_ArrowFunctionExpr.js +++ b/test/fixtures/transpiler/amd/Factory_ArrowFunctionExpr.js @@ -1 +1,2 @@ -sap.ui.define(["sap/ui/core/mvc/Controller"], (Controller) => Controller.extend("MyController", {})); +sap.ui.define(["sap/ui/core/mvc/Controller"], (Controller) => /* This comment should be above the "class" statement of MyController after transpiling */ Controller.extend("MyController", {})); +/* This comment should be below the "class" statement of MyController after transpiling */ diff --git a/test/fixtures/transpiler/amd/Factory_Identifier.js b/test/fixtures/transpiler/amd/Factory_Identifier.js new file mode 100644 index 00000000..efe60c58 --- /dev/null +++ b/test/fixtures/transpiler/amd/Factory_Identifier.js @@ -0,0 +1,5 @@ +let foo = "a"; +if (something) { + foo = "b" +} +sap.ui.define([], foo); diff --git a/test/fixtures/transpiler/amd/Factory_Literal.js b/test/fixtures/transpiler/amd/Factory_Literal.js new file mode 100644 index 00000000..4dbb7cd6 --- /dev/null +++ b/test/fixtures/transpiler/amd/Factory_Literal.js @@ -0,0 +1 @@ +sap.ui.define([], 123); diff --git a/test/lib/linter/amdTranspiler/snapshots/transpiler.ts.md b/test/lib/linter/amdTranspiler/snapshots/transpiler.ts.md index 0d840743..4dca240d 100644 --- a/test/lib/linter/amdTranspiler/snapshots/transpiler.ts.md +++ b/test/lib/linter/amdTranspiler/snapshots/transpiler.ts.md @@ -4,6 +4,26 @@ The actual snapshot is saved in `transpiler.ts.snap`. Generated by [AVA](https://avajs.dev). +## Transpile Comments.js + +> Snapshot 1 + + `import Control from "sap/ui/core/Control";␊ + //# sourceMappingURL=Comments.js.map` + +> Snapshot 2 + + { + file: 'Comments.js', + mappings: 'OACgD,OAAO,MAAxC,qBAAqB', + names: [], + sourceRoot: '', + sources: [ + 'Comments.js', + ], + version: 3, + } + ## Transpile Dependencies.js > Snapshot 1 @@ -183,15 +203,16 @@ 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";␊ + // This comment should be above the "class" statement of MyController after transpiling␊ export default class MyController_1 extends Controller {␊ - }␊ + } // This comment should be below the "class" statement of MyController after transpiling␊ //# sourceMappingURL=Factory_ArrowFunction.js.map` > Snapshot 2 { file: 'Factory_ArrowFunction.js', - mappings: 'OAAiB,UAAU,MAMzB,4BAA4B;OAC5B,yBAAyB;OACzB,6BAA6B;4CAPvB,UAAU', + mappings: 'OAAiB,UAAU,MAQzB,4BAA4B;OAC5B,yBAAyB;OACzB,6BAA6B;;4CARvB,UAAU', names: [], sourceRoot: '', sources: [ @@ -206,14 +227,14 @@ Generated by [AVA](https://avajs.dev). `import Controller from "sap/ui/core/mvc/Controller";␊ export default class MyController_1 extends Controller {␊ - }␊ + } /* This comment should be above the "class" statement of MyController after transpiling */␊ //# sourceMappingURL=Factory_ArrowFunctionExpr.js.map` > Snapshot 2 { file: 'Factory_ArrowFunctionExpr.js', - mappings: 'OAA+C,UAAU,MAA1C,4BAA4B;4CAAoB,UAAU', + mappings: 'OAA+C,UAAU,MAA1C,4BAA4B;4CAA8G,UAAU', names: [], sourceRoot: '', sources: [ @@ -485,6 +506,50 @@ Generated by [AVA](https://avajs.dev). version: 3, } +## Transpile Factory_Identifier.js + +> Snapshot 1 + + `let foo = "a";␊ + if (something) {␊ + foo = "b";␊ + }␊ + export default foo;␊ + //# sourceMappingURL=Factory_Identifier.js.map` + +> Snapshot 2 + + { + file: 'Factory_Identifier.js', + mappings: 'AAAA,IAAI,GAAG,GAAG,GAAG,CAAC;AACd,IAAI,SAAS,EAAE,CAAC;IACf,GAAG,GAAG,GAAG,CAAA;AACV,CAAC;eACiB,GAAG', + names: [], + sourceRoot: '', + sources: [ + 'Factory_Identifier.js', + ], + version: 3, + } + +## Transpile Factory_Literal.js + +> Snapshot 1 + + `export default 123;␊ + //# sourceMappingURL=Factory_Literal.js.map` + +> Snapshot 2 + + { + file: 'Factory_Literal.js', + mappings: 'eAAkB,GAAG', + names: [], + sourceRoot: '', + sources: [ + 'Factory_Literal.js', + ], + version: 3, + } + ## Transpile Factory_MultipleReturns.js > Snapshot 1 diff --git a/test/lib/linter/amdTranspiler/snapshots/transpiler.ts.snap b/test/lib/linter/amdTranspiler/snapshots/transpiler.ts.snap index b685bfae..145d86a4 100644 Binary files a/test/lib/linter/amdTranspiler/snapshots/transpiler.ts.snap and b/test/lib/linter/amdTranspiler/snapshots/transpiler.ts.snap differ