Skip to content

Commit

Permalink
Improve the performance of isolatedDeclarations quickfix (#58722)
Browse files Browse the repository at this point in the history
  • Loading branch information
blickly authored May 31, 2024
1 parent f5b2d9b commit fc42002
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
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;
}

0 comments on commit fc42002

Please sign in to comment.