fix(55494): Invalid declaration with computed property using imported symbol (#55529)

This commit is contained in:
Oleksandr T 2023-08-28 21:40:03 +03:00 committed by GitHub
parent 788239ff87
commit 9d0dc77712
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 90 additions and 0 deletions

View File

@ -100,6 +100,7 @@ import {
isBindingPattern,
isClassDeclaration,
isClassElement,
isComputedPropertyName,
isDeclaration,
isElementAccessExpression,
isEntityName,
@ -701,6 +702,9 @@ export function transformDeclarations(context: TransformationContext) {
if (elem.kind === SyntaxKind.OmittedExpression) {
return elem;
}
if (elem.propertyName && isComputedPropertyName(elem.propertyName) && isEntityNameExpression(elem.propertyName.expression)) {
checkEntityNameVisibility(elem.propertyName.expression, enclosingDeclaration);
}
if (elem.propertyName && isIdentifier(elem.propertyName) && isIdentifier(elem.name) && !elem.symbol.isReferenced && !isIdentifierANonContextualKeyword(elem.propertyName)) {
// Unnecessary property renaming is forbidden in types, so remove renaming
return factory.updateBindingElement(

View File

@ -0,0 +1,34 @@
//// [tests/cases/compiler/computedPropertyNameWithImportedKey.ts] ////
//// [a.ts]
export const a = Symbol();
//// [b.ts]
import { a } from "./a";
export function fn({ [a]: value }: any): string {
return value;
}
//// [a.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.a = void 0;
exports.a = Symbol();
//// [b.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fn = void 0;
var a_1 = require("./a");
function fn(_a) {
var _b = a_1.a, value = _a[_b];
return value;
}
exports.fn = fn;
//// [a.d.ts]
export declare const a: unique symbol;
//// [b.d.ts]
import { a } from "./a";
export declare function fn({ [a]: value }: any): string;

View File

@ -0,0 +1,20 @@
//// [tests/cases/compiler/computedPropertyNameWithImportedKey.ts] ////
=== /a.ts ===
export const a = Symbol();
>a : Symbol(a, Decl(a.ts, 0, 12))
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
=== /b.ts ===
import { a } from "./a";
>a : Symbol(a, Decl(b.ts, 0, 8))
export function fn({ [a]: value }: any): string {
>fn : Symbol(fn, Decl(b.ts, 0, 24))
>a : Symbol(a, Decl(b.ts, 0, 8))
>value : Symbol(value, Decl(b.ts, 1, 20))
return value;
>value : Symbol(value, Decl(b.ts, 1, 20))
}

View File

@ -0,0 +1,21 @@
//// [tests/cases/compiler/computedPropertyNameWithImportedKey.ts] ////
=== /a.ts ===
export const a = Symbol();
>a : unique symbol
>Symbol() : unique symbol
>Symbol : SymbolConstructor
=== /b.ts ===
import { a } from "./a";
>a : unique symbol
export function fn({ [a]: value }: any): string {
>fn : ({ [a]: value }: any) => string
>a : unique symbol
>value : any
return value;
>value : any
}

View File

@ -0,0 +1,11 @@
// @declaration: true
// @lib: es6
// @filename: /a.ts
export const a = Symbol();
// @filename: /b.ts
import { a } from "./a";
export function fn({ [a]: value }: any): string {
return value;
}