From 8a73d18fcf4ac135ec906174bc4de6bca6abbd58 Mon Sep 17 00:00:00 2001 From: Boshen Date: Mon, 11 Mar 2024 19:49:51 +0800 Subject: [PATCH] chore(parser): make sure all span.end >= span.start (#2681) closes #2679 --- crates/oxc_parser/src/cursor.rs | 1 + crates/oxc_parser/src/js/list.rs | 8 ++++++-- tasks/coverage/parser_babel.snap | 6 +++--- tasks/coverage/parser_test262.snap | 12 ++++++------ tasks/coverage/parser_typescript.snap | 2 +- 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/crates/oxc_parser/src/cursor.rs b/crates/oxc_parser/src/cursor.rs index ad4fab7d3..8bf3d9f71 100644 --- a/crates/oxc_parser/src/cursor.rs +++ b/crates/oxc_parser/src/cursor.rs @@ -27,6 +27,7 @@ impl<'a> ParserImpl<'a> { #[inline] pub(crate) fn end_span(&self, mut span: Span) -> Span { span.end = self.prev_token_end; + debug_assert!(span.end >= span.start); span } diff --git a/crates/oxc_parser/src/js/list.rs b/crates/oxc_parser/src/js/list.rs index 613f6c1ec..133897fbd 100644 --- a/crates/oxc_parser/src/js/list.rs +++ b/crates/oxc_parser/src/js/list.rs @@ -50,7 +50,9 @@ impl<'a> SeparatedList<'a> for ObjectExpressionProperties<'a> { }?; if p.at(Kind::Comma) && p.peek_at(self.close()) { - self.trailing_comma = Some(p.end_span(p.start_span())); + let span = p.start_span(); + p.bump_any(); + self.trailing_comma = Some(p.end_span(span)); } self.elements.push(element); @@ -121,7 +123,9 @@ impl<'a> SeparatedList<'a> for ArrayExpressionList<'a> { }; if p.at(Kind::Comma) && p.peek_at(self.close()) { - self.trailing_comma = Some(p.end_span(p.start_span())); + let span = p.start_span(); + p.bump_any(); + self.trailing_comma = Some(p.end_span(span)); } self.elements.push(element?); diff --git a/tasks/coverage/parser_babel.snap b/tasks/coverage/parser_babel.snap index d9a4979f4..3b311a216 100644 --- a/tasks/coverage/parser_babel.snap +++ b/tasks/coverage/parser_babel.snap @@ -2214,19 +2214,19 @@ Expect to Parse: "typescript/types/const-type-parameters-babel-7/input.ts" × Unexpected trailing comma after rest element ╭─[es2015/array-rest-spread/comma-after-rest/input.js:1:6] 1 │ [...a,] = []; - · ▲ + · ─ ╰──── × Unexpected trailing comma after rest element ╭─[es2015/array-rest-spread/comma-after-spread-for-in/input.js:1:11] 1 │ for ([...a,] in []); - · ▲ + · ─ ╰──── × Unexpected trailing comma after rest element ╭─[es2015/array-rest-spread/comma-after-spread-nested/input.js:1:7] 1 │ [[...a,]] = []; - · ▲ + · ─ ╰──── × Spread must be last element diff --git a/tasks/coverage/parser_test262.snap b/tasks/coverage/parser_test262.snap index 94c4d8246..c501c5ac3 100644 --- a/tasks/coverage/parser_test262.snap +++ b/tasks/coverage/parser_test262.snap @@ -2423,7 +2423,7 @@ Expect Syntax Error: "language/import/import-attributes/json-named-bindings.js" ╭─[language/expressions/assignment/dstr/array-rest-before-elision.js:23:9] 22 │ 23 │ 0, [...x,] = []; - · ▲ + · ─ ╰──── × Spread must be last element @@ -2437,7 +2437,7 @@ Expect Syntax Error: "language/import/import-attributes/json-named-bindings.js" ╭─[language/expressions/assignment/dstr/array-rest-elision-invalid.js:23:9] 22 │ 23 │ 0, [...x,] = []; - · ▲ + · ─ ╰──── × Cannot assign to this expression @@ -30252,7 +30252,7 @@ Expect Syntax Error: "language/import/import-attributes/json-named-bindings.js" ╭─[language/statements/for-in/dstr/array-rest-before-elision.js:32:11] 31 │ 32 │ for ([...x,] in [[]]) ; - · ▲ + · ─ ╰──── × Unexpected token @@ -30266,7 +30266,7 @@ Expect Syntax Error: "language/import/import-attributes/json-named-bindings.js" ╭─[language/statements/for-in/dstr/array-rest-elision-invalid.js:32:11] 31 │ 32 │ for ([...x,] in [[]]) ; - · ▲ + · ─ ╰──── × Unexpected token @@ -30758,7 +30758,7 @@ Expect Syntax Error: "language/import/import-attributes/json-named-bindings.js" ╭─[language/statements/for-of/dstr/array-rest-before-elision.js:32:11] 31 │ 32 │ for ([...x,] of [[]]) ; - · ▲ + · ─ ╰──── × Unexpected token @@ -30772,7 +30772,7 @@ Expect Syntax Error: "language/import/import-attributes/json-named-bindings.js" ╭─[language/statements/for-of/dstr/array-rest-elision-invalid.js:32:11] 31 │ 32 │ for ([...x,] of [[]]) ; - · ▲ + · ─ ╰──── × Unexpected token diff --git a/tasks/coverage/parser_typescript.snap b/tasks/coverage/parser_typescript.snap index 19a83b255..442c581e1 100644 --- a/tasks/coverage/parser_typescript.snap +++ b/tasks/coverage/parser_typescript.snap @@ -15205,7 +15205,7 @@ Expect to Parse: "conformance/salsa/plainJSRedeclare3.ts" ╭─[conformance/es7/trailingCommasInBindingPatterns.ts:4:7] 3 │ let c, d; 4 │ ([...c,] = []); - · ▲ + · ─ 5 │ ({...d,} = {}); ╰────