diff --git a/.gitignore b/.gitignore index 1052d7d8a..983986903 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,9 @@ /.tern-port /local /bin/_acorn.js -/acorn/dist +/acorn/dist/* /acorn-loose/dist /acorn-walk/dist /yarn.lock +!/acorn/dist/acorn.d.ts +!/acorn/dist/acorn.mjs.d.ts \ No newline at end of file diff --git a/AUTHORS b/AUTHORS index c7a0fb8a0..f73abdf0d 100644 --- a/AUTHORS +++ b/AUTHORS @@ -30,11 +30,13 @@ Fabien LOISON Felix Maier Forbes Lindesay Gilad Peleg +Huáng Jùnliàng impinball Ingvar Stepanyan Jackson Ray Hamilton Jesse McCarthy Jiaxing Wang +Joe Krump Joel Kemp Johannes Herr John-David Dalton @@ -68,6 +70,7 @@ Olivier Thomann Oskar Schöldström Paul Harper Peter Rust +piotr PlNG Praveen N Prayag Verma @@ -80,10 +83,15 @@ Sebastian McKenzie Shahar Soel Sheel Bedi Simen Bekkhus +susiwen +susiwen8 Teddy Katz Timothy Gu +Tim van der Lippe Toru Nagashima +tuesmiddt Victor Homyakov +Vladislav Tupikin Wexpo Lyu zsjforcn 龙腾道 diff --git a/acorn-loose/CHANGELOG.md b/acorn-loose/CHANGELOG.md index 83d191ec1..3c01af63b 100644 --- a/acorn-loose/CHANGELOG.md +++ b/acorn-loose/CHANGELOG.md @@ -1,3 +1,17 @@ +## 7.1.0 (2020-06-11) + +### Bug fixes + +Fix various issues in regexp validation. + +### New features + +Add support for `import.meta`. + +Add support for optional chaining (`?.`) and nullish coalescing (`??`). + +Support `export * as ns from "source"`. + ## 7.0.0 (2019-08-12) ### Breaking changes diff --git a/acorn-loose/LICENSE b/acorn-loose/LICENSE index 2c0632b6a..cc5272c96 100644 --- a/acorn-loose/LICENSE +++ b/acorn-loose/LICENSE @@ -1,3 +1,5 @@ +MIT License + Copyright (C) 2012-2018 by various contributors (see AUTHORS) Permission is hereby granted, free of charge, to any person obtaining a copy diff --git a/acorn-loose/README.md b/acorn-loose/README.md index 3d8f5bac5..4641bd4fd 100644 --- a/acorn-loose/README.md +++ b/acorn-loose/README.md @@ -60,3 +60,7 @@ take the **`LooseParser`** class exported by the module, and call its static `extend` method with one or more plugins to get a customized parser class. The class has a static `parse` method that acts like the top-level `parse` method. + +**isDummy**`(node)` takes a `Node` and returns `true` if it is a dummy node +inserted by the parser. The function performs a simple equality check on the +node's name. diff --git a/acorn-loose/package.json b/acorn-loose/package.json index 4beee085e..3bb01da11 100644 --- a/acorn-loose/package.json +++ b/acorn-loose/package.json @@ -4,7 +4,7 @@ "homepage": "https://github.com/acornjs/acorn", "main": "dist/acorn-loose.js", "module": "dist/acorn-loose.mjs", - "version": "7.0.0", + "version": "7.1.0", "engines": {"node": ">=0.4.0"}, "dependencies": { "acorn": "^7.0.0" diff --git a/acorn-loose/src/expression.js b/acorn-loose/src/expression.js index dbc41bd60..4cfe1100d 100644 --- a/acorn-loose/src/expression.js +++ b/acorn-loose/src/expression.js @@ -102,7 +102,7 @@ lp.parseExprOp = function(left, start, minPrec, noIn, indent, line) { let rightStart = this.storeCurrentPos() node.right = this.parseExprOp(this.parseMaybeUnary(false), rightStart, prec, noIn, indent, line) } - this.finishNode(node, /&&|\|\|/.test(node.operator) ? "LogicalExpression" : "BinaryExpression") + this.finishNode(node, /&&|\|\||\?\?/.test(node.operator) ? "LogicalExpression" : "BinaryExpression") return this.parseExprOp(node, start, minPrec, noIn, indent, line) } } @@ -159,17 +159,23 @@ lp.parseExprSubscripts = function() { } lp.parseSubscripts = function(base, start, noCalls, startIndent, line) { + const optionalSupported = this.options.ecmaVersion >= 11 + let optionalChained = false for (;;) { if (this.curLineStart !== line && this.curIndent <= startIndent && this.tokenStartsLine()) { if (this.tok.type === tt.dot && this.curIndent === startIndent) --startIndent else - return base + break } let maybeAsyncArrow = base.type === "Identifier" && base.name === "async" && !this.canInsertSemicolon() + let optional = optionalSupported && this.eat(tt.questionDot) + if (optional) { + optionalChained = true + } - if (this.eat(tt.dot)) { + if ((optional && this.tok.type !== tt.parenL && this.tok.type !== tt.bracketL && this.tok.type !== tt.backQuote) || this.eat(tt.dot)) { let node = this.startNodeAt(start) node.object = base if (this.curLineStart !== line && this.curIndent <= startIndent && this.tokenStartsLine()) @@ -177,6 +183,9 @@ lp.parseSubscripts = function(base, start, noCalls, startIndent, line) { else node.property = this.parsePropertyAccessor() || this.dummyIdent() node.computed = false + if (optionalSupported) { + node.optional = optional + } base = this.finishNode(node, "MemberExpression") } else if (this.tok.type === tt.bracketL) { this.pushCx() @@ -185,6 +194,9 @@ lp.parseSubscripts = function(base, start, noCalls, startIndent, line) { node.object = base node.property = this.parseExpression() node.computed = true + if (optionalSupported) { + node.optional = optional + } this.popCx() this.expect(tt.bracketR) base = this.finishNode(node, "MemberExpression") @@ -195,6 +207,9 @@ lp.parseSubscripts = function(base, start, noCalls, startIndent, line) { let node = this.startNodeAt(start) node.callee = base node.arguments = exprList + if (optionalSupported) { + node.optional = optional + } base = this.finishNode(node, "CallExpression") } else if (this.tok.type === tt.backQuote) { let node = this.startNodeAt(start) @@ -202,9 +217,16 @@ lp.parseSubscripts = function(base, start, noCalls, startIndent, line) { node.quasi = this.parseTemplate() base = this.finishNode(node, "TaggedTemplateExpression") } else { - return base + break } } + + if (optionalChained) { + const chainNode = this.startNodeAt(start) + chainNode.expression = base + base = this.finishNode(chainNode, "ChainExpression") + } + return base } lp.parseExprAtom = function() { @@ -244,7 +266,7 @@ lp.parseExprAtom = function() { node = this.startNode() node.value = this.tok.value node.raw = this.input.slice(this.tok.start, this.tok.end) - if (this.tok.type === tt.num && node.raw.charCodeAt(node.raw.length - 1) === 110) node.bigint = node.raw.slice(0, -1) + if (this.tok.type === tt.num && node.raw.charCodeAt(node.raw.length - 1) === 110) node.bigint = node.raw.slice(0, -1).replace(/_/g, "") this.next() return this.finishNode(node, "Literal") @@ -310,10 +332,13 @@ lp.parseExprAtom = function() { lp.parseExprImport = function() { const node = this.startNode() - this.next() // skip `import` + const meta = this.parseIdent(true) switch (this.tok.type) { case tt.parenL: return this.parseDynamicImport(node) + case tt.dot: + node.meta = meta + return this.parseImportMeta(node) default: node.name = "import" return this.finishNode(node, "Identifier") @@ -325,6 +350,12 @@ lp.parseDynamicImport = function(node) { return this.finishNode(node, "ImportExpression") } +lp.parseImportMeta = function(node) { + this.next() // skip '.' + node.property = this.parseIdent(true) + return this.finishNode(node, "MetaProperty") +} + lp.parseNew = function() { let node = this.startNode(), startIndent = this.curIndent, line = this.curLineStart let meta = this.parseIdent(true) diff --git a/acorn-loose/src/index.js b/acorn-loose/src/index.js index 903781c5f..cfa0b497b 100644 --- a/acorn-loose/src/index.js +++ b/acorn-loose/src/index.js @@ -36,6 +36,7 @@ import "./statement" import "./expression" export {LooseParser} from "./state" +export {isDummy} from "./parseutil" defaultOptions.tabSize = 4 diff --git a/acorn-loose/src/parseutil.js b/acorn-loose/src/parseutil.js index 4501e2d46..534efd4b0 100644 --- a/acorn-loose/src/parseutil.js +++ b/acorn-loose/src/parseutil.js @@ -1 +1,3 @@ -export function isDummy(node) { return node.name === "✖" } +export const dummyValue = "✖" + +export function isDummy(node) { return node.name === dummyValue } diff --git a/acorn-loose/src/state.js b/acorn-loose/src/state.js index 52887231d..322306fe4 100644 --- a/acorn-loose/src/state.js +++ b/acorn-loose/src/state.js @@ -1,4 +1,5 @@ import {Parser, SourceLocation, tokTypes as tt, Node, lineBreak, isNewLine} from "acorn" +import {dummyValue} from "./parseutil" function noop() {} @@ -63,13 +64,13 @@ export class LooseParser { dummyIdent() { let dummy = this.dummyNode("Identifier") - dummy.name = "✖" + dummy.name = dummyValue return dummy } dummyString() { let dummy = this.dummyNode("Literal") - dummy.value = dummy.raw = "✖" + dummy.value = dummy.raw = dummyValue return dummy } diff --git a/acorn-loose/src/statement.js b/acorn-loose/src/statement.js index a5b574044..355d278f2 100644 --- a/acorn-loose/src/statement.js +++ b/acorn-loose/src/statement.js @@ -176,10 +176,13 @@ lp.parseStatement = function() { return this.parseClass(true) case tt._import: - if (this.options.ecmaVersion > 10 && this.lookAhead(1).type === tt.parenL) { - node.expression = this.parseExpression() - this.semicolon() - return this.finishNode(node, "ExpressionStatement") + if (this.options.ecmaVersion > 10) { + const nextType = this.lookAhead(1).type + if (nextType === tt.parenL || nextType === tt.dot) { + node.expression = this.parseExpression() + this.semicolon() + return this.finishNode(node, "ExpressionStatement") + } } return this.parseImport() @@ -358,6 +361,13 @@ lp.parseExport = function() { let node = this.startNode() this.next() if (this.eat(tt.star)) { + if (this.options.ecmaVersion >= 11) { + if (this.eatContextual("as")) { + node.exported = this.parseExprAtom() + } else { + node.exported = null + } + } node.source = this.eatContextual("from") ? this.parseExprAtom() : this.dummyString() return this.finishNode(node, "ExportAllDeclaration") } diff --git a/acorn-loose/src/tokenize.js b/acorn-loose/src/tokenize.js index e838b48d8..da20b7daf 100644 --- a/acorn-loose/src/tokenize.js +++ b/acorn-loose/src/tokenize.js @@ -1,5 +1,6 @@ import {tokTypes as tt, Token, isNewLine, SourceLocation, getLineInfo, lineBreakG} from "acorn" import {LooseParser} from "./state" +import {dummyValue} from "./parseutil" const lp = LooseParser.prototype @@ -73,7 +74,7 @@ lp.readToken = function() { throw e } this.resetTo(pos) - if (replace === true) replace = {start: pos, end: pos, type: tt.name, value: "✖"} + if (replace === true) replace = {start: pos, end: pos, type: tt.name, value: dummyValue} if (replace) { if (this.options.locations) replace.loc = new SourceLocation( diff --git a/acorn-walk/CHANGELOG.md b/acorn-walk/CHANGELOG.md index 82d439902..c6c2e058a 100644 --- a/acorn-walk/CHANGELOG.md +++ b/acorn-walk/CHANGELOG.md @@ -1,3 +1,25 @@ +## 7.2.0 (2020-06-17) + +### New features + +Support optional chaining and nullish coalescing. + +Support `import.meta`. + +Add support for `export * as ns from "source"`. + +## 7.1.1 (2020-02-13) + +### Bug fixes + +Clean up the type definitions to actually work well with the main parser. + +## 7.1.0 (2020-02-11) + +### New features + +Add a TypeScript definition file for the library. + ## 7.0.0 (2017-08-12) ### New features diff --git a/acorn-walk/LICENSE b/acorn-walk/LICENSE index 2c0632b6a..cc5272c96 100644 --- a/acorn-walk/LICENSE +++ b/acorn-walk/LICENSE @@ -1,3 +1,5 @@ +MIT License + Copyright (C) 2012-2018 by various contributors (see AUTHORS) Permission is hereby granted, free of charge, to any person obtaining a copy diff --git a/acorn-walk/dist/walk.d.ts b/acorn-walk/dist/walk.d.ts new file mode 100644 index 000000000..00cc005f1 --- /dev/null +++ b/acorn-walk/dist/walk.d.ts @@ -0,0 +1,112 @@ +import {Node} from 'acorn'; + +declare module "acorn-walk" { + type FullWalkerCallback = ( + node: Node, + state: TState, + type: string + ) => void; + + type FullAncestorWalkerCallback = ( + node: Node, + state: TState | Node[], + ancestors: Node[], + type: string + ) => void; + type WalkerCallback = (node: Node, state: TState) => void; + + type SimpleWalkerFn = ( + node: Node, + state: TState + ) => void; + + type AncestorWalkerFn = ( + node: Node, + state: TState| Node[], + ancestors: Node[] + ) => void; + + type RecursiveWalkerFn = ( + node: Node, + state: TState, + callback: WalkerCallback + ) => void; + + type SimpleVisitors = { + [type: string]: SimpleWalkerFn + }; + + type AncestorVisitors = { + [type: string]: AncestorWalkerFn + }; + + type RecursiveVisitors = { + [type: string]: RecursiveWalkerFn + }; + + type FindPredicate = (type: string, node: Node) => boolean; + + interface Found { + node: Node, + state: TState + } + + export function simple( + node: Node, + visitors: SimpleVisitors, + base?: RecursiveVisitors, + state?: TState + ): void; + + export function ancestor( + node: Node, + visitors: AncestorVisitors, + base?: RecursiveVisitors, + state?: TState + ): void; + + export function recursive( + node: Node, + state: TState, + functions: RecursiveVisitors, + base?: RecursiveVisitors + ): void; + + export function full( + node: Node, + callback: FullWalkerCallback, + base?: RecursiveVisitors, + state?: TState + ): void; + + export function fullAncestor( + node: Node, + callback: FullAncestorWalkerCallback, + base?: RecursiveVisitors, + state?: TState + ): void; + + export function make( + functions: RecursiveVisitors, + base?: RecursiveVisitors + ): RecursiveVisitors; + + export function findNodeAt( + node: Node, + start: number | undefined, + end?: number | undefined, + type?: FindPredicate | string, + base?: RecursiveVisitors, + state?: TState + ): Found | undefined; + + export function findNodeAround( + node: Node, + start: number | undefined, + type?: FindPredicate | string, + base?: RecursiveVisitors, + state?: TState + ): Found | undefined; + + export const findNodeAfter: typeof findNodeAround; +} diff --git a/acorn-walk/package.json b/acorn-walk/package.json index 375c21fc8..ae8da3cf3 100644 --- a/acorn-walk/package.json +++ b/acorn-walk/package.json @@ -3,8 +3,9 @@ "description": "ECMAScript (ESTree) AST walker", "homepage": "https://github.com/acornjs/acorn", "main": "dist/walk.js", + "types": "dist/walk.d.ts", "module": "dist/walk.mjs", - "version": "7.0.0", + "version": "7.2.0", "engines": {"node": ">=0.4.0"}, "maintainers": [ { diff --git a/acorn-walk/src/index.js b/acorn-walk/src/index.js index d95af12be..2a044b7df 100644 --- a/acorn-walk/src/index.js +++ b/acorn-walk/src/index.js @@ -192,7 +192,7 @@ base.Program = base.BlockStatement = (node, st, c) => { } base.Statement = skipThrough base.EmptyStatement = ignore -base.ExpressionStatement = base.ParenthesizedExpression = +base.ExpressionStatement = base.ParenthesizedExpression = base.ChainExpression = (node, st, c) => c(node.expression, st, "Expression") base.IfStatement = (node, st, c) => { c(node.test, st, "Expression") @@ -353,6 +353,8 @@ base.ExportNamedDeclaration = base.ExportDefaultDeclaration = (node, st, c) => { if (node.source) c(node.source, st, "Expression") } base.ExportAllDeclaration = (node, st, c) => { + if (node.exported) + c(node.exported, st) c(node.source, st, "Expression") } base.ImportDeclaration = (node, st, c) => { diff --git a/acorn/CHANGELOG.md b/acorn/CHANGELOG.md index e893c221a..164fd27c6 100644 --- a/acorn/CHANGELOG.md +++ b/acorn/CHANGELOG.md @@ -1,3 +1,61 @@ +## 7.4.0 (2020-08-03) + +### New features + +Add support for logical assignment operators. + +Add support for numeric separators. + +## 7.3.1 (2020-06-11) + +### Bug fixes + +Make the string in the `version` export match the actual library version. + +## 7.3.0 (2020-06-11) + +### Bug fixes + +Fix a bug that caused parsing of object patterns with a property named `set` that had a default value to fail. + +### New features + +Add support for optional chaining (`?.`). + +## 7.2.0 (2020-05-09) + +### Bug fixes + +Fix precedence issue in parsing of async arrow functions. + +### New features + +Add support for nullish coalescing. + +Add support for `import.meta`. + +Support `export * as ...` syntax. + +Upgrade to Unicode 13. + +## 6.4.1 (2020-03-09) + +### Bug fixes + +More carefully check for valid UTF16 surrogate pairs in regexp validator. + +## 7.1.1 (2020-03-01) + +### Bug fixes + +Treat `\8` and `\9` as invalid escapes in template strings. + +Allow unicode escapes in property names that are keywords. + +Don't error on an exponential operator expression as argument to `await`. + +More carefully check for valid UTF16 surrogate pairs in regexp validator. + ## 7.1.0 (2019-09-24) ### Bug fixes diff --git a/acorn/LICENSE b/acorn/LICENSE index 2c0632b6a..cc5272c96 100644 --- a/acorn/LICENSE +++ b/acorn/LICENSE @@ -1,3 +1,5 @@ +MIT License + Copyright (C) 2012-2018 by various contributors (see AUTHORS) Permission is hereby granted, free of charge, to any person obtaining a copy diff --git a/acorn/README.md b/acorn/README.md index 585f2736f..52d2e9b71 100644 --- a/acorn/README.md +++ b/acorn/README.md @@ -266,5 +266,4 @@ Plugins for ECMAScript proposals: - [`acorn-stage3`](https://github.com/acornjs/acorn-stage3): Parse most stage 3 proposals, bundling: - [`acorn-class-fields`](https://github.com/acornjs/acorn-class-fields): Parse [class fields proposal](https://github.com/tc39/proposal-class-fields) - [`acorn-import-meta`](https://github.com/acornjs/acorn-import-meta): Parse [import.meta proposal](https://github.com/tc39/proposal-import-meta) - - [`acorn-numeric-separator`](https://github.com/acornjs/acorn-numeric-separator): Parse [numeric separator proposal](https://github.com/tc39/proposal-numeric-separator) - [`acorn-private-methods`](https://github.com/acornjs/acorn-private-methods): parse [private methods, getters and setters proposal](https://github.com/tc39/proposal-private-methods)n diff --git a/acorn/dist/acorn.mjs.d.ts b/acorn/dist/acorn.mjs.d.ts new file mode 100644 index 000000000..5f633595e --- /dev/null +++ b/acorn/dist/acorn.mjs.d.ts @@ -0,0 +1,2 @@ +import * as acorn from "./acorn"; +export = acorn; \ No newline at end of file diff --git a/acorn/package.json b/acorn/package.json index b9ff2b366..25bfa862b 100644 --- a/acorn/package.json +++ b/acorn/package.json @@ -3,8 +3,9 @@ "description": "ECMAScript parser", "homepage": "https://github.com/acornjs/acorn", "main": "dist/acorn.js", + "types": "dist/acorn.d.ts", "module": "dist/acorn.mjs", - "version": "7.1.0", + "version": "7.4.0", "engines": {"node": ">=0.4.0"}, "maintainers": [ { diff --git a/acorn/src/expression.js b/acorn/src/expression.js index de4ebd0c9..d71701374 100644 --- a/acorn/src/expression.js +++ b/acorn/src/expression.js @@ -44,9 +44,11 @@ pp.checkPropClash = function(prop, propHash, refDestructuringErrors) { if (this.options.ecmaVersion >= 6) { if (name === "__proto__" && kind === "init") { if (propHash.proto) { - if (refDestructuringErrors && refDestructuringErrors.doubleProto < 0) refDestructuringErrors.doubleProto = key.start - // Backwards-compat kludge. Can be removed in version 6.0 - else this.raiseRecoverable(key.start, "Redefinition of __proto__ property") + if (refDestructuringErrors) { + if (refDestructuringErrors.doubleProto < 0) + refDestructuringErrors.doubleProto = key.start + // Backwards-compat kludge. Can be removed in version 6.0 + } else this.raiseRecoverable(key.start, "Redefinition of __proto__ property") } propHash.proto = true } @@ -111,12 +113,11 @@ pp.parseMaybeAssign = function(noIn, refDestructuringErrors, afterLeftParse) { else this.exprAllowed = false } - let ownDestructuringErrors = false, oldParenAssign = -1, oldTrailingComma = -1, oldShorthandAssign = -1 + let ownDestructuringErrors = false, oldParenAssign = -1, oldTrailingComma = -1 if (refDestructuringErrors) { oldParenAssign = refDestructuringErrors.parenthesizedAssign oldTrailingComma = refDestructuringErrors.trailingComma - oldShorthandAssign = refDestructuringErrors.shorthandAssign - refDestructuringErrors.parenthesizedAssign = refDestructuringErrors.trailingComma = refDestructuringErrors.shorthandAssign = -1 + refDestructuringErrors.parenthesizedAssign = refDestructuringErrors.trailingComma = -1 } else { refDestructuringErrors = new DestructuringErrors ownDestructuringErrors = true @@ -131,8 +132,11 @@ pp.parseMaybeAssign = function(noIn, refDestructuringErrors, afterLeftParse) { let node = this.startNodeAt(startPos, startLoc) node.operator = this.value node.left = this.type === tt.eq ? this.toAssignable(left, false, refDestructuringErrors) : left - if (!ownDestructuringErrors) DestructuringErrors.call(refDestructuringErrors) - refDestructuringErrors.shorthandAssign = -1 // reset because shorthand default was used correctly + if (!ownDestructuringErrors) { + refDestructuringErrors.parenthesizedAssign = refDestructuringErrors.trailingComma = refDestructuringErrors.doubleProto = -1 + } + if (refDestructuringErrors.shorthandAssign >= node.left.start) + refDestructuringErrors.shorthandAssign = -1 // reset because shorthand default was used correctly this.checkLVal(left) this.next() node.right = this.parseMaybeAssign(noIn) @@ -142,7 +146,6 @@ pp.parseMaybeAssign = function(noIn, refDestructuringErrors, afterLeftParse) { } if (oldParenAssign > -1) refDestructuringErrors.parenthesizedAssign = oldParenAssign if (oldTrailingComma > -1) refDestructuringErrors.trailingComma = oldTrailingComma - if (oldShorthandAssign > -1) refDestructuringErrors.shorthandAssign = oldShorthandAssign return left } @@ -183,11 +186,20 @@ pp.parseExprOp = function(left, leftStartPos, leftStartLoc, minPrec, noIn) { if (prec != null && (!noIn || this.type !== tt._in)) { if (prec > minPrec) { let logical = this.type === tt.logicalOR || this.type === tt.logicalAND + let coalesce = this.type === tt.coalesce + if (coalesce) { + // Handle the precedence of `tt.coalesce` as equal to the range of logical expressions. + // In other words, `node.right` shouldn't contain logical expressions in order to check the mixed error. + prec = tt.logicalAND.binop + } let op = this.value this.next() let startPos = this.start, startLoc = this.startLoc let right = this.parseExprOp(this.parseMaybeUnary(null, false), startPos, startLoc, prec, noIn) - let node = this.buildBinary(leftStartPos, leftStartLoc, left, right, op, logical) + let node = this.buildBinary(leftStartPos, leftStartLoc, left, right, op, logical || coalesce) + if ((logical && this.type === tt.coalesce) || (coalesce && (this.type === tt.logicalOR || this.type === tt.logicalAND))) { + this.raiseRecoverable(this.start, "Logical expressions and coalesce expressions cannot be mixed. Wrap either by parentheses") + } return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec, noIn) } } @@ -247,8 +259,8 @@ pp.parseMaybeUnary = function(refDestructuringErrors, sawUnary) { pp.parseExprSubscripts = function(refDestructuringErrors) { let startPos = this.start, startLoc = this.startLoc let expr = this.parseExprAtom(refDestructuringErrors) - let skipArrowSubscripts = expr.type === "ArrowFunctionExpression" && this.input.slice(this.lastTokStart, this.lastTokEnd) !== ")" - if (this.checkExpressionErrors(refDestructuringErrors) || skipArrowSubscripts) return expr + if (expr.type === "ArrowFunctionExpression" && this.input.slice(this.lastTokStart, this.lastTokEnd) !== ")") + return expr let result = this.parseSubscripts(expr, startPos, startLoc) if (refDestructuringErrors && result.type === "MemberExpression") { if (refDestructuringErrors.parenthesizedAssign >= result.start) refDestructuringErrors.parenthesizedAssign = -1 @@ -259,22 +271,42 @@ pp.parseExprSubscripts = function(refDestructuringErrors) { pp.parseSubscripts = function(base, startPos, startLoc, noCalls) { let maybeAsyncArrow = this.options.ecmaVersion >= 8 && base.type === "Identifier" && base.name === "async" && - this.lastTokEnd === base.end && !this.canInsertSemicolon() && this.input.slice(base.start, base.end) === "async" + this.lastTokEnd === base.end && !this.canInsertSemicolon() && base.end - base.start === 5 && + this.potentialArrowAt === base.start + let optionalChained = false + while (true) { - let element = this.parseSubscript(base, startPos, startLoc, noCalls, maybeAsyncArrow) - if (element === base || element.type === "ArrowFunctionExpression") return element + let element = this.parseSubscript(base, startPos, startLoc, noCalls, maybeAsyncArrow, optionalChained) + + if (element.optional) optionalChained = true + if (element === base || element.type === "ArrowFunctionExpression") { + if (optionalChained) { + const chainNode = this.startNodeAt(startPos, startLoc) + chainNode.expression = element + element = this.finishNode(chainNode, "ChainExpression") + } + return element + } + base = element } } -pp.parseSubscript = function(base, startPos, startLoc, noCalls, maybeAsyncArrow) { +pp.parseSubscript = function(base, startPos, startLoc, noCalls, maybeAsyncArrow, optionalChained) { + let optionalSupported = this.options.ecmaVersion >= 11 + let optional = optionalSupported && this.eat(tt.questionDot) + if (noCalls && optional) this.raise(this.lastTokStart, "Optional chaining cannot appear in the callee of new expressions") + let computed = this.eat(tt.bracketL) - if (computed || this.eat(tt.dot)) { + if (computed || (optional && this.type !== tt.parenL && this.type !== tt.backQuote) || this.eat(tt.dot)) { let node = this.startNodeAt(startPos, startLoc) node.object = base node.property = computed ? this.parseExpression() : this.parseIdent(this.options.allowReserved !== "never") node.computed = !!computed if (computed) this.expect(tt.bracketR) + if (optionalSupported) { + node.optional = optional + } base = this.finishNode(node, "MemberExpression") } else if (!noCalls && this.eat(tt.parenL)) { let refDestructuringErrors = new DestructuringErrors, oldYieldPos = this.yieldPos, oldAwaitPos = this.awaitPos, oldAwaitIdentPos = this.awaitIdentPos @@ -282,7 +314,7 @@ pp.parseSubscript = function(base, startPos, startLoc, noCalls, maybeAsyncArrow) this.awaitPos = 0 this.awaitIdentPos = 0 let exprList = this.parseExprList(tt.parenR, this.options.ecmaVersion >= 8, false, refDestructuringErrors) - if (maybeAsyncArrow && !this.canInsertSemicolon() && this.eat(tt.arrow)) { + if (maybeAsyncArrow && !optional && !this.canInsertSemicolon() && this.eat(tt.arrow)) { this.checkPatternErrors(refDestructuringErrors, false) this.checkYieldAwaitInDefaultParams() if (this.awaitIdentPos > 0) @@ -299,8 +331,14 @@ pp.parseSubscript = function(base, startPos, startLoc, noCalls, maybeAsyncArrow) let node = this.startNodeAt(startPos, startLoc) node.callee = base node.arguments = exprList + if (optionalSupported) { + node.optional = optional + } base = this.finishNode(node, "CallExpression") } else if (this.type === tt.backQuote) { + if (optional || optionalChained) { + this.raise(this.start, "Optional chaining cannot appear in the tag of tagged template expressions") + } let node = this.startNodeAt(startPos, startLoc) node.tag = base node.quasi = this.parseTemplate({isTagged: true}) @@ -423,10 +461,18 @@ pp.parseExprAtom = function(refDestructuringErrors) { pp.parseExprImport = function() { const node = this.startNode() - this.next() // skip `import` + + // Consume `import` as an identifier for `import.meta`. + // Because `this.parseIdent(true)` doesn't check escape sequences, it needs the check of `this.containsEsc`. + if (this.containsEsc) this.raiseRecoverable(this.start, "Escape sequence in keyword import") + const meta = this.parseIdent(true) + switch (this.type) { case tt.parenL: return this.parseDynamicImport(node) + case tt.dot: + node.meta = meta + return this.parseImportMeta(node) default: this.unexpected() } @@ -451,11 +497,27 @@ pp.parseDynamicImport = function(node) { return this.finishNode(node, "ImportExpression") } +pp.parseImportMeta = function(node) { + this.next() // skip `.` + + const containsEsc = this.containsEsc + node.property = this.parseIdent(true) + + if (node.property.name !== "meta") + this.raiseRecoverable(node.property.start, "The only valid meta property for import is 'import.meta'") + if (containsEsc) + this.raiseRecoverable(node.start, "'import.meta' must not contain escaped characters") + if (this.options.sourceType !== "module") + this.raiseRecoverable(node.start, "Cannot use 'import.meta' outside a module") + + return this.finishNode(node, "MetaProperty") +} + pp.parseLiteral = function(value) { let node = this.startNode() node.value = value node.raw = this.input.slice(this.start, this.end) - if (node.raw.charCodeAt(node.raw.length - 1) === 110) node.bigint = node.raw.slice(0, -1) + if (node.raw.charCodeAt(node.raw.length - 1) === 110) node.bigint = node.raw.slice(0, -1).replace(/_/g, "") this.next() return this.finishNode(node, "Literal") } @@ -546,16 +608,19 @@ pp.parseParenArrowList = function(startPos, startLoc, exprList) { const empty = [] pp.parseNew = function() { + if (this.containsEsc) this.raiseRecoverable(this.start, "Escape sequence in keyword new") let node = this.startNode() let meta = this.parseIdent(true) if (this.options.ecmaVersion >= 6 && this.eat(tt.dot)) { node.meta = meta let containsEsc = this.containsEsc node.property = this.parseIdent(true) - if (node.property.name !== "target" || containsEsc) - this.raiseRecoverable(node.property.start, "The only valid meta property for new is new.target") + if (node.property.name !== "target") + this.raiseRecoverable(node.property.start, "The only valid meta property for new is 'new.target'") + if (containsEsc) + this.raiseRecoverable(node.start, "'new.target' must not contain escaped characters") if (!this.inNonArrowFunction()) - this.raiseRecoverable(node.start, "new.target can only be used in functions") + this.raiseRecoverable(node.start, "'new.target' can only be used in functions") return this.finishNode(node, "MetaProperty") } let startPos = this.start, startLoc = this.startLoc, isImport = this.type === tt._import @@ -699,7 +764,7 @@ pp.parsePropertyValue = function(prop, isPattern, isGenerator, isAsync, startPos } else if (!isPattern && !containsEsc && this.options.ecmaVersion >= 5 && !prop.computed && prop.key.type === "Identifier" && (prop.key.name === "get" || prop.key.name === "set") && - (this.type !== tt.comma && this.type !== tt.braceR)) { + (this.type !== tt.comma && this.type !== tt.braceR && this.type !== tt.eq)) { if (isGenerator || isAsync) this.unexpected() prop.kind = prop.key.name this.parsePropertyName(prop) @@ -834,16 +899,14 @@ pp.parseFunctionBody = function(node, isArrowFunction, isMethod) { // Add the params to varDeclaredNames to ensure that an error is thrown // if a let/const declaration in the function clashes with one of the params. this.checkParams(node, !oldStrict && !useStrict && !isArrowFunction && !isMethod && this.isSimpleParamList(node.params)) - node.body = this.parseBlock(false) + // Ensure the function name isn't a forbidden identifier in strict mode, e.g. 'eval' + if (this.strict && node.id) this.checkLVal(node.id, BIND_OUTSIDE) + node.body = this.parseBlock(false, undefined, useStrict && !oldStrict) node.expression = false this.adaptDirectivePrologue(node.body.body) this.labels = oldLabels } this.exitScope() - - // Ensure the function name isn't a forbidden identifier in strict mode, e.g. 'eval' - if (this.strict && node.id) this.checkLVal(node.id, BIND_OUTSIDE) - this.strict = oldStrict } pp.isSimpleParamList = function(params) { @@ -929,7 +992,7 @@ pp.parseIdent = function(liberal, isBinding) { } else { this.unexpected() } - this.next() + this.next(!!liberal) this.finishNode(node, "Identifier") if (!liberal) { this.checkUnreserved(node) @@ -961,6 +1024,6 @@ pp.parseAwait = function() { let node = this.startNode() this.next() - node.argument = this.parseMaybeUnary(null, true) + node.argument = this.parseMaybeUnary(null, false) return this.finishNode(node, "AwaitExpression") } diff --git a/acorn/src/identifier.js b/acorn/src/identifier.js index 339066260..0d7c3d136 100644 --- a/acorn/src/identifier.js +++ b/acorn/src/identifier.js @@ -27,8 +27,8 @@ export const keywordRelationalOperator = /^in(stanceof)?$/ // are only applied when a character is found to actually have a // code point above 128. // Generated by `bin/generate-identifier-regex.js`. -let nonASCIIidentifierStartChars = "\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u037f\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u052f\u0531-\u0556\u0559\u0560-\u0588\u05d0-\u05ea\u05ef-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u0860-\u086a\u08a0-\u08b4\u08b6-\u08bd\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u09fc\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0af9\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c39\u0c3d\u0c58-\u0c5a\u0c60\u0c61\u0c80\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d05-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d54-\u0d56\u0d5f-\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e86-\u0e8a\u0e8c-\u0ea3\u0ea5\u0ea7-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f5\u13f8-\u13fd\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f8\u1700-\u170c\u170e-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1878\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191e\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19b0-\u19c9\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4b\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1c80-\u1c88\u1c90-\u1cba\u1cbd-\u1cbf\u1ce9-\u1cec\u1cee-\u1cf3\u1cf5\u1cf6\u1cfa\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2118-\u211d\u2124\u2126\u2128\u212a-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2c2e\u2c30-\u2c5e\u2c60-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309b-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312f\u3131-\u318e\u31a0-\u31ba\u31f0-\u31ff\u3400-\u4db5\u4e00-\u9fef\ua000-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua69d\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua7bf\ua7c2-\ua7c6\ua7f7-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua8fd\ua8fe\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\ua9e0-\ua9e4\ua9e6-\ua9ef\ua9fa-\ua9fe\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa7e-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uab30-\uab5a\uab5c-\uab67\uab70-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\ufb1d\ufb1f-\ufb28\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40\ufb41\ufb43\ufb44\ufb46-\ufbb1\ufbd3-\ufd3d\ufd50-\ufd8f\ufd92-\ufdc7\ufdf0-\ufdfb\ufe70-\ufe74\ufe76-\ufefc\uff21-\uff3a\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc" -let nonASCIIidentifierChars = "\u200c\u200d\xb7\u0300-\u036f\u0387\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u064b-\u0669\u0670\u06d6-\u06dc\u06df-\u06e4\u06e7\u06e8\u06ea-\u06ed\u06f0-\u06f9\u0711\u0730-\u074a\u07a6-\u07b0\u07c0-\u07c9\u07eb-\u07f3\u07fd\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0859-\u085b\u08d3-\u08e1\u08e3-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09cb-\u09cd\u09d7\u09e2\u09e3\u09e6-\u09ef\u09fe\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2\u0ae3\u0ae6-\u0aef\u0afa-\u0aff\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b56\u0b57\u0b62\u0b63\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c00-\u0c04\u0c3e-\u0c44\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62\u0c63\u0c66-\u0c6f\u0c81-\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2\u0ce3\u0ce6-\u0cef\u0d00-\u0d03\u0d3b\u0d3c\u0d3e-\u0d44\u0d46-\u0d48\u0d4a-\u0d4d\u0d57\u0d62\u0d63\u0d66-\u0d6f\u0d82\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0de6-\u0def\u0df2\u0df3\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0e50-\u0e59\u0eb1\u0eb4-\u0ebc\u0ec8-\u0ecd\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f3e\u0f3f\u0f71-\u0f84\u0f86\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u102b-\u103e\u1040-\u1049\u1056-\u1059\u105e-\u1060\u1062-\u1064\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u1369-\u1371\u1712-\u1714\u1732-\u1734\u1752\u1753\u1772\u1773\u17b4-\u17d3\u17dd\u17e0-\u17e9\u180b-\u180d\u1810-\u1819\u18a9\u1920-\u192b\u1930-\u193b\u1946-\u194f\u19d0-\u19da\u1a17-\u1a1b\u1a55-\u1a5e\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1ab0-\u1abd\u1b00-\u1b04\u1b34-\u1b44\u1b50-\u1b59\u1b6b-\u1b73\u1b80-\u1b82\u1ba1-\u1bad\u1bb0-\u1bb9\u1be6-\u1bf3\u1c24-\u1c37\u1c40-\u1c49\u1c50-\u1c59\u1cd0-\u1cd2\u1cd4-\u1ce8\u1ced\u1cf4\u1cf7-\u1cf9\u1dc0-\u1df9\u1dfb-\u1dff\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2cef-\u2cf1\u2d7f\u2de0-\u2dff\u302a-\u302f\u3099\u309a\ua620-\ua629\ua66f\ua674-\ua67d\ua69e\ua69f\ua6f0\ua6f1\ua802\ua806\ua80b\ua823-\ua827\ua880\ua881\ua8b4-\ua8c5\ua8d0-\ua8d9\ua8e0-\ua8f1\ua8ff-\ua909\ua926-\ua92d\ua947-\ua953\ua980-\ua983\ua9b3-\ua9c0\ua9d0-\ua9d9\ua9e5\ua9f0-\ua9f9\uaa29-\uaa36\uaa43\uaa4c\uaa4d\uaa50-\uaa59\uaa7b-\uaa7d\uaab0\uaab2-\uaab4\uaab7\uaab8\uaabe\uaabf\uaac1\uaaeb-\uaaef\uaaf5\uaaf6\uabe3-\uabea\uabec\uabed\uabf0-\uabf9\ufb1e\ufe00-\ufe0f\ufe20-\ufe2f\ufe33\ufe34\ufe4d-\ufe4f\uff10-\uff19\uff3f" +let nonASCIIidentifierStartChars = "\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u037f\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u052f\u0531-\u0556\u0559\u0560-\u0588\u05d0-\u05ea\u05ef-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u0860-\u086a\u08a0-\u08b4\u08b6-\u08c7\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u09fc\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0af9\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c39\u0c3d\u0c58-\u0c5a\u0c60\u0c61\u0c80\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d04-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d54-\u0d56\u0d5f-\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e86-\u0e8a\u0e8c-\u0ea3\u0ea5\u0ea7-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f5\u13f8-\u13fd\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f8\u1700-\u170c\u170e-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1878\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191e\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19b0-\u19c9\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4b\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1c80-\u1c88\u1c90-\u1cba\u1cbd-\u1cbf\u1ce9-\u1cec\u1cee-\u1cf3\u1cf5\u1cf6\u1cfa\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2118-\u211d\u2124\u2126\u2128\u212a-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2c2e\u2c30-\u2c5e\u2c60-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309b-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312f\u3131-\u318e\u31a0-\u31bf\u31f0-\u31ff\u3400-\u4dbf\u4e00-\u9ffc\ua000-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua69d\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua7bf\ua7c2-\ua7ca\ua7f5-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua8fd\ua8fe\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\ua9e0-\ua9e4\ua9e6-\ua9ef\ua9fa-\ua9fe\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa7e-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uab30-\uab5a\uab5c-\uab69\uab70-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\ufb1d\ufb1f-\ufb28\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40\ufb41\ufb43\ufb44\ufb46-\ufbb1\ufbd3-\ufd3d\ufd50-\ufd8f\ufd92-\ufdc7\ufdf0-\ufdfb\ufe70-\ufe74\ufe76-\ufefc\uff21-\uff3a\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc" +let nonASCIIidentifierChars = "\u200c\u200d\xb7\u0300-\u036f\u0387\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u064b-\u0669\u0670\u06d6-\u06dc\u06df-\u06e4\u06e7\u06e8\u06ea-\u06ed\u06f0-\u06f9\u0711\u0730-\u074a\u07a6-\u07b0\u07c0-\u07c9\u07eb-\u07f3\u07fd\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0859-\u085b\u08d3-\u08e1\u08e3-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09cb-\u09cd\u09d7\u09e2\u09e3\u09e6-\u09ef\u09fe\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2\u0ae3\u0ae6-\u0aef\u0afa-\u0aff\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b55-\u0b57\u0b62\u0b63\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c00-\u0c04\u0c3e-\u0c44\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62\u0c63\u0c66-\u0c6f\u0c81-\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2\u0ce3\u0ce6-\u0cef\u0d00-\u0d03\u0d3b\u0d3c\u0d3e-\u0d44\u0d46-\u0d48\u0d4a-\u0d4d\u0d57\u0d62\u0d63\u0d66-\u0d6f\u0d81-\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0de6-\u0def\u0df2\u0df3\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0e50-\u0e59\u0eb1\u0eb4-\u0ebc\u0ec8-\u0ecd\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f3e\u0f3f\u0f71-\u0f84\u0f86\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u102b-\u103e\u1040-\u1049\u1056-\u1059\u105e-\u1060\u1062-\u1064\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u1369-\u1371\u1712-\u1714\u1732-\u1734\u1752\u1753\u1772\u1773\u17b4-\u17d3\u17dd\u17e0-\u17e9\u180b-\u180d\u1810-\u1819\u18a9\u1920-\u192b\u1930-\u193b\u1946-\u194f\u19d0-\u19da\u1a17-\u1a1b\u1a55-\u1a5e\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1ab0-\u1abd\u1abf\u1ac0\u1b00-\u1b04\u1b34-\u1b44\u1b50-\u1b59\u1b6b-\u1b73\u1b80-\u1b82\u1ba1-\u1bad\u1bb0-\u1bb9\u1be6-\u1bf3\u1c24-\u1c37\u1c40-\u1c49\u1c50-\u1c59\u1cd0-\u1cd2\u1cd4-\u1ce8\u1ced\u1cf4\u1cf7-\u1cf9\u1dc0-\u1df9\u1dfb-\u1dff\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2cef-\u2cf1\u2d7f\u2de0-\u2dff\u302a-\u302f\u3099\u309a\ua620-\ua629\ua66f\ua674-\ua67d\ua69e\ua69f\ua6f0\ua6f1\ua802\ua806\ua80b\ua823-\ua827\ua82c\ua880\ua881\ua8b4-\ua8c5\ua8d0-\ua8d9\ua8e0-\ua8f1\ua8ff-\ua909\ua926-\ua92d\ua947-\ua953\ua980-\ua983\ua9b3-\ua9c0\ua9d0-\ua9d9\ua9e5\ua9f0-\ua9f9\uaa29-\uaa36\uaa43\uaa4c\uaa4d\uaa50-\uaa59\uaa7b-\uaa7d\uaab0\uaab2-\uaab4\uaab7\uaab8\uaabe\uaabf\uaac1\uaaeb-\uaaef\uaaf5\uaaf6\uabe3-\uabea\uabec\uabed\uabf0-\uabf9\ufb1e\ufe00-\ufe0f\ufe20-\ufe2f\ufe33\ufe34\ufe4d-\ufe4f\uff10-\uff19\uff3f" const nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]") const nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]") @@ -42,10 +42,10 @@ nonASCIIidentifierStartChars = nonASCIIidentifierChars = null // generated by bin/generate-identifier-regex.js // eslint-disable-next-line comma-spacing -const astralIdentifierStartCodes = [0,11,2,25,2,18,2,1,2,14,3,13,35,122,70,52,268,28,4,48,48,31,14,29,6,37,11,29,3,35,5,7,2,4,43,157,19,35,5,35,5,39,9,51,157,310,10,21,11,7,153,5,3,0,2,43,2,1,4,0,3,22,11,22,10,30,66,18,2,1,11,21,11,25,71,55,7,1,65,0,16,3,2,2,2,28,43,28,4,28,36,7,2,27,28,53,11,21,11,18,14,17,111,72,56,50,14,50,14,35,477,28,11,0,9,21,155,22,13,52,76,44,33,24,27,35,30,0,12,34,4,0,13,47,15,3,22,0,2,0,36,17,2,24,85,6,2,0,2,3,2,14,2,9,8,46,39,7,3,1,3,21,2,6,2,1,2,4,4,0,19,0,13,4,159,52,19,3,21,0,33,47,21,1,2,0,185,46,42,3,37,47,21,0,60,42,14,0,72,26,230,43,117,63,32,0,161,7,3,38,17,0,2,0,29,0,11,39,8,0,22,0,12,45,20,0,35,56,264,8,2,36,18,0,50,29,113,6,2,1,2,37,22,0,26,5,2,1,2,31,15,0,328,18,270,921,103,110,18,195,2749,1070,4050,582,8634,568,8,30,114,29,19,47,17,3,32,20,6,18,689,63,129,74,6,0,67,12,65,1,2,0,29,6135,9,754,9486,286,50,2,18,3,9,395,2309,106,6,12,4,8,8,9,5991,84,2,70,2,1,3,0,3,1,3,3,2,11,2,0,2,6,2,64,2,3,3,7,2,6,2,27,2,3,2,4,2,0,4,6,2,339,3,24,2,24,2,30,2,24,2,30,2,24,2,30,2,24,2,30,2,24,2,7,2357,44,11,6,17,0,370,43,1301,196,60,67,8,0,1205,3,2,26,2,1,2,0,3,0,2,9,2,3,2,0,2,0,7,0,5,0,2,0,2,0,2,2,2,1,2,0,3,0,2,0,2,0,2,0,2,0,2,1,2,0,3,3,2,6,2,3,2,3,2,0,2,9,2,16,6,2,2,4,2,16,4421,42710,42,4148,12,221,3,5761,15,7472,3104,541] +const astralIdentifierStartCodes = [0,11,2,25,2,18,2,1,2,14,3,13,35,122,70,52,268,28,4,48,48,31,14,29,6,37,11,29,3,35,5,7,2,4,43,157,19,35,5,35,5,39,9,51,157,310,10,21,11,7,153,5,3,0,2,43,2,1,4,0,3,22,11,22,10,30,66,18,2,1,11,21,11,25,71,55,7,1,65,0,16,3,2,2,2,28,43,28,4,28,36,7,2,27,28,53,11,21,11,18,14,17,111,72,56,50,14,50,14,35,349,41,7,1,79,28,11,0,9,21,107,20,28,22,13,52,76,44,33,24,27,35,30,0,3,0,9,34,4,0,13,47,15,3,22,0,2,0,36,17,2,24,85,6,2,0,2,3,2,14,2,9,8,46,39,7,3,1,3,21,2,6,2,1,2,4,4,0,19,0,13,4,159,52,19,3,21,2,31,47,21,1,2,0,185,46,42,3,37,47,21,0,60,42,14,0,72,26,230,43,117,63,32,7,3,0,3,7,2,1,2,23,16,0,2,0,95,7,3,38,17,0,2,0,29,0,11,39,8,0,22,0,12,45,20,0,35,56,264,8,2,36,18,0,50,29,113,6,2,1,2,37,22,0,26,5,2,1,2,31,15,0,328,18,190,0,80,921,103,110,18,195,2749,1070,4050,582,8634,568,8,30,114,29,19,47,17,3,32,20,6,18,689,63,129,74,6,0,67,12,65,1,2,0,29,6135,9,1237,43,8,8952,286,50,2,18,3,9,395,2309,106,6,12,4,8,8,9,5991,84,2,70,2,1,3,0,3,1,3,3,2,11,2,0,2,6,2,64,2,3,3,7,2,6,2,27,2,3,2,4,2,0,4,6,2,339,3,24,2,24,2,30,2,24,2,30,2,24,2,30,2,24,2,30,2,24,2,7,2357,44,11,6,17,0,370,43,1301,196,60,67,8,0,1205,3,2,26,2,1,2,0,3,0,2,9,2,3,2,0,2,0,7,0,5,0,2,0,2,0,2,2,2,1,2,0,3,0,2,0,2,0,2,0,2,0,2,1,2,0,3,3,2,6,2,3,2,3,2,0,2,9,2,16,6,2,2,4,2,16,4421,42717,35,4148,12,221,3,5761,15,7472,3104,541,1507,4938] // eslint-disable-next-line comma-spacing -const astralIdentifierCodes = [509,0,227,0,150,4,294,9,1368,2,2,1,6,3,41,2,5,0,166,1,574,3,9,9,525,10,176,2,54,14,32,9,16,3,46,10,54,9,7,2,37,13,2,9,6,1,45,0,13,2,49,13,9,3,4,9,83,11,7,0,161,11,6,9,7,3,56,1,2,6,3,1,3,2,10,0,11,1,3,6,4,4,193,17,10,9,5,0,82,19,13,9,214,6,3,8,28,1,83,16,16,9,82,12,9,9,84,14,5,9,243,14,166,9,232,6,3,6,4,0,29,9,41,6,2,3,9,0,10,10,47,15,406,7,2,7,17,9,57,21,2,13,123,5,4,0,2,1,2,6,2,0,9,9,49,4,2,1,2,4,9,9,330,3,19306,9,135,4,60,6,26,9,1014,0,2,54,8,3,19723,1,5319,4,4,5,9,7,3,6,31,3,149,2,1418,49,513,54,5,49,9,0,15,0,23,4,2,14,1361,6,2,16,3,6,2,1,2,4,262,6,10,9,419,13,1495,6,110,6,6,9,792487,239] +const astralIdentifierCodes = [509,0,227,0,150,4,294,9,1368,2,2,1,6,3,41,2,5,0,166,1,574,3,9,9,370,1,154,10,176,2,54,14,32,9,16,3,46,10,54,9,7,2,37,13,2,9,6,1,45,0,13,2,49,13,9,3,2,11,83,11,7,0,161,11,6,9,7,3,56,1,2,6,3,1,3,2,10,0,11,1,3,6,4,4,193,17,10,9,5,0,82,19,13,9,214,6,3,8,28,1,83,16,16,9,82,12,9,9,84,14,5,9,243,14,166,9,71,5,2,1,3,3,2,0,2,1,13,9,120,6,3,6,4,0,29,9,41,6,2,3,9,0,10,10,47,15,406,7,2,7,17,9,57,21,2,13,123,5,4,0,2,1,2,6,2,0,9,9,49,4,2,1,2,4,9,9,330,3,19306,9,135,4,60,6,26,9,1014,0,2,54,8,3,82,0,12,1,19628,1,5319,4,4,5,9,7,3,6,31,3,149,2,1418,49,513,54,5,49,9,0,15,0,23,4,2,14,1361,6,2,16,3,6,2,1,2,4,262,6,10,9,419,13,1495,6,110,6,6,9,4759,9,787719,239] // This has a complexity linear to the value of the code. The // assumption is that looking up astral identifier characters is diff --git a/acorn/src/index.js b/acorn/src/index.js index f466a3b9f..eb118534b 100644 --- a/acorn/src/index.js +++ b/acorn/src/index.js @@ -31,7 +31,7 @@ import {isIdentifierChar, isIdentifierStart} from "./identifier" import {Token} from "./tokenize" import {isNewLine, lineBreak, lineBreakG, nonASCIIwhitespace} from "./whitespace" -export const version = "7.1.0" +export const version = "7.4.0" export { Parser, defaultOptions, diff --git a/acorn/src/lval.js b/acorn/src/lval.js index 8ad4b2639..03b3ce8c6 100644 --- a/acorn/src/lval.js +++ b/acorn/src/lval.js @@ -73,6 +73,10 @@ pp.toAssignable = function(node, isBinding, refDestructuringErrors) { this.toAssignable(node.expression, isBinding, refDestructuringErrors) break + case "ChainExpression": + this.raiseRecoverable(node.start, "Optional chaining cannot appear in left-hand side") + break + case "MemberExpression": if (!isBinding) break @@ -201,6 +205,10 @@ pp.checkLVal = function(expr, bindingType = BIND_NONE, checkClashes) { if (bindingType !== BIND_NONE && bindingType !== BIND_OUTSIDE) this.declareName(expr.name, bindingType, expr.start) break + case "ChainExpression": + this.raiseRecoverable(expr.start, "Optional chaining cannot appear in left-hand side") + break + case "MemberExpression": if (bindingType) this.raiseRecoverable(expr.start, "Binding member expression") break diff --git a/acorn/src/parseutil.js b/acorn/src/parseutil.js index bce71b54e..98e35237f 100644 --- a/acorn/src/parseutil.js +++ b/acorn/src/parseutil.js @@ -14,7 +14,14 @@ pp.strictDirective = function(start) { start += skipWhiteSpace.exec(this.input)[0].length let match = literal.exec(this.input.slice(start)) if (!match) return false - if ((match[1] || match[2]) === "use strict") return true + if ((match[1] || match[2]) === "use strict") { + skipWhiteSpace.lastIndex = start + match[0].length + let spaceAfter = skipWhiteSpace.exec(this.input), end = spaceAfter.index + spaceAfter[0].length + let next = this.input.charAt(end) + return next === ";" || next === "}" || + (lineBreak.test(spaceAfter[0]) && + !(/[(`.[+\-/*%<>=,?^&]/.test(next) || next === "!" && this.input.charAt(end + 1) === "=")) + } start += match[0].length // Skip semicolon, if any. diff --git a/acorn/src/regexp.js b/acorn/src/regexp.js index ee19bcf55..d0e66df83 100644 --- a/acorn/src/regexp.js +++ b/acorn/src/regexp.js @@ -40,47 +40,49 @@ export class RegExpValidationState { // If u flag is given, this returns the code point at the index (it combines a surrogate pair). // Otherwise, this returns the code unit of the index (can be a part of a surrogate pair). - at(i) { + at(i, forceU = false) { const s = this.source const l = s.length if (i >= l) { return -1 } const c = s.charCodeAt(i) - if (!this.switchU || c <= 0xD7FF || c >= 0xE000 || i + 1 >= l) { + if (!(forceU || this.switchU) || c <= 0xD7FF || c >= 0xE000 || i + 1 >= l) { return c } - return (c << 10) + s.charCodeAt(i + 1) - 0x35FDC00 + const next = s.charCodeAt(i + 1) + return next >= 0xDC00 && next <= 0xDFFF ? (c << 10) + next - 0x35FDC00 : c } - nextIndex(i) { + nextIndex(i, forceU = false) { const s = this.source const l = s.length if (i >= l) { return l } - const c = s.charCodeAt(i) - if (!this.switchU || c <= 0xD7FF || c >= 0xE000 || i + 1 >= l) { + let c = s.charCodeAt(i), next + if (!(forceU || this.switchU) || c <= 0xD7FF || c >= 0xE000 || i + 1 >= l || + (next = s.charCodeAt(i + 1)) < 0xDC00 || next > 0xDFFF) { return i + 1 } return i + 2 } - current() { - return this.at(this.pos) + current(forceU = false) { + return this.at(this.pos, forceU) } - lookahead() { - return this.at(this.nextIndex(this.pos)) + lookahead(forceU = false) { + return this.at(this.nextIndex(this.pos, forceU), forceU) } - advance() { - this.pos = this.nextIndex(this.pos) + advance(forceU = false) { + this.pos = this.nextIndex(this.pos, forceU) } - eat(ch) { - if (this.current() === ch) { - this.advance() + eat(ch, forceU = false) { + if (this.current(forceU) === ch) { + this.advance(forceU) return true } return false @@ -152,7 +154,7 @@ pp.regexp_pattern = function(state) { if (state.eat(0x29 /* ) */)) { state.raise("Unmatched ')'") } - if (state.eat(0x5D /* [ */) || state.eat(0x7D /* } */)) { + if (state.eat(0x5D /* ] */) || state.eat(0x7D /* } */)) { state.raise("Lone quantifier brackets") } } @@ -416,9 +418,9 @@ pp.regexp_eatExtendedPatternCharacter = function(state) { return false } -// GroupSpecifier[U] :: +// GroupSpecifier :: // [empty] -// `?` GroupName[?U] +// `?` GroupName pp.regexp_groupSpecifier = function(state) { if (state.eat(0x3F /* ? */)) { if (this.regexp_eatGroupName(state)) { @@ -432,8 +434,8 @@ pp.regexp_groupSpecifier = function(state) { } } -// GroupName[U] :: -// `<` RegExpIdentifierName[?U] `>` +// GroupName :: +// `<` RegExpIdentifierName `>` // Note: this updates `state.lastStringValue` property with the eaten name. pp.regexp_eatGroupName = function(state) { state.lastStringValue = "" @@ -446,9 +448,9 @@ pp.regexp_eatGroupName = function(state) { return false } -// RegExpIdentifierName[U] :: -// RegExpIdentifierStart[?U] -// RegExpIdentifierName[?U] RegExpIdentifierPart[?U] +// RegExpIdentifierName :: +// RegExpIdentifierStart +// RegExpIdentifierName RegExpIdentifierPart // Note: this updates `state.lastStringValue` property with the eaten name. pp.regexp_eatRegExpIdentifierName = function(state) { state.lastStringValue = "" @@ -462,17 +464,18 @@ pp.regexp_eatRegExpIdentifierName = function(state) { return false } -// RegExpIdentifierStart[U] :: +// RegExpIdentifierStart :: // UnicodeIDStart // `$` // `_` -// `\` RegExpUnicodeEscapeSequence[?U] +// `\` RegExpUnicodeEscapeSequence[+U] pp.regexp_eatRegExpIdentifierStart = function(state) { const start = state.pos - let ch = state.current() - state.advance() + const forceU = this.options.ecmaVersion >= 11 + let ch = state.current(forceU) + state.advance(forceU) - if (ch === 0x5C /* \ */ && this.regexp_eatRegExpUnicodeEscapeSequence(state)) { + if (ch === 0x5C /* \ */ && this.regexp_eatRegExpUnicodeEscapeSequence(state, forceU)) { ch = state.lastIntValue } if (isRegExpIdentifierStart(ch)) { @@ -487,19 +490,20 @@ function isRegExpIdentifierStart(ch) { return isIdentifierStart(ch, true) || ch === 0x24 /* $ */ || ch === 0x5F /* _ */ } -// RegExpIdentifierPart[U] :: +// RegExpIdentifierPart :: // UnicodeIDContinue // `$` // `_` -// `\` RegExpUnicodeEscapeSequence[?U] +// `\` RegExpUnicodeEscapeSequence[+U] // // pp.regexp_eatRegExpIdentifierPart = function(state) { const start = state.pos - let ch = state.current() - state.advance() + const forceU = this.options.ecmaVersion >= 11 + let ch = state.current(forceU) + state.advance(forceU) - if (ch === 0x5C /* \ */ && this.regexp_eatRegExpUnicodeEscapeSequence(state)) { + if (ch === 0x5C /* \ */ && this.regexp_eatRegExpUnicodeEscapeSequence(state, forceU)) { ch = state.lastIntValue } if (isRegExpIdentifierPart(ch)) { @@ -569,7 +573,7 @@ pp.regexp_eatCharacterEscape = function(state) { this.regexp_eatCControlLetter(state) || this.regexp_eatZero(state) || this.regexp_eatHexEscapeSequence(state) || - this.regexp_eatRegExpUnicodeEscapeSequence(state) || + this.regexp_eatRegExpUnicodeEscapeSequence(state, false) || (!state.switchU && this.regexp_eatLegacyOctalEscapeSequence(state)) || this.regexp_eatIdentityEscape(state) ) @@ -642,13 +646,14 @@ function isControlLetter(ch) { } // https://www.ecma-international.org/ecma-262/8.0/#prod-RegExpUnicodeEscapeSequence -pp.regexp_eatRegExpUnicodeEscapeSequence = function(state) { +pp.regexp_eatRegExpUnicodeEscapeSequence = function(state, forceU = false) { const start = state.pos + const switchU = forceU || state.switchU if (state.eat(0x75 /* u */)) { if (this.regexp_eatFixedHexDigits(state, 4)) { const lead = state.lastIntValue - if (state.switchU && lead >= 0xD800 && lead <= 0xDBFF) { + if (switchU && lead >= 0xD800 && lead <= 0xDBFF) { const leadSurrogateEnd = state.pos if (state.eat(0x5C /* \ */) && state.eat(0x75 /* u */) && this.regexp_eatFixedHexDigits(state, 4)) { const trail = state.lastIntValue @@ -663,7 +668,7 @@ pp.regexp_eatRegExpUnicodeEscapeSequence = function(state) { return true } if ( - state.switchU && + switchU && state.eat(0x7B /* { */) && this.regexp_eatHexDigits(state) && state.eat(0x7D /* } */) && @@ -671,7 +676,7 @@ pp.regexp_eatRegExpUnicodeEscapeSequence = function(state) { ) { return true } - if (state.switchU) { + if (switchU) { state.raise("Invalid unicode escape") } state.pos = start @@ -837,7 +842,7 @@ pp.regexp_eatCharacterClass = function(state) { if (state.eat(0x5B /* [ */)) { state.eat(0x5E /* ^ */) this.regexp_classRanges(state) - if (state.eat(0x5D /* [ */)) { + if (state.eat(0x5D /* ] */)) { return true } // Unreachable since it threw "unterminated regular expression" error before. @@ -885,7 +890,7 @@ pp.regexp_eatClassAtom = function(state) { } const ch = state.current() - if (ch !== 0x5D /* [ */) { + if (ch !== 0x5D /* ] */) { state.lastIntValue = ch state.advance() return true diff --git a/acorn/src/statement.js b/acorn/src/statement.js index 8cadb5118..4afea7503 100644 --- a/acorn/src/statement.js +++ b/acorn/src/statement.js @@ -122,7 +122,7 @@ pp.parseStatement = function(context, topLevel, exports) { skipWhiteSpace.lastIndex = this.pos let skip = skipWhiteSpace.exec(this.input) let next = this.pos + skip[0].length, nextCh = this.input.charCodeAt(next) - if (nextCh === 40) // '(' + if (nextCh === 40 || nextCh === 46) // '(' or '.' return this.parseExpressionStatement(node, this.parseExpression()) } @@ -418,14 +418,16 @@ pp.parseExpressionStatement = function(node, expr) { // strict"` declarations when `allowStrict` is true (used for // function bodies). -pp.parseBlock = function(createNewLexicalScope = true, node = this.startNode()) { +pp.parseBlock = function(createNewLexicalScope = true, node = this.startNode(), exitStrict) { node.body = [] this.expect(tt.braceL) if (createNewLexicalScope) this.enterScope(0) - while (!this.eat(tt.braceR)) { + while (this.type !== tt.braceR) { let stmt = this.parseStatement(null) node.body.push(stmt) } + if (exitStrict) this.strict = false + this.next() if (createNewLexicalScope) this.exitScope() return this.finishNode(node, "BlockStatement") } @@ -578,7 +580,7 @@ pp.parseClass = function(node, isStatement) { let hadConstructor = false classBody.body = [] this.expect(tt.braceL) - while (!this.eat(tt.braceR)) { + while (this.type !== tt.braceR) { const element = this.parseClassElement(node.superClass !== null) if (element) { classBody.body.push(element) @@ -588,8 +590,9 @@ pp.parseClass = function(node, isStatement) { } } } - node.body = this.finishNode(classBody, "ClassBody") this.strict = oldStrict + this.next() + node.body = this.finishNode(classBody, "ClassBody") return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression") } @@ -673,6 +676,14 @@ pp.parseExport = function(node, exports) { this.next() // export * from '...' if (this.eat(tt.star)) { + if (this.options.ecmaVersion >= 11) { + if (this.eatContextual("as")) { + node.exported = this.parseIdent(true) + this.checkExport(exports, node.exported.name, this.lastTokStart) + } else { + node.exported = null + } + } this.expectContextual("from") if (this.type !== tt.string) this.unexpected() node.source = this.parseExprAtom() diff --git a/acorn/src/tokenize.js b/acorn/src/tokenize.js index f9f6b2f6f..6c86f5c51 100644 --- a/acorn/src/tokenize.js +++ b/acorn/src/tokenize.js @@ -28,7 +28,9 @@ const pp = Parser.prototype // Move to the next token -pp.next = function() { +pp.next = function(ignoreEscapeSequenceInKeyword) { + if (!ignoreEscapeSequenceInKeyword && this.type.keyword && this.containsEsc) + this.raiseRecoverable(this.start, "Escape sequence in keyword " + this.type.keyword) if (this.options.onToken) this.options.onToken(new Token(this)) @@ -231,7 +233,13 @@ pp.readToken_mult_modulo_exp = function(code) { // '%*' pp.readToken_pipe_amp = function(code) { // '|&' let next = this.input.charCodeAt(this.pos + 1) - if (next === code) return this.finishOp(code === 124 ? tt.logicalOR : tt.logicalAND, 2) + if (next === code) { + if (this.options.ecmaVersion >= 12) { + let next2 = this.input.charCodeAt(this.pos + 2) + if (next2 === 61) return this.finishOp(tt.assign, 3) + } + return this.finishOp(code === 124 ? tt.logicalOR : tt.logicalAND, 2) + } if (next === 61) return this.finishOp(tt.assign, 2) return this.finishOp(code === 124 ? tt.bitwiseOR : tt.bitwiseAND, 1) } @@ -287,6 +295,25 @@ pp.readToken_eq_excl = function(code) { // '=!' return this.finishOp(code === 61 ? tt.eq : tt.prefix, 1) } +pp.readToken_question = function() { // '?' + const ecmaVersion = this.options.ecmaVersion + if (ecmaVersion >= 11) { + let next = this.input.charCodeAt(this.pos + 1) + if (next === 46) { + let next2 = this.input.charCodeAt(this.pos + 2) + if (next2 < 48 || next2 > 57) return this.finishOp(tt.questionDot, 2) + } + if (next === 63) { + if (ecmaVersion >= 12) { + let next2 = this.input.charCodeAt(this.pos + 2) + if (next2 === 61) return this.finishOp(tt.assign, 3) + } + return this.finishOp(tt.coalesce, 2) + } + } + return this.finishOp(tt.question, 1) +} + pp.getTokenFromCode = function(code) { switch (code) { // The interpretation of a dot depends on whether it is followed @@ -304,7 +331,6 @@ pp.getTokenFromCode = function(code) { case 123: ++this.pos; return this.finishToken(tt.braceL) case 125: ++this.pos; return this.finishToken(tt.braceR) case 58: ++this.pos; return this.finishToken(tt.colon) - case 63: ++this.pos; return this.finishToken(tt.question) case 96: // '`' if (this.options.ecmaVersion < 6) break @@ -354,6 +380,9 @@ pp.getTokenFromCode = function(code) { case 61: case 33: // '=!' return this.readToken_eq_excl(code) + case 63: // '?' + return this.readToken_question() + case 126: // '~' return this.finishOp(tt.prefix, 1) } @@ -409,30 +438,67 @@ pp.readRegexp = function() { // were read, the integer value otherwise. When `len` is given, this // will return `null` unless the integer has exactly `len` digits. -pp.readInt = function(radix, len) { - let start = this.pos, total = 0 - for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) { +pp.readInt = function(radix, len, maybeLegacyOctalNumericLiteral) { + // `len` is used for character escape sequences. In that case, disallow separators. + const allowSeparators = this.options.ecmaVersion >= 12 && len === undefined + + // `maybeLegacyOctalNumericLiteral` is true if it doesn't have prefix (0x,0o,0b) + // and isn't fraction part nor exponent part. In that case, if the first digit + // is zero then disallow separators. + const isLegacyOctalNumericLiteral = maybeLegacyOctalNumericLiteral && this.input.charCodeAt(this.pos) === 48 + + let start = this.pos, total = 0, lastCode = 0 + for (let i = 0, e = len == null ? Infinity : len; i < e; ++i, ++this.pos) { let code = this.input.charCodeAt(this.pos), val + + if (allowSeparators && code === 95) { + if (isLegacyOctalNumericLiteral) this.raiseRecoverable(this.pos, "Numeric separator is not allowed in legacy octal numeric literals") + if (lastCode === 95) this.raiseRecoverable(this.pos, "Numeric separator must be exactly one underscore") + if (i === 0) this.raiseRecoverable(this.pos, "Numeric separator is not allowed at the first of digits") + lastCode = code + continue + } + if (code >= 97) val = code - 97 + 10 // a else if (code >= 65) val = code - 65 + 10 // A else if (code >= 48 && code <= 57) val = code - 48 // 0-9 else val = Infinity if (val >= radix) break - ++this.pos + lastCode = code total = total * radix + val } + + if (allowSeparators && lastCode === 95) this.raiseRecoverable(this.pos - 1, "Numeric separator is not allowed at the last of digits") if (this.pos === start || len != null && this.pos - start !== len) return null return total } +function stringToNumber(str, isLegacyOctalNumericLiteral) { + if (isLegacyOctalNumericLiteral) { + return parseInt(str, 8) + } + + // `parseFloat(value)` stops parsing at the first numeric separator then returns a wrong value. + return parseFloat(str.replace(/_/g, "")) +} + +function stringToBigInt(str) { + if (typeof BigInt !== "function") { + return null + } + + // `BigInt(value)` throws syntax error if the string contains numeric separators. + return BigInt(str.replace(/_/g, "")) +} + pp.readRadixNumber = function(radix) { let start = this.pos this.pos += 2 // 0x let val = this.readInt(radix) if (val == null) this.raise(this.start + 2, "Expected number in radix " + radix) if (this.options.ecmaVersion >= 11 && this.input.charCodeAt(this.pos) === 110) { - val = typeof BigInt !== "undefined" ? BigInt(this.input.slice(start, this.pos)) : null + val = stringToBigInt(this.input.slice(start, this.pos)) ++this.pos } else if (isIdentifierStart(this.fullCharCodeAtPos())) this.raise(this.pos, "Identifier directly after number") return this.finishToken(tt.num, val) @@ -442,18 +508,17 @@ pp.readRadixNumber = function(radix) { pp.readNumber = function(startsWithDot) { let start = this.pos - if (!startsWithDot && this.readInt(10) === null) this.raise(start, "Invalid number") + if (!startsWithDot && this.readInt(10, undefined, true) === null) this.raise(start, "Invalid number") let octal = this.pos - start >= 2 && this.input.charCodeAt(start) === 48 if (octal && this.strict) this.raise(start, "Invalid number") - if (octal && /[89]/.test(this.input.slice(start, this.pos))) octal = false let next = this.input.charCodeAt(this.pos) if (!octal && !startsWithDot && this.options.ecmaVersion >= 11 && next === 110) { - let str = this.input.slice(start, this.pos) - let val = typeof BigInt !== "undefined" ? BigInt(str) : null + let val = stringToBigInt(this.input.slice(start, this.pos)) ++this.pos if (isIdentifierStart(this.fullCharCodeAtPos())) this.raise(this.pos, "Identifier directly after number") return this.finishToken(tt.num, val) } + if (octal && /[89]/.test(this.input.slice(start, this.pos))) octal = false if (next === 46 && !octal) { // '.' ++this.pos this.readInt(10) @@ -466,8 +531,7 @@ pp.readNumber = function(startsWithDot) { } if (isIdentifierStart(this.fullCharCodeAtPos())) this.raise(this.pos, "Identifier directly after number") - let str = this.input.slice(start, this.pos) - let val = octal ? parseInt(str, 8) : parseFloat(str) + let val = stringToNumber(this.input.slice(start, this.pos), octal) return this.finishToken(tt.num, val) } @@ -628,6 +692,18 @@ pp.readEscapedChar = function(inTemplate) { case 10: // ' \n' if (this.options.locations) { this.lineStart = this.pos; ++this.curLine } return "" + case 56: + case 57: + if (inTemplate) { + const codePos = this.pos - 1 + + this.invalidStringToken( + codePos, + "Invalid escape sequence in template string" + ) + + return null + } default: if (ch >= 48 && ch <= 55) { let octalStr = this.input.substr(this.pos - 1, 3).match(/^[0-7]+/)[0] @@ -707,7 +783,6 @@ pp.readWord = function() { let word = this.readWord1() let type = tt.name if (this.keywords.test(word)) { - if (this.containsEsc) this.raiseRecoverable(this.start, "Escape sequence in keyword " + word) type = keywordTypes[word] } return this.finishToken(type, word) diff --git a/acorn/src/tokentype.js b/acorn/src/tokentype.js index 7a8c48a6d..1e821ff40 100644 --- a/acorn/src/tokentype.js +++ b/acorn/src/tokentype.js @@ -70,6 +70,7 @@ export const types = { colon: new TokenType(":", beforeExpr), dot: new TokenType("."), question: new TokenType("?", beforeExpr), + questionDot: new TokenType("?."), arrow: new TokenType("=>", beforeExpr), template: new TokenType("template"), invalidTemplate: new TokenType("invalidTemplate"), @@ -108,6 +109,7 @@ export const types = { star: binop("*", 10), slash: binop("/", 10), starstar: new TokenType("**", {beforeExpr: true}), + coalesce: binop("??", 1), // Keyword token types. _break: kw("break"), diff --git a/bin/run_test262.js b/bin/run_test262.js index c540507d7..ff55bcd19 100644 --- a/bin/run_test262.js +++ b/bin/run_test262.js @@ -10,16 +10,17 @@ const unsupportedFeatures = [ "class-static-fields-private", "class-static-fields-public", "class-static-methods-private", - "export-star-as-namespace-from-module", - "import.meta", - "numeric-separator-literal" + "logical-assignment-operators", ]; run( - (content, {sourceType}) => parse(content, {sourceType, ecmaVersion: 11, allowHashBang: true}), + (content, {sourceType}) => parse(content, {sourceType, ecmaVersion: 12, allowHashBang: true, allowAwaitOutsideFunction: true}), { testsDirectory: path.dirname(require.resolve("test262/package.json")), skip: test => (test.attrs.features && unsupportedFeatures.some(f => test.attrs.features.includes(f))), - whitelist: fs.readFileSync("./bin/test262.whitelist", "utf8").split("\n").filter(v => v) + whitelist: fs.readFileSync("./bin/test262.whitelist", "utf8") + .split("\n") + .filter(Boolean) + .map(filename => path.sep !== "/" ? filename.split("/").join(path.sep) : filename) } ) diff --git a/bin/test262.whitelist b/bin/test262.whitelist index e69de29bb..725858f89 100644 --- a/bin/test262.whitelist +++ b/bin/test262.whitelist @@ -0,0 +1,40 @@ +built-ins/RegExp/property-escapes/generated/Emoji_Component.js (default) +built-ins/RegExp/property-escapes/generated/Emoji_Component.js (strict mode) +built-ins/RegExp/property-escapes/generated/Emoji_Modifier.js (default) +built-ins/RegExp/property-escapes/generated/Emoji_Modifier.js (strict mode) +built-ins/RegExp/property-escapes/generated/Emoji_Modifier_Base.js (default) +built-ins/RegExp/property-escapes/generated/Emoji_Modifier_Base.js (strict mode) +built-ins/RegExp/property-escapes/generated/Emoji_Presentation.js (default) +built-ins/RegExp/property-escapes/generated/Emoji_Presentation.js (strict mode) +built-ins/RegExp/property-escapes/generated/Extended_Pictographic.js (default) +built-ins/RegExp/property-escapes/generated/Extended_Pictographic.js (strict mode) +built-ins/RegExp/property-escapes/generated/Script_-_Chorasmian.js (default) +built-ins/RegExp/property-escapes/generated/Script_-_Chorasmian.js (strict mode) +built-ins/RegExp/property-escapes/generated/Script_-_Dives_Akuru.js (default) +built-ins/RegExp/property-escapes/generated/Script_-_Dives_Akuru.js (strict mode) +built-ins/RegExp/property-escapes/generated/Script_-_Khitan_Small_Script.js (default) +built-ins/RegExp/property-escapes/generated/Script_-_Khitan_Small_Script.js (strict mode) +built-ins/RegExp/property-escapes/generated/Script_-_Yezidi.js (default) +built-ins/RegExp/property-escapes/generated/Script_-_Yezidi.js (strict mode) +built-ins/RegExp/property-escapes/generated/Script_Extensions_-_Chorasmian.js (default) +built-ins/RegExp/property-escapes/generated/Script_Extensions_-_Chorasmian.js (strict mode) +built-ins/RegExp/property-escapes/generated/Script_Extensions_-_Dives_Akuru.js (default) +built-ins/RegExp/property-escapes/generated/Script_Extensions_-_Dives_Akuru.js (strict mode) +built-ins/RegExp/property-escapes/generated/Script_Extensions_-_Khitan_Small_Script.js (default) +built-ins/RegExp/property-escapes/generated/Script_Extensions_-_Khitan_Small_Script.js (strict mode) +built-ins/RegExp/property-escapes/generated/Script_Extensions_-_Yezidi.js (default) +built-ins/RegExp/property-escapes/generated/Script_Extensions_-_Yezidi.js (strict mode) + +language/literals/string/legacy-non-octal-escape-sequence-8-strict.js (strict mode) + +language/expressions/await/await-BindingIdentifier-in-global.js (default) +language/expressions/await/await-BindingIdentifier-in-global.js (strict mode) +language/expressions/await/await-in-global.js (default) +language/expressions/await/await-in-global.js (strict mode) +language/expressions/await/await-in-nested-function.js (default) +language/expressions/await/await-in-nested-function.js (strict mode) +language/expressions/await/await-in-nested-generator.js (default) +language/expressions/await/await-in-nested-generator.js (strict mode) + +language/statements/labeled/value-await-non-module.js (default) +language/statements/labeled/value-await-non-module.js (strict mode) diff --git a/package.json b/package.json index eb0dd1c73..382f59e0b 100644 --- a/package.json +++ b/package.json @@ -41,9 +41,9 @@ "eslint-plugin-standard": "^3.0.1", "rollup": "^1.7.0", "rollup-plugin-buble": "^0.19.0", - "test262": "git+https://github.com/tc39/test262.git#de567d3aa5de4eaa11e00131d26b9fe77997dfb0", + "test262": "git+https://github.com/tc39/test262.git#a6c819ad0f049f23f1a37af6b89dbb79fe3b9216", "test262-parser-runner": "^0.5.0", "test262-stream": "^1.2.1", - "unicode-12.0.0": "^0.7.9" + "unicode-13.0.0": "^0.8.0" } } diff --git a/test/run.js b/test/run.js index 4ea981b90..80d3ad42d 100644 --- a/test/run.js +++ b/test/run.js @@ -12,10 +12,17 @@ require("./tests-async-iteration.js"); require("./tests-regexp.js"); require("./tests-regexp-2018.js"); + require("./tests-regexp-2020.js"); require("./tests-json-superset.js"); require("./tests-optional-catch-binding.js"); require("./tests-bigint.js"); require("./tests-dynamic-import.js"); + require("./tests-export-all-as-ns-from-source.js"); + require("./tests-import-meta.js"); + require("./tests-nullish-coalescing.js"); + require("./tests-optional-chaining.js"); + require("./tests-logical-assignment-operators.js"); + require("./tests-numeric-separators.js"); var acorn = require("../acorn") var acorn_loose = require("../acorn-loose") diff --git a/test/tests-asyncawait.js b/test/tests-asyncawait.js index 77de3c8b8..f5d84794b 100644 --- a/test/tests-asyncawait.js +++ b/test/tests-asyncawait.js @@ -3523,3 +3523,7 @@ test( test("({ async delete() {} })", {}, {ecmaVersion: 8}) testFail("abc: async function a() {}", "Unexpected token (1:5)", {ecmaVersion: 8}) + +test("(async() => { await 4 ** 2 })()", {}, {ecmaVersion: 8}) + +testFail("4 + async() => 2", "Unexpected token (1:12)", {ecmaVersion: 8, loose: false}) diff --git a/test/tests-dynamic-import.js b/test/tests-dynamic-import.js index 675a45365..92e52586f 100644 --- a/test/tests-dynamic-import.js +++ b/test/tests-dynamic-import.js @@ -204,7 +204,7 @@ test( { ecmaVersion: 11 } ); -testFail('function failsParse() { return import.then(); }', 'Unexpected token (1:37)', { +testFail('function failsParse() { return import.then(); }', 'The only valid meta property for import is \'import.meta\' (1:38)', { ecmaVersion: 11, loose: false }); diff --git a/test/tests-export-all-as-ns-from-source.js b/test/tests-export-all-as-ns-from-source.js new file mode 100644 index 000000000..8a7145e71 --- /dev/null +++ b/test/tests-export-all-as-ns-from-source.js @@ -0,0 +1,93 @@ + +if (typeof exports != "undefined") { + var driver = require("./driver.js"); + var test = driver.test, testFail = driver.testFail; + var acorn = require("../acorn"); +} + +//------------------------------------------------------------------------------ +// export * as ns from "source" +//------------------------------------------------------------------------------ + +test("export * as ns from \"source\"", { + "type": "Program", + "start": 0, + "end": 28, + "body": [ + { + "type": "ExportAllDeclaration", + "start": 0, + "end": 28, + "exported": { + "type": "Identifier", + "start": 12, + "end": 14, + "name": "ns" + }, + "source": { + "type": "Literal", + "start": 20, + "end": 28, + "value": "source", + "raw": "\"source\"" + } + } + ], + "sourceType": "module" +}, { sourceType: "module", ecmaVersion: 11 }) + +test("export * as foo from \"bar\"", { + "type": "Program", + "start": 0, + "end": 26, + "body": [ + { + "type": "ExportAllDeclaration", + "start": 0, + "end": 26, + "exported": { + "type": "Identifier", + "start": 12, + "end": 15, + "name": "foo" + }, + "source": { + "type": "Literal", + "start": 21, + "end": 26, + "value": "bar", + "raw": "\"bar\"" + } + } + ], + "sourceType": "module" +}, { sourceType: "module", ecmaVersion: 11 }) + +test("export * from \"source\"", { + "type": "Program", + "start": 0, + "end": 22, + "body": [ + { + "type": "ExportAllDeclaration", + "start": 0, + "end": 22, + "exported": null, + "source": { + "type": "Literal", + "start": 14, + "end": 22, + "value": "source", + "raw": "\"source\"" + } + } + ], + "sourceType": "module" +}, { sourceType: "module", ecmaVersion: 11 }) + +testFail("export * as ns from \"source\"", "'import' and 'export' may appear only with 'sourceType: module' (1:0)", { sourceType: "script", ecmaVersion: 11 }) +testFail("export * as ns from \"source\"", "Unexpected token (1:9)", { sourceType: "module", ecmaVersion: 10 }) +testFail("export * as ns", "Unexpected token (1:14)", { sourceType: "module", ecmaVersion: 11 }) +testFail("export * as from \"source\"", "Unexpected token (1:17)", { sourceType: "module", ecmaVersion: 11 }) +testFail("export * as ns \"source\"", "Unexpected token (1:15)", { sourceType: "module", ecmaVersion: 11 }) +testFail("export {} as ns from \"source\"", "Unexpected token (1:10)", { sourceType: "module", ecmaVersion: 11 }) diff --git a/test/tests-harmony.js b/test/tests-harmony.js index ade46a220..7b681e80c 100644 --- a/test/tests-harmony.js +++ b/test/tests-harmony.js @@ -14781,10 +14781,10 @@ test("function foo() { new.target; }", { sourceType: "script" }, {ecmaVersion: 6}); -testFail("new.prop", "The only valid meta property for new is new.target (1:4)", {ecmaVersion: 6}); -testFail("new.target", "new.target can only be used in functions (1:0)", {ecmaVersion: 6}); +testFail("new.prop", "The only valid meta property for new is 'new.target' (1:4)", {ecmaVersion: 6}); +testFail("new.target", "'new.target' can only be used in functions (1:0)", {ecmaVersion: 6}); test("function x() { return () => new.target }", {}, {ecmaVersion: 6}); -testFail("let y = () => new.target", "new.target can only be used in functions (1:14)", {ecmaVersion: 6}); +testFail("let y = () => new.target", "'new.target' can only be used in functions (1:14)", {ecmaVersion: 6}); test("export default function foo() {} false", { body: [ @@ -16512,3 +16512,63 @@ test("let x = 1; x = 2", {}, {ecmaVersion: 6}) test("function *f2() { () => yield / 1 }", {}, {ecmaVersion: 6}) test("({ a = 42, b: c.d } = e)", {}, {ecmaVersion: 6}) + +testFail("({ a = 42, b: c = d })", "Shorthand property assignments are valid only in destructuring patterns (1:5)", {ecmaVersion: 6}) + +test("({ __proto__: x, __proto__: y, __proto__: z }) => {}", {}, {ecmaVersion: 6}) + +// Don't parse first token after a class or strict function as strict +test("class x {}\n05", {}, {ecmaVersion: 6}) +test("function x() { 'use strict' }\n05", {}, {ecmaVersion: 6}) + +test("const myFn = ({ set = '' }) => {};", { + "type": "Program", + "body": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "myFn" + }, + "init": { + "type": "ArrowFunctionExpression", + "params": [ + { + "type": "ObjectPattern", + "properties": [ + { + "type": "Property", + "key": { + "type": "Identifier", + "name": "set" + }, + "kind": "init", + "value": { + "type": "AssignmentPattern", + "left": { + "type": "Identifier", + "name": "set" + }, + "right": { + "type": "Literal", + "value": "" + } + } + } + ] + } + ], + "body": { + "type": "BlockStatement", + "body": [] + } + } + } + ], + "kind": "const" + } + ] +}, {ecmaVersion: 6}) diff --git a/test/tests-import-meta.js b/test/tests-import-meta.js new file mode 100644 index 000000000..7f93d7a3a --- /dev/null +++ b/test/tests-import-meta.js @@ -0,0 +1,97 @@ +// Tests for ECMAScript 2020 `import.meta` + +if (typeof exports != 'undefined') { + var test = require('./driver.js').test; + var testFail = require('./driver.js').testFail; +} + +test( + "import.meta", + { + "type": "Program", + "start": 0, + "end": 11, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 11, + "expression": { + "type": "MetaProperty", + "start": 0, + "end": 11, + "meta": { + "type": "Identifier", + "start": 0, + "end": 6, + "name": "import" + }, + "property": { + "type": "Identifier", + "start": 7, + "end": 11, + "name": "meta" + } + } + } + ], + "sourceType": "module" + }, + { ecmaVersion: 11, sourceType: "module" } +); + +test( + "import.meta.url", + { + "type": "Program", + "start": 0, + "end": 15, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 15, + "expression": { + "type": "MemberExpression", + "start": 0, + "end": 15, + "object": { + "type": "MetaProperty", + "start": 0, + "end": 11, + "meta": { + "type": "Identifier", + "start": 0, + "end": 6, + "name": "import" + }, + "property": { + "type": "Identifier", + "start": 7, + "end": 11, + "name": "meta" + } + }, + "property": { + "type": "Identifier", + "start": 12, + "end": 15, + "name": "url" + }, + "computed": false + } + } + ], + "sourceType": "module" + }, + { ecmaVersion: 11, sourceType: "module" } +); + +testFail("import.meta", "Unexpected token (1:6)", { ecmaVersion: 10, sourceType: "module" }); +testFail("import.meta", "Cannot use 'import.meta' outside a module (1:0)", { ecmaVersion: 11, sourceType: "script" }); +testFail("import['meta']", "Unexpected token (1:6)", { ecmaVersion: 11, sourceType: "module" }); +testFail("a = import['meta']", "Unexpected token (1:10)", { ecmaVersion: 11, sourceType: "module" }); +testFail("import.target", "The only valid meta property for import is 'import.meta' (1:7)", { ecmaVersion: 11, sourceType: "module" }); +testFail("new.meta", "The only valid meta property for new is 'new.target' (1:4)", { ecmaVersion: 11, sourceType: "module" }); +testFail("im\\u0070ort.meta", "Escape sequence in keyword import (1:0)", { ecmaVersion: 11, sourceType: "module" }); +testFail("import.\\u006d\\u0065\\u0074\\u0061", "'import.meta' must not contain escaped characters (1:0)", { ecmaVersion: 11, sourceType: "module" }); diff --git a/test/tests-logical-assignment-operators.js b/test/tests-logical-assignment-operators.js new file mode 100644 index 000000000..2e157f019 --- /dev/null +++ b/test/tests-logical-assignment-operators.js @@ -0,0 +1,182 @@ +// Tests for ECMAScript 2021 `&&=`, `||=`, `??=` + +if (typeof exports != 'undefined') { + var test = require('./driver.js').test; + var testFail = require('./driver.js').testFail; +} + +test( + "a &&= b", + { + "type": "Program", + "start": 0, + "end": 7, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 7, + "expression": { + "type": "AssignmentExpression", + "start": 0, + "end": 7, + "operator": "&&=", + "left": { + "type": "Identifier", + "start": 0, + "end": 1, + "name": "a" + }, + "right": { + "type": "Identifier", + "start": 6, + "end": 7, + "name": "b" + } + } + } + ], + "sourceType": "script" + }, + { ecmaVersion: 12 } +); + +test( + "a ||= b", + { + "type": "Program", + "start": 0, + "end": 7, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 7, + "expression": { + "type": "AssignmentExpression", + "start": 0, + "end": 7, + "operator": "||=", + "left": { + "type": "Identifier", + "start": 0, + "end": 1, + "name": "a" + }, + "right": { + "type": "Identifier", + "start": 6, + "end": 7, + "name": "b" + } + } + } + ], + "sourceType": "script" + }, + { ecmaVersion: 12 } +); + +test( + "a ??= b", + { + "type": "Program", + "start": 0, + "end": 7, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 7, + "expression": { + "type": "AssignmentExpression", + "start": 0, + "end": 7, + "operator": "??=", + "left": { + "type": "Identifier", + "start": 0, + "end": 1, + "name": "a" + }, + "right": { + "type": "Identifier", + "start": 6, + "end": 7, + "name": "b" + } + } + } + ], + "sourceType": "script" + }, + { ecmaVersion: 12 } +); + +test( + "a &&= b ||= c ??= d", + { + "type": "Program", + "start": 0, + "end": 19, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 19, + "expression": { + "type": "AssignmentExpression", + "start": 0, + "end": 19, + "operator": "&&=", + "left": { + "type": "Identifier", + "start": 0, + "end": 1, + "name": "a" + }, + "right": { + "type": "AssignmentExpression", + "start": 6, + "end": 19, + "operator": "||=", + "left": { + "type": "Identifier", + "start": 6, + "end": 7, + "name": "b" + }, + "right": { + "type": "AssignmentExpression", + "start": 12, + "end": 19, + "operator": "??=", + "left": { + "type": "Identifier", + "start": 12, + "end": 13, + "name": "c" + }, + "right": { + "type": "Identifier", + "start": 18, + "end": 19, + "name": "d" + } + } + } + } + } + ], + "sourceType": "script" + }, + { ecmaVersion: 12 } +); + +testFail("a &&= b", "Unexpected token (1:4)", { ecmaVersion: 11 }); +testFail("a ||= b", "Unexpected token (1:4)", { ecmaVersion: 11 }); +testFail("a ??= b", "Unexpected token (1:4)", { ecmaVersion: 11 }); + +testFail("({a} &&= b)", "Assigning to rvalue (1:1)", { ecmaVersion: 12 }); +testFail("({a} ||= b)", "Assigning to rvalue (1:1)", { ecmaVersion: 12 }); +testFail("({a} ??= b)", "Assigning to rvalue (1:1)", { ecmaVersion: 12 }); diff --git a/test/tests-nullish-coalescing.js b/test/tests-nullish-coalescing.js new file mode 100644 index 000000000..6cd14a5d9 --- /dev/null +++ b/test/tests-nullish-coalescing.js @@ -0,0 +1,546 @@ + +if (typeof exports != "undefined") { + var driver = require("./driver.js"); + var test = driver.test, testFail = driver.testFail, testAssert = driver.testAssert, misMatch = driver.misMatch; + var acorn = require("../acorn"); +} + +test("a ?? b", { + "type": "Program", + "start": 0, + "end": 6, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 6, + "expression": { + "type": "LogicalExpression", + "start": 0, + "end": 6, + "left": { + "type": "Identifier", + "start": 0, + "end": 1, + "name": "a" + }, + "operator": "??", + "right": { + "type": "Identifier", + "start": 5, + "end": 6, + "name": "b" + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) +test("a ?? b ?? c", { + "type": "Program", + "start": 0, + "end": 11, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 11, + "expression": { + "type": "LogicalExpression", + "start": 0, + "end": 11, + "left": { + "type": "LogicalExpression", + "start": 0, + "end": 6, + "left": { + "type": "Identifier", + "start": 0, + "end": 1, + "name": "a" + }, + "operator": "??", + "right": { + "type": "Identifier", + "start": 5, + "end": 6, + "name": "b" + } + }, + "operator": "??", + "right": { + "type": "Identifier", + "start": 10, + "end": 11, + "name": "c" + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) + +test("a | b ?? c | d", { + "type": "Program", + "start": 0, + "end": 14, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 14, + "expression": { + "type": "LogicalExpression", + "start": 0, + "end": 14, + "left": { + "type": "BinaryExpression", + "start": 0, + "end": 5, + "left": { + "type": "Identifier", + "start": 0, + "end": 1, + "name": "a" + }, + "operator": "|", + "right": { + "type": "Identifier", + "start": 4, + "end": 5, + "name": "b" + } + }, + "operator": "??", + "right": { + "type": "BinaryExpression", + "start": 9, + "end": 14, + "left": { + "type": "Identifier", + "start": 9, + "end": 10, + "name": "c" + }, + "operator": "|", + "right": { + "type": "Identifier", + "start": 13, + "end": 14, + "name": "d" + } + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) +test("a ?? b ? c : d", { + "type": "Program", + "start": 0, + "end": 14, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 14, + "expression": { + "type": "ConditionalExpression", + "start": 0, + "end": 14, + "test": { + "type": "LogicalExpression", + "start": 0, + "end": 6, + "left": { + "type": "Identifier", + "start": 0, + "end": 1, + "name": "a" + }, + "operator": "??", + "right": { + "type": "Identifier", + "start": 5, + "end": 6, + "name": "b" + } + }, + "consequent": { + "type": "Identifier", + "start": 9, + "end": 10, + "name": "c" + }, + "alternate": { + "type": "Identifier", + "start": 13, + "end": 14, + "name": "d" + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) + +test("(a || b) ?? c", { + "type": "Program", + "start": 0, + "end": 13, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 13, + "expression": { + "type": "LogicalExpression", + "start": 0, + "end": 13, + "left": { + "type": "LogicalExpression", + "start": 1, + "end": 7, + "left": { + "type": "Identifier", + "start": 1, + "end": 2, + "name": "a" + }, + "operator": "||", + "right": { + "type": "Identifier", + "start": 6, + "end": 7, + "name": "b" + } + }, + "operator": "??", + "right": { + "type": "Identifier", + "start": 12, + "end": 13, + "name": "c" + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) +test("a || (b ?? c)", { + "type": "Program", + "start": 0, + "end": 13, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 13, + "expression": { + "type": "LogicalExpression", + "start": 0, + "end": 13, + "left": { + "type": "Identifier", + "start": 0, + "end": 1, + "name": "a" + }, + "operator": "||", + "right": { + "type": "LogicalExpression", + "start": 6, + "end": 12, + "left": { + "type": "Identifier", + "start": 6, + "end": 7, + "name": "b" + }, + "operator": "??", + "right": { + "type": "Identifier", + "start": 11, + "end": 12, + "name": "c" + } + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) + +test("(a && b) ?? c", { + "type": "Program", + "start": 0, + "end": 13, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 13, + "expression": { + "type": "LogicalExpression", + "start": 0, + "end": 13, + "left": { + "type": "LogicalExpression", + "start": 1, + "end": 7, + "left": { + "type": "Identifier", + "start": 1, + "end": 2, + "name": "a" + }, + "operator": "&&", + "right": { + "type": "Identifier", + "start": 6, + "end": 7, + "name": "b" + } + }, + "operator": "??", + "right": { + "type": "Identifier", + "start": 12, + "end": 13, + "name": "c" + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) +test("a && (b ?? c)", { + "type": "Program", + "start": 0, + "end": 13, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 13, + "expression": { + "type": "LogicalExpression", + "start": 0, + "end": 13, + "left": { + "type": "Identifier", + "start": 0, + "end": 1, + "name": "a" + }, + "operator": "&&", + "right": { + "type": "LogicalExpression", + "start": 6, + "end": 12, + "left": { + "type": "Identifier", + "start": 6, + "end": 7, + "name": "b" + }, + "operator": "??", + "right": { + "type": "Identifier", + "start": 11, + "end": 12, + "name": "c" + } + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) + +test("(a ?? b) || c", { + "type": "Program", + "start": 0, + "end": 13, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 13, + "expression": { + "type": "LogicalExpression", + "start": 0, + "end": 13, + "left": { + "type": "LogicalExpression", + "start": 1, + "end": 7, + "left": { + "type": "Identifier", + "start": 1, + "end": 2, + "name": "a" + }, + "operator": "??", + "right": { + "type": "Identifier", + "start": 6, + "end": 7, + "name": "b" + } + }, + "operator": "||", + "right": { + "type": "Identifier", + "start": 12, + "end": 13, + "name": "c" + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) +test("a ?? (b || c)", { + "type": "Program", + "start": 0, + "end": 13, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 13, + "expression": { + "type": "LogicalExpression", + "start": 0, + "end": 13, + "left": { + "type": "Identifier", + "start": 0, + "end": 1, + "name": "a" + }, + "operator": "??", + "right": { + "type": "LogicalExpression", + "start": 6, + "end": 12, + "left": { + "type": "Identifier", + "start": 6, + "end": 7, + "name": "b" + }, + "operator": "||", + "right": { + "type": "Identifier", + "start": 11, + "end": 12, + "name": "c" + } + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) + +test("(a ?? b) && c", { + "type": "Program", + "start": 0, + "end": 13, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 13, + "expression": { + "type": "LogicalExpression", + "start": 0, + "end": 13, + "left": { + "type": "LogicalExpression", + "start": 1, + "end": 7, + "left": { + "type": "Identifier", + "start": 1, + "end": 2, + "name": "a" + }, + "operator": "??", + "right": { + "type": "Identifier", + "start": 6, + "end": 7, + "name": "b" + } + }, + "operator": "&&", + "right": { + "type": "Identifier", + "start": 12, + "end": 13, + "name": "c" + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) +test("a ?? (b && c)", { + "type": "Program", + "start": 0, + "end": 13, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 13, + "expression": { + "type": "LogicalExpression", + "start": 0, + "end": 13, + "left": { + "type": "Identifier", + "start": 0, + "end": 1, + "name": "a" + }, + "operator": "??", + "right": { + "type": "LogicalExpression", + "start": 6, + "end": 12, + "left": { + "type": "Identifier", + "start": 6, + "end": 7, + "name": "b" + }, + "operator": "&&", + "right": { + "type": "Identifier", + "start": 11, + "end": 12, + "name": "c" + } + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) + +testFail("a ?? b", "Unexpected token (1:3)", { ecmaVersion: 10 }) +testFail("?? b", "Unexpected token (1:0)", { ecmaVersion: 11 }) +testFail("a ??", "Unexpected token (1:4)", { ecmaVersion: 11 }) +testFail("a || b ?? c", "Logical expressions and coalesce expressions cannot be mixed. Wrap either by parentheses (1:7)", { ecmaVersion: 11 }) +testFail("a && b ?? c", "Logical expressions and coalesce expressions cannot be mixed. Wrap either by parentheses (1:7)", { ecmaVersion: 11 }) +testFail("a ?? b || c", "Logical expressions and coalesce expressions cannot be mixed. Wrap either by parentheses (1:7)", { ecmaVersion: 11 }) +testFail("a ?? b && c", "Logical expressions and coalesce expressions cannot be mixed. Wrap either by parentheses (1:7)", { ecmaVersion: 11 }) + +testFail("a+1 || b+1 ?? c", "Logical expressions and coalesce expressions cannot be mixed. Wrap either by parentheses (1:11)", { ecmaVersion: 11 }) +testFail("a+1 && b+1 ?? c", "Logical expressions and coalesce expressions cannot be mixed. Wrap either by parentheses (1:11)", { ecmaVersion: 11 }) +testFail("a+1 ?? b+1 || c", "Logical expressions and coalesce expressions cannot be mixed. Wrap either by parentheses (1:11)", { ecmaVersion: 11 }) +testFail("a+1 ?? b+1 && c", "Logical expressions and coalesce expressions cannot be mixed. Wrap either by parentheses (1:11)", { ecmaVersion: 11 }) diff --git a/test/tests-numeric-separators.js b/test/tests-numeric-separators.js new file mode 100644 index 000000000..e28ef5055 --- /dev/null +++ b/test/tests-numeric-separators.js @@ -0,0 +1,199 @@ +// Tests for ECMAScript 2021 Numeric Separators + +if (typeof exports != 'undefined') { + var test = require('./driver.js').test; + var testFail = require('./driver.js').testFail; +} + +function bigint(str) { + if (typeof BigInt !== "function") { + return null + } + return BigInt(str) +} + +test( + "123_456", + { + "type": "Program", + "start": 0, + "end": 7, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 7, + "expression": { + "type": "Literal", + "start": 0, + "end": 7, + "value": 123456, + "raw": "123_456" + } + } + ], + "sourceType": "script" + }, + { ecmaVersion: 12 } +); + +test( + "123_456.123_456e+123_456", + { + "type": "Program", + "start": 0, + "end": 24, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 24, + "expression": { + "type": "Literal", + "start": 0, + "end": 24, + "value": 123456.123456e+123456, + "raw": "123_456.123_456e+123_456" + } + } + ], + "sourceType": "script" + }, + { ecmaVersion: 12 } +); + +test( + "0b1010_0001", + { + "type": "Program", + "start": 0, + "end": 11, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 11, + "expression": { + "type": "Literal", + "start": 0, + "end": 11, + "value": 0b10100001, + "raw": "0b1010_0001" + } + } + ], + "sourceType": "script" + }, + { ecmaVersion: 12 } +); + +test( + "0xDEAD_BEAF", + { + "type": "Program", + "start": 0, + "end": 11, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 11, + "expression": { + "type": "Literal", + "start": 0, + "end": 11, + "value": 0xDEADBEAF, + "raw": "0xDEAD_BEAF" + } + } + ], + "sourceType": "script" + }, + { ecmaVersion: 12 } +); + +test( + "0o755_666", + { + "type": "Program", + "start": 0, + "end": 9, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 9, + "expression": { + "type": "Literal", + "start": 0, + "end": 9, + "value": 0o755666, + "raw": "0o755_666" + } + } + ], + "sourceType": "script" + }, + { ecmaVersion: 12 } +); + +test( + "123_456n", + { + "type": "Program", + "start": 0, + "end": 8, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 8, + "expression": { + "type": "Literal", + "start": 0, + "end": 8, + "value": bigint("123456"), + "raw": "123_456n", + "bigint": "123456" + } + } + ], + "sourceType": "script" + }, + { ecmaVersion: 12 } +); + +test( + ".012_345", + { + "type": "Program", + "start": 0, + "end": 8, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 8, + "expression": { + "type": "Literal", + "start": 0, + "end": 8, + "value": 0.012345, + "raw": ".012_345" + } + } + ], + "sourceType": "script" + }, + { ecmaVersion: 12 } +); + +testFail("123_456", "Identifier directly after number (1:3)", { ecmaVersion: 11 }); +testFail("123__456", "Numeric separator must be exactly one underscore (1:4)", { ecmaVersion: 12 }); +testFail("0._123456", "Numeric separator is not allowed at the first of digits (1:2)", { ecmaVersion: 12 }); +testFail("123456_", "Numeric separator is not allowed at the last of digits (1:6)", { ecmaVersion: 12 }); +testFail("012_345", "Numeric separator is not allowed in legacy octal numeric literals (1:3)", { ecmaVersion: 12 }); + +testFail("'\\x2_0'", "Bad character escape sequence (1:3)", { ecmaVersion: 12 }); +testFail("'\\u00_20'", "Bad character escape sequence (1:3)", { ecmaVersion: 12 }); +testFail("'\\u{2_0}'", "Bad character escape sequence (1:4)", { ecmaVersion: 12 }); diff --git a/test/tests-optional-chaining.js b/test/tests-optional-chaining.js new file mode 100644 index 000000000..18cb1f204 --- /dev/null +++ b/test/tests-optional-chaining.js @@ -0,0 +1,1429 @@ + +if (typeof exports != "undefined") { + var driver = require("./driver.js"); + var test = driver.test, testFail = driver.testFail, testAssert = driver.testAssert, misMatch = driver.misMatch; + var acorn = require("../acorn"); +} + +// Simple +test("obj?.foo", { + "type": "Program", + "start": 0, + "end": 8, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 8, + "expression": { + "type": "ChainExpression", + "start": 0, + "end": 8, + "expression": { + "type": "MemberExpression", + "start": 0, + "end": 8, + "object": { + "type": "Identifier", + "start": 0, + "end": 3, + "name": "obj" + }, + "property": { + "type": "Identifier", + "start": 5, + "end": 8, + "name": "foo" + }, + "computed": false, + "optional": true + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) +test("obj?.[foo]", { + "type": "Program", + "start": 0, + "end": 10, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 10, + "expression": { + "type": "ChainExpression", + "start": 0, + "end": 10, + "expression": { + "type": "MemberExpression", + "start": 0, + "end": 10, + "object": { + "type": "Identifier", + "start": 0, + "end": 3, + "name": "obj" + }, + "property": { + "type": "Identifier", + "start": 6, + "end": 9, + "name": "foo" + }, + "computed": true, + "optional": true + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) +test("obj?.()", { + "type": "Program", + "start": 0, + "end": 7, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 7, + "expression": { + "type": "ChainExpression", + "start": 0, + "end": 7, + "expression": { + "type": "CallExpression", + "start": 0, + "end": 7, + "callee": { + "type": "Identifier", + "start": 0, + "end": 3, + "name": "obj" + }, + "arguments": [], + "optional": true + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) + +// Simple with spacing +test("obj ?. foo", { + "type": "Program", + "start": 0, + "end": 10, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 10, + "expression": { + "type": "ChainExpression", + "start": 0, + "end": 10, + "expression": { + "type": "MemberExpression", + "start": 0, + "end": 10, + "object": { + "type": "Identifier", + "start": 0, + "end": 3, + "name": "obj" + }, + "property": { + "type": "Identifier", + "start": 7, + "end": 10, + "name": "foo" + }, + "computed": false, + "optional": true + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) +test("obj ?. [foo]", { + "type": "Program", + "start": 0, + "end": 12, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 12, + "expression": { + "type": "ChainExpression", + "start": 0, + "end": 12, + "expression": { + "type": "MemberExpression", + "start": 0, + "end": 12, + "object": { + "type": "Identifier", + "start": 0, + "end": 3, + "name": "obj" + }, + "property": { + "type": "Identifier", + "start": 8, + "end": 11, + "name": "foo" + }, + "computed": true, + "optional": true + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) +test("obj ?. ()", { + "type": "Program", + "start": 0, + "end": 9, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 9, + "expression": { + "type": "ChainExpression", + "start": 0, + "end": 9, + "expression": { + "type": "CallExpression", + "start": 0, + "end": 9, + "callee": { + "type": "Identifier", + "start": 0, + "end": 3, + "name": "obj" + }, + "arguments": [], + "optional": true + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) + +// Existing valid syntax +test("obj?.0:.1", { + "type": "Program", + "start": 0, + "end": 9, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 9, + "expression": { + "type": "ConditionalExpression", + "start": 0, + "end": 9, + "test": { + "type": "Identifier", + "start": 0, + "end": 3, + "name": "obj" + }, + "consequent": { + "type": "Literal", + "start": 4, + "end": 6, + "value": 0, + "raw": ".0" + }, + "alternate": { + "type": "Literal", + "start": 7, + "end": 9, + "value": 0.1, + "raw": ".1" + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) + +// Chaining +test("obj?.aaa?.bbb", { + "type": "Program", + "start": 0, + "end": 13, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 13, + "expression": { + "type": "ChainExpression", + "start": 0, + "end": 13, + "expression": { + "type": "MemberExpression", + "start": 0, + "end": 13, + "object": { + "type": "MemberExpression", + "start": 0, + "end": 8, + "object": { + "type": "Identifier", + "start": 0, + "end": 3, + "name": "obj" + }, + "property": { + "type": "Identifier", + "start": 5, + "end": 8, + "name": "aaa" + }, + "computed": false, + "optional": true + }, + "property": { + "type": "Identifier", + "start": 10, + "end": 13, + "name": "bbb" + }, + "computed": false, + "optional": true + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) +test("obj?.aaa.bbb", { + "type": "Program", + "start": 0, + "end": 12, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 12, + "expression": { + "type": "ChainExpression", + "start": 0, + "end": 12, + "expression": { + "type": "MemberExpression", + "start": 0, + "end": 12, + "object": { + "type": "MemberExpression", + "start": 0, + "end": 8, + "object": { + "type": "Identifier", + "start": 0, + "end": 3, + "name": "obj" + }, + "property": { + "type": "Identifier", + "start": 5, + "end": 8, + "name": "aaa" + }, + "computed": false, + "optional": true + }, + "property": { + "type": "Identifier", + "start": 9, + "end": 12, + "name": "bbb" + }, + "computed": false, + "optional": false + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) +test("(obj?.aaa)?.bbb", { + "type": "Program", + "start": 0, + "end": 15, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 15, + "expression": { + "type": "ChainExpression", + "start": 0, + "end": 15, + "expression": { + "type": "MemberExpression", + "start": 0, + "end": 15, + "object": { + "type": "ChainExpression", + "start": 1, + "end": 9, + "expression": { + "type": "MemberExpression", + "start": 1, + "end": 9, + "object": { + "type": "Identifier", + "start": 1, + "end": 4, + "name": "obj" + }, + "property": { + "type": "Identifier", + "start": 6, + "end": 9, + "name": "aaa" + }, + "computed": false, + "optional": true + } + }, + "property": { + "type": "Identifier", + "start": 12, + "end": 15, + "name": "bbb" + }, + "computed": false, + "optional": true + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) +test("(obj?.aaa).bbb", { + "type": "Program", + "start": 0, + "end": 14, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 14, + "expression": { + "type": "MemberExpression", + "start": 0, + "end": 14, + "object": { + "type": "ChainExpression", + "start": 1, + "end": 9, + "expression": { + "type": "MemberExpression", + "start": 1, + "end": 9, + "object": { + "type": "Identifier", + "start": 1, + "end": 4, + "name": "obj" + }, + "property": { + "type": "Identifier", + "start": 6, + "end": 9, + "name": "aaa" + }, + "computed": false, + "optional": true + } + }, + "property": { + "type": "Identifier", + "start": 11, + "end": 14, + "name": "bbb" + }, + "computed": false, + "optional": false + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) +test("(obj?.aaa.bbb).ccc", { + "type": "Program", + "start": 0, + "end": 18, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 18, + "expression": { + "type": "MemberExpression", + "start": 0, + "end": 18, + "object": { + "type": "ChainExpression", + "start": 1, + "end": 13, + "expression": { + "type": "MemberExpression", + "start": 1, + "end": 13, + "object": { + "type": "MemberExpression", + "start": 1, + "end": 9, + "object": { + "type": "Identifier", + "start": 1, + "end": 4, + "name": "obj" + }, + "property": { + "type": "Identifier", + "start": 6, + "end": 9, + "name": "aaa" + }, + "computed": false, + "optional": true + }, + "property": { + "type": "Identifier", + "start": 10, + "end": 13, + "name": "bbb" + }, + "computed": false, + "optional": false + } + }, + "property": { + "type": "Identifier", + "start": 15, + "end": 18, + "name": "ccc" + }, + "computed": false, + "optional": false + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) + +test("func?.()?.bbb", { + "type": "Program", + "start": 0, + "end": 13, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 13, + "expression": { + "type": "ChainExpression", + "start": 0, + "end": 13, + "expression": { + "type": "MemberExpression", + "start": 0, + "end": 13, + "object": { + "type": "CallExpression", + "start": 0, + "end": 8, + "callee": { + "type": "Identifier", + "start": 0, + "end": 4, + "name": "func" + }, + "arguments": [], + "optional": true + }, + "property": { + "type": "Identifier", + "start": 10, + "end": 13, + "name": "bbb" + }, + "computed": false, + "optional": true + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) +test("func?.().bbb", { + "type": "Program", + "start": 0, + "end": 12, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 12, + "expression": { + "type": "ChainExpression", + "start": 0, + "end": 12, + "expression": { + "type": "MemberExpression", + "start": 0, + "end": 12, + "object": { + "type": "CallExpression", + "start": 0, + "end": 8, + "callee": { + "type": "Identifier", + "start": 0, + "end": 4, + "name": "func" + }, + "arguments": [], + "optional": true + }, + "property": { + "type": "Identifier", + "start": 9, + "end": 12, + "name": "bbb" + }, + "computed": false, + "optional": false + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) +test("(func?.())?.bbb", { + "type": "Program", + "start": 0, + "end": 15, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 15, + "expression": { + "type": "ChainExpression", + "start": 0, + "end": 15, + "expression": { + "type": "MemberExpression", + "start": 0, + "end": 15, + "object": { + "type": "ChainExpression", + "start": 1, + "end": 9, + "expression": { + "type": "CallExpression", + "start": 1, + "end": 9, + "callee": { + "type": "Identifier", + "start": 1, + "end": 5, + "name": "func" + }, + "arguments": [], + "optional": true + } + }, + "property": { + "type": "Identifier", + "start": 12, + "end": 15, + "name": "bbb" + }, + "computed": false, + "optional": true + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) +test("(func?.()).bbb", { + "type": "Program", + "start": 0, + "end": 14, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 14, + "expression": { + "type": "MemberExpression", + "start": 0, + "end": 14, + "object": { + "type": "ChainExpression", + "start": 1, + "end": 9, + "expression": { + "type": "CallExpression", + "start": 1, + "end": 9, + "callee": { + "type": "Identifier", + "start": 1, + "end": 5, + "name": "func" + }, + "arguments": [], + "optional": true + } + }, + "property": { + "type": "Identifier", + "start": 11, + "end": 14, + "name": "bbb" + }, + "computed": false, + "optional": false + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) + +test("obj?.aaa?.()", { + "type": "Program", + "start": 0, + "end": 12, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 12, + "expression": { + "type": "ChainExpression", + "start": 0, + "end": 12, + "expression": { + "type": "CallExpression", + "start": 0, + "end": 12, + "callee": { + "type": "MemberExpression", + "start": 0, + "end": 8, + "object": { + "type": "Identifier", + "start": 0, + "end": 3, + "name": "obj" + }, + "property": { + "type": "Identifier", + "start": 5, + "end": 8, + "name": "aaa" + }, + "computed": false, + "optional": true + }, + "arguments": [], + "optional": true + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) +test("obj?.aaa()", { + "type": "Program", + "start": 0, + "end": 10, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 10, + "expression": { + "type": "ChainExpression", + "start": 0, + "end": 10, + "expression": { + "type": "CallExpression", + "start": 0, + "end": 10, + "callee": { + "type": "MemberExpression", + "start": 0, + "end": 8, + "object": { + "type": "Identifier", + "start": 0, + "end": 3, + "name": "obj" + }, + "property": { + "type": "Identifier", + "start": 5, + "end": 8, + "name": "aaa" + }, + "computed": false, + "optional": true + }, + "arguments": [], + "optional": false + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) +test("(obj?.aaa)?.()", { + "type": "Program", + "start": 0, + "end": 14, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 14, + "expression": { + "type": "ChainExpression", + "start": 0, + "end": 14, + "expression": { + "type": "CallExpression", + "start": 0, + "end": 14, + "callee": { + "type": "ChainExpression", + "start": 1, + "end": 9, + "expression": { + "type": "MemberExpression", + "start": 1, + "end": 9, + "object": { + "type": "Identifier", + "start": 1, + "end": 4, + "name": "obj" + }, + "property": { + "type": "Identifier", + "start": 6, + "end": 9, + "name": "aaa" + }, + "computed": false, + "optional": true + } + }, + "arguments": [], + "optional": true + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) +test("(obj?.aaa)()", { + "type": "Program", + "start": 0, + "end": 12, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 12, + "expression": { + "type": "CallExpression", + "start": 0, + "end": 12, + "callee": { + "type": "ChainExpression", + "start": 1, + "end": 9, + "expression": { + "type": "MemberExpression", + "start": 1, + "end": 9, + "object": { + "type": "Identifier", + "start": 1, + "end": 4, + "name": "obj" + }, + "property": { + "type": "Identifier", + "start": 6, + "end": 9, + "name": "aaa" + }, + "computed": false, + "optional": true + } + }, + "arguments": [], + "optional": false + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) + +test("delete obj?.foo", { + "type": "Program", + "start": 0, + "end": 15, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 15, + "expression": { + "type": "UnaryExpression", + "start": 0, + "end": 15, + "operator": "delete", + "prefix": true, + "argument": { + "type": "ChainExpression", + "start": 7, + "end": 15, + "expression": { + "type": "MemberExpression", + "start": 7, + "end": 15, + "object": { + "type": "Identifier", + "start": 7, + "end": 10, + "name": "obj" + }, + "property": { + "type": "Identifier", + "start": 12, + "end": 15, + "name": "foo" + }, + "computed": false, + "optional": true + } + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) + +// OK if paranthesized. +test("new (obj?.foo)()", { + "type": "Program", + "start": 0, + "end": 16, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 16, + "expression": { + "type": "NewExpression", + "start": 0, + "end": 16, + "callee": { + "type": "ChainExpression", + "start": 5, + "end": 13, + "expression": { + "type": "MemberExpression", + "start": 5, + "end": 13, + "object": { + "type": "Identifier", + "start": 5, + "end": 8, + "name": "obj" + }, + "property": { + "type": "Identifier", + "start": 10, + "end": 13, + "name": "foo" + }, + "computed": false, + "optional": true + } + }, + "arguments": [] + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) +test("(obj?.foo)`template`", { + "type": "Program", + "start": 0, + "end": 20, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 20, + "expression": { + "type": "TaggedTemplateExpression", + "start": 0, + "end": 20, + "tag": { + "type": "ChainExpression", + "start": 1, + "end": 9, + "expression": { + "type": "MemberExpression", + "start": 1, + "end": 9, + "object": { + "type": "Identifier", + "start": 1, + "end": 4, + "name": "obj" + }, + "property": { + "type": "Identifier", + "start": 6, + "end": 9, + "name": "foo" + }, + "computed": false, + "optional": true + } + }, + "quasi": { + "type": "TemplateLiteral", + "start": 10, + "end": 20, + "expressions": [], + "quasis": [ + { + "type": "TemplateElement", + "start": 11, + "end": 19, + "value": { + "raw": "template", + "cooked": "template" + }, + "tail": true + } + ] + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) +test("(obj?.foo).bar = 0", { + "type": "Program", + "start": 0, + "end": 18, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 18, + "expression": { + "type": "AssignmentExpression", + "start": 0, + "end": 18, + "operator": "=", + "left": { + "type": "MemberExpression", + "start": 0, + "end": 14, + "object": { + "type": "ChainExpression", + "start": 1, + "end": 9, + "expression": { + "type": "MemberExpression", + "start": 1, + "end": 9, + "object": { + "type": "Identifier", + "start": 1, + "end": 4, + "name": "obj" + }, + "property": { + "type": "Identifier", + "start": 6, + "end": 9, + "name": "foo" + }, + "computed": false, + "optional": true + } + }, + "property": { + "type": "Identifier", + "start": 11, + "end": 14, + "name": "bar" + }, + "computed": false, + "optional": false + }, + "right": { + "type": "Literal", + "start": 17, + "end": 18, + "value": 0, + "raw": "0" + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) +test("(obj?.foo).bar++", { + "type": "Program", + "start": 0, + "end": 16, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 16, + "expression": { + "type": "UpdateExpression", + "start": 0, + "end": 16, + "operator": "++", + "prefix": false, + "argument": { + "type": "MemberExpression", + "start": 0, + "end": 14, + "object": { + "type": "ChainExpression", + "start": 1, + "end": 9, + "expression": { + "type": "MemberExpression", + "start": 1, + "end": 9, + "object": { + "type": "Identifier", + "start": 1, + "end": 4, + "name": "obj" + }, + "property": { + "type": "Identifier", + "start": 6, + "end": 9, + "name": "foo" + }, + "computed": false, + "optional": true + } + }, + "property": { + "type": "Identifier", + "start": 11, + "end": 14, + "name": "bar" + }, + "computed": false, + "optional": false + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) +test("for ((obj?.foo).bar of []);", { + "type": "Program", + "start": 0, + "end": 27, + "body": [ + { + "type": "ForOfStatement", + "start": 0, + "end": 27, + "await": false, + "left": { + "type": "MemberExpression", + "start": 5, + "end": 19, + "object": { + "type": "ChainExpression", + "start": 6, + "end": 14, + "expression": { + "type": "MemberExpression", + "start": 6, + "end": 14, + "object": { + "type": "Identifier", + "start": 6, + "end": 9, + "name": "obj" + }, + "property": { + "type": "Identifier", + "start": 11, + "end": 14, + "name": "foo" + }, + "computed": false, + "optional": true + } + }, + "property": { + "type": "Identifier", + "start": 16, + "end": 19, + "name": "bar" + }, + "computed": false, + "optional": false + }, + "right": { + "type": "ArrayExpression", + "start": 23, + "end": 25, + "elements": [] + }, + "body": { + "type": "EmptyStatement", + "start": 26, + "end": 27 + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) + +// With ParenthesizedExpression +test("(obj?.aaa).bbb", { + "type": "Program", + "start": 0, + "end": 14, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 14, + "expression": { + "type": "MemberExpression", + "start": 0, + "end": 14, + "object": { + "type": "ParenthesizedExpression", + "start": 0, + "end": 10, + "expression": { + "type": "ChainExpression", + "start": 1, + "end": 9, + "expression": { + "type": "MemberExpression", + "start": 1, + "end": 9, + "object": { + "type": "Identifier", + "start": 1, + "end": 4, + "name": "obj" + }, + "property": { + "type": "Identifier", + "start": 6, + "end": 9, + "name": "aaa" + }, + "computed": false, + "optional": true + } + } + }, + "property": { + "type": "Identifier", + "start": 11, + "end": 14, + "name": "bbb" + }, + "computed": false, + "optional": false + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11, preserveParens: true }) + +// No ChainExpression if `callee|object` doesn't contain `?.` even if it's parenthesized. +test("(obj.foo.bar)?.buz", { + "type": "Program", + "start": 0, + "end": 18, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 18, + "expression": { + "type": "ChainExpression", + "start": 0, + "end": 18, + "expression": { + "type": "MemberExpression", + "start": 0, + "end": 18, + "object": { + "type": "MemberExpression", + "start": 1, + "end": 12, + "object": { + "type": "MemberExpression", + "start": 1, + "end": 8, + "object": { + "type": "Identifier", + "start": 1, + "end": 4, + "name": "obj" + }, + "property": { + "type": "Identifier", + "start": 5, + "end": 8, + "name": "foo" + }, + "computed": false, + "optional": false + }, + "property": { + "type": "Identifier", + "start": 9, + "end": 12, + "name": "bar" + }, + "computed": false, + "optional": false + }, + "property": { + "type": "Identifier", + "start": 15, + "end": 18, + "name": "buz" + }, + "computed": false, + "optional": true + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) +test("(obj.foo())?.buz", { + "type": "Program", + "start": 0, + "end": 16, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 16, + "expression": { + "type": "ChainExpression", + "start": 0, + "end": 16, + "expression": { + "type": "MemberExpression", + "start": 0, + "end": 16, + "object": { + "type": "CallExpression", + "start": 1, + "end": 10, + "callee": { + "type": "MemberExpression", + "start": 1, + "end": 8, + "object": { + "type": "Identifier", + "start": 1, + "end": 4, + "name": "obj" + }, + "property": { + "type": "Identifier", + "start": 5, + "end": 8, + "name": "foo" + }, + "computed": false, + "optional": false + }, + "arguments": [], + "optional": false + }, + "property": { + "type": "Identifier", + "start": 13, + "end": 16, + "name": "buz" + }, + "computed": false, + "optional": true + } + } + } + ], + "sourceType": "script" +}, { ecmaVersion: 11 }) + +// With an old ecmaVersion. +testFail("obj?.foo", "Unexpected token (1:4)", { ecmaVersion: 10 }) +testFail("obj?.[foo]", "Unexpected token (1:4)", { ecmaVersion: 10 }) +testFail("obj?.()", "Unexpected token (1:4)", { ecmaVersion: 10 }) + +// Syntax errors. +testFail("obj?.0", "Unexpected token (1:6)", { ecmaVersion: 11 }) +testFail("async?.() => {}", "Unexpected token (1:10)", { ecmaVersion: 11 }) +testFail("new obj?.()", "Optional chaining cannot appear in the callee of new expressions (1:7)", { ecmaVersion: 11 }) +testFail("new obj?.foo()", "Optional chaining cannot appear in the callee of new expressions (1:7)", { ecmaVersion: 11 }) +testFail("obj?.foo\n`template`", "Optional chaining cannot appear in the tag of tagged template expressions (2:0)", { ecmaVersion: 11 }) +testFail("obj?.foo = 0", "Optional chaining cannot appear in left-hand side (1:0)", { ecmaVersion: 11 }) +testFail("obj?.foo.bar = 0", "Optional chaining cannot appear in left-hand side (1:0)", { ecmaVersion: 11 }) +testFail("obj?.().foo = 0", "Optional chaining cannot appear in left-hand side (1:0)", { ecmaVersion: 11 }) +testFail("obj?.foo++", "Optional chaining cannot appear in left-hand side (1:0)", { ecmaVersion: 11 }) +testFail("obj?.foo--", "Optional chaining cannot appear in left-hand side (1:0)", { ecmaVersion: 11 }) +testFail("++obj?.foo", "Optional chaining cannot appear in left-hand side (1:2)", { ecmaVersion: 11 }) +testFail("--obj?.foo", "Optional chaining cannot appear in left-hand side (1:2)", { ecmaVersion: 11 }) +testFail("obj?.foo.bar++", "Optional chaining cannot appear in left-hand side (1:0)", { ecmaVersion: 11 }) +testFail("for (obj?.foo in {});", "Optional chaining cannot appear in left-hand side (1:5)", { ecmaVersion: 11 }) +testFail("for (obj?.foo.bar in {});", "Optional chaining cannot appear in left-hand side (1:5)", { ecmaVersion: 11 }) +testFail("for (obj?.foo of []);", "Optional chaining cannot appear in left-hand side (1:5)", { ecmaVersion: 11 }) +testFail("for (obj?.foo.bar of []);", "Optional chaining cannot appear in left-hand side (1:5)", { ecmaVersion: 11 }) diff --git a/test/tests-regexp-2020.js b/test/tests-regexp-2020.js new file mode 100644 index 000000000..7caff8f2a --- /dev/null +++ b/test/tests-regexp-2020.js @@ -0,0 +1,21 @@ +if (typeof exports != "undefined") { + var test = require("./driver.js").test + var testFail = require("./driver.js").testFail +} + +// https://github.com/tc39/ecma262/pull/1869 +testFail("/(?<\\ud835\\udc9c>.)/", "Invalid regular expression: /(?<\\ud835\\udc9c>.)/: Invalid capture group name (1:1)", { ecmaVersion: 2019 }) +test("/(?<\\ud835\\udc9c>.)/", {}, { ecmaVersion: 2020 }) +test("/(?<\\ud835\\udc9c>.)/u", {}, { ecmaVersion: 2019 }) +test("/(?<\\ud835\\udc9c>.)/u", {}, { ecmaVersion: 2020 }) + +testFail("/(?<\\u{1d49c}>.)/", "Invalid regular expression: /(?<\\u{1d49c}>.)/: Invalid capture group name (1:1)", { ecmaVersion: 2019 }) +test("/(?<\\u{1d49c}>.)/", {}, { ecmaVersion: 2020 }) +test("/(?<\\u{1d49c}>.)/u", {}, { ecmaVersion: 2019 }) +test("/(?<\\u{1d49c}>.)/u", {}, { ecmaVersion: 2020 }) + +testFail("/(?<𝒜>.)/", "Invalid regular expression: /(?<𝒜>.)/: Invalid capture group name (1:1)", { ecmaVersion: 2019 }) +test("/(?<𝒜>.)/", {}, { ecmaVersion: 2020 }) +test("/(?<𝒜>.)/u", {}, { ecmaVersion: 2019 }) +test("/(?<𝒜>.)/u", {}, { ecmaVersion: 2020 }) + diff --git a/test/tests-regexp.js b/test/tests-regexp.js index 6c4719486..804e00a59 100644 --- a/test/tests-regexp.js +++ b/test/tests-regexp.js @@ -1049,6 +1049,7 @@ test("/[\\d][\\12-\\14]{1,}[^\\d]/", {}, { ecmaVersion: 2015 }) testFail("/[\\d][\\12-\\14]{1,}[^\\d]/u", "Invalid regular expression flag (1:1)", { ecmaVersion: 5 }) testFail("/[\\d][\\12-\\14]{1,}[^\\d]/u", "Invalid regular expression: /[\\d][\\12-\\14]{1,}[^\\d]/: Invalid class escape (1:1)", { ecmaVersion: 2015 }) test("/([a ]\\b)*\\b/", {}, { ecmaVersion: 5 }) +test("/[x-*]/u".replace("*", String.fromCharCode(0xd800)), {}, {ecmaVersion: 6}) /* // This is test case generator. diff --git a/test/tests.js b/test/tests.js index 35fe29ca5..0a0031484 100644 --- a/test/tests.js +++ b/test/tests.js @@ -29428,7 +29428,7 @@ test("(\\u0061sync ())", { }, {ecmaVersion: 8}) testFail("({ \\u0061sync x() { await x } })", "Unexpected token (1:14)", {ecmaVersion: 8}) testFail("for (x \\u006ff y) {}", "Unexpected token (1:7)", {ecmaVersion: 6}) -testFail("function x () { new.ta\\u0072get }", "The only valid meta property for new is new.target (1:20)", {ecmaVersion: 6}) +testFail("function x () { new.ta\\u0072get }", "'new.target' must not contain escaped characters (1:16)", {ecmaVersion: 6}) testFail("class X { st\\u0061tic y() {} }", "Unexpected token (1:22)", {ecmaVersion: 6}) testFail("(x=1)=2", "Parenthesized pattern (1:0)") @@ -29464,3 +29464,16 @@ test("/**/ --> comment\n", {}) test("x.class++", {}) testFail("½", "Unexpected character '½' (1:0)") + +// Ignore use-strict strings that aren't a stand-along expression +test("'use strict'.foo; 05", {}, {ecmaVersion: 6}) +test("'use strict'\n.foo; 05", {}, {ecmaVersion: 6}) +test("\"use strict\"(foo); 05", {}, {ecmaVersion: 6}) +test("'use strict'`foo`; 05", {}, {ecmaVersion: 6}) +test("'use strict'\n+ 10; 05", {}, {ecmaVersion: 6}) +test("'use strict'\n!=10; 05", {}, {ecmaVersion: 6}) + +// Don't ignore those that are, even with semicolon insertion +testFail("\"use strict\"\nfoo\n05", "Invalid number (3:0)", {ecmaVersion: 6}) +testFail("\"use strict\"\n;(foo)\n05", "Invalid number (3:0)", {ecmaVersion: 6}) +testFail("'use strict'\n!blah; 05", "Invalid number (2:7)", {ecmaVersion: 6})