Skip to content

Commit 21514b3

Browse files
authored
Add support for private identifiers (#209)
* Reproduce issue #208 related to private identifiers * Fix bug
1 parent fe49c11 commit 21514b3

File tree

4 files changed

+85
-3
lines changed

4 files changed

+85
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export class ClassWithPrivate {
2+
#privateField
3+
#privateFieldWithInitializer = 42
4+
5+
#privateMethod() {
6+
this.#privateField = 'private field'
7+
return this.#privateField
8+
}
9+
10+
static #privateStaticField
11+
static #privateStaticFieldWithInitializer = 42
12+
13+
static #privateStaticMethod() {}
14+
public publicMethod(): any[] {
15+
return [
16+
this.#privateField,
17+
this.#privateFieldWithInitializer,
18+
this.#privateMethod(),
19+
ClassWithPrivate.#privateStaticMethod(),
20+
ClassWithPrivate.#privateStaticField,
21+
ClassWithPrivate.#privateStaticFieldWithInitializer,
22+
]
23+
}
24+
}

snapshots/input/syntax/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
33
"outDir": "dist",
4-
"target": "ES5"
4+
"target": "ES2015"
55
}
66
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
export class ClassWithPrivate {
2+
// definition syntax 1.0.0 src/`ClassWithPrivate.ts`/
3+
//documentation ```ts\nmodule "ClassWithPrivate.ts"\n```
4+
// ^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`ClassWithPrivate.ts`/ClassWithPrivate#
5+
// documentation ```ts\nclass ClassWithPrivate\n```
6+
#privateField
7+
// ^^^^^^^^^^^^^ definition syntax 1.0.0 src/`ClassWithPrivate.ts`/ClassWithPrivate#`#privateField`.
8+
// documentation ```ts\n(property) #privateField: any\n```
9+
#privateFieldWithInitializer = 42
10+
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`ClassWithPrivate.ts`/ClassWithPrivate#`#privateFieldWithInitializer`.
11+
// documentation ```ts\n(property) #privateFieldWithInitializer: number\n```
12+
13+
#privateMethod() {
14+
// ^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`ClassWithPrivate.ts`/ClassWithPrivate#`#privateMethod`().
15+
// documentation ```ts\n(method) #privateMethod(): any\n```
16+
this.#privateField = 'private field'
17+
// ^^^^^^^^^^^^^ reference syntax 1.0.0 src/`ClassWithPrivate.ts`/ClassWithPrivate#`#privateField`.
18+
return this.#privateField
19+
// ^^^^^^^^^^^^^ reference syntax 1.0.0 src/`ClassWithPrivate.ts`/ClassWithPrivate#`#privateField`.
20+
}
21+
22+
static #privateStaticField
23+
// ^^^^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`ClassWithPrivate.ts`/ClassWithPrivate#`#privateStaticField`.
24+
// documentation ```ts\n(property) #privateStaticField: any\n```
25+
static #privateStaticFieldWithInitializer = 42
26+
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`ClassWithPrivate.ts`/ClassWithPrivate#`#privateStaticFieldWithInitializer`.
27+
// documentation ```ts\n(property) #privateStaticFieldWithInitializer: number\n```
28+
29+
static #privateStaticMethod() {}
30+
// ^^^^^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`ClassWithPrivate.ts`/ClassWithPrivate#`#privateStaticMethod`().
31+
// documentation ```ts\n(method) #privateStaticMethod(): void\n```
32+
public publicMethod(): any[] {
33+
// ^^^^^^^^^^^^ definition syntax 1.0.0 src/`ClassWithPrivate.ts`/ClassWithPrivate#publicMethod().
34+
// documentation ```ts\n(method) publicMethod(): any[]\n```
35+
return [
36+
this.#privateField,
37+
// ^^^^^^^^^^^^^ reference syntax 1.0.0 src/`ClassWithPrivate.ts`/ClassWithPrivate#`#privateField`.
38+
this.#privateFieldWithInitializer,
39+
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`ClassWithPrivate.ts`/ClassWithPrivate#`#privateFieldWithInitializer`.
40+
this.#privateMethod(),
41+
// ^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`ClassWithPrivate.ts`/ClassWithPrivate#`#privateMethod`().
42+
ClassWithPrivate.#privateStaticMethod(),
43+
// ^^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`ClassWithPrivate.ts`/ClassWithPrivate#
44+
// ^^^^^^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`ClassWithPrivate.ts`/ClassWithPrivate#`#privateStaticMethod`().
45+
ClassWithPrivate.#privateStaticField,
46+
// ^^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`ClassWithPrivate.ts`/ClassWithPrivate#
47+
// ^^^^^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`ClassWithPrivate.ts`/ClassWithPrivate#`#privateStaticField`.
48+
ClassWithPrivate.#privateStaticFieldWithInitializer,
49+
// ^^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`ClassWithPrivate.ts`/ClassWithPrivate#
50+
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`ClassWithPrivate.ts`/ClassWithPrivate#`#privateStaticFieldWithInitializer`.
51+
]
52+
}
53+
}
54+

src/FileIndexer.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class FileIndexer {
3838
}
3939
public index(): void {
4040
// Uncomment below if you want to skip certain files for local development.
41-
// if (!this.sourceFile.fileName.includes('conflicting-const')) {
41+
// if (!this.sourceFile.fileName.includes('ClassWithPrivate')) {
4242
// return
4343
// }
4444
this.emitSourceFileOccurrence()
@@ -66,7 +66,11 @@ export class FileIndexer {
6666
)
6767
}
6868
private visit(node: ts.Node): void {
69-
if (ts.isIdentifier(node) || ts.isStringLiteralLike(node)) {
69+
if (
70+
ts.isIdentifier(node) ||
71+
ts.isPrivateIdentifier(node) ||
72+
ts.isStringLiteralLike(node)
73+
) {
7074
const sym = this.getTSSymbolAtLocation(node)
7175
if (sym) {
7276
this.visitSymbolOccurrence(node, sym)

0 commit comments

Comments
 (0)