V8intrinsic syntax plugin (#10148)

* feat: v8intrinsic syntax plugin

Implement V8 Intrinsic Syntax Extension. Here we check the execution branch inside the parseSubscript to make sure the V8IntrinsicIdentifier is immediately followed by a call expression.

* feat: disable combining placeholders and v8intrisic

per https://github.com/babel/babel/issues/10104#issuecomment-506950969

* test: add more error cases

* refactor: parse v8 intrinsic in parseExprAtom

This approach is identical to V8’s implementation. Move the test cases as the behaviour changes.

* fix: plugin-name typo

* test: add yield-expression test case

* feat: require startsExpr on modulo for v8intrinsic

* perf: skip eof and braceR check as they must return false

* Print V8IntrinsicIdentifier

* feat: add v8intrinsic to parser typings

* Add generated type helpers

* fix: incorrect type definition

* fix: allow V8IntrinsicIdentifier to be callee
This commit is contained in:
Huáng Jùnliàng
2019-09-06 11:43:19 -04:00
committed by Nicolò Ribaudo
parent b02e35c19a
commit da0af5fd99
38 changed files with 961 additions and 5 deletions

View File

@@ -2191,7 +2191,7 @@ export default class ExpressionParser extends LValParser {
if (
this.match(tt.semi) ||
(!this.match(tt.star) && !this.state.type.startsExpr) ||
this.canInsertSemicolon()
this.hasPrecedingLineBreak()
) {
node.delegate = false;
node.argument = null;

View File

@@ -69,6 +69,10 @@ export function validatePlugins(plugins: PluginList) {
throw new Error("Cannot combine flow and typescript plugins.");
}
if (hasPlugin(plugins, "placeholders") && hasPlugin(plugins, "v8intrinsic")) {
throw new Error("Cannot combine placeholders and v8intrinsic plugins.");
}
if (
hasPlugin(plugins, "pipelineOperator") &&
!PIPELINE_PROPOSALS.includes(
@@ -89,6 +93,7 @@ import flow from "./plugins/flow";
import jsx from "./plugins/jsx";
import typescript from "./plugins/typescript";
import placeholders from "./plugins/placeholders";
import v8intrinsic from "./plugins/v8intrinsic";
// NOTE: order is important. estree must come first; placeholders must come last.
export const mixinPlugins: { [name: string]: MixinPlugin } = {
@@ -96,6 +101,7 @@ export const mixinPlugins: { [name: string]: MixinPlugin } = {
jsx,
flow,
typescript,
v8intrinsic,
placeholders,
};

View File

@@ -0,0 +1,32 @@
import type Parser from "../parser";
import { types as tt } from "../tokenizer/types";
import * as N from "../types";
export default (superClass: Class<Parser>): Class<Parser> =>
class extends superClass {
parseV8Intrinsic(): N.Expression {
if (this.match(tt.modulo)) {
const v8IntrinsicStart = this.state.start;
// let the `loc` of Identifier starts from `%`
const node = this.startNode();
this.eat(tt.modulo);
if (this.match(tt.name)) {
const name = this.parseIdentifierName(this.state.start);
const identifier = this.createIdentifier(node, name);
identifier.type = "V8IntrinsicIdentifier";
if (this.match(tt.parenL)) {
return identifier;
}
}
this.unexpected(v8IntrinsicStart);
}
}
/* ============================================================ *
* parser/expression.js *
* ============================================================ */
parseExprAtom(): N.Expression {
return this.parseV8Intrinsic() || super.parseExprAtom(...arguments);
}
};

View File

@@ -148,7 +148,8 @@ export const types: { [name: string]: TokenType } = {
relational: createBinop("</>/<=/>=", 8),
bitShift: createBinop("<</>>/>>>", 9),
plusMin: new TokenType("+/-", { beforeExpr, binop: 10, prefix, startsExpr }),
modulo: createBinop("%", 11),
// startsExpr: required by v8intrinsic plugin
modulo: new TokenType("%", { beforeExpr, binop: 11, startsExpr }),
star: createBinop("*", 11),
slash: createBinop("/", 11),
exponent: new TokenType("**", {