Skip to content

Commit 703fdd8

Browse files
fix: panic with js lint plugins and invalid js syntax (#28006)
Noticed that the LSP might panic during serialization when working on a file with a syntax error. This PR changes the serialization so that invalid nodes are simply serialized to the invalid node `0`. The plugin code treats the node with id `0` as an invalid node and will ignore it during visiting. I'm not sure how to write a test for the LSP.
1 parent a82326d commit 703fdd8

File tree

1 file changed

+26
-7
lines changed
  • cli/tools/lint/ast_buffer

1 file changed

+26
-7
lines changed

cli/tools/lint/ast_buffer/swc.rs

+26-7
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,10 @@ fn serialize_module_decl(
241241
// Already handled earlier
242242
ExportSpecifier::Namespace(_) => unreachable!(),
243243
// this is not syntactically valid
244-
ExportSpecifier::Default(_) => unreachable!(),
244+
ExportSpecifier::Default(_) => {
245+
// Ignore syntax errors
246+
NodeRef(0)
247+
}
245248
}
246249
})
247250
.collect::<Vec<_>>();
@@ -444,7 +447,10 @@ fn serialize_import_attrs(
444447
.map(|prop| {
445448
let (key, value) = match prop {
446449
// Invalid syntax
447-
PropOrSpread::Spread(_) => unreachable!(),
450+
PropOrSpread::Spread(_) => {
451+
// Ignore syntax errors
452+
(NodeRef(0), NodeRef(0))
453+
}
448454
PropOrSpread::Prop(prop) => {
449455
match prop.as_ref() {
450456
Prop::Shorthand(ident) => (
@@ -459,7 +465,10 @@ fn serialize_import_attrs(
459465
Prop::Assign(_)
460466
| Prop::Getter(_)
461467
| Prop::Setter(_)
462-
| Prop::Method(_) => unreachable!(),
468+
| Prop::Method(_) => {
469+
// Ignore syntax errors
470+
(NodeRef(0), NodeRef(0))
471+
}
463472
}
464473
}
465474
};
@@ -770,7 +779,10 @@ fn serialize_expr(ctx: &mut TsEsTreeBuilder, expr: &Expr) -> NodeRef {
770779
SimpleAssignTarget::TsInstantiation(target) => {
771780
serialize_expr(ctx, &Expr::TsInstantiation(target.clone()))
772781
}
773-
SimpleAssignTarget::Invalid(_) => unreachable!(),
782+
SimpleAssignTarget::Invalid(_) => {
783+
// Ignore syntax errors
784+
NodeRef(0)
785+
}
774786
}
775787
}
776788
AssignTarget::Pat(target) => match target {
@@ -780,7 +792,10 @@ fn serialize_expr(ctx: &mut TsEsTreeBuilder, expr: &Expr) -> NodeRef {
780792
AssignTargetPat::Object(object_pat) => {
781793
serialize_pat(ctx, &Pat::Object(object_pat.clone()))
782794
}
783-
AssignTargetPat::Invalid(_) => unreachable!(),
795+
AssignTargetPat::Invalid(_) => {
796+
// Ignore syntax errors
797+
NodeRef(0)
798+
}
784799
},
785800
};
786801

@@ -1101,7 +1116,8 @@ fn serialize_expr(ctx: &mut TsEsTreeBuilder, expr: &Expr) -> NodeRef {
11011116
ctx.write_chain_expr(&node.span, expr)
11021117
}
11031118
Expr::Invalid(_) => {
1104-
unreachable!()
1119+
// Ignore syntax errors
1120+
NodeRef(0)
11051121
}
11061122
}
11071123
}
@@ -1908,7 +1924,10 @@ fn serialize_pat(ctx: &mut TsEsTreeBuilder, pat: &Pat) -> NodeRef {
19081924

19091925
ctx.write_assign_pat(&node.span, left, right)
19101926
}
1911-
Pat::Invalid(_) => unreachable!(),
1927+
Pat::Invalid(_) => {
1928+
// Ignore syntax errors
1929+
NodeRef(0)
1930+
}
19121931
Pat::Expr(node) => serialize_expr(ctx, node),
19131932
}
19141933
}

0 commit comments

Comments
 (0)