From a4c56a5de8dfe1a7866099747cb8189fce2255a5 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Fri, 27 Dec 2019 23:55:34 -0500 Subject: [PATCH 01/11] String literal span on import/export specifiers should only include the string literal. --- ecmascript/parser/src/parser/stmt/module_item.rs | 6 ++++-- .../parser/tests/typescript/custom/issue-535/input.ts | 4 +++- .../parser/tests/typescript/custom/issue-535/input.ts.json | 6 +++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/ecmascript/parser/src/parser/stmt/module_item.rs b/ecmascript/parser/src/parser/stmt/module_item.rs index d9e2e29a5da6..b6cf4c1788fa 100644 --- a/ecmascript/parser/src/parser/stmt/module_item.rs +++ b/ecmascript/parser/src/parser/stmt/module_item.rs @@ -29,11 +29,12 @@ impl<'a, I: Tokens> Parser<'a, I> { if let Ok(&Token::Str { .. }) = cur!(false) { match bump!() { Token::Str { value, has_escape } => { + let str_end = self.input.prev_span().hi(); expect!(';'); return Ok(ModuleDecl::Import(ImportDecl { span: span!(start), src: Str { - span: span!(str_start), + span: Span::new(str_start, str_end, Default::default()), value, has_escape, }, @@ -450,12 +451,13 @@ impl<'a, I: Tokens> Parser<'a, I> { match *cur!(true)? { Token::Str { .. } => match bump!() { Token::Str { value, has_escape } => { + let end = self.input.prev_span().hi(); expect!(';'); Ok(Str { value, has_escape, - span: Span::new(start, self.input.prev_span().hi(), Default::default()), + span: Span::new(start, end, Default::default()), }) } _ => unreachable!(), diff --git a/ecmascript/parser/tests/typescript/custom/issue-535/input.ts b/ecmascript/parser/tests/typescript/custom/issue-535/input.ts index 65df9f3025ec..527875f93dcf 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 * as t 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..fbcdac22ad45 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": 68, "ctxt": 0 }, "body": [ @@ -10,14 +10,14 @@ "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", From 3d4a05e6324bfa22216c7b8897f2af47d2dbbda7 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Sat, 28 Dec 2019 00:08:35 -0500 Subject: [PATCH 02/11] Update other tests to have the correct string literal end. --- .../parser/tests/jsx/basic/custom/unary-paren/input.js.json | 2 +- ecmascript/parser/tests/jsx/basic/custom/unary/input.js.json | 2 +- .../parser/tests/typescript/custom/tsx-unary/input.tsx.json | 2 +- .../parser/tests/typescript/import/not-top-level/input.ts.json | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) 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..a52cf312df49 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", 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/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", From dc89978c59ca3c7aecc34745a6829a3939ea62b7 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Sat, 28 Dec 2019 00:14:45 -0500 Subject: [PATCH 03/11] Maybe correct input.ts.json... Filled this out based on other files. --- .../typescript/custom/issue-535/input.ts | 2 +- .../typescript/custom/issue-535/input.ts.json | 60 ++++++++++++++++++- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/ecmascript/parser/tests/typescript/custom/issue-535/input.ts b/ecmascript/parser/tests/typescript/custom/issue-535/input.ts index 527875f93dcf..764c8b7a0e51 100644 --- a/ecmascript/parser/tests/typescript/custom/issue-535/input.ts +++ b/ecmascript/parser/tests/typescript/custom/issue-535/input.ts @@ -1,3 +1,3 @@ export * from "test" ; import "test" ; -import * as t from "test" ; \ No newline at end of file +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 fbcdac22ad45..e9fdd39f8032 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": 68, + "end": 66, "ctxt": 0 }, "body": [ @@ -23,6 +23,64 @@ "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": 7, + "end": 12, + "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", + "hasEscape": false + } } ], "interpreter": null From 0846d65ac21ce5af864868a88e8d732b65bc5e68 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Sat, 28 Dec 2019 00:42:08 -0500 Subject: [PATCH 04/11] Declaration should include semi-colon. String literal span only be string literal. --- .../parser/tests/jsx/basic/custom/unary-paren/input.js.json | 4 ++-- ecmascript/parser/tests/jsx/basic/custom/unary/input.js.json | 4 ++-- .../parser/tests/typescript/custom/tsx-unary/input.tsx.json | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) 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 a52cf312df49..9120d94d78fa 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 @@ -10,7 +10,7 @@ "type": "ImportDeclaration", "span": { "start": 0, - "end": 26, + "end": 27, "ctxt": 0 }, "specifiers": [ @@ -38,7 +38,7 @@ "type": "StringLiteral", "span": { "start": 18, - "end": 25, + "end": 26, "ctxt": 0 }, "value": "react", 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 d6ad9cbcf8b1..9506395e22cd 100644 --- a/ecmascript/parser/tests/jsx/basic/custom/unary/input.js.json +++ b/ecmascript/parser/tests/jsx/basic/custom/unary/input.js.json @@ -10,7 +10,7 @@ "type": "ImportDeclaration", "span": { "start": 0, - "end": 26, + "end": 27, "ctxt": 0 }, "specifiers": [ @@ -38,7 +38,7 @@ "type": "StringLiteral", "span": { "start": 18, - "end": 25, + "end": 26, "ctxt": 0 }, "value": "react", 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 d6ad9cbcf8b1..9506395e22cd 100644 --- a/ecmascript/parser/tests/typescript/custom/tsx-unary/input.tsx.json +++ b/ecmascript/parser/tests/typescript/custom/tsx-unary/input.tsx.json @@ -10,7 +10,7 @@ "type": "ImportDeclaration", "span": { "start": 0, - "end": 26, + "end": 27, "ctxt": 0 }, "specifiers": [ @@ -38,7 +38,7 @@ "type": "StringLiteral", "span": { "start": 18, - "end": 25, + "end": 26, "ctxt": 0 }, "value": "react", From 734e9f48b4e4f4bb3341e2aa43439270fb105146 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Sat, 28 Dec 2019 02:16:43 -0500 Subject: [PATCH 05/11] Fix some issues in json files. --- .../parser/tests/jsx/basic/custom/unary-paren/input.js.json | 6 +++--- .../parser/tests/typescript/custom/issue-535/input.ts.json | 6 +++--- .../parser/tests/typescript/custom/tsx-unary/input.tsx.json | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) 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 9120d94d78fa..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 @@ -10,7 +10,7 @@ "type": "ImportDeclaration", "span": { "start": 0, - "end": 27, + "end": 26, "ctxt": 0 }, "specifiers": [ @@ -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/typescript/custom/issue-535/input.ts.json b/ecmascript/parser/tests/typescript/custom/issue-535/input.ts.json index e9fdd39f8032..c8c90a239c5c 100644 --- a/ecmascript/parser/tests/typescript/custom/issue-535/input.ts.json +++ b/ecmascript/parser/tests/typescript/custom/issue-535/input.ts.json @@ -54,8 +54,8 @@ { "type": "ImportDefaultSpecifier", "span": { - "start": 7, - "end": 12, + "start": 48, + "end": 52, "ctxt": 0 }, "local": { @@ -84,4 +84,4 @@ } ], "interpreter": null -} +} \ No newline at end of file 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 9506395e22cd..01d1f8cbaa20 100644 --- a/ecmascript/parser/tests/typescript/custom/tsx-unary/input.tsx.json +++ b/ecmascript/parser/tests/typescript/custom/tsx-unary/input.tsx.json @@ -10,7 +10,7 @@ "type": "ImportDeclaration", "span": { "start": 0, - "end": 27, + "end": 26, "ctxt": 0 }, "specifiers": [ @@ -38,7 +38,7 @@ "type": "StringLiteral", "span": { "start": 18, - "end": 26, + "end": 25, "ctxt": 0 }, "value": "react", @@ -445,4 +445,4 @@ } ], "interpreter": null -} +} \ No newline at end of file From d697d4decac8173efbe00ef78d43615b55017cb5 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Sat, 28 Dec 2019 02:28:20 -0500 Subject: [PATCH 06/11] Fix span of import declaration. --- ecmascript/parser/src/parser/stmt/module_item.rs | 2 +- ecmascript/parser/tests/jsx/basic/custom/unary/input.js.json | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ecmascript/parser/src/parser/stmt/module_item.rs b/ecmascript/parser/src/parser/stmt/module_item.rs index b6cf4c1788fa..648742c1bacf 100644 --- a/ecmascript/parser/src/parser/stmt/module_item.rs +++ b/ecmascript/parser/src/parser/stmt/module_item.rs @@ -87,7 +87,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, })) 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 9506395e22cd..d6ad9cbcf8b1 100644 --- a/ecmascript/parser/tests/jsx/basic/custom/unary/input.js.json +++ b/ecmascript/parser/tests/jsx/basic/custom/unary/input.js.json @@ -10,7 +10,7 @@ "type": "ImportDeclaration", "span": { "start": 0, - "end": 27, + "end": 26, "ctxt": 0 }, "specifiers": [ @@ -38,7 +38,7 @@ "type": "StringLiteral", "span": { "start": 18, - "end": 26, + "end": 25, "ctxt": 0 }, "value": "react", From 54e18cc969fc64e1de9103b1769f0b4d72f09821 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Sat, 28 Dec 2019 02:31:36 -0500 Subject: [PATCH 07/11] Fix ExportAll span. --- ecmascript/parser/src/parser/stmt/module_item.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ecmascript/parser/src/parser/stmt/module_item.rs b/ecmascript/parser/src/parser/stmt/module_item.rs index 648742c1bacf..e6f307aa66f0 100644 --- a/ecmascript/parser/src/parser/stmt/module_item.rs +++ b/ecmascript/parser/src/parser/stmt/module_item.rs @@ -216,7 +216,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, })); } From 32a64fce9a27b64c5748f564f04206eae36255ed Mon Sep 17 00:00:00 2001 From: David Sherret Date: Sat, 28 Dec 2019 02:37:14 -0500 Subject: [PATCH 08/11] Update local var name to be a bit more clear. --- ecmascript/parser/src/parser/stmt/module_item.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ecmascript/parser/src/parser/stmt/module_item.rs b/ecmascript/parser/src/parser/stmt/module_item.rs index e6f307aa66f0..17838e12bffb 100644 --- a/ecmascript/parser/src/parser/stmt/module_item.rs +++ b/ecmascript/parser/src/parser/stmt/module_item.rs @@ -447,17 +447,17 @@ impl<'a, I: Tokens> Parser<'a, I> { fn parse_from_clause_and_semi(&mut self) -> PResult<'a, Str> { expect!("from"); - let start = cur_pos!(); + let str_start = cur_pos!(); match *cur!(true)? { Token::Str { .. } => match bump!() { Token::Str { value, has_escape } => { - let end = self.input.prev_span().hi(); + let str_end = self.input.prev_span().hi(); expect!(';'); Ok(Str { value, has_escape, - span: Span::new(start, end, Default::default()), + span: Span::new(str_start, str_end, Default::default()), }) } _ => unreachable!(), From 7547fcdca2fb47bdbb6b01277773228afcea51d9 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Sat, 28 Dec 2019 11:19:34 -0500 Subject: [PATCH 09/11] Capture Str in place. --- .../parser/src/parser/stmt/module_item.rs | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/ecmascript/parser/src/parser/stmt/module_item.rs b/ecmascript/parser/src/parser/stmt/module_item.rs index 17838e12bffb..385416b54fa6 100644 --- a/ecmascript/parser/src/parser/stmt/module_item.rs +++ b/ecmascript/parser/src/parser/stmt/module_item.rs @@ -29,15 +29,15 @@ impl<'a, I: Tokens> Parser<'a, I> { if let Ok(&Token::Str { .. }) = cur!(false) { match bump!() { Token::Str { value, has_escape } => { - let str_end = self.input.prev_span().hi(); + let src = Str { + span: span!(str_start), + value, + has_escape, + }; expect!(';'); return Ok(ModuleDecl::Import(ImportDecl { span: span!(start), - src: Str { - span: Span::new(str_start, str_end, Default::default()), - value, - has_escape, - }, + src, specifiers: vec![], })) .map(ModuleItem::from); @@ -451,14 +451,13 @@ impl<'a, I: Tokens> Parser<'a, I> { match *cur!(true)? { Token::Str { .. } => match bump!() { Token::Str { value, has_escape } => { - let str_end = self.input.prev_span().hi(); - expect!(';'); - - Ok(Str { + let src = Str { value, has_escape, - span: Span::new(str_start, str_end, Default::default()), - }) + span: span!(str_start), + }; + expect!(';'); + Ok(src) } _ => unreachable!(), }, From 87343b6df641acb39d955fa4f40e222655a400d4 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Sat, 28 Dec 2019 11:28:16 -0500 Subject: [PATCH 10/11] Bit cleaner maybe? Let me know if I should revert. --- .../parser/src/parser/stmt/module_item.rs | 50 +++++++++---------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/ecmascript/parser/src/parser/stmt/module_item.rs b/ecmascript/parser/src/parser/stmt/module_item.rs index 385416b54fa6..6835e92d93e7 100644 --- a/ecmascript/parser/src/parser/stmt/module_item.rs +++ b/ecmascript/parser/src/parser/stmt/module_item.rs @@ -27,23 +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 } => { - let src = Str { - span: span!(str_start), - value, - has_escape, - }; - expect!(';'); - return Ok(ModuleDecl::Import(ImportDecl { - span: span!(start), - src, - 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![]; @@ -448,21 +446,19 @@ impl<'a, I: Tokens> Parser<'a, I> { expect!("from"); let str_start = cur_pos!(); - match *cur!(true)? { + let src = match *cur!(true)? { Token::Str { .. } => match bump!() { - Token::Str { value, has_escape } => { - let src = Str { - value, - has_escape, - span: span!(str_start), - }; - expect!(';'); - Ok(src) - } + Token::Str { value, has_escape } => Str { + value, + has_escape, + span: span!(str_start), + }, _ => unreachable!(), }, _ => unexpected!(), - } + }; + expect!(';'); + Ok(src) } } From 1e1fe422d1cdc4f28ea6d680d4e8cdb9e110571a Mon Sep 17 00:00:00 2001 From: David Sherret Date: Sat, 28 Dec 2019 11:36:01 -0500 Subject: [PATCH 11/11] Fix file line end (my editor automatically did this, but the tests reverted it back to this) --- .../parser/tests/typescript/custom/issue-535/input.ts.json | 2 +- .../parser/tests/typescript/custom/tsx-unary/input.tsx.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 c8c90a239c5c..91eca8fec617 100644 --- a/ecmascript/parser/tests/typescript/custom/issue-535/input.ts.json +++ b/ecmascript/parser/tests/typescript/custom/issue-535/input.ts.json @@ -84,4 +84,4 @@ } ], "interpreter": null -} \ No newline at end of file +} 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 01d1f8cbaa20..d6ad9cbcf8b1 100644 --- a/ecmascript/parser/tests/typescript/custom/tsx-unary/input.tsx.json +++ b/ecmascript/parser/tests/typescript/custom/tsx-unary/input.tsx.json @@ -445,4 +445,4 @@ } ], "interpreter": null -} \ No newline at end of file +}