From afd0638b741602635adcc244aad6668b5e8a7526 Mon Sep 17 00:00:00 2001 From: James DiGioia Date: Sun, 22 Jul 2018 18:46:31 -0400 Subject: [PATCH] Reuse hash token instead of new primaryTopicToken Set whether we're in a pipeline in order to determine how to parse the hash. The error message changes as a result, since the `hash` never enters the block. --- .../babel-parser/src/parser/expression.js | 41 +++++++++++-------- packages/babel-parser/src/tokenizer/index.js | 2 +- packages/babel-parser/src/tokenizer/state.js | 2 + packages/babel-parser/src/tokenizer/types.js | 1 - .../options.json | 2 +- 5 files changed, 27 insertions(+), 21 deletions(-) diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index c71b952ad1..b04e26bac3 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -298,6 +298,7 @@ export default class ExpressionParser extends LValParser { const op = this.state.type; if (op === tt.pipeline) { + this.state.inPipeline = true; this.checkPipelineAtInfixOperator(left, leftStartPos); } else if (op === tt.nullishCoalescing) { this.expectPlugin("nullishCoalescingOperator"); @@ -931,26 +932,30 @@ export default class ExpressionParser extends LValParser { } } - case tt.primaryTopicReference: { - this.expectPlugin("pipelineOperator"); - node = this.startNode(); + case tt.hash: { + if (this.state.inPipeline) { + this.expectPlugin("pipelineOperator"); + node = this.startNode(); - if ("smart" !== this.getPluginOption("pipelineOperator", "proposal")) { - this.raise( - node.start, - "Primary Topic Reference found but pipelineOperator not passed 'smart' for 'proposal' option.", - ); - } + if ( + "smart" !== this.getPluginOption("pipelineOperator", "proposal") + ) { + this.raise( + node.start, + "Primary Topic Reference found but pipelineOperator not passed 'smart' for 'proposal' option.", + ); + } - this.next(); - if (this.primaryTopicReferenceIsAllowedInCurrentTopicContext()) { - this.registerTopicReference(); - return this.finishNode(node, "PrimaryTopicReference"); - } else { - throw this.raise( - node.start, - `Topic reference was used in a lexical context without topic binding`, - ); + this.next(); + if (this.primaryTopicReferenceIsAllowedInCurrentTopicContext()) { + this.registerTopicReference(); + return this.finishNode(node, "PrimaryTopicReference"); + } else { + throw this.raise( + node.start, + `Topic reference was used in a lexical context without topic binding`, + ); + } } } diff --git a/packages/babel-parser/src/tokenizer/index.js b/packages/babel-parser/src/tokenizer/index.js index 67346a8bce..69a845b525 100644 --- a/packages/babel-parser/src/tokenizer/index.js +++ b/packages/babel-parser/src/tokenizer/index.js @@ -431,7 +431,7 @@ export default class Tokenizer extends LocationParser { )}'`, ); } else { - this.finishOp(tt.primaryTopicReference, 1); + this.finishOp(tt.hash, 1); } } else { this.raise( diff --git a/packages/babel-parser/src/tokenizer/state.js b/packages/babel-parser/src/tokenizer/state.js index 936d3a5dc2..1c71ada99d 100644 --- a/packages/babel-parser/src/tokenizer/state.js +++ b/packages/babel-parser/src/tokenizer/state.js @@ -26,6 +26,7 @@ export default class State { this.maybeInArrowParameters = false; this.inGenerator = false; this.inAsync = false; + this.inPipeline = false; this.inPropertyName = false; this.inType = false; this.inClassProperty = false; @@ -110,6 +111,7 @@ export default class State { inGenerator: boolean; inMethod: boolean | N.MethodKind; inAsync: boolean; + inPipeline: boolean; inType: boolean; noAnonFunctionType: boolean; inPropertyName: boolean; diff --git a/packages/babel-parser/src/tokenizer/types.js b/packages/babel-parser/src/tokenizer/types.js index 1142a47f24..3c401f163f 100644 --- a/packages/babel-parser/src/tokenizer/types.js +++ b/packages/babel-parser/src/tokenizer/types.js @@ -86,7 +86,6 @@ export const types: { [name: string]: TokenType } = { regexp: new TokenType("regexp", { startsExpr }), string: new TokenType("string", { startsExpr }), name: new TokenType("name", { startsExpr }), - primaryTopicReference: new TokenType("#", { startsExpr }), eof: new TokenType("eof"), // Punctuation token types. diff --git a/packages/babel-parser/test/fixtures/experimental/pipeline-operator/proposal-smart-error,-unbound-topic,-simple/options.json b/packages/babel-parser/test/fixtures/experimental/pipeline-operator/proposal-smart-error,-unbound-topic,-simple/options.json index 79a8e0a385..736cde0e10 100644 --- a/packages/babel-parser/test/fixtures/experimental/pipeline-operator/proposal-smart-error,-unbound-topic,-simple/options.json +++ b/packages/babel-parser/test/fixtures/experimental/pipeline-operator/proposal-smart-error,-unbound-topic,-simple/options.json @@ -1,4 +1,4 @@ { "plugins": [["pipelineOperator", { "proposal": "smart" }]], - "throws": "Topic reference was used in a lexical context without topic binding (1:4)" + "throws": "Unexpected token (1:4)" }