Skip to content

fix(36068): Incorrect quick fix for undeclared private field i… #36373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -5056,6 +5056,10 @@
"category": "Message",
"code": 90034
},
"Declare a private field named '{0}'.": {
"category": "Message",
"code": 90053
},
"Convert function to an ES2015 class": {
"category": "Message",
"code": 95001
Expand Down
40 changes: 32 additions & 8 deletions src/services/codefixes/fixAddMissingMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace ts.codefix {
const { parentDeclaration, declSourceFile, inJs, makeStatic, token, call } = info;
const methodCodeAction = call && getActionForMethodDeclaration(context, declSourceFile, parentDeclaration, token, call, makeStatic, inJs, context.preferences);
const addMember = inJs && !isInterfaceDeclaration(parentDeclaration) ?
singleElementArray(getActionsForAddMissingMemberInJavascriptFile(context, declSourceFile, parentDeclaration, token.text, makeStatic)) :
singleElementArray(getActionsForAddMissingMemberInJavascriptFile(context, declSourceFile, parentDeclaration, token, makeStatic)) :
getActionsForAddMissingMemberInTypeScriptFile(context, declSourceFile, parentDeclaration, token, makeStatic);
return concatenate(singleElementArray(methodCodeAction), addMember);
},
Expand Down Expand Up @@ -70,7 +70,7 @@ namespace ts.codefix {
}
else {
if (inJs && !isInterfaceDeclaration(parentDeclaration)) {
addMissingMemberInJs(changes, declSourceFile, parentDeclaration, token.text, makeStatic);
addMissingMemberInJs(changes, declSourceFile, parentDeclaration, token, makeStatic);
}
else {
const typeNode = getTypeNode(program.getTypeChecker(), parentDeclaration, token);
Expand Down Expand Up @@ -140,7 +140,7 @@ namespace ts.codefix {
if (classOrInterface && !program.isSourceFileFromExternalLibrary(classOrInterface.getSourceFile())) {
const makeStatic = ((leftExpressionType as TypeReference).target || leftExpressionType) !== checker.getDeclaredTypeOfSymbol(symbol);
// Static private identifier properties are not supported yet.
if (makeStatic && isPrivateIdentifier(token)) { return undefined; }
if (makeStatic && isPrivateIdentifier(token)) return undefined;
const declSourceFile = classOrInterface.getSourceFile();
const inJs = isSourceFileJS(declSourceFile);
const call = tryCast(parent.parent, isCallExpression);
Expand All @@ -153,13 +153,20 @@ namespace ts.codefix {
return undefined;
}

function getActionsForAddMissingMemberInJavascriptFile(context: CodeFixContext, declSourceFile: SourceFile, classDeclaration: ClassLikeDeclaration, tokenName: string, makeStatic: boolean): CodeFixAction | undefined {
const changes = textChanges.ChangeTracker.with(context, t => addMissingMemberInJs(t, declSourceFile, classDeclaration, tokenName, makeStatic));
return changes.length === 0 ? undefined
: createCodeFixAction(fixName, changes, [makeStatic ? Diagnostics.Initialize_static_property_0 : Diagnostics.Initialize_property_0_in_the_constructor, tokenName], fixId, Diagnostics.Add_all_missing_members);
function getActionsForAddMissingMemberInJavascriptFile(context: CodeFixContext, declSourceFile: SourceFile, classDeclaration: ClassLikeDeclaration, token: Identifier | PrivateIdentifier, makeStatic: boolean): CodeFixAction | undefined {
const changes = textChanges.ChangeTracker.with(context, t => addMissingMemberInJs(t, declSourceFile, classDeclaration, token, makeStatic));
if (changes.length === 0) {
return undefined;
}

const diagnostic = makeStatic ? Diagnostics.Initialize_static_property_0 :
isPrivateIdentifier(token) ? Diagnostics.Declare_a_private_field_named_0 : Diagnostics.Initialize_property_0_in_the_constructor;

return createCodeFixAction(fixName, changes, [diagnostic, token.text], fixId, Diagnostics.Add_all_missing_members);
}

function addMissingMemberInJs(changeTracker: textChanges.ChangeTracker, declSourceFile: SourceFile, classDeclaration: ClassLikeDeclaration, tokenName: string, makeStatic: boolean): void {
function addMissingMemberInJs(changeTracker: textChanges.ChangeTracker, declSourceFile: SourceFile, classDeclaration: ClassLikeDeclaration, token: Identifier | PrivateIdentifier, makeStatic: boolean): void {
const tokenName = token.text;
if (makeStatic) {
if (classDeclaration.kind === SyntaxKind.ClassExpression) {
return;
Expand All @@ -168,6 +175,23 @@ namespace ts.codefix {
const staticInitialization = initializePropertyToUndefined(createIdentifier(className), tokenName);
changeTracker.insertNodeAfter(declSourceFile, classDeclaration, staticInitialization);
}
else if (isPrivateIdentifier(token)) {
const property = createProperty(
/*decorators*/ undefined,
/*modifiers*/ undefined,
tokenName,
/*questionToken*/ undefined,
/*type*/ undefined,
/*initializer*/ undefined);

const lastProp = getNodeToInsertPropertyAfter(classDeclaration);
if (lastProp) {
changeTracker.insertNodeAfter(declSourceFile, lastProp, property);
}
else {
changeTracker.insertNodeAtClassStart(declSourceFile, classDeclaration, property);
}
}
else {
const classConstructor = getFirstConstructorWithBody(classDeclaration);
if (!classConstructor) {
Expand Down
29 changes: 29 additions & 0 deletions tests/cases/fourslash/codeFixInitializePrivatePropertyJS.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/// <reference path='fourslash.ts' />

// @allowjs: true
// @checkJs: true

// @Filename: /a.js
////class Foo {
//// constructor(name) {
//// this.[|#name|] = name;
//// }
////}

verify.codeFixAvailable([
{ description: "Declare a private field named '#name'." },
{ description: "Ignore this error message" },
{ description: "Disable checking for this file" },
{ description: "Infer parameter types from usage" },
]);

verify.codeFix({
index: 0,
description: [ts.Diagnostics.Declare_a_private_field_named_0.message, '#name'],
newFileContent: `class Foo {
#name;
constructor(name) {
this.#name = name;
}
}`
});