Don't track private symbol roots in other files during js declaration emit (#55390)

This commit is contained in:
Wesley Wigham 2023-08-16 14:13:45 -07:00 committed by GitHub
parent 08b2566e5b
commit ffec968d79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 224 additions and 1 deletions

View File

@ -8456,7 +8456,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// Lookup the root symbol of the chain of refs we'll use to access it and serialize it
const chain = lookupSymbolChainWorker(sym, context, meaning);
if (!(sym.flags & SymbolFlags.Property)) {
includePrivateSymbol(chain[0]);
// Only include referenced privates in the same file. Weird JS aliases may expose privates
// from other files - assume JS transforms will make those available via expected means
const root = chain[0];
const contextFile = getSourceFileOfNode(oldcontext.enclosingDeclaration);
if (some(root.declarations, d => getSourceFileOfNode(d) === contextFile)) {
includePrivateSymbol(root);
}
}
}
else if (oldcontext.tracker.inner?.trackSymbol) {

View File

@ -0,0 +1,62 @@
//// [tests/cases/compiler/jsDeclarationEmitDoesNotRenameImport.ts] ////
//// [Test.js]
/** @module test/Test */
class Test {}
export default Test;
//// [Test.js]
/** @module Test */
class Test {}
export default Test;
//// [index.js]
import Test from './test/Test.js'
/**
* @typedef {Object} Options
* @property {typeof import("./Test.js").default} [test]
*/
class X extends Test {
/**
* @param {Options} options
*/
constructor(options) {
super();
if (options.test) {
this.test = new options.test();
}
}
}
export default X;
//// [Test.d.ts]
export default Test;
/** @module test/Test */
declare class Test {
}
//// [Test.d.ts]
export default Test;
/** @module Test */
declare class Test {
}
//// [index.d.ts]
export default X;
export type Options = {
test?: typeof import("./Test.js").default | undefined;
};
/**
* @typedef {Object} Options
* @property {typeof import("./Test.js").default} [test]
*/
declare class X extends Test {
/**
* @param {Options} options
*/
constructor(options: Options);
test: import("./Test.js").default | undefined;
}
import Test from './test/Test.js';

View File

@ -0,0 +1,59 @@
//// [tests/cases/compiler/jsDeclarationEmitDoesNotRenameImport.ts] ////
=== test/Test.js ===
/** @module test/Test */
class Test {}
>Test : Symbol(Test, Decl(Test.js, 0, 0))
export default Test;
>Test : Symbol(Test, Decl(Test.js, 0, 0))
=== Test.js ===
/** @module Test */
class Test {}
>Test : Symbol(Test, Decl(Test.js, 0, 0))
export default Test;
>Test : Symbol(Test, Decl(Test.js, 0, 0))
=== index.js ===
import Test from './test/Test.js'
>Test : Symbol(Test, Decl(index.js, 0, 6))
/**
* @typedef {Object} Options
* @property {typeof import("./Test.js").default} [test]
*/
class X extends Test {
>X : Symbol(X, Decl(index.js, 0, 33))
>Test : Symbol(Test, Decl(index.js, 0, 6))
/**
* @param {Options} options
*/
constructor(options) {
>options : Symbol(options, Decl(index.js, 11, 16))
super();
>super : Symbol(Test, Decl(Test.js, 0, 0))
if (options.test) {
>options.test : Symbol(test, Decl(index.js, 4, 3))
>options : Symbol(options, Decl(index.js, 11, 16))
>test : Symbol(test, Decl(index.js, 4, 3))
this.test = new options.test();
>this.test : Symbol(X.test, Decl(index.js, 13, 27))
>this : Symbol(X, Decl(index.js, 0, 33))
>test : Symbol(X.test, Decl(index.js, 13, 27))
>options.test : Symbol(test, Decl(index.js, 4, 3))
>options : Symbol(options, Decl(index.js, 11, 16))
>test : Symbol(test, Decl(index.js, 4, 3))
}
}
}
export default X;
>X : Symbol(X, Decl(index.js, 0, 33))

View File

@ -0,0 +1,62 @@
//// [tests/cases/compiler/jsDeclarationEmitDoesNotRenameImport.ts] ////
=== test/Test.js ===
/** @module test/Test */
class Test {}
>Test : Test
export default Test;
>Test : Test
=== Test.js ===
/** @module Test */
class Test {}
>Test : Test
export default Test;
>Test : Test
=== index.js ===
import Test from './test/Test.js'
>Test : typeof Test
/**
* @typedef {Object} Options
* @property {typeof import("./Test.js").default} [test]
*/
class X extends Test {
>X : X
>Test : Test
/**
* @param {Options} options
*/
constructor(options) {
>options : Options
super();
>super() : void
>super : typeof Test
if (options.test) {
>options.test : typeof import("Test").default | undefined
>options : Options
>test : typeof import("Test").default | undefined
this.test = new options.test();
>this.test = new options.test() : import("Test").default
>this.test : any
>this : this
>test : any
>new options.test() : import("Test").default
>options.test : typeof import("Test").default
>options : Options
>test : typeof import("Test").default
}
}
}
export default X;
>X : X

View File

@ -0,0 +1,34 @@
// @allowJs: true
// @declaration: true
// @emitDeclarationOnly: true
// @strict: false
// @strictNullChecks: true
// @filename: test/Test.js
/** @module test/Test */
class Test {}
export default Test;
// @filename: Test.js
/** @module Test */
class Test {}
export default Test;
// @filename: index.js
import Test from './test/Test.js'
/**
* @typedef {Object} Options
* @property {typeof import("./Test.js").default} [test]
*/
class X extends Test {
/**
* @param {Options} options
*/
constructor(options) {
super();
if (options.test) {
this.test = new options.test();
}
}
}
export default X;