Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8455,7 +8455,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) {
Expand Down
62 changes: 62 additions & 0 deletions tests/baselines/reference/jsDeclarationEmitDoesNotRenameImport.js
Original file line number Diff line number Diff line change
@@ -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';
Original file line number Diff line number Diff line change
@@ -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))

Original file line number Diff line number Diff line change
@@ -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

34 changes: 34 additions & 0 deletions tests/cases/compiler/jsDeclarationEmitDoesNotRenameImport.ts
Original file line number Diff line number Diff line change
@@ -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;