Skip to content
Open
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
29 changes: 27 additions & 2 deletions src/services/codefixes/fixClassIncorrectlyImplementsInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import {
createMissingMemberNodes,
getNoopSymbolTrackerWithResolver,
registerCodeFix,
TypeConstructionContext,
} from "../_namespaces/ts.codefix.js";
import {
addToSeen,
and,
ClassElement,
ClassLikeDeclaration,
CodeFixAction,
CodeFixContextBase,
createSymbolTable,
Debug,
Diagnostic,
Diagnostics,
ExpressionWithTypeArguments,
find,
Expand All @@ -30,6 +31,7 @@ import {
isConstructorDeclaration,
mapDefined,
ModifierFlags,
Node,
SourceFile,
Symbol,
SymbolTable,
Expand Down Expand Up @@ -76,7 +78,7 @@ function symbolPointsToNonPrivateMember(symbol: Symbol) {
}

function addMissingDeclarations(
context: TypeConstructionContext,
context: CodeFixContextBase,
implementedTypeNode: ExpressionWithTypeArguments,
sourceFile: SourceFile,
classDeclaration: ClassLikeDeclaration,
Expand All @@ -90,6 +92,13 @@ function addMissingDeclarations(
const implementedType = checker.getTypeAtLocation(implementedTypeNode) as InterfaceType;
const implementedTypeSymbols = checker.getPropertiesOfType(implementedType);
const nonPrivateAndNotExistedInHeritageClauseMembers = implementedTypeSymbols.filter(and(symbolPointsToNonPrivateMember, symbol => !maybeHeritageClauseSymbol.has(symbol.escapedName)));
const semanticDiagnostics = new Map<SourceFile, readonly Diagnostic[]>();
if (
hasBlockingSemanticError(context, semanticDiagnostics, implementedTypeNode) ||
nonPrivateAndNotExistedInHeritageClauseMembers.some(symbol => symbol.getDeclarations()?.some(declaration => hasBlockingSemanticError(context, semanticDiagnostics, declaration)))
) {
Comment thread
danyalahmed1995 marked this conversation as resolved.
return;
}

const classType = checker.getTypeAtLocation(classDeclaration);
const constructor = find(classDeclaration.members, m => isConstructorDeclaration(m));
Expand Down Expand Up @@ -123,6 +132,22 @@ function addMissingDeclarations(
}
}

function hasBlockingSemanticError(context: CodeFixContextBase, semanticDiagnostics: Map<SourceFile, readonly Diagnostic[]>, node: Node): boolean {
const sourceFile = node.getSourceFile();
let diagnostics = semanticDiagnostics.get(sourceFile);
if (!diagnostics) {
diagnostics = context.program.getSemanticDiagnostics(sourceFile, context.cancellationToken);
semanticDiagnostics.set(sourceFile, diagnostics);
}
return diagnostics.some(diagnostic =>
diagnostic.start !== undefined &&
diagnostic.length !== undefined &&
!errorCodes.includes(diagnostic.code) &&
diagnostic.start >= node.pos &&
diagnostic.start + diagnostic.length <= node.end
);
}

function getHeritageClauseSymbolTable(classDeclaration: ClassLikeDeclaration, checker: TypeChecker): SymbolTable {
const heritageClauseNode = getEffectiveBaseTypeNode(classDeclaration);
if (!heritageClauseNode) return createSymbolTable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@

// TODO: (arozga) Don't know how to instantiate in codeFix
// if instantiation is invalid.
// Should be verify.codeFixAvailable([]);
verify.codeFixAvailable([{ description: "Implement interface 'I<number>'" }]);
verify.codeFixAvailable([]);
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@
// Since we can't guess the programmer's intent here, we do nothing.

// TODO: (aozgaa) Acknowledge other errors on class/implemented interface/extended abstract class.
// Should be verify.codeFixAvailable([]);
verify.codeFixAvailable([{ description: "Implement interface 'I'" }]);
verify.codeFixAvailable([]);