Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(amdTranspiler): Various improvements #364

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/linter/ui5Types/amdTranspiler/moduleDeclarationToDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand All @@ -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));
Expand Down
10 changes: 8 additions & 2 deletions src/linter/ui5Types/amdTranspiler/parseModuleDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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(
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/transpiler/amd/Comments.js
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions test/fixtures/transpiler/amd/Factory_ArrowFunction.js
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/transpiler/amd/Factory_ArrowFunctionExpr.js
Original file line number Diff line number Diff line change
@@ -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 */
5 changes: 5 additions & 0 deletions test/fixtures/transpiler/amd/Factory_Identifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let foo = "a";
if (something) {
foo = "b"
}
sap.ui.define([], foo);
1 change: 1 addition & 0 deletions test/fixtures/transpiler/amd/Factory_Literal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sap.ui.define([], 123);
73 changes: 69 additions & 4 deletions test/lib/linter/amdTranspiler/snapshots/transpiler.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: [
Expand All @@ -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: [
Expand Down Expand Up @@ -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
Expand Down
Binary file modified test/lib/linter/amdTranspiler/snapshots/transpiler.ts.snap
Binary file not shown.