Skip to content

Commit 9d0dc77

Browse files
authored
fix(55494): Invalid declaration with computed property using imported symbol (#55529)
1 parent 788239f commit 9d0dc77

File tree

5 files changed

+90
-0
lines changed

5 files changed

+90
-0
lines changed

src/compiler/transformers/declarations.ts

+4
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ import {
100100
isBindingPattern,
101101
isClassDeclaration,
102102
isClassElement,
103+
isComputedPropertyName,
103104
isDeclaration,
104105
isElementAccessExpression,
105106
isEntityName,
@@ -701,6 +702,9 @@ export function transformDeclarations(context: TransformationContext) {
701702
if (elem.kind === SyntaxKind.OmittedExpression) {
702703
return elem;
703704
}
705+
if (elem.propertyName && isComputedPropertyName(elem.propertyName) && isEntityNameExpression(elem.propertyName.expression)) {
706+
checkEntityNameVisibility(elem.propertyName.expression, enclosingDeclaration);
707+
}
704708
if (elem.propertyName && isIdentifier(elem.propertyName) && isIdentifier(elem.name) && !elem.symbol.isReferenced && !isIdentifierANonContextualKeyword(elem.propertyName)) {
705709
// Unnecessary property renaming is forbidden in types, so remove renaming
706710
return factory.updateBindingElement(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//// [tests/cases/compiler/computedPropertyNameWithImportedKey.ts] ////
2+
3+
//// [a.ts]
4+
export const a = Symbol();
5+
6+
//// [b.ts]
7+
import { a } from "./a";
8+
export function fn({ [a]: value }: any): string {
9+
return value;
10+
}
11+
12+
13+
//// [a.js]
14+
"use strict";
15+
Object.defineProperty(exports, "__esModule", { value: true });
16+
exports.a = void 0;
17+
exports.a = Symbol();
18+
//// [b.js]
19+
"use strict";
20+
Object.defineProperty(exports, "__esModule", { value: true });
21+
exports.fn = void 0;
22+
var a_1 = require("./a");
23+
function fn(_a) {
24+
var _b = a_1.a, value = _a[_b];
25+
return value;
26+
}
27+
exports.fn = fn;
28+
29+
30+
//// [a.d.ts]
31+
export declare const a: unique symbol;
32+
//// [b.d.ts]
33+
import { a } from "./a";
34+
export declare function fn({ [a]: value }: any): string;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//// [tests/cases/compiler/computedPropertyNameWithImportedKey.ts] ////
2+
3+
=== /a.ts ===
4+
export const a = Symbol();
5+
>a : Symbol(a, Decl(a.ts, 0, 12))
6+
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
7+
8+
=== /b.ts ===
9+
import { a } from "./a";
10+
>a : Symbol(a, Decl(b.ts, 0, 8))
11+
12+
export function fn({ [a]: value }: any): string {
13+
>fn : Symbol(fn, Decl(b.ts, 0, 24))
14+
>a : Symbol(a, Decl(b.ts, 0, 8))
15+
>value : Symbol(value, Decl(b.ts, 1, 20))
16+
17+
return value;
18+
>value : Symbol(value, Decl(b.ts, 1, 20))
19+
}
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [tests/cases/compiler/computedPropertyNameWithImportedKey.ts] ////
2+
3+
=== /a.ts ===
4+
export const a = Symbol();
5+
>a : unique symbol
6+
>Symbol() : unique symbol
7+
>Symbol : SymbolConstructor
8+
9+
=== /b.ts ===
10+
import { a } from "./a";
11+
>a : unique symbol
12+
13+
export function fn({ [a]: value }: any): string {
14+
>fn : ({ [a]: value }: any) => string
15+
>a : unique symbol
16+
>value : any
17+
18+
return value;
19+
>value : any
20+
}
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @declaration: true
2+
// @lib: es6
3+
4+
// @filename: /a.ts
5+
export const a = Symbol();
6+
7+
// @filename: /b.ts
8+
import { a } from "./a";
9+
export function fn({ [a]: value }: any): string {
10+
return value;
11+
}

0 commit comments

Comments
 (0)