Skip to content

Commit

Permalink
Skip private members with decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Bocharov committed Oct 22, 2021
1 parent 7e4dfda commit 44096a7
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
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) { }

0 comments on commit 44096a7

Please sign in to comment.