Skip to content

Commit bbf9115

Browse files
andrewbranchtypescript-bot
authored andcommitted
Cherry-pick PR microsoft#37264 into release-3.8
Component commits: 2669ce7 Fix longer type-only property access in non-emitting heritage clauses eced0e0 Rename misnomer function
1 parent 712967b commit bbf9115

File tree

5 files changed

+88
-5
lines changed

5 files changed

+88
-5
lines changed

src/compiler/utilities.ts

+15-5
Original file line numberDiff line numberDiff line change
@@ -6167,7 +6167,7 @@ namespace ts {
61676167
export function isValidTypeOnlyAliasUseSite(useSite: Node): boolean {
61686168
return !!(useSite.flags & NodeFlags.Ambient)
61696169
|| isPartOfTypeQuery(useSite)
6170-
|| isFirstIdentifierOfNonEmittingHeritageClause(useSite)
6170+
|| isIdentifierInNonEmittingHeritageClause(useSite)
61716171
|| isPartOfPossiblyValidTypeOrAbstractComputedPropertyName(useSite)
61726172
|| !isExpressionNode(useSite);
61736173
}
@@ -6190,10 +6190,20 @@ namespace ts {
61906190
return containerKind === SyntaxKind.InterfaceDeclaration || containerKind === SyntaxKind.TypeLiteral;
61916191
}
61926192

6193-
/** Returns true for the first identifier of 1) an `implements` clause, and 2) an `extends` clause of an interface. */
6194-
function isFirstIdentifierOfNonEmittingHeritageClause(node: Node): boolean {
6195-
// Number of parents to climb from identifier is 2 for `implements I`, 3 for `implements x.I`
6196-
const heritageClause = tryCast(node.parent.parent, isHeritageClause) ?? tryCast(node.parent.parent?.parent, isHeritageClause);
6193+
/** Returns true for an identifier in 1) an `implements` clause, and 2) an `extends` clause of an interface. */
6194+
function isIdentifierInNonEmittingHeritageClause(node: Node): boolean {
6195+
if (node.kind !== SyntaxKind.Identifier) return false;
6196+
const heritageClause = findAncestor(node.parent, parent => {
6197+
switch (parent.kind) {
6198+
case SyntaxKind.HeritageClause:
6199+
return true;
6200+
case SyntaxKind.PropertyAccessExpression:
6201+
case SyntaxKind.ExpressionWithTypeArguments:
6202+
return false;
6203+
default:
6204+
return "quit";
6205+
}
6206+
}) as HeritageClause | undefined;
61976207
return heritageClause?.token === SyntaxKind.ImplementsKeyword || heritageClause?.parent.kind === SyntaxKind.InterfaceDeclaration;
61986208
}
61996209
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//// [tests/cases/conformance/externalModules/typeOnly/nestedNamespace.ts] ////
2+
3+
//// [a.ts]
4+
export namespace types {
5+
export class A {}
6+
}
7+
8+
//// [b.ts]
9+
import type * as a from './a';
10+
interface B extends a.types.A {}
11+
12+
13+
//// [a.js]
14+
"use strict";
15+
exports.__esModule = true;
16+
exports.types = void 0;
17+
var types;
18+
(function (types) {
19+
var A = /** @class */ (function () {
20+
function A() {
21+
}
22+
return A;
23+
}());
24+
types.A = A;
25+
})(types = exports.types || (exports.types = {}));
26+
//// [b.js]
27+
"use strict";
28+
exports.__esModule = true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
=== tests/cases/conformance/externalModules/typeOnly/a.ts ===
2+
export namespace types {
3+
>types : Symbol(types, Decl(a.ts, 0, 0))
4+
5+
export class A {}
6+
>A : Symbol(A, Decl(a.ts, 0, 24))
7+
}
8+
9+
=== tests/cases/conformance/externalModules/typeOnly/b.ts ===
10+
import type * as a from './a';
11+
>a : Symbol(a, Decl(b.ts, 0, 11))
12+
13+
interface B extends a.types.A {}
14+
>B : Symbol(B, Decl(b.ts, 0, 30))
15+
>a.types.A : Symbol(a.types.A, Decl(a.ts, 0, 24))
16+
>a.types : Symbol(a.types, Decl(a.ts, 0, 0))
17+
>a : Symbol(a, Decl(b.ts, 0, 11))
18+
>types : Symbol(a.types, Decl(a.ts, 0, 0))
19+
>A : Symbol(a.types.A, Decl(a.ts, 0, 24))
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/conformance/externalModules/typeOnly/a.ts ===
2+
export namespace types {
3+
>types : typeof types
4+
5+
export class A {}
6+
>A : A
7+
}
8+
9+
=== tests/cases/conformance/externalModules/typeOnly/b.ts ===
10+
import type * as a from './a';
11+
>a : typeof a
12+
13+
interface B extends a.types.A {}
14+
>a.types : typeof a.types
15+
>a : typeof a
16+
>types : typeof a.types
17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @Filename: a.ts
2+
export namespace types {
3+
export class A {}
4+
}
5+
6+
// @Filename: b.ts
7+
import type * as a from './a';
8+
interface B extends a.types.A {}

0 commit comments

Comments
 (0)