Skip to content
Merged
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
49 changes: 49 additions & 0 deletions packages/plugin/__test__/__snapshots__/test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,13 @@ exports[`classes class-import-meta.js 1`] = `

exports[`classic sap-ui-define.ts 1`] = `"sap.ui.define("", ["foo/bar/MyResource"]);"`;

exports[`classic sap-ui-define-copyright.ts 1`] = `
"/*!
* \${copyright}
*/
sap.ui.define("", ["foo/bar/MyResource"]);"
`;

exports[`comments copyright.js 1`] = `
"/*!
* \${copyright}
Expand Down Expand Up @@ -1362,6 +1369,27 @@ exports[`typescript ts-class-anonymous.ts 1`] = `
});"
`;

exports[`typescript ts-class-anonymous-copyright.ts 1`] = `
"/*!
* \${copyright}
*/
sap.ui.define(["sap/Class"], function (SAPClass) {
/**
* @name MyClass
*/
const MyClass = SAPClass.extend("MyClass", {
createAnonymousClass: function _createAnonymousClass() {
return new class {
getClipboardContentType() {
/* ... */
}
}();
}
});
return MyClass;
});"
`;

exports[`typescript ts-class-param-props.ts 1`] = `
"sap.ui.define(["sap/Class"], function (SAPClass) {
const MyClass = SAPClass.extend("MyClass", {
Expand All @@ -1377,6 +1405,27 @@ exports[`typescript ts-class-param-props.ts 1`] = `
});"
`;

exports[`typescript ts-class-param-props-copyright.ts 1`] = `
"/*!
* \${copyright}
*/
sap.ui.define(["sap/Class"], function (SAPClass) {
/**
* @name MyClass
*/
const MyClass = SAPClass.extend("MyClass", {
constructor: function _constructor(bar, x, y) {
SAPClass.prototype.constructor.call(this);
this.bar = bar;
this.x = x;
this.y = y;
this.foo = bar.getFoo();
}
});
return MyClass;
});"
`;

exports[`typescript ts-class-props.ts 1`] = `
"sap.ui.define(["sap/Class"], function (SAPClass) {
const MyClass = SAPClass.extend("MyClass", {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*!
* ${copyright}
*/
sap.ui.define("", ["foo/bar/MyResource"]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*!
* ${copyright}
*/
import SAPClass from "sap/Class";
import IClipboardContent from "sap/IClipboardContent";

/**
* @name MyClass
*/
export default class MyClass extends SAPClass {
createAnonymousClass() {
return new (class implements IClipboardContent {
getClipboardContentType() {
/* ... */
}
})();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*!
* ${copyright}
*/
import SAPClass from "sap/Class";
import SAPBar from "sap/Bar";
import SAPFoo from "sap/Foo";

/**
* @name MyClass
*/
export default class MyClass extends SAPClass {
foo: SAPFoo;

constructor(public bar: SAPBar, private x: string, readonly y: string) {
super();
this.foo = bar.getFoo();
}
}
3 changes: 2 additions & 1 deletion packages/plugin/__test__/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ function processDirectory(dir) {
plugins,
presets,
sourceRoot: __dirname,
comments: filePath.includes("comments"),
comments:
filePath.includes("comments") || filename.includes("copyright"),
babelrc: false,
}).code;

Expand Down
1 change: 1 addition & 0 deletions packages/plugin/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ module.exports = () => {

// Properties for Module Transform
this.programNode = path.node;
this.comments = path.parent.comments;
this.defaultExport = null;
this.defaultExportNode = null;
this.exportGlobal = false;
Expand Down
17 changes: 15 additions & 2 deletions packages/plugin/src/modules/helpers/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,27 @@ export function wrap(visitor, programNode, opts) {

let { body } = programNode;

// find the copyright comment from the original program body
const copyright = body?.[0]?.leadingComments?.find((comment, idx, arr) => {
// find the copyright comment from the original program body and remove it there
// since it need to be put around the new program body (which is sap.ui.define)
let copyright = body?.[0]?.leadingComments?.find((comment, idx, arr) => {
if (comment.value.startsWith("!")) {
arr.splice(idx, 1);
return true;
}
});

// in case of TypeScript transpiling taking place upfront, the copyright comment
// is associcated with the program and not the program body, so we need to find it
// and move it to the new program body (which is sap.ui.define)
if (!copyright) {
copyright = visitor.comments?.find((comment, idx, arr) => {
if (comment.value.startsWith("!")) {
arr.splice(idx, 1);
return true;
}
});
}

let allExportHelperAdded = false;
let extendAdded = false;

Expand Down