Merge pull request #25182 from Kingwl/fix-missing-enum-member

add quick fix for add missing enum member
This commit is contained in:
Mohamed Hegazy
2018-06-29 10:19:23 -07:00
committed by GitHub
15 changed files with 297 additions and 21 deletions

View File

@@ -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
}
}

View File

@@ -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<true>();
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))
));
}
}