diff --git a/ecmascript/parser/src/parser/stmt/module_item.rs b/ecmascript/parser/src/parser/stmt/module_item.rs index d9e2e29a5da6..6835e92d93e7 100644 --- a/ecmascript/parser/src/parser/stmt/module_item.rs +++ b/ecmascript/parser/src/parser/stmt/module_item.rs @@ -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![]; @@ -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, })) @@ -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, })); } @@ -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) } } diff --git a/ecmascript/parser/tests/jsx/basic/custom/unary-paren/input.js.json b/ecmascript/parser/tests/jsx/basic/custom/unary-paren/input.js.json index 7357f0c36b01..c3d1eaad7a7c 100644 --- a/ecmascript/parser/tests/jsx/basic/custom/unary-paren/input.js.json +++ b/ecmascript/parser/tests/jsx/basic/custom/unary-paren/input.js.json @@ -38,7 +38,7 @@ "type": "StringLiteral", "span": { "start": 18, - "end": 26, + "end": 25, "ctxt": 0 }, "value": "react", @@ -461,4 +461,4 @@ } ], "interpreter": null -} +} \ No newline at end of file diff --git a/ecmascript/parser/tests/jsx/basic/custom/unary/input.js.json b/ecmascript/parser/tests/jsx/basic/custom/unary/input.js.json index 87c2f636036b..d6ad9cbcf8b1 100644 --- a/ecmascript/parser/tests/jsx/basic/custom/unary/input.js.json +++ b/ecmascript/parser/tests/jsx/basic/custom/unary/input.js.json @@ -38,7 +38,7 @@ "type": "StringLiteral", "span": { "start": 18, - "end": 26, + "end": 25, "ctxt": 0 }, "value": "react", diff --git a/ecmascript/parser/tests/typescript/custom/issue-535/input.ts b/ecmascript/parser/tests/typescript/custom/issue-535/input.ts index 65df9f3025ec..764c8b7a0e51 100644 --- a/ecmascript/parser/tests/typescript/custom/issue-535/input.ts +++ b/ecmascript/parser/tests/typescript/custom/issue-535/input.ts @@ -1 +1,3 @@ -export * from "test"; \ No newline at end of file +export * from "test" ; +import "test" ; +import Test from "test" ; \ No newline at end of file diff --git a/ecmascript/parser/tests/typescript/custom/issue-535/input.ts.json b/ecmascript/parser/tests/typescript/custom/issue-535/input.ts.json index 12523c29206d..91eca8fec617 100644 --- a/ecmascript/parser/tests/typescript/custom/issue-535/input.ts.json +++ b/ecmascript/parser/tests/typescript/custom/issue-535/input.ts.json @@ -2,7 +2,7 @@ "type": "Module", "span": { "start": 0, - "end": 21, + "end": 66, "ctxt": 0 }, "body": [ @@ -10,14 +10,72 @@ "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", diff --git a/ecmascript/parser/tests/typescript/custom/tsx-unary/input.tsx.json b/ecmascript/parser/tests/typescript/custom/tsx-unary/input.tsx.json index 87c2f636036b..d6ad9cbcf8b1 100644 --- a/ecmascript/parser/tests/typescript/custom/tsx-unary/input.tsx.json +++ b/ecmascript/parser/tests/typescript/custom/tsx-unary/input.tsx.json @@ -38,7 +38,7 @@ "type": "StringLiteral", "span": { "start": 18, - "end": 26, + "end": 25, "ctxt": 0 }, "value": "react", diff --git a/ecmascript/parser/tests/typescript/import/not-top-level/input.ts.json b/ecmascript/parser/tests/typescript/import/not-top-level/input.ts.json index ba23fb012dbc..078be2af0de2 100644 --- a/ecmascript/parser/tests/typescript/import/not-top-level/input.ts.json +++ b/ecmascript/parser/tests/typescript/import/not-top-level/input.ts.json @@ -65,7 +65,7 @@ "type": "StringLiteral", "span": { "start": 45, - "end": 49, + "end": 48, "ctxt": 0 }, "value": "a",