diff --git a/packages/babel-parser/src/tokenizer/index.js b/packages/babel-parser/src/tokenizer/index.js index 1eefa40ade01..96ff0c21297a 100644 --- a/packages/babel-parser/src/tokenizer/index.js +++ b/packages/babel-parser/src/tokenizer/index.js @@ -113,6 +113,7 @@ export default class Tokenizer extends LocationParser { // parser/util.js /*:: +unexpected: (pos?: ?number, messageOrType?: string | TokenType) => empty; + +expectPlugin: (name: string, pos?: ?number) => true; */ isLookahead: boolean; @@ -405,10 +406,14 @@ export default class Tokenizer extends LocationParser { } if ( - this.hasPlugin("recordAndTuple") && - (next === charCodes.leftCurlyBrace || - next === charCodes.leftSquareBracket) + next === charCodes.leftCurlyBrace || + (next === charCodes.leftSquareBracket && this.hasPlugin("recordAndTuple")) ) { + // When we see `#{`, it is likely to be a hash record. + // However we don't yell at `#[` since users may intend to use "computed private fields", + // which is not allowed in the spec. Throwing expecting recordAndTuple is + // misleading + this.expectPlugin("recordAndTuple"); if (this.getPluginOption("recordAndTuple", "syntaxType") !== "hash") { throw this.raise( this.state.pos, @@ -426,14 +431,8 @@ export default class Tokenizer extends LocationParser { this.finishToken(tt.bracketHashL); } this.state.pos += 2; - } else if ( - this.hasPlugin("classPrivateProperties") || - this.hasPlugin("classPrivateMethods") || - this.getPluginOption("pipelineOperator", "proposal") === "smart" - ) { - this.finishOp(tt.hash, 1); } else { - throw this.raise(this.state.pos, Errors.InvalidOrUnexpectedToken, "#"); + this.finishOp(tt.hash, 1); } } @@ -1080,8 +1079,13 @@ export default class Tokenizer extends LocationParser { if (val == null) { this.raise(this.state.start + 2, Errors.InvalidDigit, radix); } + const next = this.input.charCodeAt(this.state.pos); - if (this.input.charCodeAt(this.state.pos) === charCodes.lowercaseN) { + if (next === charCodes.underscore) { + this.expectPlugin("numericSeparator", this.state.pos); + } + + if (next === charCodes.lowercaseN) { ++this.state.pos; isBigInt = true; } @@ -1154,6 +1158,10 @@ export default class Tokenizer extends LocationParser { } } + if (next === charCodes.underscore) { + this.expectPlugin("numericSeparator", this.state.pos); + } + if (next === charCodes.lowercaseN) { // disallow floats, legacy octal syntax and non octal decimals // new style octal ("0o") is handled in this.readRadixNumber diff --git a/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0065/options.json b/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0065/options.json index 895412e4e5f7..ff1ac8247e68 100644 --- a/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0065/options.json +++ b/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0065/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected character '#' (1:2)" -} + "throws": "Unexpected token, expected \";\" (1:2)" +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/.bigint/input.js b/packages/babel-parser/test/fixtures/experimental/_no-plugin/.bigint/input.js deleted file mode 100644 index 7a5a058e169c..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/_no-plugin/.bigint/input.js +++ /dev/null @@ -1 +0,0 @@ -1n diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/.class-private-properties/options.json b/packages/babel-parser/test/fixtures/experimental/_no-plugin/.class-private-properties/options.json deleted file mode 100644 index 85d7fee65f66..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/_no-plugin/.class-private-properties/options.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "throws": "This experimental syntax requires enabling the parser plugin: 'classPrivateProperties' (2:3)", - "plugins": [] -} diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/.class-private-properties/input.js b/packages/babel-parser/test/fixtures/experimental/_no-plugin/class-private-properties/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/_no-plugin/.class-private-properties/input.js rename to packages/babel-parser/test/fixtures/experimental/_no-plugin/class-private-properties/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/class-private-properties/options.json b/packages/babel-parser/test/fixtures/experimental/_no-plugin/class-private-properties/options.json new file mode 100644 index 000000000000..6904d075e9f1 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/class-private-properties/options.json @@ -0,0 +1,3 @@ +{ + "throws": "This experimental syntax requires enabling one of the following parser plugin(s): 'classPrivateProperties, classPrivateMethods' (2:2)" +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/numeric-separator-radix/input.js b/packages/babel-parser/test/fixtures/experimental/_no-plugin/numeric-separator-radix/input.js new file mode 100644 index 000000000000..0fa2562844bb --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/numeric-separator-radix/input.js @@ -0,0 +1 @@ +0b1_0 diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/.bigint/options.json b/packages/babel-parser/test/fixtures/experimental/_no-plugin/numeric-separator-radix/options.json similarity index 58% rename from packages/babel-parser/test/fixtures/experimental/_no-plugin/.bigint/options.json rename to packages/babel-parser/test/fixtures/experimental/_no-plugin/numeric-separator-radix/options.json index 8e44fabed2f5..180f7a692d48 100644 --- a/packages/babel-parser/test/fixtures/experimental/_no-plugin/.bigint/options.json +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/numeric-separator-radix/options.json @@ -1,4 +1,3 @@ { - "throws": "This experimental syntax requires enabling the parser plugin: 'bigInt' (1:1)", - "plugins": [] -} + "throws": "This experimental syntax requires enabling the parser plugin: 'numericSeparator' (1:3)" +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/.numeric-separator/input.js b/packages/babel-parser/test/fixtures/experimental/_no-plugin/numeric-separator/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/_no-plugin/.numeric-separator/input.js rename to packages/babel-parser/test/fixtures/experimental/_no-plugin/numeric-separator/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/.numeric-separator/options.json b/packages/babel-parser/test/fixtures/experimental/_no-plugin/numeric-separator/options.json similarity index 53% rename from packages/babel-parser/test/fixtures/experimental/_no-plugin/.numeric-separator/options.json rename to packages/babel-parser/test/fixtures/experimental/_no-plugin/numeric-separator/options.json index 6996ba6f1573..ed83acd92c8c 100644 --- a/packages/babel-parser/test/fixtures/experimental/_no-plugin/.numeric-separator/options.json +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/numeric-separator/options.json @@ -1,4 +1,3 @@ { - "throws": "This experimental syntax requires enabling the parser plugin: 'numericSeparator' (1:17)", - "plugins": [] -} + "throws": "This experimental syntax requires enabling the parser plugin: 'numericSeparator' (1:1)" +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/record-and-tuple/not-enabled-hash/options.json b/packages/babel-parser/test/fixtures/experimental/record-and-tuple/not-enabled-hash/options.json index 52f524480cd0..9565d05f7929 100644 --- a/packages/babel-parser/test/fixtures/experimental/record-and-tuple/not-enabled-hash/options.json +++ b/packages/babel-parser/test/fixtures/experimental/record-and-tuple/not-enabled-hash/options.json @@ -1,4 +1,3 @@ { - "plugins": [], - "throws": "Unexpected character '#' (1:0)" -} + "throws": "This experimental syntax requires enabling the parser plugin: 'recordAndTuple' (1:0)" +} \ No newline at end of file