diff --git a/src/linter/ui5Types/amdTranspiler/rewriteExtendCall.ts b/src/linter/ui5Types/amdTranspiler/rewriteExtendCall.ts index 3e5b2d908..2462a21f3 100644 --- a/src/linter/ui5Types/amdTranspiler/rewriteExtendCall.ts +++ b/src/linter/ui5Types/amdTranspiler/rewriteExtendCall.ts @@ -62,12 +62,19 @@ function getClassBodyFromArguments( if (!classBody) { throw new UnsupportedExtendCall(`No class body found in extends call at ${toPosStr(callExp)}`); } - if (classBody.properties.find((prop) => ts.isSpreadAssignment(prop) || ts.isShorthandPropertyAssignment(prop))) { + if (classBody.properties.find((prop) => ts.isSpreadAssignment(prop))) { // TODO: Support spread elements(?) throw new UnsupportedExtendCall(`Unsupported spread element in extends call body`); } return classBody.properties.map((prop): ts.ClassElement | undefined => { - if (ts.isMethodDeclaration(prop)) { + if (ts.isShorthandPropertyAssignment(prop)) { + // When a property is declared as a shorthand, create a property declaration to itself. + const staticModifier = (ts.isIdentifier(prop.name) && ["renderer", "metadata"].includes(prop.name.text)) ? + [nodeFactory.createToken(ts.SyntaxKind.StaticKeyword)] : + []; + return nodeFactory.createPropertyDeclaration( + staticModifier, prop.name, undefined, undefined, prop.name); + } else if (ts.isMethodDeclaration(prop)) { // Use method declarations as-is // e.g. "method() {}" diff --git a/test/fixtures/transpiler/amd/Factory_ClassLocalVariable.js b/test/fixtures/transpiler/amd/Factory_ClassLocalVariable.js index 321d52993..c88e3ec79 100644 --- a/test/fixtures/transpiler/amd/Factory_ClassLocalVariable.js +++ b/test/fixtures/transpiler/amd/Factory_ClassLocalVariable.js @@ -1,4 +1,4 @@ -sap.ui.define(["sap/ui/core/Control"], function(Control) { +sap.ui.define(["sap/ui/core/Control", "sap/m/Button"], function(Control, Button) { /* This comment should be above the "class" statement of ExampleControl1 after transpiling */ const ExampleControl1 = Control.extend("test.ExampleControl1", { @@ -43,6 +43,23 @@ sap.ui.define(["sap/ui/core/Control"], function(Control) { } }); })(); + + const metadata = {}; + const renderer = function (oRm, oMyControl) {}; + const createButton = function() { + var btn = new Button({ + blocked: true + }); + btn.attachTap(function() { + console.log("Tapped"); + }); + return btn; + }; + var myControl = Control.extend("myControl", { + metadata, + renderer, + createButton, + }); // This comment should be above the "class" statement of ExampleControl9 after transpiling return Control.extend("test.ExampleControl9", { diff --git a/test/lib/linter/amdTranspiler/snapshots/transpiler.ts.md b/test/lib/linter/amdTranspiler/snapshots/transpiler.ts.md index ba775d2b9..fdba05de3 100644 --- a/test/lib/linter/amdTranspiler/snapshots/transpiler.ts.md +++ b/test/lib/linter/amdTranspiler/snapshots/transpiler.ts.md @@ -261,6 +261,7 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 `import Control from "sap/ui/core/Control";␊ + import Button from "sap/m/Button";␊ /* This comment should be above the "class" statement of ExampleControl1 after transpiling */␊ class ExampleControl1 extends Control {␊ static metadata = {};␊ @@ -299,6 +300,22 @@ Generated by [AVA](https://avajs.dev). }␊ }␊ })();␊ + const metadata = {};␊ + const renderer = function (oRm, oMyControl) { };␊ + const createButton = function () {␊ + var btn = new Button({␊ + blocked: true␊ + });␊ + btn.attachTap(function () {␊ + console.log("Tapped");␊ + });␊ + return btn;␊ + };␊ + class myControl extends Control {␊ + static metadata = metadata;␊ + static renderer = renderer;␊ + createButton = createButton;␊ + }␊ // This comment should be above the "class" statement of ExampleControl9 after transpiling␊ export default class ExampleControl9_1 extends Control {␊ static metadata = {};␊ @@ -311,7 +328,7 @@ Generated by [AVA](https://avajs.dev). { file: 'Factory_ClassLocalVariable.js', - mappings: 'OAAgD,OAAO,MAAxC,qBAAqB;;8BAGX,OAAO;WAC9B,QAAQ,GAAE,EAAE;WACZ,QAAQ,GAAE;QACT,UAAU,EAAE,CAAC;QACb,MAAM,EAAE,UAAS,GAAG,EAAE,QAAQ;QAC9B,CAAC;KACD;;AAEF,6FAA6F;AAE7F,MAAM,CAAC,GAAG,CAA+D,CAAC;8BAA3C,OAAO;;AAEtC,MAAM,CAAC,GAAG,CAAC,EAGV,CAAC,GAAG,C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AM,QAAQ,GAAG,UAAU,GAAG,EAAE,UAAU,IAAG,CAAC,CAAC;AAC/C,MAAM,YAAY,GAAG;IACpB,IAAI,GAAG,GAAG,IAAI,MAAM,CAAC;QACpB,OAAO,EAAE,IAAI;KACb,CAAC,CAAC;IACH,GAAG,CAAC,SAAS,CAAC;QACb,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IACvB,CAAC,CAAC,CAAC;IACH,OAAO,GAAG,CAAC;AACZ,CAAC,CAAC;wBACc,OAAO;WACtB,QAAQ,GAAR,QAAQ;WACR,QAAQ,GAAR,QAAQ;IACR,YAAY,GAAZ,YAAY;;;+CAIN,OAAO;WACb,QAAQ,GAAE,EAAE;IACZ,OAAA,QAAQ,CAAC,GAAG,EAAE,QAAQ;IACtB,CAAC', names: [], sourceRoot: '', sources: [ diff --git a/test/lib/linter/amdTranspiler/snapshots/transpiler.ts.snap b/test/lib/linter/amdTranspiler/snapshots/transpiler.ts.snap index 5a50f86b1..70fe19472 100644 Binary files a/test/lib/linter/amdTranspiler/snapshots/transpiler.ts.snap and b/test/lib/linter/amdTranspiler/snapshots/transpiler.ts.snap differ