From e207bd703e761d5565ee59d15253260ae8e90a79 Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Thu, 24 Jun 2021 11:38:03 -0700 Subject: [PATCH] Update: Support top-level await and regexp match indices (#505) * Update: Support top-level await and regexp match indices * Fix failing test * Fix test * Clean up regex test * Update readme * Update tests/fixtures/ecma-version/13/regexp-match-indices/regexp-match-indices.result.js Co-authored-by: Milos Djermanovic * Update tests/fixtures/ecma-version/13/regexp-match-indices/regexp-match-indices.result.js Co-authored-by: Milos Djermanovic * Ensure top-level await test only runs in modules * Cleanup file structure * Update Acorn Co-authored-by: Milos Djermanovic --- README.md | 2 + package.json | 2 +- .../top-level-await/top-await.result.js | 183 ++++++++++++++++ .../modules/top-level-await/top-await.src.js | 1 + .../regexp-match-indices.result.js | 198 ++++++++++++++++++ .../regexp-match-indices.src.js | 1 + tests/lib/conditional-regex-value.js | 44 +++- 7 files changed, 429 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/ecma-version/13/modules/top-level-await/top-await.result.js create mode 100644 tests/fixtures/ecma-version/13/modules/top-level-await/top-await.src.js create mode 100644 tests/fixtures/ecma-version/13/regexp-match-indices/regexp-match-indices.result.js create mode 100644 tests/fixtures/ecma-version/13/regexp-match-indices/regexp-match-indices.src.js diff --git a/README.md b/README.md index a13abb91..d4c49c75 100644 --- a/README.md +++ b/README.md @@ -236,6 +236,8 @@ Because ECMAScript 2022 is still under development, we are implementing features * [Class instance fields](https://github.com/tc39/proposal-class-fields) * [Class private instance methods and accessors](https://github.com/tc39/proposal-private-methods) * [Class static fields, static private methods and accessors](https://github.com/tc39/proposal-static-class-features) +* [RegExp match indices](https://github.com/tc39/proposal-regexp-match-indices) +* [Top-level await](https://github.com/tc39/proposal-top-level-await) See [finished-proposals.md](https://github.com/tc39/proposals/blob/master/finished-proposals.md) to know what features are finalized. diff --git a/package.json b/package.json index 128db95a..10031411 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ }, "license": "BSD-2-Clause", "dependencies": { - "acorn": "^8.2.2", + "acorn": "^8.4.1", "acorn-jsx": "^5.3.1", "eslint-visitor-keys": "^2.1.0" }, diff --git a/tests/fixtures/ecma-version/13/modules/top-level-await/top-await.result.js b/tests/fixtures/ecma-version/13/modules/top-level-await/top-await.result.js new file mode 100644 index 00000000..d7b70cf0 --- /dev/null +++ b/tests/fixtures/ecma-version/13/modules/top-level-await/top-await.result.js @@ -0,0 +1,183 @@ +export default { + "type": "Program", + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "range": [ + 0, + 12 + ], + "body": [ + { + "type": "ExpressionStatement", + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "range": [ + 0, + 12 + ], + "expression": { + "type": "AwaitExpression", + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "range": [ + 0, + 11 + ], + "argument": { + "type": "CallExpression", + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "range": [ + 6, + 11 + ], + "callee": { + "type": "Identifier", + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "range": [ + 6, + 9 + ], + "name": "foo" + }, + "arguments": [], + "optional": false + } + } + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Identifier", + "value": "await", + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 5 + } + }, + "range": [ + 0, + 5 + ] + }, + { + "type": "Identifier", + "value": "foo", + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "range": [ + 6, + 9 + ] + }, + { + "type": "Punctuator", + "value": "(", + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "range": [ + 9, + 10 + ] + }, + { + "type": "Punctuator", + "value": ")", + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "range": [ + 10, + 11 + ] + }, + { + "type": "Punctuator", + "value": ";", + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "range": [ + 11, + 12 + ] + } + ] +}; \ No newline at end of file diff --git a/tests/fixtures/ecma-version/13/modules/top-level-await/top-await.src.js b/tests/fixtures/ecma-version/13/modules/top-level-await/top-await.src.js new file mode 100644 index 00000000..41756a59 --- /dev/null +++ b/tests/fixtures/ecma-version/13/modules/top-level-await/top-await.src.js @@ -0,0 +1 @@ +await foo(); diff --git a/tests/fixtures/ecma-version/13/regexp-match-indices/regexp-match-indices.result.js b/tests/fixtures/ecma-version/13/regexp-match-indices/regexp-match-indices.result.js new file mode 100644 index 00000000..d30ace1d --- /dev/null +++ b/tests/fixtures/ecma-version/13/regexp-match-indices/regexp-match-indices.result.js @@ -0,0 +1,198 @@ +import conditionalRegex from "../../../../lib/conditional-regex-value.js"; + +export default { + "type": "Program", + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "range": [ + 0, + 25 + ], + "body": [ + { + "type": "VariableDeclaration", + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "range": [ + 0, + 25 + ], + "declarations": [ + { + "type": "VariableDeclarator", + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "range": [ + 6, + 24 + ], + "id": { + "type": "Identifier", + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "range": [ + 6, + 9 + ], + "name": "re1" + }, + "init": conditionalRegex({ + "type": "Literal", + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "range": [ + 12, + 24 + ], + + // /d flag no supported in Node.js 12 or 14 yet + "value": null, + "raw": "/a+(z)?/d", + "regex": { + "pattern": "a+(z)?", + "flags": "d" + } + }) + } + ], + "kind": "const" + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Keyword", + "value": "const", + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 5 + } + }, + "range": [ + 0, + 5 + ] + }, + { + "type": "Identifier", + "value": "re1", + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "range": [ + 6, + 9 + ] + }, + { + "type": "Punctuator", + "value": "=", + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "range": [ + 10, + 11 + ] + }, + { + "type": "RegularExpression", + "value": "/a+(z)?/d", + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "range": [ + 12, + 24 + ], + "regex": { + "flags": "d", + "pattern": "a+(z)?" + } + }, + { + "type": "Punctuator", + "value": ";", + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "range": [ + 24, + 25 + ] + } + ] +}; diff --git a/tests/fixtures/ecma-version/13/regexp-match-indices/regexp-match-indices.src.js b/tests/fixtures/ecma-version/13/regexp-match-indices/regexp-match-indices.src.js new file mode 100644 index 00000000..0c0d5c29 --- /dev/null +++ b/tests/fixtures/ecma-version/13/regexp-match-indices/regexp-match-indices.src.js @@ -0,0 +1 @@ +const re1 = /a+(z)?/d; diff --git a/tests/lib/conditional-regex-value.js b/tests/lib/conditional-regex-value.js index e2d568b2..192df527 100644 --- a/tests/lib/conditional-regex-value.js +++ b/tests/lib/conditional-regex-value.js @@ -1,4 +1,17 @@ -// eslint-disable-next-line jsdoc/require-jsdoc +/** + * @fileoverview A function for working regex flags in tests. + * @author Mike Reinstein + */ + +/** + * Given an AST node representing a regular expression, this function + * determines whether or not to create a RegExp object to use in the + * value property of the node. This is because not all Node.js versions + * support all RegExp flags, in which case the value property should be + * null. + * @param {ASTNode} literalNode The node to work on. + * @returns {ASTNode} The node that was passed in. + */ export default function(literalNode) { if (literalNode.regex) { try { @@ -10,3 +23,32 @@ export default function(literalNode) { } return literalNode; } + +/* +Example usage: + +conditionalRegex({ + "type": "Literal", + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "range": [ + 10, + 16 + ], + "value": null, + "raw": "/foo/y", + "regex": { + "pattern": "foo", + "flags": "y" + } +}) + +*/