From db440feef4c38b56cfa213c784832a3fdf15cb18 Mon Sep 17 00:00:00 2001 From: Denis Pushkarev Date: Mon, 22 Mar 2021 16:17:25 +0700 Subject: [PATCH] improve linting settings --- .eslintrc.js | 12 +++++++++++- package.json | 4 ++-- .../internals/fix-regexp-well-known-symbol-logic.js | 1 + tests/compat/tests.js | 1 + tests/tests/es.string.replace.js | 6 ++++++ 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 6fee4956cb55..f790bf959e10 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -379,6 +379,8 @@ const base = { 'unicorn/no-hex-escape': 'error', // disallow if as the only statement in an else block 'unicorn/no-lonely-if': 'error', + // forbid classes that only have static members + 'unicorn/no-static-only-class': 'error', // disallow unreadable array destructuring 'unicorn/no-unreadable-array-destructuring': 'error', // disallow unsafe regular expressions @@ -457,12 +459,18 @@ const base = { 'regexp/no-escape-backspace': 'error', // disallow invisible raw character 'regexp/no-invisible-character': 'error', + // disallow legacy RegExp features + 'regexp/no-legacy-features': 'error', // disallow octal escape sequence 'regexp/no-octal': 'error', + // disallow unused capturing group + 'regexp/no-unused-capturing-group': 'error', // disallow useless backreferences in regular expressions 'regexp/no-useless-backreference': 'error', // disallow character class with one character 'regexp/no-useless-character-class': 'error', + // disallow useless `$` replacements in replacement string + 'regexp/no-useless-dollar-replacements': 'error', // disallow unnecessary string escaping 'regexp/no-useless-escape': 'error', // disallow unnecessary exactly quantifier @@ -479,8 +487,10 @@ const base = { 'regexp/order-in-character-class': 'error', // enforce using character class 'regexp/prefer-character-class': 'error', - // enforce using '\d' + // enforce using `\d` 'regexp/prefer-d': 'error', + // enforces escape of replacement `$` character (`$$`) + 'regexp/prefer-escape-replacement-dollar-char': 'error', // enforce using `+` quantifier 'regexp/prefer-plus-quantifier': 'error', // enforce using quantifier diff --git a/package.json b/package.json index a8ef5cde1610..3ad2973e7d28 100644 --- a/package.json +++ b/package.json @@ -40,8 +40,8 @@ "eslint-plugin-optimize-regex": "^1.2.0", "eslint-plugin-qunit": "^6.0.0", "eslint-plugin-radar": "~0.2.1", - "eslint-plugin-regexp": "~0.5.0", - "eslint-plugin-unicorn": "^28.0.2", + "eslint-plugin-regexp": "~0.6.1", + "eslint-plugin-unicorn": "^29.0.0", "karma": "^6.2.0", "karma-chrome-launcher": "^3.1.0", "karma-phantomjs-launcher": "~1.0.4", diff --git a/packages/core-js/internals/fix-regexp-well-known-symbol-logic.js b/packages/core-js/internals/fix-regexp-well-known-symbol-logic.js index d3e0f410e81a..4565bc84ecd6 100644 --- a/packages/core-js/internals/fix-regexp-well-known-symbol-logic.js +++ b/packages/core-js/internals/fix-regexp-well-known-symbol-logic.js @@ -25,6 +25,7 @@ var REPLACE_SUPPORTS_NAMED_GROUPS = !fails(function () { // IE <= 11 replaces $0 with the whole match, as if it was $& // https://stackoverflow.com/questions/6024666/getting-ie-to-replace-a-regex-with-the-literal-string-0 var REPLACE_KEEPS_$0 = (function () { + // eslint-disable-next-line regexp/prefer-escape-replacement-dollar-char -- required for testing return 'a'.replace(/./, '$0') === '$0'; })(); diff --git a/tests/compat/tests.js b/tests/compat/tests.js index c2751cd56a2d..24791933c478 100644 --- a/tests/compat/tests.js +++ b/tests/compat/tests.js @@ -870,6 +870,7 @@ GLOBAL.tests = { return ''.replace(O) == 7 && execCalled && ''.replace(re2, '$') === '7' + // eslint-disable-next-line regexp/prefer-escape-replacement-dollar-char -- required for testing && 'a'.replace(/./, '$0') === '$0' && /./[Symbol.replace]('a', '$0') === '$0'; }, diff --git a/tests/tests/es.string.replace.js b/tests/tests/es.string.replace.js index 34fad021aa60..45d3455ab17a 100644 --- a/tests/tests/es.string.replace.js +++ b/tests/tests/es.string.replace.js @@ -1,3 +1,4 @@ +/* eslint-disable regexp/no-unused-capturing-group -- required for testibg */ import { GLOBAL, NATIVE, STRICT } from '../helpers/constants'; import { patchRegExp$exec } from '../helpers/helpers'; @@ -124,7 +125,9 @@ const run = assert => { assert.strictEqual('aaaaaaaaaa,aaaaaaaaaaaaaaa'.replace(/^(a+)\1*,\1+$/, '$1'), 'aaaaa', 'S15.5.4.11_A5_T1'); // https://github.com/zloirock/core-js/issues/471 + // eslint-disable-next-line regexp/no-useless-dollar-replacements -- required for testing assert.strictEqual('{price} Retail'.replace(/{price}/g, '$25.00'), '$25.00 Retail'); + // eslint-disable-next-line regexp/prefer-escape-replacement-dollar-char -- required for testing assert.strictEqual('a'.replace(/(.)/, '$0'), '$0'); }; @@ -203,12 +206,15 @@ QUnit.test('RegExp#@@replace correctly handles substitutions', assert => { result.index = 1; return result; }; + // eslint-disable-next-line regexp/no-useless-dollar-replacements -- false positive assert.strictEqual('1234'.replace(re, '$1'), '174'); + // eslint-disable-next-line regexp/no-useless-dollar-replacements -- required for testing assert.strictEqual('1234'.replace(re, '$'), '174'); assert.strictEqual('1234'.replace(re, '$`'), '114'); assert.strictEqual('1234'.replace(re, '$\''), '144'); assert.strictEqual('1234'.replace(re, '$$'), '1$4'); assert.strictEqual('1234'.replace(re, '$&'), '1234'); + // eslint-disable-next-line regexp/prefer-escape-replacement-dollar-char -- required for testing assert.strictEqual('1234'.replace(re, '$x'), '1$x4'); let args;