fix(49546): create computed property name for symbol props (#49554)

This commit is contained in:
Oleksandr T 2022-06-20 23:46:47 +03:00 committed by GitHub
parent c01afb5ef3
commit 74d76e93b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 1 deletions

View File

@ -485,7 +485,7 @@ namespace ts.codefix {
const checker = context.program.getTypeChecker();
const props = map(info.properties, prop => {
const initializer = tryGetValueFromType(context, checker, importAdder, quotePreference, checker.getTypeOfSymbol(prop), info.parentDeclaration);
return factory.createPropertyAssignment(createPropertyNameNodeForIdentifierOrLiteral(prop.name, target, quotePreference === QuotePreference.Single), initializer);
return factory.createPropertyAssignment(createPropertyNameFromSymbol(prop, target, quotePreference, checker), initializer);
});
const options = {
leadingTriviaOption: textChanges.LeadingTriviaOption.Exclude,
@ -608,4 +608,14 @@ namespace ts.codefix {
const declaration = findAncestor(callExpression, n => isMethodDeclaration(n) || isConstructorDeclaration(n));
return declaration && declaration.parent === node ? declaration : undefined;
}
function createPropertyNameFromSymbol(symbol: Symbol, target: ScriptTarget, quotePreference: QuotePreference, checker: TypeChecker) {
if (isTransientSymbol(symbol) && symbol.nameType && symbol.nameType.flags & TypeFlags.UniqueESSymbol) {
const expression = checker.symbolToExpression((symbol.nameType as UniqueESSymbolType).symbol, SymbolFlags.Value, symbol.valueDeclaration, NodeBuilderFlags.AllowUniqueESSymbolType);
if (expression) {
return factory.createComputedPropertyName(expression);
}
}
return createPropertyNameNodeForIdentifierOrLiteral(symbol.name, target, quotePreference === QuotePreference.Single);
}
}

View File

@ -0,0 +1,15 @@
/// <reference path="fourslash.ts" />
// @lib: es2020
////const x: Iterable<number> = {}
verify.codeFix({
index: 0,
description: ts.Diagnostics.Add_missing_properties.message,
newFileContent:
`const x: Iterable<number> = {
[Symbol.iterator]: function(): Iterator<number, any, undefined> {
throw new Error("Function not implemented.");
}
}`,
});