Fix span for patterns with a type annotation (#558)

swc_ecma_parser:
* fix span for patterns with a type annotation.

Co-authored-by: 강동윤 <kdy1997.dev@gmail.com>
This commit is contained in:
David Sherret 2020-01-03 00:37:48 -05:00 committed by 강동윤
parent 5acf557e35
commit c43bc471dd
46 changed files with 162 additions and 77 deletions

View File

@ -63,12 +63,12 @@ pub struct AssignPat {
/// EsTree `RestElement`
#[ast_node("RestElement")]
pub struct RestPat {
pub span: Span,
#[serde(rename = "rest")]
#[span(lo)]
pub dot3_token: Span,
#[serde(rename = "argument")]
#[span(hi)]
pub arg: Box<Pat>,
#[serde(default, rename = "typeAnnotation")]

View File

@ -1120,6 +1120,7 @@ impl<'a, I: Tokens> Parser<'a, I> {
let modifier_start = start;
let has_modifier = self.eat_any_ts_modifier()?;
let pat_start = cur_pos!();
let mut arg = {
if self.input.syntax().typescript()
@ -1227,6 +1228,7 @@ impl<'a, I: Tokens> Parser<'a, I> {
}
rest_span = Some(span);
pat = Pat::Rest(RestPat {
span: span!(pat_start),
dot3_token: span,
arg: Box::new(pat),
type_ann: None,
@ -1234,20 +1236,40 @@ impl<'a, I: Tokens> Parser<'a, I> {
}
match pat {
Pat::Ident(Ident {
ref mut type_ann, ..
ref mut type_ann,
ref mut span,
..
})
| Pat::Array(ArrayPat {
ref mut type_ann, ..
ref mut type_ann,
ref mut span,
..
})
| Pat::Assign(AssignPat {
ref mut type_ann, ..
ref mut type_ann,
ref mut span,
..
})
| Pat::Object(ObjectPat {
ref mut type_ann, ..
ref mut type_ann,
ref mut span,
..
})
| Pat::Rest(RestPat {
ref mut type_ann, ..
}) => *type_ann = self.try_parse_ts_type_ann()?,
ref mut type_ann,
ref mut span,
..
}) => {
let new_type_ann = self.try_parse_ts_type_ann()?;
if new_type_ann.is_some() {
*span = Span::new(
pat_start,
self.input.prev_span().hi(),
Default::default(),
);
}
*type_ann = new_type_ann;
}
Pat::Expr(ref expr) => unreachable!("invalid pattern: Expr({:?})", expr),
Pat::Invalid(ref i) => unreachable!("invalid pattern: {:?}", i.span),
}

View File

@ -104,6 +104,7 @@ fn object_rest_pat() {
params: vec![Pat::Object(ObjectPat {
span,
props: vec![ObjectPatProp::Rest(RestPat {
span,
dot3_token: span,
arg: box Pat::Ident(Ident::new("a34".into(), span)),
type_ann: None,
@ -220,6 +221,7 @@ fn arrow_fn_rest() {
is_async: false,
is_generator: false,
params: vec![Pat::Rest(RestPat {
span,
dot3_token: span,
arg: box Pat::Ident(Ident::new("a".into(), span)),
type_ann: None

View File

@ -390,6 +390,7 @@ impl<'a, I: Tokens> ParseObject<'a, Pat> for Parser<'a, I> {
let arg = Box::new(self.parse_binding_pat_or_ident()?);
return Ok(ObjectPatProp::Rest(RestPat {
span: span!(start),
dot3_token,
arg,
type_ann: None,

View File

@ -97,6 +97,7 @@ impl<'a, I: Tokens> Parser<'a, I> {
let pat = self.parse_binding_pat_or_ident()?;
let pat = Pat::Rest(RestPat {
span: span!(start),
dot3_token,
arg: Box::new(pat),
type_ann: None,
@ -143,6 +144,7 @@ impl<'a, I: Tokens> Parser<'a, I> {
let has_modifier = self.eat_any_ts_modifier()?;
let pat_start = cur_pos!();
let mut pat = self.parse_binding_element()?;
// let mut opt = false;
@ -164,21 +166,36 @@ impl<'a, I: Tokens> Parser<'a, I> {
match pat {
Pat::Array(ArrayPat {
ref mut type_ann, ..
ref mut type_ann,
ref mut span,
..
})
| Pat::Assign(AssignPat {
ref mut type_ann, ..
ref mut type_ann,
ref mut span,
..
})
| Pat::Ident(Ident {
ref mut type_ann, ..
ref mut type_ann,
ref mut span,
..
})
| Pat::Object(ObjectPat {
ref mut type_ann, ..
ref mut type_ann,
ref mut span,
..
})
| Pat::Rest(RestPat {
ref mut type_ann, ..
ref mut type_ann,
ref mut span,
..
}) => {
*type_ann = self.try_parse_ts_type_ann()?;
let new_type_ann = self.try_parse_ts_type_ann()?;
if new_type_ann.is_some() {
*span =
Span::new(pat_start, self.input.prev_span().hi(), Default::default());
}
*type_ann = new_type_ann;
}
Pat::Invalid(..) => {}
_ => unreachable!("invalid syntax: Pat: {:?}", pat),
@ -242,6 +259,7 @@ impl<'a, I: Tokens> Parser<'a, I> {
};
let pat = Pat::Rest(RestPat {
span: span!(start),
dot3_token,
arg: Box::new(pat),
type_ann,
@ -341,6 +359,7 @@ impl<'a, I: Tokens> Parser<'a, I> {
};
let pat = Pat::Rest(RestPat {
span: span!(start),
dot3_token,
arg: Box::new(pat),
type_ann,
@ -547,6 +566,7 @@ impl<'a, I: Tokens> Parser<'a, I> {
PropOrSpread::Spread(SpreadElement { dot3_token, expr }) => {
Ok(ObjectPatProp::Rest(RestPat {
span,
dot3_token,
// FIXME: is BindingPat correct?
arg: Box::new(
@ -613,9 +633,11 @@ impl<'a, I: Tokens> Parser<'a, I> {
expr,
}) => {
// TODO: is BindingPat correct?
let expr_span = expr.span();
self.reparse_expr_as_pat(pat_ty.element(), expr)
.map(|pat| {
Pat::Rest(RestPat {
span: expr_span,
dot3_token,
arg: Box::new(pat),
type_ann: None,
@ -696,13 +718,17 @@ impl<'a, I: Tokens> Parser<'a, I> {
PatOrExprOrSpread::ExprOrSpread(ExprOrSpread {
spread: Some(dot3_token),
expr,
}) => self.reparse_expr_as_pat(pat_ty, expr).map(|pat| {
Pat::Rest(RestPat {
dot3_token,
arg: Box::new(pat),
type_ann: None,
})
})?,
}) => {
let expr_span = expr.span();
self.reparse_expr_as_pat(pat_ty, expr).map(|pat| {
Pat::Rest(RestPat {
span: expr_span,
dot3_token,
arg: Box::new(pat),
type_ann: None,
})
})?
}
PatOrExprOrSpread::ExprOrSpread(ExprOrSpread { expr, .. }) => {
self.reparse_expr_as_pat(pat_ty, expr)?
}

View File

@ -1155,6 +1155,7 @@ mod tests {
param: Pat::Object(ObjectPat {
span,
props: vec![ObjectPatProp::Rest(RestPat {
span,
dot3_token: span,
arg: box Pat::Ident(Ident::new("a34".into(), span)),
type_ann: None

View File

@ -25,7 +25,7 @@
"type": "Identifier",
"span": {
"start": 1,
"end": 2,
"end": 10,
"ctxt": 0
},
"value": "x",

View File

@ -25,7 +25,7 @@
"type": "Identifier",
"span": {
"start": 10,
"end": 11,
"end": 14,
"ctxt": 0
},
"value": "a",

View File

@ -23,6 +23,11 @@
"params": [
{
"type": "RestElement",
"span": {
"start": 6,
"end": 21,
"ctxt": 0
},
"rest": {
"start": 6,
"end": 9,

View File

@ -23,6 +23,11 @@
"params": [
{
"type": "RestElement",
"span": {
"start": 7,
"end": 21,
"ctxt": 0
},
"rest": {
"start": 7,
"end": 10,

View File

@ -25,7 +25,7 @@
"type": "Identifier",
"span": {
"start": 7,
"end": 8,
"end": 17,
"ctxt": 0
},
"value": "x",

View File

@ -32,7 +32,7 @@
"type": "Identifier",
"span": {
"start": 1,
"end": 2,
"end": 10,
"ctxt": 0
},
"value": "x",

View File

@ -25,7 +25,7 @@
"type": "Identifier",
"span": {
"start": 66,
"end": 67,
"end": 70,
"ctxt": 0
},
"value": "a",

View File

@ -25,7 +25,7 @@
"type": "Identifier",
"span": {
"start": 4,
"end": 5,
"end": 8,
"ctxt": 0
},
"value": "a",

View File

@ -25,7 +25,7 @@
"type": "Identifier",
"span": {
"start": 1,
"end": 2,
"end": 11,
"ctxt": 0
},
"value": "x",

View File

@ -25,7 +25,7 @@
"type": "Identifier",
"span": {
"start": 1,
"end": 2,
"end": 7,
"ctxt": 0
},
"value": "x",

View File

@ -104,7 +104,7 @@
"type": "Identifier",
"span": {
"start": 60,
"end": 63,
"end": 68,
"ctxt": 0
},
"value": "set",
@ -131,7 +131,7 @@
"type": "Identifier",
"span": {
"start": 70,
"end": 78,
"end": 87,
"ctxt": 0
},
"value": "readonly",

View File

@ -50,7 +50,7 @@
"type": "Identifier",
"span": {
"start": 27,
"end": 28,
"end": 36,
"ctxt": 0
},
"value": "x",
@ -77,7 +77,7 @@
"type": "Identifier",
"span": {
"start": 38,
"end": 39,
"end": 47,
"ctxt": 0
},
"value": "y",
@ -128,7 +128,7 @@
"type": "Identifier",
"span": {
"start": 67,
"end": 68,
"end": 76,
"ctxt": 0
},
"value": "x",
@ -155,7 +155,7 @@
"type": "Identifier",
"span": {
"start": 78,
"end": 79,
"end": 87,
"ctxt": 0
},
"value": "y",
@ -206,7 +206,7 @@
"type": "Identifier",
"span": {
"start": 107,
"end": 108,
"end": 113,
"ctxt": 0
},
"value": "x",
@ -233,7 +233,7 @@
"type": "Identifier",
"span": {
"start": 115,
"end": 116,
"end": 121,
"ctxt": 0
},
"value": "y",

View File

@ -51,7 +51,7 @@
"type": "Identifier",
"span": {
"start": 20,
"end": 21,
"end": 24,
"ctxt": 0
},
"value": "a",
@ -89,7 +89,7 @@
"type": "Identifier",
"span": {
"start": 26,
"end": 27,
"end": 31,
"ctxt": 0
},
"value": "b",
@ -125,6 +125,11 @@
},
{
"type": "RestElement",
"span": {
"start": 33,
"end": 42,
"ctxt": 0
},
"rest": {
"start": 33,
"end": 36,

View File

@ -82,7 +82,7 @@
"type": "Identifier",
"span": {
"start": 65,
"end": 67,
"end": 75,
"ctxt": 0
},
"value": "pu",
@ -142,7 +142,7 @@
"type": "Identifier",
"span": {
"start": 118,
"end": 120,
"end": 129,
"ctxt": 0
},
"value": "pi",
@ -249,7 +249,7 @@
"type": "Identifier",
"span": {
"start": 246,
"end": 247,
"end": 256,
"ctxt": 0
},
"value": "y",

View File

@ -116,7 +116,7 @@
"type": "Identifier",
"span": {
"start": 81,
"end": 86,
"end": 94,
"ctxt": 0
},
"value": "nodes",
@ -162,7 +162,7 @@
"type": "Identifier",
"span": {
"start": 96,
"end": 100,
"end": 125,
"ctxt": 0
},
"value": "test",
@ -185,7 +185,7 @@
"type": "Identifier",
"span": {
"start": 103,
"end": 107,
"end": 113,
"ctxt": 0
},
"value": "node",
@ -246,7 +246,7 @@
"type": "Identifier",
"span": {
"start": 127,
"end": 134,
"end": 143,
"ctxt": 0
},
"value": "message",

View File

@ -32,7 +32,7 @@
"type": "Identifier",
"span": {
"start": 49,
"end": 67,
"end": 99,
"ctxt": 0
},
"value": "tsConfigSourceFile",
@ -89,7 +89,7 @@
"type": "Identifier",
"span": {
"start": 101,
"end": 108,
"end": 116,
"ctxt": 0
},
"value": "propKey",
@ -116,7 +116,7 @@
"type": "Identifier",
"span": {
"start": 118,
"end": 130,
"end": 138,
"ctxt": 0
},
"value": "elementValue",

View File

@ -53,7 +53,7 @@
"type": "Identifier",
"span": {
"start": 9,
"end": 17,
"end": 25,
"ctxt": 0
},
"value": "greeting",
@ -92,7 +92,7 @@
"type": "Identifier",
"span": {
"start": 37,
"end": 43,
"end": 51,
"ctxt": 0
},
"value": "target",

View File

@ -32,7 +32,7 @@
"type": "Identifier",
"span": {
"start": 14,
"end": 22,
"end": 30,
"ctxt": 0
},
"value": "greeting",
@ -71,7 +71,7 @@
"type": "Identifier",
"span": {
"start": 42,
"end": 48,
"end": 56,
"ctxt": 0
},
"value": "target",

View File

@ -32,7 +32,7 @@
"type": "Identifier",
"span": {
"start": 52,
"end": 56,
"end": 61,
"ctxt": 0
},
"value": "this",
@ -59,7 +59,7 @@
"type": "Identifier",
"span": {
"start": 63,
"end": 69,
"end": 85,
"ctxt": 0
},
"value": "$scope",
@ -111,7 +111,7 @@
"type": "Identifier",
"span": {
"start": 87,
"end": 93,
"end": 114,
"ctxt": 0
},
"value": "$attrs",

View File

@ -60,7 +60,7 @@
"type": "Identifier",
"span": {
"start": 18,
"end": 19,
"end": 27,
"ctxt": 0
},
"value": "x",

View File

@ -102,7 +102,7 @@
"type": "Identifier",
"span": {
"start": 62,
"end": 69,
"end": 74,
"ctxt": 0
},
"value": "variant",

View File

@ -61,7 +61,7 @@
"type": "Identifier",
"span": {
"start": 58,
"end": 65,
"end": 102,
"ctxt": 0
},
"value": "entries",
@ -308,7 +308,7 @@
"type": "Identifier",
"span": {
"start": 149,
"end": 156,
"end": 182,
"ctxt": 0
},
"value": "entries",

View File

@ -25,7 +25,7 @@
"type": "Identifier",
"span": {
"start": 14,
"end": 15,
"end": 19,
"ctxt": 0
},
"value": "x",

View File

@ -42,7 +42,7 @@
"type": "Identifier",
"span": {
"start": 22,
"end": 23,
"end": 27,
"ctxt": 0
},
"value": "x",

View File

@ -21,7 +21,7 @@
"type": "Identifier",
"span": {
"start": 24,
"end": 25,
"end": 34,
"ctxt": 0
},
"value": "x",

View File

@ -32,7 +32,7 @@
"type": "Identifier",
"span": {
"start": 18,
"end": 19,
"end": 27,
"ctxt": 0
},
"value": "x",
@ -111,7 +111,7 @@
"type": "Identifier",
"span": {
"start": 57,
"end": 58,
"end": 66,
"ctxt": 0
},
"value": "x",

View File

@ -25,7 +25,7 @@
"type": "Identifier",
"span": {
"start": 11,
"end": 12,
"end": 17,
"ctxt": 0
},
"value": "x",
@ -134,7 +134,7 @@
"type": "Identifier",
"span": {
"start": 47,
"end": 48,
"end": 53,
"ctxt": 0
},
"value": "x",

View File

@ -47,7 +47,7 @@
"type": "Identifier",
"span": {
"start": 20,
"end": 21,
"end": 29,
"ctxt": 0
},
"value": "x",

View File

@ -47,7 +47,7 @@
"type": "Identifier",
"span": {
"start": 24,
"end": 25,
"end": 33,
"ctxt": 0
},
"value": "x",

View File

@ -86,7 +86,7 @@
"type": "Identifier",
"span": {
"start": 31,
"end": 32,
"end": 41,
"ctxt": 0
},
"value": "x",
@ -111,6 +111,11 @@
},
{
"type": "RestElement",
"span": {
"start": 43,
"end": 57,
"ctxt": 0
},
"rest": {
"start": 43,
"end": 46,

View File

@ -38,7 +38,7 @@
"type": "ObjectPattern",
"span": {
"start": 15,
"end": 24,
"end": 29,
"ctxt": 0
},
"properties": [

View File

@ -77,7 +77,7 @@
"type": "Identifier",
"span": {
"start": 28,
"end": 31,
"end": 34,
"ctxt": 0
},
"value": "bar",

View File

@ -50,7 +50,7 @@
"type": "Identifier",
"span": {
"start": 11,
"end": 12,
"end": 15,
"ctxt": 0
},
"value": "a",

View File

@ -50,7 +50,7 @@
"type": "Identifier",
"span": {
"start": 8,
"end": 12,
"end": 20,
"ctxt": 0
},
"value": "this",

View File

@ -50,7 +50,7 @@
"type": "Identifier",
"span": {
"start": 8,
"end": 9,
"end": 17,
"ctxt": 0
},
"value": "a",
@ -77,7 +77,7 @@
"type": "Identifier",
"span": {
"start": 19,
"end": 20,
"end": 29,
"ctxt": 0
},
"value": "b",
@ -102,6 +102,11 @@
},
{
"type": "RestElement",
"span": {
"start": 31,
"end": 45,
"ctxt": 0
},
"rest": {
"start": 31,
"end": 34,

View File

@ -60,7 +60,7 @@
"type": "Identifier",
"span": {
"start": 49,
"end": 54,
"end": 57,
"ctxt": 0
},
"value": "value",

View File

@ -23,6 +23,11 @@
"params": [
{
"type": "RestElement",
"span": {
"start": 13,
"end": 52,
"ctxt": 0
},
"rest": {
"start": 13,
"end": 16,

View File

@ -255,6 +255,7 @@ impl Fold<Expr> for MethodFolder {
is_async: false,
is_generator: false,
params: vec![Pat::Rest(RestPat {
span: DUMMY_SP,
dot3_token: DUMMY_SP,
arg: box Pat::Ident(args_ident.clone()),
type_ann: Default::default(),

View File

@ -269,6 +269,7 @@ impl Decorators {
accessibility: Default::default(),
params: if super_class_ident.is_some() {
vec![PatOrTsParamProp::Pat(Pat::Rest(RestPat {
span: DUMMY_SP,
dot3_token: DUMMY_SP,
arg: box Pat::Ident(quote_ident!("args")),
type_ann: Default::default(),

View File

@ -1291,6 +1291,7 @@ pub fn default_constructor(has_super: bool) -> Constructor {
is_optional: false,
params: if has_super {
vec![PatOrTsParamProp::Pat(Pat::Rest(RestPat {
span: DUMMY_SP,
dot3_token: DUMMY_SP,
arg: box Pat::Ident(quote_ident!(span, "args")),
type_ann: Default::default(),