Skip to content

Commit

Permalink
Try not to fail in cases where IsolatedDeclaration
Browse files Browse the repository at this point in the history
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 <hanajoo@google.com>
  • Loading branch information
h-joo committed Aug 30, 2023
1 parent fe34c87 commit 9942551
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
15 changes: 11 additions & 4 deletions external-declarations/src/code-mod/code-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,19 @@ const canHaveExplicitTypeAnnotation = new Set<ts.SyntaxKind>([
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;
Expand Down
15 changes: 10 additions & 5 deletions src/services/codefixes/fixMissingTypeAnnotationOnExports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 9942551

Please sign in to comment.