Skip to content

Commit

Permalink
Fix %== parsing in hack pipes (#13536)
Browse files Browse the repository at this point in the history
* Fix `%==` parsing in hack pipes

* Use custom token type

* Update packages/babel-parser/src/parser/expression.js

Co-authored-by: Hu谩ng J霉nli脿ng <jlhwung@gmail.com>

* Apply suggestions from code review

Co-authored-by: J.聽S.聽Choi <jschoi@jschoi.org>

Co-authored-by: Hu谩ng J霉nli脿ng <jlhwung@gmail.com>
Co-authored-by: J.聽S.聽Choi <jschoi@jschoi.org>
  • Loading branch information
3 people committed Aug 3, 2021
1 parent 35e4e1f commit ff287ac
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
20 changes: 20 additions & 0 deletions packages/babel-parser/src/parser/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,26 @@ export default class ExpressionParser extends LValParser {
return node;
}

case tt.moduloAssign:
if (
this.getPluginOption("pipelineOperator", "proposal") === "hack" &&
this.getPluginOption("pipelineOperator", "topicToken") === "%"
) {
// If we find %= in an expression position, and the Hack-pipes proposal is active,
// then the % could be the topic token (e.g., in x |> %==y or x |> %===y), and so we
// reparse it as %.
// The next readToken() call will start parsing from =.

this.state.value = "%";
this.state.type = tt.modulo;
this.state.pos--;
this.state.end--;
this.state.endLoc.column--;
} else {
throw this.unexpected();
}

// falls through
case tt.modulo:
case tt.hash: {
const pipeProposal = this.getPluginOption(
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-parser/src/tokenizer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ export default class Tokenizer extends ParserErrors {

if (next === charCodes.equalsTo && !this.state.inType) {
width++;
type = tt.assign;
type = code === charCodes.percentSign ? tt.moduloAssign : tt.assign;
}

this.finishOp(type, width);
Expand Down
3 changes: 3 additions & 0 deletions packages/babel-parser/src/tokenizer/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ export const types: { [name: string]: TokenType } = {
eq: new TokenType("=", { beforeExpr, isAssign }),
assign: new TokenType("_=", { beforeExpr, isAssign }),
slashAssign: new TokenType("_=", { beforeExpr, isAssign }),
// This is only needed to support % as a Hack-pipe topic token. If the proposal
// ends up choosing a different token, it can be merged with tt.assign.
moduloAssign: new TokenType("_=", { beforeExpr, isAssign }),
incDec: new TokenType("++/--", { prefix, postfix, startsExpr }),
bang: new TokenType("!", { beforeExpr, prefix, startsExpr }),
tilde: new TokenType("~", { beforeExpr, prefix, startsExpr }),
Expand Down

0 comments on commit ff287ac

Please sign in to comment.