Skip to content

Commit

Permalink
deleteDeclaration: don't crash on the top node.
Browse files Browse the repository at this point in the history
A misbehaved client can sometimes cause the server to reach
`deleteDeclaration` with the SourceFile, and it will crash due to no
`node.parent`.  I couldn't find a good way to create a test for it, but
I could trigger it manually by having a file with just a `,`, and
sending an explicit `getCodeFixes` command to the server with
`errorCodes: [6133]`.

I can only guess some scenario in which Emacs or other editors could
lead to that, but it's easy to just avoid the crash instead.  (Doing the
default of deleting the whole thing -- not really important since it's a
broken client situation anyway--?)

Fixes microsoft#33726
  • Loading branch information
elibarzilay committed Feb 25, 2021
1 parent ccdd688 commit a280777
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/services/textChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1361,7 +1361,10 @@ namespace ts.textChanges {
break;

default:
if (isImportClause(node.parent) && node.parent.name === node) {
if (node.parent === node) {
// a misbehaving client can reach here with the SourceFile node
deleteNode(changes, sourceFile, node);
} else if (isImportClause(node.parent) && node.parent.name === node) {
deleteDefaultImport(changes, sourceFile, node.parent);
}
else if (isCallExpression(node.parent) && contains(node.parent.arguments, node)) {
Expand Down

0 comments on commit a280777

Please sign in to comment.