Skip to content

Commit fc42002

Browse files
authored
Improve the performance of isolatedDeclarations quickfix (#58722)
1 parent f5b2d9b commit fc42002

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,6 +1763,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
17631763
getImmediateAliasedSymbol,
17641764
getAliasedSymbol: resolveAlias,
17651765
getEmitResolver,
1766+
requiresAddingImplicitUndefined,
17661767
getExportsOfModule: getExportsOfModuleAsArray,
17671768
getExportsAndPropertiesOfModule,
17681769
forEachExportAndPropertyOfModule,

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5265,6 +5265,7 @@ export interface TypeChecker {
52655265
/** @internal */ getDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[];
52665266
/** @internal */ getGlobalDiagnostics(): Diagnostic[];
52675267
/** @internal */ getEmitResolver(sourceFile?: SourceFile, cancellationToken?: CancellationToken, forceDts?: boolean): EmitResolver;
5268+
/** @internal */ requiresAddingImplicitUndefined(parameter: ParameterDeclaration | JSDocParameterTag): boolean;
52685269

52695270
/** @internal */ getNodeCount(): number;
52705271
/** @internal */ getIdentifierCount(): number;

src/services/codefixes/fixMissingTypeAnnotationOnExports.ts

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ registerCodeFix({
183183

184184
addCodeAction(addInlineTypeAssertion, fixes, context, TypePrintMode.Full, f => f.addInlineAssertion(context.span));
185185
addCodeAction(addInlineTypeAssertion, fixes, context, TypePrintMode.Relative, f => f.addInlineAssertion(context.span));
186-
addCodeAction(addAnnotationFix, fixes, context, TypePrintMode.Widened, f => f.addInlineAssertion(context.span));
186+
addCodeAction(addInlineTypeAssertion, fixes, context, TypePrintMode.Widened, f => f.addInlineAssertion(context.span));
187187

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

@@ -237,7 +237,6 @@ function withContext<T>(
237237
const sourceFile: SourceFile = context.sourceFile;
238238
const program = context.program;
239239
const typeChecker: TypeChecker = program.getTypeChecker();
240-
const emitResolver = typeChecker.getEmitResolver();
241240
const scriptTarget = getEmitScriptTarget(program.getCompilerOptions());
242241
const importAdder = createImportAdder(context.sourceFile, context.program, context.preferences, context.host);
243242
const fixedNodes = new Set<Node>();
@@ -887,7 +886,7 @@ function withContext<T>(
887886
type = widenedType;
888887
}
889888

890-
if (isParameter(node) && emitResolver.requiresAddingImplicitUndefined(node)) {
889+
if (isParameter(node) && typeChecker.requiresAddingImplicitUndefined(node)) {
891890
type = typeChecker.getUnionType([typeChecker.getUndefinedType(), type], UnionReduction.None);
892891
}
893892
const flags = (
@@ -1108,26 +1107,26 @@ function withContext<T>(
11081107
setEmitFlags(node, EmitFlags.None);
11091108
return result;
11101109
}
1111-
}
11121110

1113-
// Some --isolatedDeclarations errors are not present on the node that directly needs type annotation, so look in the
1114-
// ancestors to look for node that needs type annotation. This function can return undefined if the AST is ill-formed.
1115-
function findAncestorWithMissingType(node: Node): Node | undefined {
1116-
return findAncestor(node, n => {
1117-
return canHaveTypeAnnotation.has(n.kind) &&
1118-
((!isObjectBindingPattern(n) && !isArrayBindingPattern(n)) || isVariableDeclaration(n.parent));
1119-
});
1120-
}
1121-
1122-
function findBestFittingNode(node: Node, span: TextSpan) {
1123-
while (node && node.end < span.start + span.length) {
1124-
node = node.parent;
1125-
}
1126-
while (node.parent.pos === node.pos && node.parent.end === node.end) {
1127-
node = node.parent;
1111+
// Some --isolatedDeclarations errors are not present on the node that directly needs type annotation, so look in the
1112+
// ancestors to look for node that needs type annotation. This function can return undefined if the AST is ill-formed.
1113+
function findAncestorWithMissingType(node: Node): Node | undefined {
1114+
return findAncestor(node, n => {
1115+
return canHaveTypeAnnotation.has(n.kind) &&
1116+
((!isObjectBindingPattern(n) && !isArrayBindingPattern(n)) || isVariableDeclaration(n.parent));
1117+
});
11281118
}
1129-
if (isIdentifier(node) && hasInitializer(node.parent) && node.parent.initializer) {
1130-
return node.parent.initializer;
1119+
1120+
function findBestFittingNode(node: Node, span: TextSpan) {
1121+
while (node && node.end < span.start + span.length) {
1122+
node = node.parent;
1123+
}
1124+
while (node.parent.pos === node.pos && node.parent.end === node.end) {
1125+
node = node.parent;
1126+
}
1127+
if (isIdentifier(node) && hasInitializer(node.parent) && node.parent.initializer) {
1128+
return node.parent.initializer;
1129+
}
1130+
return node;
11311131
}
1132-
return node;
11331132
}

0 commit comments

Comments
 (0)