Skip to content

Commit ffec968

Browse files
authored
Don't track private symbol roots in other files during js declaration emit (microsoft#55390)
1 parent 08b2566 commit ffec968

5 files changed

+224
-1
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8456,7 +8456,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
84568456
// Lookup the root symbol of the chain of refs we'll use to access it and serialize it
84578457
const chain = lookupSymbolChainWorker(sym, context, meaning);
84588458
if (!(sym.flags & SymbolFlags.Property)) {
8459-
includePrivateSymbol(chain[0]);
8459+
// Only include referenced privates in the same file. Weird JS aliases may expose privates
8460+
// from other files - assume JS transforms will make those available via expected means
8461+
const root = chain[0];
8462+
const contextFile = getSourceFileOfNode(oldcontext.enclosingDeclaration);
8463+
if (some(root.declarations, d => getSourceFileOfNode(d) === contextFile)) {
8464+
includePrivateSymbol(root);
8465+
}
84608466
}
84618467
}
84628468
else if (oldcontext.tracker.inner?.trackSymbol) {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//// [tests/cases/compiler/jsDeclarationEmitDoesNotRenameImport.ts] ////
2+
3+
//// [Test.js]
4+
/** @module test/Test */
5+
class Test {}
6+
export default Test;
7+
//// [Test.js]
8+
/** @module Test */
9+
class Test {}
10+
export default Test;
11+
//// [index.js]
12+
import Test from './test/Test.js'
13+
14+
/**
15+
* @typedef {Object} Options
16+
* @property {typeof import("./Test.js").default} [test]
17+
*/
18+
19+
class X extends Test {
20+
/**
21+
* @param {Options} options
22+
*/
23+
constructor(options) {
24+
super();
25+
if (options.test) {
26+
this.test = new options.test();
27+
}
28+
}
29+
}
30+
31+
export default X;
32+
33+
34+
35+
36+
//// [Test.d.ts]
37+
export default Test;
38+
/** @module test/Test */
39+
declare class Test {
40+
}
41+
//// [Test.d.ts]
42+
export default Test;
43+
/** @module Test */
44+
declare class Test {
45+
}
46+
//// [index.d.ts]
47+
export default X;
48+
export type Options = {
49+
test?: typeof import("./Test.js").default | undefined;
50+
};
51+
/**
52+
* @typedef {Object} Options
53+
* @property {typeof import("./Test.js").default} [test]
54+
*/
55+
declare class X extends Test {
56+
/**
57+
* @param {Options} options
58+
*/
59+
constructor(options: Options);
60+
test: import("./Test.js").default | undefined;
61+
}
62+
import Test from './test/Test.js';
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//// [tests/cases/compiler/jsDeclarationEmitDoesNotRenameImport.ts] ////
2+
3+
=== test/Test.js ===
4+
/** @module test/Test */
5+
class Test {}
6+
>Test : Symbol(Test, Decl(Test.js, 0, 0))
7+
8+
export default Test;
9+
>Test : Symbol(Test, Decl(Test.js, 0, 0))
10+
11+
=== Test.js ===
12+
/** @module Test */
13+
class Test {}
14+
>Test : Symbol(Test, Decl(Test.js, 0, 0))
15+
16+
export default Test;
17+
>Test : Symbol(Test, Decl(Test.js, 0, 0))
18+
19+
=== index.js ===
20+
import Test from './test/Test.js'
21+
>Test : Symbol(Test, Decl(index.js, 0, 6))
22+
23+
/**
24+
* @typedef {Object} Options
25+
* @property {typeof import("./Test.js").default} [test]
26+
*/
27+
28+
class X extends Test {
29+
>X : Symbol(X, Decl(index.js, 0, 33))
30+
>Test : Symbol(Test, Decl(index.js, 0, 6))
31+
32+
/**
33+
* @param {Options} options
34+
*/
35+
constructor(options) {
36+
>options : Symbol(options, Decl(index.js, 11, 16))
37+
38+
super();
39+
>super : Symbol(Test, Decl(Test.js, 0, 0))
40+
41+
if (options.test) {
42+
>options.test : Symbol(test, Decl(index.js, 4, 3))
43+
>options : Symbol(options, Decl(index.js, 11, 16))
44+
>test : Symbol(test, Decl(index.js, 4, 3))
45+
46+
this.test = new options.test();
47+
>this.test : Symbol(X.test, Decl(index.js, 13, 27))
48+
>this : Symbol(X, Decl(index.js, 0, 33))
49+
>test : Symbol(X.test, Decl(index.js, 13, 27))
50+
>options.test : Symbol(test, Decl(index.js, 4, 3))
51+
>options : Symbol(options, Decl(index.js, 11, 16))
52+
>test : Symbol(test, Decl(index.js, 4, 3))
53+
}
54+
}
55+
}
56+
57+
export default X;
58+
>X : Symbol(X, Decl(index.js, 0, 33))
59+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//// [tests/cases/compiler/jsDeclarationEmitDoesNotRenameImport.ts] ////
2+
3+
=== test/Test.js ===
4+
/** @module test/Test */
5+
class Test {}
6+
>Test : Test
7+
8+
export default Test;
9+
>Test : Test
10+
11+
=== Test.js ===
12+
/** @module Test */
13+
class Test {}
14+
>Test : Test
15+
16+
export default Test;
17+
>Test : Test
18+
19+
=== index.js ===
20+
import Test from './test/Test.js'
21+
>Test : typeof Test
22+
23+
/**
24+
* @typedef {Object} Options
25+
* @property {typeof import("./Test.js").default} [test]
26+
*/
27+
28+
class X extends Test {
29+
>X : X
30+
>Test : Test
31+
32+
/**
33+
* @param {Options} options
34+
*/
35+
constructor(options) {
36+
>options : Options
37+
38+
super();
39+
>super() : void
40+
>super : typeof Test
41+
42+
if (options.test) {
43+
>options.test : typeof import("Test").default | undefined
44+
>options : Options
45+
>test : typeof import("Test").default | undefined
46+
47+
this.test = new options.test();
48+
>this.test = new options.test() : import("Test").default
49+
>this.test : any
50+
>this : this
51+
>test : any
52+
>new options.test() : import("Test").default
53+
>options.test : typeof import("Test").default
54+
>options : Options
55+
>test : typeof import("Test").default
56+
}
57+
}
58+
}
59+
60+
export default X;
61+
>X : X
62+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// @allowJs: true
2+
// @declaration: true
3+
// @emitDeclarationOnly: true
4+
// @strict: false
5+
// @strictNullChecks: true
6+
// @filename: test/Test.js
7+
/** @module test/Test */
8+
class Test {}
9+
export default Test;
10+
// @filename: Test.js
11+
/** @module Test */
12+
class Test {}
13+
export default Test;
14+
// @filename: index.js
15+
import Test from './test/Test.js'
16+
17+
/**
18+
* @typedef {Object} Options
19+
* @property {typeof import("./Test.js").default} [test]
20+
*/
21+
22+
class X extends Test {
23+
/**
24+
* @param {Options} options
25+
*/
26+
constructor(options) {
27+
super();
28+
if (options.test) {
29+
this.test = new options.test();
30+
}
31+
}
32+
}
33+
34+
export default X;

0 commit comments

Comments
 (0)