fix(es/parser): Improve error message (#2304)

swc_ecma_parser:
 - Improve error message for unexpected tokens.
This commit is contained in:
Pig Fang 2021-09-26 00:19:08 +08:00 committed by GitHub
parent 83153a0f85
commit 552fc2374d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
85 changed files with 85 additions and 85 deletions

2
Cargo.lock generated
View File

@ -2661,7 +2661,7 @@ dependencies = [
[[package]]
name = "swc_ecma_parser"
version = "0.70.3"
version = "0.70.4"
dependencies = [
"either",
"enum_kind",

View File

@ -7,7 +7,7 @@ include = ["Cargo.toml", "src/**/*.rs", "examples/**/*.rs"]
license = "Apache-2.0/MIT"
name = "swc_ecma_parser"
repository = "https://github.com/swc-project/swc.git"
version = "0.70.3"
version = "0.70.4"
[package.metadata.docs.rs]
all-features = true

View File

@ -308,7 +308,7 @@ impl SyntaxError {
SyntaxError::ReservedWordInImport => "cannot import as reserved word".into(),
SyntaxError::AssignProperty => "assignment property is invalid syntax".into(),
SyntaxError::Expected(token, ref got) => {
format!("Expected {:?}, got {}", token, got).into()
format!("Expected '{:?}', got '{}'", token, got).into()
}
SyntaxError::ExpectedSemiForExprStmt { .. } => "Expected ';', '}' or <eof>".into(),

View File

@ -1,4 +1,4 @@
error: Expected ,, got {
error: Expected ',', got '{'
--> $DIR/tests/test262-parser/fail/08fa65d2ecddcfbe.js:1:13
|
1 | ({ set: s() { } })

View File

@ -1,4 +1,4 @@
error: Expected ;, got numeric literal
error: Expected ';', got 'numeric literal'
--> $DIR/tests/test262-parser/fail/0a225effb5493c00.js:1:15
|
1 | for (const of 42);

View File

@ -1,4 +1,4 @@
error: Expected ;, got )
error: Expected ';', got ')'
--> $DIR/tests/test262-parser/fail/0ddab4a1a651034c.js:1:24
|
1 | for (let x = 42 in list) process(x);

View File

@ -1,4 +1,4 @@
error: Expected ,, got .
error: Expected ',', got '.'
--> $DIR/tests/test262-parser/fail/143481afd6573e9b.js:1:18
|
1 | function* a({e: a.b}) {}

View File

@ -1,4 +1,4 @@
error: Expected ;, got )
error: Expected ';', got ')'
--> $DIR/tests/test262-parser/fail/154f02d86fce5e81.js:1:22
|
1 | for (const x = 0 in y){}

View File

@ -1,4 +1,4 @@
error: Expected (, got yield
error: Expected '(', got 'yield'
--> $DIR/tests/test262-parser/fail/16947dc1d11e5e70.js:1:11
|
1 | (function*yield(){})

View File

@ -1,4 +1,4 @@
error: Expected {, got +
error: Expected '{', got '+'
--> $DIR/tests/test262-parser/fail/1976350e287d5156.js:1:19
|
1 | class A extends a + b {}

View File

@ -1,4 +1,4 @@
error: Expected }, got ;
error: Expected '}', got ';'
--> $DIR/tests/test262-parser/fail/1acada3c651821cf.js:1:12
|
1 | `hello ${10;test`

View File

@ -1,4 +1,4 @@
error: Expected {, got numeric literal
error: Expected '{', got 'numeric literal'
--> $DIR/tests/test262-parser/fail/1b0b9bca042d4440.module.js:1:8
|
1 | export 3

View File

@ -1,4 +1,4 @@
error: Expected ,, got .
error: Expected ',', got '.'
--> $DIR/tests/test262-parser/fail/1b2e164ac5015a12.js:1:10
|
1 | ({a({e: a.b}){}})

View File

@ -1,4 +1,4 @@
error: Expected from, got ;
error: Expected 'from', got ';'
--> $DIR/tests/test262-parser/fail/1bc43dd97a16b9bb.module.js:1:10
|
1 | import {};

View File

@ -1,4 +1,4 @@
error: Expected ], got iter
error: Expected ']', got 'iter'
--> $DIR/tests/test262-parser/fail/1bde73ba53c309c8.js:1:12
|
1 | ({ *[yield iter]() {} })

View File

@ -1,4 +1,4 @@
error: Expected ), got (
error: Expected ')', got '('
--> $DIR/tests/test262-parser/fail/25b1013a4046bd70.js:1:21
|
1 | try {} catch (answer()) {}

View File

@ -1,4 +1,4 @@
error: Expected ,, got b
error: Expected ',', got 'b'
--> $DIR/tests/test262-parser/fail/26c0710a6449872a.module.js:1:12
|
1 | export {as b} from a

View File

@ -1,4 +1,4 @@
error: Expected {, got <eof>
error: Expected '{', got '<eof>'
--> $DIR/tests/test262-parser/fail/2d1410e37ecc3647.js:1:23
|
1 | function f(a, ...b = 0)

View File

@ -1,4 +1,4 @@
error: Expected ;, got )
error: Expected ';', got ')'
--> $DIR/tests/test262-parser/fail/3162394f5bc07198.js:1:21
|
1 | for(const a = 0 in b);

View File

@ -1,4 +1,4 @@
error: Expected ;, got )
error: Expected ';', got ')'
--> $DIR/tests/test262-parser/fail/33d43e9f01bda5ce.js:1:20
|
1 | for (let x = 0 in y){}

View File

@ -1,4 +1,4 @@
error: Expected ,, got numeric literal
error: Expected ',', got 'numeric literal'
--> $DIR/tests/test262-parser/fail/3425ca087ec1adb1.js:1:19
|
1 | var {x: y = yield 3} = z;

View File

@ -1,4 +1,4 @@
error: Expected ,, got .
error: Expected ',', got '.'
--> $DIR/tests/test262-parser/fail/369676814db0cbbf.js:1:14
|
1 | ({set a({e: a.b}){}})

View File

@ -1,4 +1,4 @@
error: Expected ], got ,
error: Expected ']', got ','
--> $DIR/tests/test262-parser/fail/3a9fa392421db6dd.js:1:5
|
1 | ({[a,b]:0})

View File

@ -1,4 +1,4 @@
error: Expected {, got /
error: Expected '{', got '/'
--> $DIR/tests/test262-parser/fail/45db351b2b07663a.module.js:1:8
|
1 | export / from a

View File

@ -1,4 +1,4 @@
error: Expected ;, got )
error: Expected ';', got ')'
--> $DIR/tests/test262-parser/fail/49861fa3ca0ffc30.js:1:15
|
1 | for ((i in {}));

View File

@ -1,4 +1,4 @@
error: Expected ,, got .
error: Expected ',', got '.'
--> $DIR/tests/test262-parser/fail/4c7ea6a86bafaf0f.js:1:17
|
1 | (function ({e: a.b}) {})

View File

@ -1,4 +1,4 @@
error: Expected ,, got .
error: Expected ',', got '.'
--> $DIR/tests/test262-parser/fail/4daec155c0322d5e.js:1:17
|
1 | function a({e: a.b}) {}

View File

@ -1,4 +1,4 @@
error: Expected ,, got .
error: Expected ',', got '.'
--> $DIR/tests/test262-parser/fail/537c4a516d7c8d7f.js:1:20
|
1 | try {} catch ({e: x.a}) {}

View File

@ -10,7 +10,7 @@ note: This is the expression part of an expression statement
1 | void { [1, 2]: 3 };
| ^^^^^^^^^^^^
error: Expected ], got ,
error: Expected ']', got ','
--> $DIR/tests/test262-parser/fail/58707e130fe451a8.js:1:10
|
1 | void { [1, 2]: 3 };

View File

@ -1,4 +1,4 @@
error: Expected from, got ;
error: Expected 'from', got ';'
--> $DIR/tests/test262-parser/fail/5bbf7371c3c77f3c.module.js:1:7
|
1 | import;

View File

@ -1,4 +1,4 @@
error: Expected from, got b
error: Expected 'from', got 'b'
--> $DIR/tests/test262-parser/fail/5fe7ff5c5cb5f438.module.js:1:11
|
1 | import a, b from 'a'

View File

@ -1,4 +1,4 @@
error: Expected {, got [
error: Expected '{', got '['
--> $DIR/tests/test262-parser/fail/65a73ccacb2dc502.js:1:8
|
1 | (class [a] {})

View File

@ -1,4 +1,4 @@
error: Expected {, got ;
error: Expected '{', got ';'
--> $DIR/tests/test262-parser/fail/6775a7f39c6b90fa.js:1:8
|
1 | class A;

View File

@ -1,4 +1,4 @@
error: Expected ;, got numeric literal
error: Expected ';', got 'numeric literal'
--> $DIR/tests/test262-parser/fail/6f7a5e4988b7d758.js:1:12
|
1 | for(let of 0);

View File

@ -1,4 +1,4 @@
error: Expected from, got ,
error: Expected 'from', got ','
--> $DIR/tests/test262-parser/fail/7464b0dfc4974a48.module.js:1:14
|
1 | import * as b, a from 'a'

View File

@ -1,4 +1,4 @@
error: Expected while, got *
error: Expected 'while', got '*'
--> $DIR/tests/test262-parser/fail/78e777115c4cfcc2.js:1:10
|
1 | do { x } *

View File

@ -1,4 +1,4 @@
error: Expected (, got }
error: Expected '(', got '}'
--> $DIR/tests/test262-parser/fail/7b70beed5a327548.js:1:7
|
1 | ({ *a })

View File

@ -1,4 +1,4 @@
error: Expected ,, got numeric literal
error: Expected ',', got 'numeric literal'
--> $DIR/tests/test262-parser/fail/7f9bbf314145e16f.js:1:6
|
1 | ({}=>0)

View File

@ -1,4 +1,4 @@
error: Expected ), got =
error: Expected ')', got '='
--> $DIR/tests/test262-parser/fail/831b4a1caf3c5a06.js:1:20
|
1 | try { } catch ([a] = []) { }

View File

@ -1,4 +1,4 @@
error: Expected ;, got .
error: Expected ';', got '.'
--> $DIR/tests/test262-parser/fail/84633c379e4010bf.js:1:29
|
1 | function *g() { return yield.x; }

View File

@ -1,4 +1,4 @@
error: Expected ,, got <eof>
error: Expected ',', got '<eof>'
--> $DIR/tests/test262-parser/fail/888d17b2ef3b2afc.js:1:9
|
1 | 1 + { t:t

View File

@ -1,4 +1,4 @@
error: Expected }, got <eof>
error: Expected '}', got '<eof>'
--> $DIR/tests/test262-parser/fail/8ba15f5246ca756c.js:1:9
|
1 | class A {

View File

@ -1,4 +1,4 @@
error: Expected (, got :
error: Expected '(', got ':'
--> $DIR/tests/test262-parser/fail/8e93bcbd389c6bf6.js:1:8
|
1 | ({ *[0]: 0 })

View File

@ -1,4 +1,4 @@
error: Expected as, got As
error: Expected 'as', got 'As'
--> $DIR/tests/test262-parser/fail/943297e7b6314bd8.module.js:1:10
|
1 | import * As a from 'a'

View File

@ -1,4 +1,4 @@
error: Expected ], got ,
error: Expected ']', got ','
--> $DIR/tests/test262-parser/fail/96d7443d69f12eea.js:1:8
|
1 | ({get[a,b]:0})

View File

@ -1,4 +1,4 @@
error: Expected {, got ,
error: Expected '{', got ','
--> $DIR/tests/test262-parser/fail/9767d9a6c9ea543c.js:1:17
|
1 | (class extends a,b {})

View File

@ -1,4 +1,4 @@
error: Expected {, got ;
error: Expected '{', got ';'
--> $DIR/tests/test262-parser/fail/98e0b4521b7ec370.module.js:1:7
|
1 | export;

View File

@ -1,4 +1,4 @@
error: Expected ,, got {
error: Expected ',', got '{'
--> $DIR/tests/test262-parser/fail/9c9196e04a0615c0.js:1:14
|
1 | ({ get: g(d) { } })

View File

@ -1,4 +1,4 @@
error: Expected from, got From
error: Expected 'from', got 'From'
--> $DIR/tests/test262-parser/fail/9c961739e5d9e2ff.module.js:1:10
|
1 | export * From "a"

View File

@ -1,4 +1,4 @@
error: Expected ,, got .
error: Expected ',', got '.'
--> $DIR/tests/test262-parser/fail/a02fdefc85b71fee.js:1:18
|
1 | (function* ({e: a.b}) {})

View File

@ -1,4 +1,4 @@
error: Expected }, got <eof>
error: Expected '}', got '<eof>'
--> $DIR/tests/test262-parser/fail/a069c2ecbeb0f43a.js:1:36
|
1 | switch (cond) { case 10: let a = 20;

View File

@ -1,4 +1,4 @@
error: Expected ,, got .
error: Expected ',', got '.'
--> $DIR/tests/test262-parser/fail/a31d1202f94df972.js:1:10
|
1 | var {a: b.c} = 0;

View File

@ -1,4 +1,4 @@
error: Expected from, got <eof>
error: Expected 'from', got '<eof>'
--> $DIR/tests/test262-parser/fail/aae75623a18fa153.module.js:1:9
|
1 | import {}

View File

@ -1,4 +1,4 @@
error: Expected ], got ,
error: Expected ']', got ','
--> $DIR/tests/test262-parser/fail/b1f1e599a3dcf9c5.js:1:10
|
1 | let [...a,] = 0

View File

@ -1,4 +1,4 @@
error: Expected ,, got ...
error: Expected ',', got '...'
--> $DIR/tests/test262-parser/fail/b996783239113de2.js:1:4
|
1 | (a ...b) => 0

View File

@ -1,4 +1,4 @@
error: Expected {, got numeric literal
error: Expected '{', got 'numeric literal'
--> $DIR/tests/test262-parser/fail/c0d2e6ca2ab7f753.js:1:20
|
1 | x = { get method() 42 }

View File

@ -1,4 +1,4 @@
error: Expected ,, got .
error: Expected ',', got '.'
--> $DIR/tests/test262-parser/fail/c1a24e62e6724892.js:1:11
|
1 | ({*a({e: a.b}){}})

View File

@ -1,4 +1,4 @@
error: Expected ;, got )
error: Expected ';', got ')'
--> $DIR/tests/test262-parser/fail/c75e9f5ea55611f3.js:1:23
|
1 | for(let x=1 in [1,2,3]) 0

View File

@ -1,4 +1,4 @@
error: Expected ,, got =>
error: Expected ',', got '=>'
--> $DIR/tests/test262-parser/fail/ca3dd7ea0b4626dd.js:1:23
|
1 | console.log(typeof () => {});

View File

@ -1,4 +1,4 @@
error: Expected (, got :
error: Expected '(', got ':'
--> $DIR/tests/test262-parser/fail/cee6d8878d1e6589.js:1:6
|
1 | ({ *a: 0 })

View File

@ -1,4 +1,4 @@
error: Expected (, got }
error: Expected '(', got '}'
--> $DIR/tests/test262-parser/fail/ceeb9c1fda84ac72.js:1:13
|
1 | ({a:function} = 0)

View File

@ -1,4 +1,4 @@
error: Expected ), got <eof>
error: Expected ')', got '<eof>'
--> $DIR/tests/test262-parser/fail/d4e259842c4520e1.js:1:5
|
1 | 1 + (

View File

@ -1,4 +1,4 @@
error: Expected ,, got {
error: Expected ',', got '{'
--> $DIR/tests/test262-parser/fail/d5f84d71afd585da.js:1:17
|
1 | ({ set: s(a, b) { } })

View File

@ -1,4 +1,4 @@
error: Expected from, got <eof>
error: Expected 'from', got '<eof>'
--> $DIR/tests/test262-parser/fail/d942f64886578d87.module.js:1:1
|
1 | import

View File

@ -1,4 +1,4 @@
error: Expected {, got <eof>
error: Expected '{', got '<eof>'
--> $DIR/tests/test262-parser/fail/da4d9520d81ec621.js:1:7
|
1 | class A

View File

@ -1,4 +1,4 @@
error: Expected (, got .
error: Expected '(', got '.'
--> $DIR/tests/test262-parser/fail/dcdc6f42d4d981d6.js:1:3
|
1 | if.a;

View File

@ -1,4 +1,4 @@
error: Expected ;, got )
error: Expected ';', got ')'
--> $DIR/tests/test262-parser/fail/e01265e2211e48b3.js:1:19
|
1 | for(let a = 0 in b);

View File

@ -1,4 +1,4 @@
error: Expected {, got numeric literal
error: Expected '{', got 'numeric literal'
--> $DIR/tests/test262-parser/fail/e82474d52b99f1b7.js:1:14
|
1 | function a() 1 // expression closure is not supported

View File

@ -1,4 +1,4 @@
error: Expected (, got <eof>
error: Expected '(', got '<eof>'
--> $DIR/tests/test262-parser/fail/e922f1c6f6e5fdef.module.js:1:16
|
1 | export default function

View File

@ -1,4 +1,4 @@
error: Expected (, got `
error: Expected '(', got '`'
--> $DIR/tests/test262-parser/fail/e98777d7b4c239a8.js:1:8
|
1 | switch `test`

View File

@ -1,4 +1,4 @@
error: Expected ], got ,
error: Expected ']', got ','
--> $DIR/tests/test262-parser/fail/ea9e5f218423bd4f.js:1:5
|
1 | ({[1,2]:3})

View File

@ -1,4 +1,4 @@
error: Expected from, got /
error: Expected 'from', got '/'
--> $DIR/tests/test262-parser/fail/ec34d41ac5849c84.module.js:1:8
|
1 | import / as a from 'a'

View File

@ -1,4 +1,4 @@
error: Expected ], got ,
error: Expected ']', got ','
--> $DIR/tests/test262-parser/fail/ed0332c84e50834c.js:1:11
|
1 | (class {[a,b](){}})

View File

@ -1,4 +1,4 @@
error: Expected ,, got as
error: Expected ',', got 'as'
--> $DIR/tests/test262-parser/fail/ee9b2096b4e379a6.module.js:1:10
|
1 | import a as b from 'a'

View File

@ -1,4 +1,4 @@
error: Expected {, got <eof>
error: Expected '{', got '<eof>'
--> $DIR/tests/test262-parser/fail/f0ff9ff5dab859aa.js:1:22
|
1 | function f(a, ...b, c)

View File

@ -1,4 +1,4 @@
error: Expected {, got }
error: Expected '{', got '}'
--> $DIR/tests/test262-parser/fail/fa6a6d04f835e4c2.js:1:44
|
1 | class A { constructor() {} 'constructor'() }

View File

@ -1,4 +1,4 @@
error: Expected {, got v
error: Expected '{', got 'v'
--> $DIR/tests/test262-parser/fail/fc75a023944da475.js:1:23
|
1 | x = { set method(val) v = val }

View File

@ -1,4 +1,4 @@
error: Expected ,, got v
error: Expected ',', got 'v'
--> $DIR/tests/typescript-errors/class/override-parameter-property/input.ts:3:33
|
3 | constructor(readonly override v: boolean)

View File

@ -1,4 +1,4 @@
error: Expected ,, got Some(c)
error: Expected ',', got 'Some(c)'
--> $DIR/tests/typescript-errors/custom/const-enum-2/input.ts:4:5
|
4 | c

View File

@ -1,4 +1,4 @@
error: Expected ,, got Some(g)
error: Expected ',', got 'Some(g)'
--> $DIR/tests/typescript-errors/custom/const-enum/input.ts:4:5
|
4 | g = CONST

View File

@ -1,4 +1,4 @@
error: Expected (, got class
error: Expected '(', got 'class'
--> $DIR/tests/typescript-errors/decorators/type-arguments-invalid/input.ts:2:1
|
2 | class Test {}

View File

@ -1,4 +1,4 @@
error: Expected ,, got b
error: Expected ',', got 'b'
--> $DIR/tests/typescript-errors/multiple-specifiers/export/input.ts:1:12
|
1 | export { a b c }

View File

@ -1,4 +1,4 @@
error: Expected ,, got b
error: Expected ',', got 'b'
--> $DIR/tests/typescript-errors/multiple-specifiers/import/input.ts:1:12
|
1 | import { a b c } from 'mod'

View File

@ -1,4 +1,4 @@
error: Expected from, got =
error: Expected 'from', got '='
--> $DIR/tests/typescript-errors/type-only-import-equals-decl/named/input.ts:1:24
|
1 | import type { MyType } = require('commonjs-package')

View File

@ -1,4 +1,4 @@
error: Expected from, got =
error: Expected 'from', got '='
--> $DIR/tests/typescript-errors/type-only-import-equals-decl/namespace/input.ts:1:25
|
1 | import type * as MyType = require('commonjs-package')