Skip to content

Commit

Permalink
fix(49546): create computed property name for symbol props (#49554)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-tarasyuk committed Jun 20, 2022
1 parent c01afb5 commit 74d76e9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/services/codefixes/fixAddMissingMember.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -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 changes: 15 additions & 0 deletions tests/cases/fourslash/codeFixAddMissingProperties22.ts
@@ -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.");
}
}`,
});

0 comments on commit 74d76e9

Please sign in to comment.