Skip to content

fix(45225): Add missing member codefix changes lib files #45231

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
Jul 29, 2021
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
12 changes: 8 additions & 4 deletions src/services/codefixes/fixAddMissingMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ namespace ts.codefix {
if (isIdentifier(token) && isCallExpression(parent.parent)) {
const moduleDeclaration = find(symbol.declarations, isModuleDeclaration);
const moduleDeclarationSourceFile = moduleDeclaration?.getSourceFile();
if (moduleDeclaration && moduleDeclarationSourceFile && !program.isSourceFileFromExternalLibrary(moduleDeclarationSourceFile)) {
if (moduleDeclaration && moduleDeclarationSourceFile && !isSourceFileFromLibrary(program, moduleDeclarationSourceFile)) {
return { kind: InfoKind.Function, token, call: parent.parent, sourceFile, modifierFlags: ModifierFlags.Export, parentDeclaration: moduleDeclaration };
}

Expand All @@ -180,7 +180,7 @@ namespace ts.codefix {
return;
}

if (moduleSourceFile && !program.isSourceFileFromExternalLibrary(moduleSourceFile)) {
if (moduleSourceFile && !isSourceFileFromLibrary(program, moduleSourceFile)) {
return { kind: InfoKind.Function, token, call: parent.parent, sourceFile: moduleSourceFile, modifierFlags: ModifierFlags.Export, parentDeclaration: moduleSourceFile };
}
}
Expand All @@ -193,7 +193,7 @@ namespace ts.codefix {

// Prefer to change the class instead of the interface if they are merged
const classOrInterface = classDeclaration || find(symbol.declarations, isInterfaceDeclaration);
if (classOrInterface && !program.isSourceFileFromExternalLibrary(classOrInterface.getSourceFile())) {
if (classOrInterface && !isSourceFileFromLibrary(program, classOrInterface.getSourceFile())) {
const makeStatic = ((leftExpressionType as TypeReference).target || leftExpressionType) !== checker.getDeclaredTypeOfSymbol(symbol);
if (makeStatic && (isPrivateIdentifier(token) || isInterfaceDeclaration(classOrInterface))) {
return undefined;
Expand All @@ -207,12 +207,16 @@ namespace ts.codefix {
}

const enumDeclaration = find(symbol.declarations, isEnumDeclaration);
if (enumDeclaration && !isPrivateIdentifier(token) && !program.isSourceFileFromExternalLibrary(enumDeclaration.getSourceFile())) {
if (enumDeclaration && !isPrivateIdentifier(token) && !isSourceFileFromLibrary(program, enumDeclaration.getSourceFile())) {
return { kind: InfoKind.Enum, token, parentDeclaration: enumDeclaration };
}
return undefined;
}

function isSourceFileFromLibrary(program: Program, node: SourceFile) {
return program.isSourceFileFromExternalLibrary(node) || program.isSourceFileDefaultLibrary(node);
}

function getActionsForMissingMemberDeclaration(context: CodeFixContext, info: ClassOrInterfaceInfo): CodeFixAction[] | undefined {
return info.isJSFile ? singleElementArray(createActionForAddMissingMemberInJavascriptFile(context, info)) :
createActionsForAddMissingMemberInTypeScriptFile(context, info);
Expand Down
8 changes: 8 additions & 0 deletions tests/cases/fourslash/codeFixAddMissingMember21.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// <reference path='fourslash.ts' />

////declare let p: Promise<string>;
////async function f() {
//// p.toLowerCase();
////}

verify.not.codeFixAvailable("fixMissingMember");