Skip to content

Commit

Permalink
fix: preserve comments for class members (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
akudev committed Nov 23, 2023
1 parent 82b76e3 commit b5329cb
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 14 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -705,19 +706,19 @@ 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
UI5 supports Modularization through a mechanism called `preload`, which can compile many JavaScript and xml files into just one preload file.
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)
Expand Down
33 changes: 33 additions & 0 deletions packages/plugin/__test__/__snapshots__/test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = {}
}
18 changes: 14 additions & 4 deletions packages/plugin/src/classes/helpers/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit b5329cb

Please sign in to comment.