Skip to content

Commit

Permalink
feat: Allow usage of shorthand properties in extend call (#385)
Browse files Browse the repository at this point in the history
In UI5 there might be Control extend calls in the following form:

```javascript
const renderer =  function(oRm, oMyControl) {
    oRm.openStart("div", oMyControl);
    oRm.class("mycssclass");
    oRm.openEnd();
    oRm.close("div");
};
var myControl = Control.extend("myControl", {
  metadata: {},
  renderer
});
```

This change tries to improve transpilation of shorthand properties
  • Loading branch information
d3xter666 authored Oct 28, 2024
1 parent 31432b0 commit 64a27d8
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/linter/ui5Types/amdTranspiler/rewriteExtendCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}"

Expand Down
19 changes: 18 additions & 1 deletion test/fixtures/transpiler/amd/Factory_ClassLocalVariable.js
Original file line number Diff line number Diff line change
@@ -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", {
Expand Down Expand Up @@ -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", {
Expand Down
19 changes: 18 additions & 1 deletion test/lib/linter/amdTranspiler/snapshots/transpiler.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};␊
Expand Down Expand Up @@ -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 = {};␊
Expand All @@ -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;8BA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AAC,EAAE,CAAC;AAEL,MAAM,QAAQ,GAAG,EAAE,CAAC;AACpB,MAAM,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: [
Expand Down
Binary file modified test/lib/linter/amdTranspiler/snapshots/transpiler.ts.snap
Binary file not shown.

0 comments on commit 64a27d8

Please sign in to comment.