Skip to content

Commit

Permalink
Merge pull request #14 from mad-gooze/fix-reference-ctor-params
Browse files Browse the repository at this point in the history
fix referencing constructor params in constructor body (fixes #13)
  • Loading branch information
timocov authored Oct 30, 2019
2 parents 4ed6748 + 5c33b01 commit a538969
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/properties-minifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export class PropertiesMinifier {
return this.createNewAccessExpression(node, program);
} else if (ts.isBindingElement(node)) {
return this.createNewBindingElement(node, program);
} else if (isConstructorParameterReference(node, program)) {
return this.createNewNode(program, node, ts.createIdentifier);
} else if (isConstructorParameter(node) && isPrivateNonStatic(node)) {
return this.createNewConstructorParameter(node, program);
}
Expand Down Expand Up @@ -251,6 +253,16 @@ function getClassName(classNode: ts.ClassLikeDeclaration): string {
return classNode.name.getText();
}

function isConstructorParameterReference(node: ts.Node, program: ts.Program): node is ts.Identifier {
if (!ts.isIdentifier(node)) {
return false;
}

const typeChecker = program.getTypeChecker();
const symbol = typeChecker.getSymbolAtLocation(node);
return isPrivateNonStaticClassMember(symbol);
}

function isPrivateNonStaticClassMember(symbol: ts.Symbol | undefined): boolean {
// for some reason ts.Symbol.declarations can be undefined (for example in order to accessing to proto member)
if (symbol === undefined || symbol.declarations === undefined) { // tslint:disable-line:strict-type-predicates
Expand Down

0 comments on commit a538969

Please sign in to comment.