Skip to content

Commit 1d89e34

Browse files
authored
Merge pull request #9 from timocov/fix-defining-properties
2 parents 027c497 + d946089 commit 1d89e34

File tree

6 files changed

+88
-2
lines changed

6 files changed

+88
-2
lines changed

src/exports-symbol-tree.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ export class ExportsSymbolTree {
5151
}
5252

5353
private computeTreeForChildren(targetSymbolsSet: Set<ts.Symbol>, node: ts.Node, visitedSymbols: Set<ts.Symbol>): void {
54+
// it's similar to handling ts.Block node - both Block and variable's initializer are part of _implementation_
55+
// and we don't care about that implementation at all - we just only need to worry it's definition
56+
// for functions it is arguments and return type
57+
// for variables - the type of a variable
58+
if (ts.isVariableDeclaration(node)) {
59+
const typeChecker = this.program.getTypeChecker();
60+
const variableType = typeChecker.getTypeAtLocation(node);
61+
const variableTypeSymbol = variableType.getSymbol();
62+
if (variableTypeSymbol !== undefined) {
63+
targetSymbolsSet.add(variableTypeSymbol);
64+
}
65+
66+
return;
67+
}
68+
5469
ts.forEachChild(node, (childNode: ts.Node) => this.computeTreeForNode(targetSymbolsSet, childNode, visitedSymbols));
5570
}
5671

src/typescript-helpers.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,14 @@ export function getExportsForSourceFile(typeChecker: ts.TypeChecker, sourceFileS
101101
return result.map((symbol: ts.Symbol) => getActualSymbol(symbol, typeChecker));
102102
}
103103

104-
export type ClassMember = ts.MethodDeclaration | ts.PropertyDeclaration;
104+
export type ClassMember =
105+
| ts.MethodDeclaration
106+
| ts.PropertyDeclaration
107+
| ts.GetAccessorDeclaration
108+
| ts.SetAccessorDeclaration;
105109

106110
export function isClassMember(node: ts.Node): node is ClassMember {
107-
return ts.isMethodDeclaration(node) || ts.isPropertyDeclaration(node);
111+
return ts.isMethodDeclaration(node) || ts.isPropertyDeclaration(node) || ts.isGetAccessor(node) || ts.isSetAccessor(node);
108112
}
109113

110114
export function hasPrivateKeyword(node: ClassMember | ts.ParameterDeclaration): boolean {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class TestProperties {
2+
private _salad: number = 0;
3+
public dressing: number = 0;
4+
public get salad(): number { return this._salad; }
5+
public set salad(val: number) { this._salad = val; }
6+
}
7+
8+
const test1 = new TestProperties();
9+
test1.salad = 0;
10+
test1.dressing = 0;
11+
export const totalSalad = test1.salad + 1;
12+
export const totalDressing = test1.dressing + 0;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
var TestProperties = /** @class */ (function () {
4+
function TestProperties() {
5+
this._private__salad = 0;
6+
this._internal_dressing = 0;
7+
}
8+
Object.defineProperty(TestProperties.prototype, "_internal_salad", {
9+
get: function () { return this._private__salad; },
10+
set: function (val) { this._private__salad = val; },
11+
enumerable: true,
12+
configurable: true
13+
});
14+
return TestProperties;
15+
}());
16+
var test1 = new TestProperties();
17+
test1._internal_salad = 0;
18+
test1._internal_dressing = 0;
19+
exports.totalSalad = test1._internal_salad + 1;
20+
exports.totalDressing = test1._internal_dressing + 0;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class InternalClass {
2+
public publicProperty: number = 1;
3+
}
4+
5+
class InternalClass2 {
6+
public publicProperty: number = 1;
7+
}
8+
9+
export const exportedVariable = (() => {
10+
return new InternalClass();
11+
})();
12+
13+
export const exportedVariable2 = (() => {
14+
return new InternalClass2().publicProperty;
15+
})();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
var InternalClass = /** @class */ (function () {
4+
function InternalClass() {
5+
this.publicProperty = 1;
6+
}
7+
return InternalClass;
8+
}());
9+
var InternalClass2 = /** @class */ (function () {
10+
function InternalClass2() {
11+
this._internal_publicProperty = 1;
12+
}
13+
return InternalClass2;
14+
}());
15+
exports.exportedVariable = (function () {
16+
return new InternalClass();
17+
})();
18+
exports.exportedVariable2 = (function () {
19+
return new InternalClass2()._internal_publicProperty;
20+
})();

0 commit comments

Comments
 (0)