diff --git a/README.md b/README.md index 4fe8ddf..b92bd54 100644 --- a/README.md +++ b/README.md @@ -378,7 +378,7 @@ If the default file-based namespace does not work for you (perhaps the app name ##### JSDoc -The simplest way to override the names is to use JSDoc. This approach will also work well with classes output from TypeScript if you configure TypeScript to generate ES6 or higher, and don't enable removeComments. +The simplest way to override the names is to use JSDoc. This approach will also work well with classes output from TypeScript if you configure TypeScript to generate ES6 or higher, and don't enable `removeComments``. You can set the `@name`/`@alias` directly or just the `@namespace` and have the name derived from the ES class name. @@ -603,12 +603,11 @@ is converted to ```js const MyExtension = ControllerExtension.extend("MyExtension", { - overrides: { - onPageReady: function () {} - } - }); - return MyExtension; -});" + overrides: { + onPageReady: function () {} + } +}); +return MyExtension; ``` Since class properties are an early ES proposal, TypeScript's compiler (like babel's class properties transform) moves static properties outside the class definition, and moves instance properties inside the constructor (even if TypeScript is configured to output ESNext). @@ -662,6 +661,8 @@ will be converted to: sap.ui.define(["sap/ui/core/Control"], function(Control) { /* ... */ }); ``` +In general, comments are preserved, but for each class property/method whose position is changed, only the leading comment(s) are actively moved along with the member. Others may disappear. + ## Options ### Imports @@ -705,11 +706,11 @@ TODO: more options and better description. ## Example -[openui5-master-detail-app-ts](https://github.com/r-murphy/openui5-masterdetail-app-ts), which is my fork of SAP's openui5-master-detail-app converted to TypeScript. +[openui5-master-detail-app-ts](https://github.com/r-murphy/openui5-masterdetail-app-ts), which is a fork of SAP's openui5-master-detail-app converted to TypeScript. ## Building with Webpack -Take a look at [ui5-loader](https://github.com/MagicCube/ui5-loader) (I have not tried this). +Take a look at [ui5-loader](https://github.com/MagicCube/ui5-loader) (we have not tried this). ## Modularization / Preload @@ -717,7 +718,7 @@ UI5 supports Modularization through a mechanism called `preload`, which can comp Some preload plugins: -- Module/CLI: [openui5-preload](https://github.com/r-murphy/openui5-preload) (Mine) +- Module/CLI: [openui5-preload](https://github.com/r-murphy/openui5-preload) (also created by Ryan Murphy) - Gulp: [gulp-ui5-lib](https://github.com/MagicCube/gulp-ui5-lib) (MagicCube) - Grunt: [grunt-openui5](https://github.com/SAP/grunt-openui5) (Official SAP) diff --git a/packages/plugin/__test__/__snapshots__/test.js.snap b/packages/plugin/__test__/__snapshots__/test.js.snap index 46ba572..90b3c8c 100644 --- a/packages/plugin/__test__/__snapshots__/test.js.snap +++ b/packages/plugin/__test__/__snapshots__/test.js.snap @@ -757,6 +757,39 @@ sap.ui.define(["sap/m/Button"], function (Button) { });" `; +exports[`comments preserve-for-class-members.js 1`] = ` +"sap.ui.define(["sap/SAPClass"], function (SAPClass) { + "use strict"; + + const Formatter = Formatter; + const mdata = {}; + + /** + * @name test.fixtures.classes.MyClass + */ + const MyClass = SAPClass.extend("test.fixtures.classes.MyClass", { + constructor: function constructor() { + SAPClass.prototype.constructor.apply(this, arguments); + // some comment + this.myNumber = 1; + }, + // this property is not moved into the constructor + overrides: {}, + // this is MY method + myMethod: function _myMethod() {}, + /** + * this is also my method + */ + myOtherMethod: function _myOtherMethod() {} + }); + /** + * block comment + */ + MyClass.myOtherNumber = 42; + return MyClass; +});" +`; + exports[`decorators mixin.js 1`] = ` "sap.ui.define(["sap/ui/core/mvc/Controller", "../lib/decorators", "../lib/RoutingSupport"], function (Controller, ___lib_decorators, __RoutingSupport) { "use strict"; diff --git a/packages/plugin/__test__/fixtures/comments/preserve-for-class-members.js b/packages/plugin/__test__/fixtures/comments/preserve-for-class-members.js new file mode 100644 index 0000000..c4fde18 --- /dev/null +++ b/packages/plugin/__test__/fixtures/comments/preserve-for-class-members.js @@ -0,0 +1,28 @@ +import SAPClass from "sap/SAPClass"; + +const Formatter = Formatter; +const mdata = {}; + +/** + * @name test.fixtures.classes.MyClass + */ +export default class MyClass extends SAPClass { + // some comment + myNumber = 1; + + /** + * block comment + */ + static myOtherNumber = 42; + + // this is MY method + myMethod() { } + + /** + * this is also my method + */ + myOtherMethod() { } + + // this property is not moved into the constructor + overrides = {} +} diff --git a/packages/plugin/src/classes/helpers/classes.js b/packages/plugin/src/classes/helpers/classes.js index 3ea1084..9a4c3a3 100644 --- a/packages/plugin/src/classes/helpers/classes.js +++ b/packages/plugin/src/classes/helpers/classes.js @@ -338,17 +338,27 @@ function getFileBaseNamespace(path, pluginOpts) { } } -const buildObjectProperty = (member) => - t.objectProperty(member.key, member.value, member.computed); +const buildObjectProperty = (member) => { + const newObjectProperty = t.objectProperty( + member.key, + member.value, + member.computed + ); + newObjectProperty.leadingComments = member.leadingComments; + return newObjectProperty; +}; -const buildMemberAssignmentStatement = (objectIdentifier, member) => - t.expressionStatement( +const buildMemberAssignmentStatement = (objectIdentifier, member) => { + const newMember = t.expressionStatement( t.assignmentExpression( "=", t.memberExpression(objectIdentifier, member.key, member.computed), member.value ) ); + newMember.leadingComments = member.leadingComments; + return newMember; +}; const buildThisMemberAssignmentStatement = buildMemberAssignmentStatement.bind( null,