From a25cb31b9db9128e69bcf70ee7b9e15baf265dff Mon Sep 17 00:00:00 2001 From: Tom Jenkinson Date: Mon, 31 Jan 2022 20:38:27 +0000 Subject: [PATCH 1/6] enable strict mode --- parser.js | 1 + 1 file changed, 1 insertion(+) diff --git a/parser.js b/parser.js index e6c601f..bdbd6bf 100644 --- a/parser.js +++ b/parser.js @@ -208,6 +208,7 @@ // one of & - ! # % , : ; < = > @ _ ` ~ // +"use strict"; (function() { var fromCodePoint = String.fromCodePoint || (function() { From 6ed94ca1cb7600ecdab496251bf67313cf76d155 Mon Sep 17 00:00:00 2001 From: Tom Jenkinson Date: Mon, 31 Jan 2022 20:38:52 +0000 Subject: [PATCH 2/6] declare `res` --- parser.js | 1 + test/test-data.json | 80 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/parser.js b/parser.js index bdbd6bf..e3e2dea 100644 --- a/parser.js +++ b/parser.js @@ -575,6 +575,7 @@ // If no unicode flag, then try to parse ExtendedAtom -> ExtendedPatternCharacter. // ExtendedPatternCharacter + var res; if (!isUnicodeMode && (res = matchReg(/^{/))) { atom = createCharacter(res); } else { diff --git a/test/test-data.json b/test/test-data.json index 53ca0ed..e6ec676 100644 --- a/test/test-data.json +++ b/test/test-data.json @@ -38409,5 +38409,85 @@ 6 ], "raw": "([\\1])" + }, + "a{1,,2}": { + "type": "alternative", + "body": [ + { + "type": "value", + "kind": "symbol", + "codePoint": 97, + "range": [ + 0, + 1 + ], + "raw": "a" + }, + { + "type": "value", + "kind": "symbol", + "codePoint": 123, + "range": [ + 1, + 2 + ], + "raw": "{" + }, + { + "type": "value", + "kind": "symbol", + "codePoint": 49, + "range": [ + 2, + 3 + ], + "raw": "1" + }, + { + "type": "value", + "kind": "symbol", + "codePoint": 44, + "range": [ + 3, + 4 + ], + "raw": "," + }, + { + "type": "value", + "kind": "symbol", + "codePoint": 44, + "range": [ + 4, + 5 + ], + "raw": "," + }, + { + "type": "value", + "kind": "symbol", + "codePoint": 50, + "range": [ + 5, + 6 + ], + "raw": "2" + }, + { + "type": "value", + "kind": "symbol", + "codePoint": 125, + "range": [ + 6, + 7 + ], + "raw": "}" + } + ], + "range": [ + 0, + 7 + ], + "raw": "a{1,,2}" } } From 72a8616f1569c58407a7516708fff0596a390792 Mon Sep 17 00:00:00 2001 From: Tom Jenkinson Date: Mon, 31 Jan 2022 20:47:31 +0000 Subject: [PATCH 3/6] configure eslint with some general rules and fix issues --- .eslintrc.js | 14 ++++++++++++++ demo/main.js | 2 +- package.json | 2 ++ parser.js | 10 +++++----- test/index.js | 3 +-- tools/generate-identifier-regex.js | 1 - 6 files changed, 23 insertions(+), 9 deletions(-) create mode 100644 .eslintrc.js diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 0000000..7e6e769 --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,14 @@ +module.exports = { + extends: ["eslint:recommended"], + + env: { + browser: true, + node: true, + }, + + rules: { + "no-cond-assign": ["off"], + "no-useless-escape": ["off"], + "no-empty": ["off"], + }, +}; diff --git a/demo/main.js b/demo/main.js index 2c57905..010f20e 100644 --- a/demo/main.js +++ b/demo/main.js @@ -46,7 +46,7 @@ } permalink.hash = encode('/' + regexSource + '/' + regexFlags); storage && (storage.regexSource = regexSource) && (storage.regexFlags = regexFlags); - }; + } regex.oninput = flags.oninput = update; diff --git a/package.json b/package.json index 080fae0..8ff3b48 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "url": "git@github.com:jviereck/regjsparser.git" }, "scripts": { + "lint": "eslint --ext .js --max-warnings 0 .", "test": "run-p test:*", "test:src": "node test/index.js", "test:types": "tsc test/types.ts --noEmit" @@ -27,6 +28,7 @@ "jsesc": "~0.5.0" }, "devDependencies": { + "eslint": "^8.8.0", "npm-run-all": "^4.1.5", "regenerate": "~1.0.1", "typescript": "^4.5.2", diff --git a/parser.js b/parser.js index e3e2dea..247b284 100644 --- a/parser.js +++ b/parser.js @@ -563,11 +563,12 @@ } var atom = parseAtomAndExtendedAtom(); + var quantifier; if (!atom) { // Check if a quantifier is following. A quantifier without an atom // is an error. var pos_backup = pos - var quantifier = parseQuantifier() || false; + quantifier = parseQuantifier() || false; if (quantifier) { pos = pos_backup bail('Expected atom'); @@ -582,7 +583,7 @@ bail('Expected atom'); } } - var quantifier = parseQuantifier() || false; + quantifier = parseQuantifier() || false; if (quantifier) { quantifier.body = flattenBody(atom); // The quantifier contains the atom. Therefore, the beginning of the @@ -634,7 +635,6 @@ // \ B // ( ? = Disjunction ) // ( ? ! Disjunction ) - var res, from = pos; if (match('^')) { return createAnchor('start', 1 /* rawLength */); @@ -1081,6 +1081,7 @@ // Taken from the Esprima parser. function isIdentifierPart(ch) { // Generated by `tools/generate-identifier-regex.js`. + // eslint-disable-next-line no-misleading-character-class var NonAsciiIdentifierPartOnly = /[0-9_\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-\u0EB9\u0EBB\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\u1CF2-\u1CF4\u1CF7-\u1CF9\u1DC0-\u1DF9\u1DFB-\u1DFF\u200C\u200D\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]|\uD800[\uDDFD\uDEE0\uDF76-\uDF7A]|\uD801[\uDCA0-\uDCA9]|\uD802[\uDE01-\uDE03\uDE05\uDE06\uDE0C-\uDE0F\uDE38-\uDE3A\uDE3F\uDEE5\uDEE6]|\uD803[\uDD24-\uDD27\uDD30-\uDD39\uDF46-\uDF50]|\uD804[\uDC00-\uDC02\uDC38-\uDC46\uDC66-\uDC6F\uDC7F-\uDC82\uDCB0-\uDCBA\uDCF0-\uDCF9\uDD00-\uDD02\uDD27-\uDD34\uDD36-\uDD3F\uDD45\uDD46\uDD73\uDD80-\uDD82\uDDB3-\uDDC0\uDDC9-\uDDCC\uDDD0-\uDDD9\uDE2C-\uDE37\uDE3E\uDEDF-\uDEEA\uDEF0-\uDEF9\uDF00-\uDF03\uDF3B\uDF3C\uDF3E-\uDF44\uDF47\uDF48\uDF4B-\uDF4D\uDF57\uDF62\uDF63\uDF66-\uDF6C\uDF70-\uDF74]|\uD805[\uDC35-\uDC46\uDC50-\uDC59\uDC5E\uDCB0-\uDCC3\uDCD0-\uDCD9\uDDAF-\uDDB5\uDDB8-\uDDC0\uDDDC\uDDDD\uDE30-\uDE40\uDE50-\uDE59\uDEAB-\uDEB7\uDEC0-\uDEC9\uDF1D-\uDF2B\uDF30-\uDF39]|\uD806[\uDC2C-\uDC3A\uDCE0-\uDCE9\uDE01-\uDE0A\uDE33-\uDE39\uDE3B-\uDE3E\uDE47\uDE51-\uDE5B\uDE8A-\uDE99]|\uD807[\uDC2F-\uDC36\uDC38-\uDC3F\uDC50-\uDC59\uDC92-\uDCA7\uDCA9-\uDCB6\uDD31-\uDD36\uDD3A\uDD3C\uDD3D\uDD3F-\uDD45\uDD47\uDD50-\uDD59\uDD8A-\uDD8E\uDD90\uDD91\uDD93-\uDD97\uDDA0-\uDDA9\uDEF3-\uDEF6]|\uD81A[\uDE60-\uDE69\uDEF0-\uDEF4\uDF30-\uDF36\uDF50-\uDF59]|\uD81B[\uDF51-\uDF7E\uDF8F-\uDF92]|\uD82F[\uDC9D\uDC9E]|\uD834[\uDD65-\uDD69\uDD6D-\uDD72\uDD7B-\uDD82\uDD85-\uDD8B\uDDAA-\uDDAD\uDE42-\uDE44]|\uD835[\uDFCE-\uDFFF]|\uD836[\uDE00-\uDE36\uDE3B-\uDE6C\uDE75\uDE84\uDE9B-\uDE9F\uDEA1-\uDEAF]|\uD838[\uDC00-\uDC06\uDC08-\uDC18\uDC1B-\uDC21\uDC23\uDC24\uDC26-\uDC2A]|\uD83A[\uDCD0-\uDCD6\uDD44-\uDD4A\uDD50-\uDD59]|\uDB40[\uDD00-\uDDEF]/; return isIdentifierStart(ch) || @@ -1299,7 +1300,6 @@ var body = []; var kind; - var from = pos; var operand = parseClassOperand(/* allowRanges*/ true); body.push(operand); @@ -1435,7 +1435,7 @@ var res; if (res = matchReg(/^[^()[\]{}/\-\\|]/)) { return createCharacter(res); - }; + } } function parseClassCharacterEscapedHelper() { diff --git a/test/index.js b/test/index.js index 37d8154..5ae49f3 100644 --- a/test/index.js +++ b/test/index.js @@ -1,4 +1,3 @@ -var fs = require('fs'); var jsesc = require('jsesc'); var parse = require('../parser').parse; @@ -13,7 +12,7 @@ var stringify = function(obj) { var runTests = function(data_path, flags, features) { console.log('Testing:', data_path); - data = require(data_path) + var data = require(data_path) Object.keys(data).forEach(function(regex) { var results = data[regex]; flags || (flags = ''); diff --git a/tools/generate-identifier-regex.js b/tools/generate-identifier-regex.js index a060397..1873c92 100644 --- a/tools/generate-identifier-regex.js +++ b/tools/generate-identifier-regex.js @@ -13,7 +13,6 @@ var get = function(what) { // Get the Unicode properties needed to construct the regex. var ID_Start = get('Binary_Property/ID_Start'); var ID_Continue = get('Binary_Property/ID_Continue'); -var Other_ID_Start = get('Binary_Property/Other_ID_Start'); var generateRegex = function() { // https://tc39.github.io/ecma262/#sec-identifier-names-static-semantics-early-errors From 7a83c1f50a1f8bcea2569a8114e44e9d992485aa Mon Sep 17 00:00:00 2001 From: Tom Jenkinson Date: Mon, 31 Jan 2022 20:48:40 +0000 Subject: [PATCH 4/6] add some more missing `res` declarations --- parser.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/parser.js b/parser.js index 247b284..48dd54e 100644 --- a/parser.js +++ b/parser.js @@ -1417,6 +1417,7 @@ // \ b if (match('\\')) { + var res; if (res = parseClassCharacterEscapedHelper()) { return res; } else { @@ -1445,6 +1446,7 @@ // \ ClassHalfOfDouble // \ b + var res; if (match('b')) { return createEscaped('singleEscape', 0x0008, '\\b'); } else if (match('B')) { From 4576fb2be3caa012ffeee076c30a1c71a0b3f935 Mon Sep 17 00:00:00 2001 From: Tom Jenkinson Date: Mon, 31 Jan 2022 20:50:02 +0000 Subject: [PATCH 5/6] set missing `from` --- parser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parser.js b/parser.js index 48dd54e..fd40a56 100644 --- a/parser.js +++ b/parser.js @@ -1417,7 +1417,7 @@ // \ b if (match('\\')) { - var res; + var res, from = pos; if (res = parseClassCharacterEscapedHelper()) { return res; } else { From 9c98fc374c14c7ae808c1fd129f8f4a6bd0247e3 Mon Sep 17 00:00:00 2001 From: Tom Jenkinson Date: Mon, 31 Jan 2022 20:53:21 +0000 Subject: [PATCH 6/6] run lint after tests --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8ff3b48..2d17f13 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ }, "scripts": { "lint": "eslint --ext .js --max-warnings 0 .", - "test": "run-p test:*", + "test": "run-p test:* && npm run lint", "test:src": "node test/index.js", "test:types": "tsc test/types.ts --noEmit" },