diff --git a/lib/convert.js b/lib/convert.js index 8bd3cf7..d9d7626 100644 --- a/lib/convert.js +++ b/lib/convert.js @@ -1507,17 +1507,28 @@ module.exports = function convert(config) { }); break; - case SyntaxKind.RegularExpressionLiteral: + case SyntaxKind.RegularExpressionLiteral: { + const pattern = node.text.slice(1, node.text.lastIndexOf("/")); + const flags = node.text.slice(node.text.lastIndexOf("/") + 1); + + let regex = null; + try { + regex = new RegExp(pattern, flags); + } catch (exception) { + regex = null; + } + Object.assign(result, { type: AST_NODE_TYPES.Literal, - value: Number(node.text), + value: regex, raw: node.text, regex: { - pattern: node.text.slice(1, node.text.lastIndexOf("/")), - flags: node.text.slice(node.text.lastIndexOf("/") + 1) + pattern, + flags } }); break; + } case SyntaxKind.TrueKeyword: Object.assign(result, { diff --git a/tests/fixtures/ecma-features/regex/regexp-simple.result.js b/tests/fixtures/ecma-features/regex/regexp-simple.result.js new file mode 100644 index 0000000..e1d4f35 --- /dev/null +++ b/tests/fixtures/ecma-features/regex/regexp-simple.result.js @@ -0,0 +1,194 @@ +module.exports = { + "type": "Program", + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "range": [ + 0, + 17 + ], + "body": [ + { + "type": "VariableDeclaration", + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "range": [ + 0, + 17 + ], + "declarations": [ + { + "type": "VariableDeclarator", + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "range": [ + 4, + 16 + ], + "id": { + "type": "Identifier", + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "range": [ + 4, + 7 + ], + "name": "foo" + }, + "init": { + "type": "Literal", + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "range": [ + 10, + 16 + ], + "value": {}, + "raw": "/foo./", + "regex": { + "pattern": "foo.", + "flags": "" + } + } + } + ], + "kind": "var" + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Keyword", + "value": "var", + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 3 + } + }, + "range": [ + 0, + 3 + ] + }, + { + "type": "Identifier", + "value": "foo", + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "range": [ + 4, + 7 + ] + }, + { + "type": "Punctuator", + "value": "=", + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "range": [ + 8, + 9 + ] + }, + { + "type": "RegularExpression", + "value": "/foo./", + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "range": [ + 10, + 16 + ], + "regex": { + "flags": "", + "pattern": "foo." + } + }, + { + "type": "Punctuator", + "value": ";", + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "range": [ + 16, + 17 + ] + } + ] +}; diff --git a/tests/fixtures/ecma-features/regex/regexp-simple.src.js b/tests/fixtures/ecma-features/regex/regexp-simple.src.js new file mode 100644 index 0000000..68f882c --- /dev/null +++ b/tests/fixtures/ecma-features/regex/regexp-simple.src.js @@ -0,0 +1 @@ +var foo = /foo./; diff --git a/tests/fixtures/ecma-features/regexUFlag/regex-u-extended-escape.supported.result.js b/tests/fixtures/ecma-features/regexUFlag/regex-u-extended-escape.supported.result.js new file mode 100644 index 0000000..739842f --- /dev/null +++ b/tests/fixtures/ecma-features/regexUFlag/regex-u-extended-escape.supported.result.js @@ -0,0 +1,194 @@ +module.exports = { + "type": "Program", + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 41 + } + }, + "range": [ + 0, + 41 + ], + "body": [ + { + "type": "VariableDeclaration", + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 41 + } + }, + "range": [ + 0, + 41 + ], + "declarations": [ + { + "type": "VariableDeclarator", + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 40 + } + }, + "range": [ + 4, + 40 + ], + "id": { + "type": "Identifier", + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 5 + } + }, + "range": [ + 4, + 5 + ], + "name": "x" + }, + "init": { + "type": "Literal", + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 40 + } + }, + "range": [ + 8, + 40 + ], + "value": {}, + "raw": "/[\\u{0000000000000061}-\\u{7A}]/u", + "regex": { + "pattern": "[\\u{0000000000000061}-\\u{7A}]", + "flags": "u" + } + } + } + ], + "kind": "var" + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Keyword", + "value": "var", + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 3 + } + }, + "range": [ + 0, + 3 + ] + }, + { + "type": "Identifier", + "value": "x", + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 5 + } + }, + "range": [ + 4, + 5 + ] + }, + { + "type": "Punctuator", + "value": "=", + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "range": [ + 6, + 7 + ] + }, + { + "type": "RegularExpression", + "value": "/[\\u{0000000000000061}-\\u{7A}]/u", + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 40 + } + }, + "range": [ + 8, + 40 + ], + "regex": { + "flags": "u", + "pattern": "[\\u{0000000000000061}-\\u{7A}]" + } + }, + { + "type": "Punctuator", + "value": ";", + "loc": { + "start": { + "line": 1, + "column": 40 + }, + "end": { + "line": 1, + "column": 41 + } + }, + "range": [ + 40, + 41 + ] + } + ] +}; diff --git a/tests/fixtures/ecma-features/regexUFlag/regex-u-extended-escape.result.js b/tests/fixtures/ecma-features/regexUFlag/regex-u-extended-escape.unsupported.result.js similarity index 100% rename from tests/fixtures/ecma-features/regexUFlag/regex-u-extended-escape.result.js rename to tests/fixtures/ecma-features/regexUFlag/regex-u-extended-escape.unsupported.result.js diff --git a/tests/fixtures/ecma-features/regexUFlag/regex-u-simple.supported.result.js b/tests/fixtures/ecma-features/regexUFlag/regex-u-simple.supported.result.js new file mode 100644 index 0000000..8a2cb69 --- /dev/null +++ b/tests/fixtures/ecma-features/regexUFlag/regex-u-simple.supported.result.js @@ -0,0 +1,194 @@ +module.exports = { + "type": "Program", + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "range": [ + 0, + 17 + ], + "body": [ + { + "type": "VariableDeclaration", + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "range": [ + 0, + 17 + ], + "declarations": [ + { + "type": "VariableDeclarator", + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "range": [ + 4, + 16 + ], + "id": { + "type": "Identifier", + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "range": [ + 4, + 7 + ], + "name": "foo" + }, + "init": { + "type": "Literal", + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "range": [ + 10, + 16 + ], + "value": {}, + "raw": "/foo/u", + "regex": { + "pattern": "foo", + "flags": "u" + } + } + } + ], + "kind": "var" + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Keyword", + "value": "var", + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 3 + } + }, + "range": [ + 0, + 3 + ] + }, + { + "type": "Identifier", + "value": "foo", + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "range": [ + 4, + 7 + ] + }, + { + "type": "Punctuator", + "value": "=", + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "range": [ + 8, + 9 + ] + }, + { + "type": "RegularExpression", + "value": "/foo/u", + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "range": [ + 10, + 16 + ], + "regex": { + "flags": "u", + "pattern": "foo" + } + }, + { + "type": "Punctuator", + "value": ";", + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "range": [ + 16, + 17 + ] + } + ] +}; diff --git a/tests/fixtures/ecma-features/regexUFlag/regex-u-simple.result.js b/tests/fixtures/ecma-features/regexUFlag/regex-u-simple.unsupported.result.js similarity index 100% rename from tests/fixtures/ecma-features/regexUFlag/regex-u-simple.result.js rename to tests/fixtures/ecma-features/regexUFlag/regex-u-simple.unsupported.result.js diff --git a/tests/fixtures/ecma-features/regexYFlag/regexp-y-simple.supported.result.js b/tests/fixtures/ecma-features/regexYFlag/regexp-y-simple.supported.result.js new file mode 100644 index 0000000..cf9a425 --- /dev/null +++ b/tests/fixtures/ecma-features/regexYFlag/regexp-y-simple.supported.result.js @@ -0,0 +1,194 @@ +module.exports = { + "type": "Program", + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "range": [ + 0, + 17 + ], + "body": [ + { + "type": "VariableDeclaration", + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "range": [ + 0, + 17 + ], + "declarations": [ + { + "type": "VariableDeclarator", + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "range": [ + 4, + 16 + ], + "id": { + "type": "Identifier", + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "range": [ + 4, + 7 + ], + "name": "foo" + }, + "init": { + "type": "Literal", + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "range": [ + 10, + 16 + ], + "value": {}, + "raw": "/foo/y", + "regex": { + "pattern": "foo", + "flags": "y" + } + } + } + ], + "kind": "var" + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Keyword", + "value": "var", + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 3 + } + }, + "range": [ + 0, + 3 + ] + }, + { + "type": "Identifier", + "value": "foo", + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "range": [ + 4, + 7 + ] + }, + { + "type": "Punctuator", + "value": "=", + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "range": [ + 8, + 9 + ] + }, + { + "type": "RegularExpression", + "value": "/foo/y", + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "range": [ + 10, + 16 + ], + "regex": { + "flags": "y", + "pattern": "foo" + } + }, + { + "type": "Punctuator", + "value": ";", + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "range": [ + 16, + 17 + ] + } + ] +}; diff --git a/tests/fixtures/ecma-features/regexYFlag/regexp-y-simple.result.js b/tests/fixtures/ecma-features/regexYFlag/regexp-y-simple.unsupported.result.js similarity index 100% rename from tests/fixtures/ecma-features/regexYFlag/regexp-y-simple.result.js rename to tests/fixtures/ecma-features/regexYFlag/regexp-y-simple.unsupported.result.js diff --git a/tests/lib/ecma-features.js b/tests/lib/ecma-features.js index b698ac9..51a1c65 100644 --- a/tests/lib/ecma-features.js +++ b/tests/lib/ecma-features.js @@ -74,7 +74,26 @@ describe("ecmaFeatures", () => { it(`should parse correctly when ${feature} is true`, () => { config.ecmaFeatures[feature] = true; - const expected = require(`${path.resolve(__dirname, "../../", FIXTURES_DIR, filename)}.result.js`); + let expected = null; + + const regexFilenames = [ + "regexYFlag/regexp-y-simple", + "regexUFlag/regex-u-simple", + "regexUFlag/regex-u-extended-escape" + ]; + if (regexFilenames.indexOf(filename) !== -1) { + const nodeVersions = process.versions; + const nodeVersionParts = nodeVersions.node.split("."); + const nodeMajorVersion = parseInt(nodeVersionParts[0], 10); + + if (nodeMajorVersion >= 6) { + expected = require(`${path.resolve(__dirname, "../../", FIXTURES_DIR, filename)}.supported.result.js`); + } else { + expected = require(`${path.resolve(__dirname, "../../", FIXTURES_DIR, filename)}.unsupported.result.js`); + } + } else { + expected = require(`${path.resolve(__dirname, "../../", FIXTURES_DIR, filename)}.result.js`); + } let result; try {