Skip to content

Allow type-only namespace imports in implements clauses #36464

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
Jan 28, 2020
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
6 changes: 6 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1792,6 +1792,11 @@ namespace ts {
return containerKind === SyntaxKind.InterfaceDeclaration || containerKind === SyntaxKind.TypeLiteral;
}

export function isFirstIdentifierOfImplementsClause(node: Node) {
return node.parent?.parent?.parent?.kind === SyntaxKind.HeritageClause
Copy link
Member

Choose a reason for hiding this comment

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

why does it have to be the first identifier? Why can't it be anywhere?

(That long chain of ?.s gives me the shivers.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Because this is a hot path and any other location in the node will be covered by other criteria of isValidTypeOnlyAliasUseSite, so it doesn’t make sense to check more than necessary.

node could be an identifier at the top level of a SourceFile, so I think that’d be SourceFile > ExpressionStatement > Identifier, so I guess only the last ?. is needed.

Copy link
Member

Choose a reason for hiding this comment

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

Also I don't think it checks the scenario of type ? types.SomeInterfaceContainingNamespace.Component since its parent is going to be one more level up than just types.Component ?

Copy link
Member Author

@andrewbranch andrewbranch Jan 27, 2020

Choose a reason for hiding this comment

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

any other location in the node will be covered by other criteria of isValidTypeOnlyAliasUseSite,

double checking that this is true

Copy link
Member Author

Choose a reason for hiding this comment

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

@sheetalkamat this only gets called on identifiers from resolveName, which will only be the left-most identifier. The other parts of entity names get checked elsewhere.

&& (node.parent.parent.parent as HeritageClause).token === SyntaxKind.ImplementsKeyword;
}

export function isExternalModuleImportEqualsDeclaration(node: Node): node is ImportEqualsDeclaration & { moduleReference: ExternalModuleReference } {
return node.kind === SyntaxKind.ImportEqualsDeclaration && (<ImportEqualsDeclaration>node).moduleReference.kind === SyntaxKind.ExternalModuleReference;
}
Expand Down Expand Up @@ -6138,6 +6143,7 @@ namespace ts {
export function isValidTypeOnlyAliasUseSite(useSite: Node): boolean {
return !!(useSite.flags & NodeFlags.Ambient)
|| isPartOfTypeQuery(useSite)
|| isFirstIdentifierOfImplementsClause(useSite)
|| isPartOfPossiblyValidTypeOrAbstractComputedPropertyName(useSite)
|| !isExpressionNode(useSite);
}
Expand Down
36 changes: 36 additions & 0 deletions tests/baselines/reference/implementsClause.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//// [tests/cases/conformance/externalModules/typeOnly/implementsClause.ts] ////

//// [types.ts]
export interface Component {}

//// [ns.ts]
import type * as types from './types';
export { types };

//// [index.ts]
import type * as types from './types';
import * as nestedNamespace from './ns';

class C implements types.Component {}
class D implements nestedNamespace.types.Component {}


//// [types.js]
"use strict";
exports.__esModule = true;
//// [ns.js]
"use strict";
exports.__esModule = true;
//// [index.js]
"use strict";
exports.__esModule = true;
var C = /** @class */ (function () {
function C() {
}
return C;
}());
var D = /** @class */ (function () {
function D() {
}
return D;
}());
32 changes: 32 additions & 0 deletions tests/baselines/reference/implementsClause.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
=== tests/cases/conformance/externalModules/typeOnly/types.ts ===
export interface Component {}
>Component : Symbol(Component, Decl(types.ts, 0, 0))

=== tests/cases/conformance/externalModules/typeOnly/ns.ts ===
import type * as types from './types';
>types : Symbol(types, Decl(ns.ts, 0, 11))

export { types };
>types : Symbol(types, Decl(ns.ts, 1, 8))

=== tests/cases/conformance/externalModules/typeOnly/index.ts ===
import type * as types from './types';
>types : Symbol(types, Decl(index.ts, 0, 11))

import * as nestedNamespace from './ns';
>nestedNamespace : Symbol(nestedNamespace, Decl(index.ts, 1, 6))

class C implements types.Component {}
>C : Symbol(C, Decl(index.ts, 1, 40))
>types.Component : Symbol(types.Component, Decl(types.ts, 0, 0))
>types : Symbol(types, Decl(index.ts, 0, 11))
>Component : Symbol(types.Component, Decl(types.ts, 0, 0))

class D implements nestedNamespace.types.Component {}
>D : Symbol(D, Decl(index.ts, 3, 37))
>nestedNamespace.types.Component : Symbol(types.Component, Decl(types.ts, 0, 0))
>nestedNamespace.types : Symbol(nestedNamespace.types, Decl(ns.ts, 1, 8))
>nestedNamespace : Symbol(nestedNamespace, Decl(index.ts, 1, 6))
>types : Symbol(nestedNamespace.types, Decl(ns.ts, 1, 8))
>Component : Symbol(types.Component, Decl(types.ts, 0, 0))

27 changes: 27 additions & 0 deletions tests/baselines/reference/implementsClause.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
=== tests/cases/conformance/externalModules/typeOnly/types.ts ===
export interface Component {}
No type information for this code.
No type information for this code.=== tests/cases/conformance/externalModules/typeOnly/ns.ts ===
import type * as types from './types';
>types : typeof types

export { types };
>types : typeof types

=== tests/cases/conformance/externalModules/typeOnly/index.ts ===
import type * as types from './types';
>types : typeof types

import * as nestedNamespace from './ns';
>nestedNamespace : typeof nestedNamespace

class C implements types.Component {}
>C : C
>types : typeof types

class D implements nestedNamespace.types.Component {}
>D : D
>nestedNamespace.types : any
>nestedNamespace : typeof nestedNamespace
>types : any

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @Filename: types.ts
export interface Component {}

// @Filename: ns.ts
import type * as types from './types';
export { types };

// @Filename: index.ts
import type * as types from './types';
import * as nestedNamespace from './ns';

class C implements types.Component {}
class D implements nestedNamespace.types.Component {}