From 99425517ee2fc34825dd7fc1b536c78fb5320ed9 Mon Sep 17 00:00:00 2001 From: Hana Joo Date: Wed, 30 Aug 2023 12:46:47 +0000 Subject: [PATCH] Try not to fail in cases where IsolatedDeclaration can report errors in non-sensical places such as ill-formed AST where it still succeeds to parse, but the fixer would not be able to find a proper node to annotate types. Signed-off-by: Hana Joo --- .../src/code-mod/code-transform.ts | 15 +++++++++++---- .../fixMissingTypeAnnotationOnExports.ts | 15 ++++++++++----- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/external-declarations/src/code-mod/code-transform.ts b/external-declarations/src/code-mod/code-transform.ts index 2471c856c25d1..8da7af30fab01 100644 --- a/external-declarations/src/code-mod/code-transform.ts +++ b/external-declarations/src/code-mod/code-transform.ts @@ -30,12 +30,19 @@ const canHaveExplicitTypeAnnotation = new Set([ ts.SyntaxKind.ArrayBindingPattern, ]); -// Currently, the diagnostics for the error is not given in the exact node of which that needs type annotation -function findNearestParentWithTypeAnnotation(node: ts.Node): ts.Node { - while (((ts.isObjectBindingPattern(node) || ts.isArrayBindingPattern(node)) && !ts.isVariableDeclaration(node.parent)) || - !canHaveExplicitTypeAnnotation.has(node.kind)) { +// Currently, the diagnostics for the error is not given in the exact node of which that needs type annotation. +// If this is coming from an ill-formed AST with syntax errors, you cannot assume that it'll find a node +// to annotate types, this will return undefined - meaning that it couldn't find the node to annotate types. +function findNearestParentWithTypeAnnotation(node: ts.Node): ts.Node | undefined { + while (node && + (((ts.isObjectBindingPattern(node) || ts.isArrayBindingPattern(node)) && + !ts.isVariableDeclaration(node.parent)) || + !canHaveExplicitTypeAnnotation.has(node.kind))) { node = node.parent; } + if (!node) { + return undefined; + } if (ts.isObjectBindingPattern(node) || ts.isArrayBindingPattern(node)) { // return VariableStatement return node.parent.parent.parent; diff --git a/src/services/codefixes/fixMissingTypeAnnotationOnExports.ts b/src/services/codefixes/fixMissingTypeAnnotationOnExports.ts index d351f52548939..56bf5c5e445af 100644 --- a/src/services/codefixes/fixMissingTypeAnnotationOnExports.ts +++ b/src/services/codefixes/fixMissingTypeAnnotationOnExports.ts @@ -90,13 +90,18 @@ registerCodeFix({ function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, typeChecker: TypeChecker, nodeWithDiag: Node): void { const nodeWithNoType = findNearestParentWithTypeAnnotation(nodeWithDiag); - fixupForIsolatedDeclarations(nodeWithNoType, nodeWithDiag, sourceFile, typeChecker, changes); + if (nodeWithNoType) { + fixupForIsolatedDeclarations(nodeWithNoType, nodeWithDiag, sourceFile, typeChecker, changes); + } } -// Currently, the diagnostics for the error is not given in the exact node of which that needs type annotation -function findNearestParentWithTypeAnnotation(node: Node): Node { - while (((isObjectBindingPattern(node) || isArrayBindingPattern(node)) && !isVariableDeclaration(node.parent)) || - !canHaveExplicitTypeAnnotation.has(node.kind)) { +// Currently, the diagnostics for the error is not given in the exact node of which that needs type annotation. +// If this is coming from an ill-formed AST with syntax errors, you cannot assume that it'll find a node +// to annotate types, this will return undefined - meaning that it couldn't find the node to annotate types. +function findNearestParentWithTypeAnnotation(node: Node): Node | undefined { + while (node && + (((isObjectBindingPattern(node) || isArrayBindingPattern(node)) && + !isVariableDeclaration(node.parent)) || !canHaveExplicitTypeAnnotation.has(node.kind))) { node = node.parent; } return node;