mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-06 11:08:51 -06:00
fix(49546): create computed property name for symbol props (#49554)
This commit is contained in:
parent
c01afb5ef3
commit
74d76e93b3
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
15
tests/cases/fourslash/codeFixAddMissingProperties22.ts
Normal file
15
tests/cases/fourslash/codeFixAddMissingProperties22.ts
Normal 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.");
|
||||
}
|
||||
}`,
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user