diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 5550cad86b7..b64b0c99076 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -4446,5 +4446,13 @@ "Convert named export to default export": { "category": "Message", "code": 95062 + }, + "Add missing enum member '{0}'": { + "category": "Message", + "code": 95063 + }, + "Add all missing enum members": { + "category": "Message", + "code": 95064 } } diff --git a/src/services/codefixes/fixAddMissingMember.ts b/src/services/codefixes/fixAddMissingMember.ts index 38f4324af62..b63ba394b0b 100644 --- a/src/services/codefixes/fixAddMissingMember.ts +++ b/src/services/codefixes/fixAddMissingMember.ts @@ -11,6 +11,12 @@ namespace ts.codefix { getCodeActions(context) { const info = getInfo(context.sourceFile, context.span.start, context.program.getTypeChecker()); if (!info) return undefined; + + if (info.kind === InfoKind.enum) { + const { token, enumDeclaration } = info; + const changes = textChanges.ChangeTracker.with(context, t => addEnumMemberDeclaration(t, context.program.getTypeChecker(), token, enumDeclaration)); + return singleElementArray(createCodeFixAction(fixName, changes, [Diagnostics.Add_missing_enum_member_0, token.text], fixId, Diagnostics.Add_all_missing_enum_members)); + } const { classDeclaration, classDeclarationSourceFile, inJs, makeStatic, token, call } = info; const methodCodeAction = call && getActionForMethodDeclaration(context, classDeclarationSourceFile, classDeclaration, token, call, makeStatic, inJs, context.preferences); const addMember = inJs ? @@ -23,31 +29,41 @@ namespace ts.codefix { const seenNames = createMap(); return codeFixAll(context, errorCodes, (changes, diag) => { const { program, preferences } = context; - const info = getInfo(diag.file, diag.start, program.getTypeChecker()); - if (!info) return; - const { classDeclaration, classDeclarationSourceFile, inJs, makeStatic, token, call } = info; - if (!addToSeen(seenNames, token.text)) { + const checker = program.getTypeChecker(); + const info = getInfo(diag.file, diag.start, checker); + if (!info || !addToSeen(seenNames, info.token.text)) { return; } - // Always prefer to add a method declaration if possible. - if (call) { - addMethodDeclaration(context, changes, classDeclarationSourceFile, classDeclaration, token, call, makeStatic, inJs, preferences); + if (info.kind === InfoKind.enum) { + const { token, enumDeclaration } = info; + addEnumMemberDeclaration(changes, checker, token, enumDeclaration); } else { - if (inJs) { - addMissingMemberInJs(changes, classDeclarationSourceFile, classDeclaration, token.text, makeStatic); + const { classDeclaration, classDeclarationSourceFile, inJs, makeStatic, token, call } = info; + // Always prefer to add a method declaration if possible. + if (call) { + addMethodDeclaration(context, changes, classDeclarationSourceFile, classDeclaration, token, call, makeStatic, inJs, preferences); } else { - const typeNode = getTypeNode(program.getTypeChecker(), classDeclaration, token); - addPropertyDeclaration(changes, classDeclarationSourceFile, classDeclaration, token.text, typeNode, makeStatic); + if (inJs) { + addMissingMemberInJs(changes, classDeclarationSourceFile, classDeclaration, token.text, makeStatic); + } + else { + const typeNode = getTypeNode(program.getTypeChecker(), classDeclaration, token); + addPropertyDeclaration(changes, classDeclarationSourceFile, classDeclaration, token.text, typeNode, makeStatic); + } } } }); }, }); - interface Info { token: Identifier; classDeclaration: ClassLikeDeclaration; makeStatic: boolean; classDeclarationSourceFile: SourceFile; inJs: boolean; call: CallExpression | undefined; } + enum InfoKind { enum, class } + interface EnumInfo { kind: InfoKind.enum; token: Identifier; enumDeclaration: EnumDeclaration; } + interface ClassInfo { kind: InfoKind.class; token: Identifier; classDeclaration: ClassLikeDeclaration; makeStatic: boolean; classDeclarationSourceFile: SourceFile; inJs: boolean; call: CallExpression | undefined; } + type Info = EnumInfo | ClassInfo; + function getInfo(tokenSourceFile: SourceFile, tokenPos: number, checker: TypeChecker): Info | undefined { // The identifier of the missing property. eg: // this.missing = 1; @@ -62,15 +78,21 @@ namespace ts.codefix { const leftExpressionType = skipConstraint(checker.getTypeAtLocation(parent.expression)!); const { symbol } = leftExpressionType; - const classDeclaration = symbol && symbol.declarations && find(symbol.declarations, isClassLike); - if (!classDeclaration) return undefined; + if (!symbol || !symbol.declarations) return undefined; - const makeStatic = (leftExpressionType as TypeReference).target !== checker.getDeclaredTypeOfSymbol(symbol); - const classDeclarationSourceFile = classDeclaration.getSourceFile(); - const inJs = isSourceFileJavaScript(classDeclarationSourceFile); - const call = tryCast(parent.parent, isCallExpression); - - return { token, classDeclaration, makeStatic, classDeclarationSourceFile, inJs, call }; + const classDeclaration = find(symbol.declarations, isClassLike); + if (classDeclaration) { + const makeStatic = (leftExpressionType as TypeReference).target !== checker.getDeclaredTypeOfSymbol(symbol); + const classDeclarationSourceFile = classDeclaration.getSourceFile(); + const inJs = isSourceFileJavaScript(classDeclarationSourceFile); + const call = tryCast(parent.parent, isCallExpression); + return { kind: InfoKind.class, token, classDeclaration, makeStatic, classDeclarationSourceFile, inJs, call }; + } + const enumDeclaration = find(symbol.declarations, isEnumDeclaration); + if (enumDeclaration) { + return { kind: InfoKind.enum, token, enumDeclaration }; + } + return undefined; } function getActionsForAddMissingMemberInJavaScriptFile(context: CodeFixContext, classDeclarationSourceFile: SourceFile, classDeclaration: ClassLikeDeclaration, tokenName: string, makeStatic: boolean): CodeFixAction | undefined { @@ -209,4 +231,25 @@ namespace ts.codefix { changeTracker.insertNodeAtClassStart(classDeclarationSourceFile, classDeclaration, methodDeclaration); } } + + function addEnumMemberDeclaration(changes: textChanges.ChangeTracker, checker: TypeChecker, token: Identifier, enumDeclaration: EnumDeclaration) { + /** + * create initializer only literal enum that has string initializer. + * value of initializer is a string literal that equal to name of enum member. + * numeric enum or empty enum will not create initializer. + */ + const hasStringInitializer = some(enumDeclaration.members, member => { + const type = checker.getTypeAtLocation(member); + return !!(type && type.flags & TypeFlags.StringLike); + }); + + const enumMember = createEnumMember(token, hasStringInitializer ? createStringLiteral(token.text) : undefined); + changes.replaceNode(enumDeclaration.getSourceFile(), enumDeclaration, updateEnumDeclaration( + enumDeclaration, + enumDeclaration.decorators, + enumDeclaration.modifiers, + enumDeclaration.name, + concatenate(enumDeclaration.members, singleElementArray(enumMember)) + )); + } } diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 05702bab14b..6fd61aa7369 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -5946,6 +5946,8 @@ declare namespace ts { Remove_braces_from_arrow_function: DiagnosticMessage; Convert_default_export_to_named_export: DiagnosticMessage; Convert_named_export_to_default_export: DiagnosticMessage; + Add_missing_enum_member_0: DiagnosticMessage; + Add_all_missing_enum_members: DiagnosticMessage; }; } declare namespace ts { diff --git a/tests/cases/fourslash/codeFixAddMissingEnumMember1.ts b/tests/cases/fourslash/codeFixAddMissingEnumMember1.ts new file mode 100644 index 00000000000..e99b653ee08 --- /dev/null +++ b/tests/cases/fourslash/codeFixAddMissingEnumMember1.ts @@ -0,0 +1,16 @@ +/// + +////enum E { +//// a +////} +////E.b + +verify.codeFix({ + description: "Add missing enum member 'b'", + newFileContent: `enum E { + a, + b +} +E.b` +}); + diff --git a/tests/cases/fourslash/codeFixAddMissingEnumMember10.ts b/tests/cases/fourslash/codeFixAddMissingEnumMember10.ts new file mode 100644 index 00000000000..dba3051fee9 --- /dev/null +++ b/tests/cases/fourslash/codeFixAddMissingEnumMember10.ts @@ -0,0 +1,25 @@ +/// + +////enum E { +//// a, +//// b = 1, +//// c = "123" +////} +////enum A { +//// a = E.c +////} +////A.b + +verify.codeFix({ + description: "Add missing enum member 'b'", + newFileContent: `enum E { + a, + b = 1, + c = "123" +} +enum A { + a = E.c, + b +} +A.b` +}); diff --git a/tests/cases/fourslash/codeFixAddMissingEnumMember11.ts b/tests/cases/fourslash/codeFixAddMissingEnumMember11.ts new file mode 100644 index 00000000000..4928b52dba6 --- /dev/null +++ b/tests/cases/fourslash/codeFixAddMissingEnumMember11.ts @@ -0,0 +1,31 @@ +/// + +////enum E { +//// a, +//// b = 1, +//// c = "123" +////} +////enum A { +//// a = E.c +////} +////enum B { +//// b = A.a +////} +////B.c + +verify.codeFix({ + description: "Add missing enum member 'c'", + newFileContent: `enum E { + a, + b = 1, + c = "123" +} +enum A { + a = E.c +} +enum B { + b = A.a, + c +} +B.c` +}); diff --git a/tests/cases/fourslash/codeFixAddMissingEnumMember2.ts b/tests/cases/fourslash/codeFixAddMissingEnumMember2.ts new file mode 100644 index 00000000000..16e456beba7 --- /dev/null +++ b/tests/cases/fourslash/codeFixAddMissingEnumMember2.ts @@ -0,0 +1,16 @@ +/// + +////enum E { +//// a = 1 +////} +////E.b + +verify.codeFix({ + description: "Add missing enum member 'b'", + newFileContent: `enum E { + a = 1, + b +} +E.b` +}); + diff --git a/tests/cases/fourslash/codeFixAddMissingEnumMember3.ts b/tests/cases/fourslash/codeFixAddMissingEnumMember3.ts new file mode 100644 index 00000000000..b6848021d1f --- /dev/null +++ b/tests/cases/fourslash/codeFixAddMissingEnumMember3.ts @@ -0,0 +1,21 @@ +/// + +////enum E { +//// a, +//// b = 1, +//// c +////} +////E.d + +verify.codeFix({ + description: "Add missing enum member 'd'", + newFileContent: `enum E { + a, + b = 1, + c, + d +} +E.d` +}); + + diff --git a/tests/cases/fourslash/codeFixAddMissingEnumMember4.ts b/tests/cases/fourslash/codeFixAddMissingEnumMember4.ts new file mode 100644 index 00000000000..a4e20401556 --- /dev/null +++ b/tests/cases/fourslash/codeFixAddMissingEnumMember4.ts @@ -0,0 +1,17 @@ +/// + +////enum E { +//// a = "a", +////} +////E.b + +verify.codeFix({ + description: "Add missing enum member 'b'", + newFileContent: `enum E { + a = "a", + b = "b" +} +E.b` +}); + + diff --git a/tests/cases/fourslash/codeFixAddMissingEnumMember5.ts b/tests/cases/fourslash/codeFixAddMissingEnumMember5.ts new file mode 100644 index 00000000000..9d0fe55859e --- /dev/null +++ b/tests/cases/fourslash/codeFixAddMissingEnumMember5.ts @@ -0,0 +1,17 @@ +/// + +////enum E { +//// a = "a" + "-", +////} +////E.b + +verify.codeFix({ + description: "Add missing enum member 'b'", + newFileContent: `enum E { + a = "a" + "-", + b = "b" +} +E.b` +}); + + diff --git a/tests/cases/fourslash/codeFixAddMissingEnumMember6.ts b/tests/cases/fourslash/codeFixAddMissingEnumMember6.ts new file mode 100644 index 00000000000..71155a81736 --- /dev/null +++ b/tests/cases/fourslash/codeFixAddMissingEnumMember6.ts @@ -0,0 +1,17 @@ +/// + +////enum E { +//// a = "b" +////} +////E.b + +verify.codeFix({ + description: "Add missing enum member 'b'", + newFileContent: `enum E { + a = "b", + b = "b" +} +E.b` +}); + + diff --git a/tests/cases/fourslash/codeFixAddMissingEnumMember7.ts b/tests/cases/fourslash/codeFixAddMissingEnumMember7.ts new file mode 100644 index 00000000000..4b18502b8a5 --- /dev/null +++ b/tests/cases/fourslash/codeFixAddMissingEnumMember7.ts @@ -0,0 +1,15 @@ +/// + +////enum E { +////} +////E.a + +verify.codeFix({ + description: "Add missing enum member 'a'", + newFileContent: `enum E { + a +} +E.a` +}); + + diff --git a/tests/cases/fourslash/codeFixAddMissingEnumMember8.ts b/tests/cases/fourslash/codeFixAddMissingEnumMember8.ts new file mode 100644 index 00000000000..fdbbfa68255 --- /dev/null +++ b/tests/cases/fourslash/codeFixAddMissingEnumMember8.ts @@ -0,0 +1,21 @@ +/// + +////enum E { +//// a, +//// b = 1, +//// c = "123" +////} +////E.d + +verify.codeFix({ + description: "Add missing enum member 'd'", + newFileContent: `enum E { + a, + b = 1, + c = "123", + d = "d" +} +E.d` +}); + + diff --git a/tests/cases/fourslash/codeFixAddMissingEnumMember9.ts b/tests/cases/fourslash/codeFixAddMissingEnumMember9.ts new file mode 100644 index 00000000000..acc27a6a4ea --- /dev/null +++ b/tests/cases/fourslash/codeFixAddMissingEnumMember9.ts @@ -0,0 +1,25 @@ +/// + +////enum E { +//// a, +//// b = 1, +//// c = "123" +////} +////enum A { +//// a = E.a +////} +////A.b + +verify.codeFix({ + description: "Add missing enum member 'b'", + newFileContent: `enum E { + a, + b = 1, + c = "123" +} +enum A { + a = E.a, + b +} +A.b` +}); diff --git a/tests/cases/fourslash/codeFixUndeclaredPropertyAccesses.ts b/tests/cases/fourslash/codeFixUndeclaredPropertyAccesses.ts index 2576dbb1fb7..b83a4c857e3 100644 --- a/tests/cases/fourslash/codeFixUndeclaredPropertyAccesses.ts +++ b/tests/cases/fourslash/codeFixUndeclaredPropertyAccesses.ts @@ -14,4 +14,6 @@ //// let t: T; //// t.x; -verify.not.codeFixAvailable(); +verify.codeFixAvailable([{ + description: "Add missing enum member 'c'" +}]);