eslint-parser: ES2020 features (#11815)

* chore: update espree test on nullish coalescing

* feat: add optional chaining support

* fix: adapt to estree AST shape

* chore: update lockfile

* add estree optional-chaining test fixtures

* address review comments

* chore: simplify smoke test

* export * support

Co-authored-by: Brian Ng <bng412@gmail.com>
This commit is contained in:
Huáng Jùnliàng
2020-07-29 16:46:29 -04:00
committed by GitHub
parent 059e9124ff
commit d7347fb8bd
36 changed files with 690 additions and 60 deletions

View File

@@ -388,8 +388,6 @@ export default (superClass: Class<Parser>): Class<Parser> =>
delete node.arguments;
// $FlowIgnore - callee isn't optional in the type definition
delete node.callee;
} else if (node.type === "CallExpression") {
(node: N.Node).optional = false;
}
return node;
@@ -431,10 +429,38 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return node;
}
parseSubscript(...args) {
const node = super.parseSubscript(...args);
parseSubscript(
base: N.Expression,
startPos: number,
startLoc: Position,
noCalls: ?boolean,
state: N.ParseSubscriptState,
) {
const node = super.parseSubscript(
base,
startPos,
startLoc,
noCalls,
state,
);
if (node.type === "MemberExpression") {
if (state.optionalChainMember) {
// https://github.com/estree/estree/blob/master/es2020.md#chainexpression
if (
node.type === "OptionalMemberExpression" ||
node.type === "OptionalCallExpression"
) {
node.type = node.type.substring(8); // strip Optional prefix
}
if (state.stop) {
const chain = this.startNodeAtNode(node);
chain.expression = node;
return this.finishNode(chain, "ChainExpression");
}
} else if (
node.type === "MemberExpression" ||
node.type === "CallExpression"
) {
node.optional = false;
}