-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Add visible alias statements for non-visible binding patterns when emitting declaration #48869
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4612,13 +4612,25 @@ namespace ts { | |
&& isDeclarationVisible(declaration.parent)) { | ||
return addVisibleAlias(declaration, declaration); | ||
} | ||
else if (symbol.flags & SymbolFlags.Alias && isBindingElement(declaration) && isInJSFile(declaration) && declaration.parent?.parent // exported import-like top-level JS require statement | ||
&& isVariableDeclaration(declaration.parent.parent) | ||
&& declaration.parent.parent.parent?.parent && isVariableStatement(declaration.parent.parent.parent.parent) | ||
&& !hasSyntacticModifier(declaration.parent.parent.parent.parent, ModifierFlags.Export) | ||
&& declaration.parent.parent.parent.parent.parent // check if the thing containing the variable statement is visible (ie, the file) | ||
&& isDeclarationVisible(declaration.parent.parent.parent.parent.parent)) { | ||
return addVisibleAlias(declaration, declaration.parent.parent.parent.parent); | ||
else if (isBindingElement(declaration)) { | ||
if (symbol.flags & SymbolFlags.Alias && isInJSFile(declaration) && declaration.parent?.parent // exported import-like top-level JS require statement | ||
&& isVariableDeclaration(declaration.parent.parent) | ||
&& declaration.parent.parent.parent?.parent && isVariableStatement(declaration.parent.parent.parent.parent) | ||
&& !hasSyntacticModifier(declaration.parent.parent.parent.parent, ModifierFlags.Export) | ||
&& declaration.parent.parent.parent.parent.parent // check if the thing containing the variable statement is visible (ie, the file) | ||
&& isDeclarationVisible(declaration.parent.parent.parent.parent.parent)) { | ||
return addVisibleAlias(declaration, declaration.parent.parent.parent.parent); | ||
} | ||
else if (symbol.flags & SymbolFlags.BlockScopedVariable) { | ||
const variableStatement = findAncestor(declaration, isVariableStatement)!; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My only remaining comment is the new branch's use of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it makes sense here because binding patterns can be nested and thus I can't use specific checks like the other branches. The PR has tests covering those "recursive" scenarios. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, thanks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, does this need to have some negative termination condition to prevent it from going too far, e.g. not go out to the wrong scope? Or is that impossible? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that it's possible to go out too far here - I'm targeting the nearest variable statement that is guaranteed to be in the current scope. It's also not possible (I think) that this process statements from inner scopes as if it did then we'd have bugs in other branches of this code too. |
||
if (hasSyntacticModifier(variableStatement, ModifierFlags.Export)) { | ||
return true; | ||
} | ||
if (!isDeclarationVisible(variableStatement.parent)) { | ||
return false; | ||
} | ||
return addVisibleAlias(declaration, variableStatement); | ||
} | ||
} | ||
|
||
// Declaration is not visible | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,19 @@ | ||
=== /b.ts === | ||
const { Foo } = require("./a"); | ||
>Foo : Symbol(Foo, Decl(b.ts, 0, 7)) | ||
>require : Symbol(require, Decl(index.d.ts, 0, 13)) | ||
|
||
export default class extends Foo {} | ||
>Foo : Symbol(Foo, Decl(b.ts, 0, 7)) | ||
|
||
=== /node_modules/@types/node/index.d.ts === | ||
declare const require: any; | ||
>require : Symbol(require, Decl(index.d.ts, 0, 13)) | ||
|
||
=== /a.ts === | ||
export class Foo {} | ||
>Foo : Symbol(Foo, Decl(a.ts, 0, 0)) | ||
|
||
=== /b.ts === | ||
import * as A from "./a"; | ||
>A : Symbol(A, Decl(b.ts, 0, 6)) | ||
|
||
const { Foo } = A; | ||
>Foo : Symbol(Foo, Decl(b.ts, 1, 7)) | ||
>A : Symbol(A, Decl(b.ts, 0, 6)) | ||
|
||
export default class extends Foo {} | ||
>Foo : Symbol(Foo, Decl(b.ts, 1, 7)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,19 @@ | ||
=== /b.ts === | ||
const { Foo } = require("./a"); | ||
>Foo : any | ||
>require("./a") : any | ||
>require : any | ||
>"./a" : "./a" | ||
|
||
export default class extends Foo {} | ||
>Foo : any | ||
|
||
=== /node_modules/@types/node/index.d.ts === | ||
declare const require: any; | ||
>require : any | ||
|
||
=== /a.ts === | ||
export class Foo {} | ||
>Foo : Foo | ||
|
||
=== /b.ts === | ||
import * as A from "./a"; | ||
>A : typeof A | ||
|
||
const { Foo } = A; | ||
>Foo : typeof A.Foo | ||
>A : typeof A | ||
|
||
export default class extends Foo {} | ||
>Foo : A.Foo | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
//// [test.ts] | ||
function getFoo() { | ||
return { foo: { test: 42 } } | ||
} | ||
|
||
const { foo } = getFoo() | ||
|
||
export type AliasType = typeof foo | ||
|
||
const { foo: renamed } = getFoo() | ||
|
||
export type AliasType2 = typeof renamed | ||
|
||
function getNested() { | ||
return { a: { b: { c: 'd' } } } | ||
} | ||
|
||
const { a: { b: { c } } } = getNested() | ||
|
||
export type AliasType3 = typeof c | ||
|
||
|
||
//// [test.js] | ||
"use strict"; | ||
exports.__esModule = true; | ||
function getFoo() { | ||
return { foo: { test: 42 } }; | ||
} | ||
var foo = getFoo().foo; | ||
var renamed = getFoo().foo; | ||
function getNested() { | ||
return { a: { b: { c: 'd' } } }; | ||
} | ||
var c = getNested().a.b.c; | ||
|
||
|
||
//// [test.d.ts] | ||
declare const foo: { | ||
test: number; | ||
}; | ||
export declare type AliasType = typeof foo; | ||
declare const renamed: { | ||
test: number; | ||
}; | ||
export declare type AliasType2 = typeof renamed; | ||
declare const c: string; | ||
export declare type AliasType3 = typeof c; | ||
export {}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
=== tests/cases/compiler/test.ts === | ||
function getFoo() { | ||
>getFoo : Symbol(getFoo, Decl(test.ts, 0, 0)) | ||
|
||
return { foo: { test: 42 } } | ||
>foo : Symbol(foo, Decl(test.ts, 1, 10)) | ||
>test : Symbol(test, Decl(test.ts, 1, 17)) | ||
} | ||
|
||
const { foo } = getFoo() | ||
>foo : Symbol(foo, Decl(test.ts, 4, 7)) | ||
>getFoo : Symbol(getFoo, Decl(test.ts, 0, 0)) | ||
|
||
export type AliasType = typeof foo | ||
>AliasType : Symbol(AliasType, Decl(test.ts, 4, 24)) | ||
>foo : Symbol(foo, Decl(test.ts, 4, 7)) | ||
|
||
const { foo: renamed } = getFoo() | ||
>foo : Symbol(foo, Decl(test.ts, 1, 10)) | ||
>renamed : Symbol(renamed, Decl(test.ts, 8, 7)) | ||
>getFoo : Symbol(getFoo, Decl(test.ts, 0, 0)) | ||
|
||
export type AliasType2 = typeof renamed | ||
>AliasType2 : Symbol(AliasType2, Decl(test.ts, 8, 33)) | ||
>renamed : Symbol(renamed, Decl(test.ts, 8, 7)) | ||
|
||
function getNested() { | ||
>getNested : Symbol(getNested, Decl(test.ts, 10, 39)) | ||
|
||
return { a: { b: { c: 'd' } } } | ||
>a : Symbol(a, Decl(test.ts, 13, 10)) | ||
>b : Symbol(b, Decl(test.ts, 13, 15)) | ||
>c : Symbol(c, Decl(test.ts, 13, 20)) | ||
} | ||
|
||
const { a: { b: { c } } } = getNested() | ||
>a : Symbol(a, Decl(test.ts, 13, 10)) | ||
>b : Symbol(b, Decl(test.ts, 13, 15)) | ||
>c : Symbol(c, Decl(test.ts, 16, 17)) | ||
>getNested : Symbol(getNested, Decl(test.ts, 10, 39)) | ||
|
||
export type AliasType3 = typeof c | ||
>AliasType3 : Symbol(AliasType3, Decl(test.ts, 16, 39)) | ||
>c : Symbol(c, Decl(test.ts, 16, 17)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
=== tests/cases/compiler/test.ts === | ||
function getFoo() { | ||
>getFoo : () => { foo: { test: number; }; } | ||
|
||
return { foo: { test: 42 } } | ||
>{ foo: { test: 42 } } : { foo: { test: number; }; } | ||
>foo : { test: number; } | ||
>{ test: 42 } : { test: number; } | ||
>test : number | ||
>42 : 42 | ||
} | ||
|
||
const { foo } = getFoo() | ||
>foo : { test: number; } | ||
>getFoo() : { foo: { test: number; }; } | ||
>getFoo : () => { foo: { test: number; }; } | ||
|
||
export type AliasType = typeof foo | ||
>AliasType : { test: number; } | ||
>foo : { test: number; } | ||
|
||
const { foo: renamed } = getFoo() | ||
>foo : any | ||
>renamed : { test: number; } | ||
>getFoo() : { foo: { test: number; }; } | ||
>getFoo : () => { foo: { test: number; }; } | ||
|
||
export type AliasType2 = typeof renamed | ||
>AliasType2 : { test: number; } | ||
>renamed : { test: number; } | ||
|
||
function getNested() { | ||
>getNested : () => { a: { b: { c: string; }; }; } | ||
|
||
return { a: { b: { c: 'd' } } } | ||
>{ a: { b: { c: 'd' } } } : { a: { b: { c: string; }; }; } | ||
>a : { b: { c: string; }; } | ||
>{ b: { c: 'd' } } : { b: { c: string; }; } | ||
>b : { c: string; } | ||
>{ c: 'd' } : { c: string; } | ||
>c : string | ||
>'d' : "d" | ||
} | ||
|
||
const { a: { b: { c } } } = getNested() | ||
>a : any | ||
>b : any | ||
>c : string | ||
>getNested() : { a: { b: { c: string; }; }; } | ||
>getNested : () => { a: { b: { c: string; }; }; } | ||
|
||
export type AliasType3 = typeof c | ||
>AliasType3 : string | ||
>c : string | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// @declaration: true | ||
// @filename: test.ts | ||
|
||
function getFoo() { | ||
return { foo: { test: 42 } } | ||
} | ||
|
||
const { foo } = getFoo() | ||
|
||
export type AliasType = typeof foo | ||
|
||
const { foo: renamed } = getFoo() | ||
|
||
export type AliasType2 = typeof renamed | ||
|
||
function getNested() { | ||
return { a: { b: { c: 'd' } } } | ||
} | ||
|
||
const { a: { b: { c } } } = getNested() | ||
|
||
export type AliasType3 = typeof c |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the new code is only within this
else if
block