Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix string literal span on import/export specifiers to only include string literal #540

Merged
merged 11 commits into from
Dec 29, 2019
55 changes: 26 additions & 29 deletions ecmascript/parser/src/parser/stmt/module_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,21 @@ impl<'a, I: Tokens> Parser<'a, I> {
// Handle import 'mod.js'
let str_start = cur_pos!();
if let Ok(&Token::Str { .. }) = cur!(false) {
match bump!() {
Token::Str { value, has_escape } => {
expect!(';');
return Ok(ModuleDecl::Import(ImportDecl {
span: span!(start),
src: Str {
span: span!(str_start),
value,
has_escape,
},
specifiers: vec![],
}))
.map(ModuleItem::from);
}
let src = match bump!() {
Token::Str { value, has_escape } => Str {
span: span!(str_start),
value,
has_escape,
},
_ => unreachable!(),
}
};
expect!(';');
return Ok(ModuleDecl::Import(ImportDecl {
span: span!(start),
src,
specifiers: vec![],
}))
.map(ModuleItem::from);
}

let mut specifiers = vec![];
Expand Down Expand Up @@ -86,7 +85,7 @@ impl<'a, I: Tokens> Parser<'a, I> {
let src = self.parse_from_clause_and_semi()?;

Ok(ModuleDecl::Import(ImportDecl {
span: Span::new(start, src.span.hi(), Default::default()),
span: span!(start),
specifiers,
src,
}))
Expand Down Expand Up @@ -215,7 +214,7 @@ impl<'a, I: Tokens> Parser<'a, I> {
if is!("from") {
let src = self.parse_from_clause_and_semi()?;
return Ok(ModuleDecl::ExportAll(ExportAll {
span: Span::new(start, src.span.hi(), Default::default()),
span: span!(start),
src,
}));
}
Expand Down Expand Up @@ -446,22 +445,20 @@ impl<'a, I: Tokens> Parser<'a, I> {
fn parse_from_clause_and_semi(&mut self) -> PResult<'a, Str> {
expect!("from");

let start = cur_pos!();
match *cur!(true)? {
let str_start = cur_pos!();
let src = match *cur!(true)? {
Token::Str { .. } => match bump!() {
Token::Str { value, has_escape } => {
expect!(';');

Ok(Str {
value,
has_escape,
span: Span::new(start, self.input.prev_span().hi(), Default::default()),
})
}
Token::Str { value, has_escape } => Str {
value,
has_escape,
span: span!(str_start),
},
_ => unreachable!(),
},
_ => unexpected!(),
}
};
expect!(';');
Ok(src)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"type": "StringLiteral",
"span": {
"start": 18,
"end": 26,
"end": 25,
"ctxt": 0
},
"value": "react",
Expand Down Expand Up @@ -461,4 +461,4 @@
}
],
"interpreter": null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"type": "StringLiteral",
"span": {
"start": 18,
"end": 26,
"end": 25,
"ctxt": 0
},
"value": "react",
Expand Down
4 changes: 3 additions & 1 deletion ecmascript/parser/tests/typescript/custom/issue-535/input.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from "test";
export * from "test" ;
import "test" ;
import Test from "test" ;
64 changes: 61 additions & 3 deletions ecmascript/parser/tests/typescript/custom/issue-535/input.ts.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,80 @@
"type": "Module",
"span": {
"start": 0,
"end": 21,
"end": 66,
"ctxt": 0
},
"body": [
{
"type": "ExportAllDeclaration",
"span": {
"start": 0,
"end": 21,
"end": 22,
"ctxt": 0
},
"source": {
"type": "StringLiteral",
"span": {
"start": 14,
"end": 21,
"end": 20,
"ctxt": 0
},
"value": "test",
"hasEscape": false
}
},
{
"type": "ImportDeclaration",
"span": {
"start": 24,
"end": 39,
"ctxt": 0
},
"specifiers": [],
"source": {
"type": "StringLiteral",
"span": {
"start": 31,
"end": 37,
"ctxt": 0
},
"value": "test",
"hasEscape": false
}
},
{
"type": "ImportDeclaration",
"span": {
"start": 41,
"end": 66,
"ctxt": 0
},
"specifiers": [
{
"type": "ImportDefaultSpecifier",
"span": {
"start": 48,
"end": 52,
"ctxt": 0
},
"local": {
"type": "Identifier",
"span": {
"start": 48,
"end": 52,
"ctxt": 0
},
"value": "Test",
"typeAnnotation": null,
"optional": false
}
}
],
"source": {
"type": "StringLiteral",
"span": {
"start": 58,
"end": 64,
"ctxt": 0
},
"value": "test",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"type": "StringLiteral",
"span": {
"start": 18,
"end": 26,
"end": 25,
"ctxt": 0
},
"value": "react",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"type": "StringLiteral",
"span": {
"start": 45,
"end": 49,
"end": 48,
"ctxt": 0
},
"value": "a",
Expand Down