Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip private members with decorators #18

Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ A TypeScript custom transformer which minify names of private class members.
For now it just renames private members with prepending some prefix to name.
For example, if you have `privateMember`, then after transformation the name will be `_private_privateMember`.
After that you can use terser/uglify with mangle options to minify that members.
Note, that private class members with decorators won't be prefixed and further minified.

## Caution!!!

Expand Down
7 changes: 6 additions & 1 deletion src/properties-minifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ function isPrivateNonStaticClassMember(symbol: ts.Symbol | undefined): boolean {
}

return symbol.declarations.some((x: ts.Declaration) => {
return (isClassMember(x) || isConstructorParameter(x)) && isPrivateNonStatic(x);
// terser / uglify property minifiers aren't able to handle decorators
return (isClassMember(x) && !hasDecorators(x) || isConstructorParameter(x)) && isPrivateNonStatic(x);
});
}

function hasDecorators(node: ts.Node): boolean {
return !!node.decorators;
}
1 change: 1 addition & 0 deletions tests/functional-test-cases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ describe('Functional tests', () => {
rootNames: [testCase.inputFileName],
options: {
target: ts.ScriptTarget.ES5,
experimentalDecorators: true,
},
});

Expand Down
17 changes: 17 additions & 0 deletions tests/test-cases/private-with-decorators/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export class Class {
public publicField: number = 123;
//@ts-ignore
@decorator private privateField: string = 'string-value';

public constructor() {
this.privateMethod(this.privateField);
this.privateMethod(this.publicField);

this['privateMethod'](this.privateField);
}

//@ts-ignore
@decorator private privateMethod(a: string | number): void { }
}

function decorator(target: any, propertyKey: string): void {}
29 changes: 29 additions & 0 deletions tests/test-cases/private-with-decorators/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var Class = /** @class */ (function () {
function Class() {
this.publicField = 123;
//@ts-ignore
this.privateField = 'string-value';
this.privateMethod(this.privateField);
this.privateMethod(this.publicField);
this['privateMethod'](this.privateField);
}
//@ts-ignore
Class.prototype.privateMethod = function (a) { };
__decorate([
decorator
], Class.prototype, "privateField", void 0);
__decorate([
decorator
], Class.prototype, "privateMethod", null);
return Class;
}());
exports.Class = Class;
function decorator(target, propertyKey) { }