Skip to content

Improve the performance of isolatedDeclarations quickfix #58722

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 3 commits into from
May 31, 2024
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
1 change: 1 addition & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1763,6 +1763,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
getImmediateAliasedSymbol,
getAliasedSymbol: resolveAlias,
getEmitResolver,
requiresAddingImplicitUndefined,
getExportsOfModule: getExportsOfModuleAsArray,
getExportsAndPropertiesOfModule,
forEachExportAndPropertyOfModule,
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5265,6 +5265,7 @@ export interface TypeChecker {
/** @internal */ getDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[];
/** @internal */ getGlobalDiagnostics(): Diagnostic[];
/** @internal */ getEmitResolver(sourceFile?: SourceFile, cancellationToken?: CancellationToken, forceDts?: boolean): EmitResolver;
/** @internal */ requiresAddingImplicitUndefined(parameter: ParameterDeclaration | JSDocParameterTag): boolean;

/** @internal */ getNodeCount(): number;
/** @internal */ getIdentifierCount(): number;
Expand Down
43 changes: 21 additions & 22 deletions src/services/codefixes/fixMissingTypeAnnotationOnExports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ registerCodeFix({

addCodeAction(addInlineTypeAssertion, fixes, context, TypePrintMode.Full, f => f.addInlineAssertion(context.span));
addCodeAction(addInlineTypeAssertion, fixes, context, TypePrintMode.Relative, f => f.addInlineAssertion(context.span));
addCodeAction(addAnnotationFix, fixes, context, TypePrintMode.Widened, f => f.addInlineAssertion(context.span));
addCodeAction(addInlineTypeAssertion, fixes, context, TypePrintMode.Widened, f => f.addInlineAssertion(context.span));

addCodeAction(extractExpression, fixes, context, TypePrintMode.Full, f => f.extractAsVariable(context.span));

Expand Down Expand Up @@ -237,7 +237,6 @@ function withContext<T>(
const sourceFile: SourceFile = context.sourceFile;
const program = context.program;
const typeChecker: TypeChecker = program.getTypeChecker();
const emitResolver = typeChecker.getEmitResolver();
const scriptTarget = getEmitScriptTarget(program.getCompilerOptions());
const importAdder = createImportAdder(context.sourceFile, context.program, context.preferences, context.host);
const fixedNodes = new Set<Node>();
Expand Down Expand Up @@ -887,7 +886,7 @@ function withContext<T>(
type = widenedType;
}

if (isParameter(node) && emitResolver.requiresAddingImplicitUndefined(node)) {
if (isParameter(node) && typeChecker.requiresAddingImplicitUndefined(node)) {
type = typeChecker.getUnionType([typeChecker.getUndefinedType(), type], UnionReduction.None);
}
const flags = (
Expand Down Expand Up @@ -1108,26 +1107,26 @@ function withContext<T>(
setEmitFlags(node, EmitFlags.None);
return result;
}
}

// Some --isolatedDeclarations errors are not present on the node that directly needs type annotation, so look in the
// ancestors to look for node that needs type annotation. This function can return undefined if the AST is ill-formed.
function findAncestorWithMissingType(node: Node): Node | undefined {
return findAncestor(node, n => {
return canHaveTypeAnnotation.has(n.kind) &&
((!isObjectBindingPattern(n) && !isArrayBindingPattern(n)) || isVariableDeclaration(n.parent));
});
}

function findBestFittingNode(node: Node, span: TextSpan) {
while (node && node.end < span.start + span.length) {
node = node.parent;
}
while (node.parent.pos === node.pos && node.parent.end === node.end) {
node = node.parent;
// Some --isolatedDeclarations errors are not present on the node that directly needs type annotation, so look in the
// ancestors to look for node that needs type annotation. This function can return undefined if the AST is ill-formed.
function findAncestorWithMissingType(node: Node): Node | undefined {
return findAncestor(node, n => {
return canHaveTypeAnnotation.has(n.kind) &&
((!isObjectBindingPattern(n) && !isArrayBindingPattern(n)) || isVariableDeclaration(n.parent));
});
}
if (isIdentifier(node) && hasInitializer(node.parent) && node.parent.initializer) {
return node.parent.initializer;

function findBestFittingNode(node: Node, span: TextSpan) {
while (node && node.end < span.start + span.length) {
node = node.parent;
}
while (node.parent.pos === node.pos && node.parent.end === node.end) {
node = node.parent;
}
if (isIdentifier(node) && hasInitializer(node.parent) && node.parent.initializer) {
return node.parent.initializer;
}
return node;
}
return node;
}