Skip to content

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

Merged
merged 3 commits into from
Jul 6, 2022
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
26 changes: 19 additions & 7 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor Author

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

&& 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)!;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only remaining comment is the new branch's use of findAncestor, as compared to the existing code's direct use of parents and specific checks; I'm not entirely certain that it matters but I do wonder how similar the two paths in the new isBindingElement block are and if they should look the same and/or be sharing code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks.

Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Expand Down

This file was deleted.

29 changes: 26 additions & 3 deletions tests/baselines/reference/declarationEmitExpressionInExtends6.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,25 @@
//// [index.d.ts]
declare const require: any;

//// [a.js]
//// [a.ts]
export class Foo {}

//// [b.ts]
const { Foo } = require("./a");
import * as A from "./a";
const { Foo } = A;
export default class extends Foo {}


//// [a.js]
"use strict";
exports.__esModule = true;
exports.Foo = void 0;
var Foo = /** @class */ (function () {
function Foo() {
}
return Foo;
}());
exports.Foo = Foo;
//// [b.js]
"use strict";
var __extends = (this && this.__extends) || (function () {
Expand All @@ -29,7 +40,8 @@ var __extends = (this && this.__extends) || (function () {
};
})();
exports.__esModule = true;
var Foo = require("./a").Foo;
var A = require("./a");
var Foo = A.Foo;
var default_1 = /** @class */ (function (_super) {
__extends(default_1, _super);
function default_1() {
Expand All @@ -38,3 +50,14 @@ var default_1 = /** @class */ (function (_super) {
return default_1;
}(Foo));
exports["default"] = default_1;


//// [a.d.ts]
export declare class Foo {
}
//// [b.d.ts]
import * as A from "./a";
declare const Foo: typeof A.Foo;
export default class extends Foo {
}
export {};
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))

25 changes: 15 additions & 10 deletions tests/baselines/reference/declarationEmitExpressionInExtends6.types
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

5 changes: 3 additions & 2 deletions tests/cases/compiler/declarationEmitExpressionInExtends6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
// @Filename: /node_modules/@types/node/index.d.ts
declare const require: any;

// @Filename: /a.js
// @Filename: /a.ts
export class Foo {}

// @Filename: /b.ts
const { Foo } = require("./a");
import * as A from "./a";
const { Foo } = A;
export default class extends Foo {}
22 changes: 22 additions & 0 deletions tests/cases/compiler/declarationEmitNonExportedBindingPattern.ts
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