From 83153a0f8588f30a72f59525d9605035c43af4a2 Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Sat, 25 Sep 2021 22:53:57 +0800 Subject: [PATCH] fix(es/parser): Report errors for multiple import/export specifiers without comma (#2302) --- .../parser/src/parser/stmt/module_item.rs | 26 +++++++++---------- .../fail/26c0710a6449872a.module.js.stderr | 6 ++--- .../multiple-specifiers/export/input.ts | 1 + .../export/input.ts.stderr | 6 +++++ .../multiple-specifiers/import/input.ts | 1 + .../import/input.ts.stderr | 6 +++++ 6 files changed, 29 insertions(+), 17 deletions(-) create mode 100644 ecmascript/parser/tests/typescript-errors/multiple-specifiers/export/input.ts create mode 100644 ecmascript/parser/tests/typescript-errors/multiple-specifiers/export/input.ts.stderr create mode 100644 ecmascript/parser/tests/typescript-errors/multiple-specifiers/import/input.ts create mode 100644 ecmascript/parser/tests/typescript-errors/multiple-specifiers/import/input.ts.stderr diff --git a/ecmascript/parser/src/parser/stmt/module_item.rs b/ecmascript/parser/src/parser/stmt/module_item.rs index b551541184d8..79248adec9b3 100644 --- a/ecmascript/parser/src/parser/stmt/module_item.rs +++ b/ecmascript/parser/src/parser/stmt/module_item.rs @@ -118,15 +118,14 @@ impl<'a, I: Tokens> Parser { local, })); } else if eat!(self, '{') { - let mut first = true; while !eof!(self) && !is!(self, '}') { - if first { - first = false; - } else if eat!(self, ',') && is!(self, '}') { + specifiers.push(self.parse_import_specifier()?); + + if is!(self, '}') { break; + } else { + expect!(self, ','); } - - specifiers.push(self.parse_import_specifier()?); } expect!(self, '}'); } @@ -513,18 +512,17 @@ impl<'a, I: Tokens> Parser { exported: default, })) } - let mut first = true; - while is_one_of!(self, ',', IdentName) { - if first { - first = false; - } else if eat!(self, ',') && is!(self, '}') { - break; - } - + while !eof!(self) && !is!(self, '}') { specifiers.push( self.parse_named_export_specifier() .map(ExportSpecifier::Named)?, ); + + if is!(self, '}') { + break; + } else { + expect!(self, ','); + } } expect!(self, '}'); diff --git a/ecmascript/parser/tests/test262-error-references/fail/26c0710a6449872a.module.js.stderr b/ecmascript/parser/tests/test262-error-references/fail/26c0710a6449872a.module.js.stderr index e92e20b85c39..10dd3266cd05 100644 --- a/ecmascript/parser/tests/test262-error-references/fail/26c0710a6449872a.module.js.stderr +++ b/ecmascript/parser/tests/test262-error-references/fail/26c0710a6449872a.module.js.stderr @@ -1,6 +1,6 @@ -error: Unexpected token `a`. Expected a string literal - --> $DIR/tests/test262-parser/fail/26c0710a6449872a.module.js:1:20 +error: Expected ,, got b + --> $DIR/tests/test262-parser/fail/26c0710a6449872a.module.js:1:12 | 1 | export {as b} from a - | ^ + | ^ diff --git a/ecmascript/parser/tests/typescript-errors/multiple-specifiers/export/input.ts b/ecmascript/parser/tests/typescript-errors/multiple-specifiers/export/input.ts new file mode 100644 index 000000000000..62352117ea7b --- /dev/null +++ b/ecmascript/parser/tests/typescript-errors/multiple-specifiers/export/input.ts @@ -0,0 +1 @@ +export { a b c } diff --git a/ecmascript/parser/tests/typescript-errors/multiple-specifiers/export/input.ts.stderr b/ecmascript/parser/tests/typescript-errors/multiple-specifiers/export/input.ts.stderr new file mode 100644 index 000000000000..0d02af58e814 --- /dev/null +++ b/ecmascript/parser/tests/typescript-errors/multiple-specifiers/export/input.ts.stderr @@ -0,0 +1,6 @@ +error: Expected ,, got b + --> $DIR/tests/typescript-errors/multiple-specifiers/export/input.ts:1:12 + | +1 | export { a b c } + | ^ + diff --git a/ecmascript/parser/tests/typescript-errors/multiple-specifiers/import/input.ts b/ecmascript/parser/tests/typescript-errors/multiple-specifiers/import/input.ts new file mode 100644 index 000000000000..659e2b40a1ed --- /dev/null +++ b/ecmascript/parser/tests/typescript-errors/multiple-specifiers/import/input.ts @@ -0,0 +1 @@ +import { a b c } from 'mod' diff --git a/ecmascript/parser/tests/typescript-errors/multiple-specifiers/import/input.ts.stderr b/ecmascript/parser/tests/typescript-errors/multiple-specifiers/import/input.ts.stderr new file mode 100644 index 000000000000..acf6fb6699e7 --- /dev/null +++ b/ecmascript/parser/tests/typescript-errors/multiple-specifiers/import/input.ts.stderr @@ -0,0 +1,6 @@ +error: Expected ,, got b + --> $DIR/tests/typescript-errors/multiple-specifiers/import/input.ts:1:12 + | +1 | import { a b c } from 'mod' + | ^ +