diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 48dd82d673..e1ea1aa55d 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -923,7 +923,19 @@ export default class ExpressionParser extends LValParser { if (isPrivate) { this.expectOnePlugin(["classPrivateProperties", "classPrivateMethods"]); const node = this.startNode(); + const columnHashEnd = this.state.end; this.next(); + const columnIdentifierStart = this.state.start; + + const spacesBetweenHashAndIdentifier = + columnIdentifierStart - columnHashEnd; + if (spacesBetweenHashAndIdentifier != 0) { + this.raise( + columnIdentifierStart, + "Unexpected space between # and identifier", + ); + } + node.id = this.parseIdentifier(true); return this.finishNode(node, "PrivateName"); } else { diff --git a/packages/babel-parser/test/fixtures/experimental/class-private-methods/failure-spaces/input.js b/packages/babel-parser/test/fixtures/experimental/class-private-methods/failure-spaces/input.js new file mode 100644 index 0000000000..b49e1327f4 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/class-private-methods/failure-spaces/input.js @@ -0,0 +1,5 @@ +class Spaces { + # wrongSpaces() { + return fail(); + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/class-private-methods/failure-spaces/options.json b/packages/babel-parser/test/fixtures/experimental/class-private-methods/failure-spaces/options.json new file mode 100644 index 0000000000..f8dba0c322 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/class-private-methods/failure-spaces/options.json @@ -0,0 +1,4 @@ +{ + "throws": "Unexpected space between # and identifier (2:5)", + "plugins": ["classPrivateMethods"] +} diff --git a/packages/babel-parser/test/fixtures/experimental/class-private-properties/failure-spaces/input.js b/packages/babel-parser/test/fixtures/experimental/class-private-properties/failure-spaces/input.js new file mode 100644 index 0000000000..023684c53e --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/class-private-properties/failure-spaces/input.js @@ -0,0 +1,3 @@ +class Spaces { + # wrongSpaces; +} diff --git a/packages/babel-parser/test/fixtures/experimental/class-private-properties/failure-spaces/options.json b/packages/babel-parser/test/fixtures/experimental/class-private-properties/failure-spaces/options.json new file mode 100644 index 0000000000..f8dba0c322 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/class-private-properties/failure-spaces/options.json @@ -0,0 +1,4 @@ +{ + "throws": "Unexpected space between # and identifier (2:5)", + "plugins": ["classPrivateMethods"] +}