From b6771157027cf7b0adb91aaae1599faf5cf8322d Mon Sep 17 00:00:00 2001 From: David Sherret Date: Sat, 14 Mar 2020 19:18:28 -0400 Subject: [PATCH] Fix async arrow expression and TsIndexSignature param ident spans (#721) - Fix async arrow expression start. - Fix TsIndexSignature inner ident and type ann spans. --- ecmascript/parser/Cargo.toml | 2 +- ecmascript/parser/src/parser/expr.rs | 14 +++++++------- ecmascript/parser/src/parser/typescript.rs | 14 ++++++++------ .../arrow-function/async-rest/input.ts.json | 2 +- .../typescript/arrow-function/async/input.ts.json | 2 +- .../tests/typescript/class/declare/input.ts.json | 4 ++-- .../typescript/class/index-signature/input.ts.json | 8 ++++---- .../typescript/custom/issue-410-1/input.ts.json | 2 +- .../typescript/custom/issue-410-2/input.ts.json | 2 +- .../interface/index-signature/input.ts.json | 4 ++-- 10 files changed, 28 insertions(+), 26 deletions(-) diff --git a/ecmascript/parser/Cargo.toml b/ecmascript/parser/Cargo.toml index cb8c83c4088..6ca8f8ebed0 100644 --- a/ecmascript/parser/Cargo.toml +++ b/ecmascript/parser/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "swc_ecma_parser" -version = "0.21.4" +version = "0.21.5" authors = ["강동윤 "] license = "Apache-2.0/MIT" repository = "https://github.com/swc-project/swc.git" diff --git a/ecmascript/parser/src/parser/expr.rs b/ecmascript/parser/src/parser/expr.rs index 1879cd8dd27..3cc0a2c66d2 100644 --- a/ecmascript/parser/src/parser/expr.rs +++ b/ecmascript/parser/src/parser/expr.rs @@ -572,7 +572,7 @@ impl<'a, I: Tokens> Parser<'a, I> { ) -> PResult<'a, Box> { trace_cur!(parse_paren_expr_or_arrow_fn); - let start = cur_pos!(); + let expr_start = async_span.map(|x| x.lo()).unwrap_or(cur_pos!()); // At this point, we can't know if it's parenthesized // expression or head of arrow function. @@ -602,7 +602,7 @@ impl<'a, I: Tokens> Parser<'a, I> { let body: BlockStmtOrExpr = p.parse_fn_body(async_span.is_some(), false)?; Ok(Some(Box::new(Expr::Arrow(ArrowExpr { - span: span!(start), + span: span!(expr_start), is_async: async_span.is_some(), is_generator: false, params, @@ -625,7 +625,7 @@ impl<'a, I: Tokens> Parser<'a, I> { // we parse arrow function at here, to handle it efficiently. if has_pattern || return_type.is_some() || is!("=>") { if self.input.had_line_break_before_cur() { - syntax_error!(span!(start), SyntaxError::LineBreakBeforeArrow); + syntax_error!(span!(expr_start), SyntaxError::LineBreakBeforeArrow); } if !can_be_arrow { unexpected!() @@ -639,7 +639,7 @@ impl<'a, I: Tokens> Parser<'a, I> { let body: BlockStmtOrExpr = self.parse_fn_body(async_span.is_some(), false)?; let arrow_expr = ArrowExpr { - span: span!(start), + span: span!(expr_start), is_async: async_span.is_some(), is_generator: false, params, @@ -695,7 +695,7 @@ impl<'a, I: Tokens> Parser<'a, I> { if expr_or_spreads.is_empty() { syntax_error!( - Span::new(start, last_pos!(), Default::default()), + Span::new(expr_start, last_pos!(), Default::default()), SyntaxError::EmptyParenExpr ); } @@ -712,7 +712,7 @@ impl<'a, I: Tokens> Parser<'a, I> { ExprOrSpread { expr, .. } => expr, }; Ok(Box::new(Expr::Paren(ParenExpr { - span: span!(start), + span: span!(expr_start), expr, }))) } else { @@ -740,7 +740,7 @@ impl<'a, I: Tokens> Parser<'a, I> { exprs, })); Ok(Box::new(Expr::Paren(ParenExpr { - span: span!(start), + span: span!(expr_start), expr: seq_expr, }))) } diff --git a/ecmascript/parser/src/parser/typescript.rs b/ecmascript/parser/src/parser/typescript.rs index 2f1310c5859..28600166801 100644 --- a/ecmascript/parser/src/parser/typescript.rs +++ b/ecmascript/parser/src/parser/typescript.rs @@ -1143,7 +1143,7 @@ impl<'a, I: Tokens> Parser<'a, I> { /// `tsTryParseIndexSignature` pub(super) fn try_parse_ts_index_signature( &mut self, - start: BytePos, + index_signature_start: BytePos, readonly: bool, ) -> PResult<'a, Option> { if !(is!('[') && self.ts_look_ahead(|p| p.is_ts_unambiguously_index_signature())?) { @@ -1152,7 +1152,9 @@ impl<'a, I: Tokens> Parser<'a, I> { expect!('['); + let ident_start = cur_pos!(); let mut id = self.parse_ident_name()?; + let type_ann_start = cur_pos!(); if eat!(',') { self.emit_err(id.span, SyntaxError::TS1096); @@ -1160,10 +1162,10 @@ impl<'a, I: Tokens> Parser<'a, I> { expect!(':'); } - let cur_pos = cur_pos!(); - id.type_ann = self - .parse_ts_type_ann(/* eat_colon */ false, cur_pos) - .map(Some)?; + let type_ann = self.parse_ts_type_ann(/* eat_colon */ false, type_ann_start)?; + id.span = span!(ident_start); + id.type_ann = Some(type_ann); + expect!(']'); let params = vec![TsFnParam::Ident(id)]; @@ -1172,7 +1174,7 @@ impl<'a, I: Tokens> Parser<'a, I> { self.parse_ts_type_member_semicolon()?; Ok(Some(TsIndexSignature { - span: span!(start), + span: span!(index_signature_start), readonly, params, type_ann, diff --git a/ecmascript/parser/tests/typescript/arrow-function/async-rest/input.ts.json b/ecmascript/parser/tests/typescript/arrow-function/async-rest/input.ts.json index e6b7b12bc51..2f9e7c7d564 100644 --- a/ecmascript/parser/tests/typescript/arrow-function/async-rest/input.ts.json +++ b/ecmascript/parser/tests/typescript/arrow-function/async-rest/input.ts.json @@ -16,7 +16,7 @@ "expression": { "type": "ArrowFunctionExpression", "span": { - "start": 6, + "start": 0, "end": 34, "ctxt": 0 }, diff --git a/ecmascript/parser/tests/typescript/arrow-function/async/input.ts.json b/ecmascript/parser/tests/typescript/arrow-function/async/input.ts.json index 6dad93c7334..bf6922e4ae3 100644 --- a/ecmascript/parser/tests/typescript/arrow-function/async/input.ts.json +++ b/ecmascript/parser/tests/typescript/arrow-function/async/input.ts.json @@ -16,7 +16,7 @@ "expression": { "type": "ArrowFunctionExpression", "span": { - "start": 6, + "start": 0, "end": 28, "ctxt": 0 }, diff --git a/ecmascript/parser/tests/typescript/class/declare/input.ts.json b/ecmascript/parser/tests/typescript/class/declare/input.ts.json index b7c6abb6082..52602dbad70 100644 --- a/ecmascript/parser/tests/typescript/class/declare/input.ts.json +++ b/ecmascript/parser/tests/typescript/class/declare/input.ts.json @@ -34,14 +34,14 @@ "type": "Identifier", "span": { "start": 23, - "end": 24, + "end": 32, "ctxt": 0 }, "value": "x", "typeAnnotation": { "type": "TsTypeAnnotation", "span": { - "start": 26, + "start": 24, "end": 32, "ctxt": 0 }, diff --git a/ecmascript/parser/tests/typescript/class/index-signature/input.ts.json b/ecmascript/parser/tests/typescript/class/index-signature/input.ts.json index e14a12b1fda..ca45d04758c 100644 --- a/ecmascript/parser/tests/typescript/class/index-signature/input.ts.json +++ b/ecmascript/parser/tests/typescript/class/index-signature/input.ts.json @@ -34,14 +34,14 @@ "type": "Identifier", "span": { "start": 15, - "end": 16, + "end": 24, "ctxt": 0 }, "value": "x", "typeAnnotation": { "type": "TsTypeAnnotation", "span": { - "start": 18, + "start": 16, "end": 24, "ctxt": 0 }, @@ -89,14 +89,14 @@ "type": "Identifier", "span": { "start": 46, - "end": 47, + "end": 55, "ctxt": 0 }, "value": "x", "typeAnnotation": { "type": "TsTypeAnnotation", "span": { - "start": 49, + "start": 47, "end": 55, "ctxt": 0 }, diff --git a/ecmascript/parser/tests/typescript/custom/issue-410-1/input.ts.json b/ecmascript/parser/tests/typescript/custom/issue-410-1/input.ts.json index 4f7f9184607..45296fb181f 100644 --- a/ecmascript/parser/tests/typescript/custom/issue-410-1/input.ts.json +++ b/ecmascript/parser/tests/typescript/custom/issue-410-1/input.ts.json @@ -30,7 +30,7 @@ "expression": { "type": "ArrowFunctionExpression", "span": { - "start": 7, + "start": 1, "end": 15, "ctxt": 0 }, diff --git a/ecmascript/parser/tests/typescript/custom/issue-410-2/input.ts.json b/ecmascript/parser/tests/typescript/custom/issue-410-2/input.ts.json index a5b9ac46d17..715fd45ef89 100644 --- a/ecmascript/parser/tests/typescript/custom/issue-410-2/input.ts.json +++ b/ecmascript/parser/tests/typescript/custom/issue-410-2/input.ts.json @@ -16,7 +16,7 @@ "expression": { "type": "ArrowFunctionExpression", "span": { - "start": 6, + "start": 0, "end": 14, "ctxt": 0 }, diff --git a/ecmascript/parser/tests/typescript/interface/index-signature/input.ts.json b/ecmascript/parser/tests/typescript/interface/index-signature/input.ts.json index b4983f38428..9ac67ff7e82 100644 --- a/ecmascript/parser/tests/typescript/interface/index-signature/input.ts.json +++ b/ecmascript/parser/tests/typescript/interface/index-signature/input.ts.json @@ -42,14 +42,14 @@ "type": "Identifier", "span": { "start": 19, - "end": 20, + "end": 28, "ctxt": 0 }, "value": "s", "typeAnnotation": { "type": "TsTypeAnnotation", "span": { - "start": 22, + "start": 20, "end": 28, "ctxt": 0 },