From 13308b2bee96cd78c1b02c529bca114f790ae55f Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Sat, 31 Dec 2016 21:52:56 -0800 Subject: [PATCH 1/4] tools: remove no-useless-regex-char-class-escape The `no-useless-regex-char-class-escape` custom lint rule was introduced as a less aggressive alternative to some enhancements that were introduced into ESLint. Those enhancements were blocking us from updating ESLint. However, they have since been relaxed and the custom rule is no longer needed. Remove it. PR-URL: https://github.com/nodejs/node/pull/10561 Reviewed-By: Teddy Katz Reviewed-By: James M Snell Reviewed-By: Sam Roberts --- .eslintrc.yaml | 1 - .../no-useless-regex-char-class-escape.js | 190 ------------------ 2 files changed, 191 deletions(-) delete mode 100644 tools/eslint-rules/no-useless-regex-char-class-escape.js diff --git a/.eslintrc.yaml b/.eslintrc.yaml index 97b67ff89bd49b..10acbae6e1f67b 100644 --- a/.eslintrc.yaml +++ b/.eslintrc.yaml @@ -162,7 +162,6 @@ rules: # Custom rules in tools/eslint-rules align-multiline-assignment: 2 assert-throws-arguments: [2, { requireTwo: true }] - no-useless-regex-char-class-escape: [2, { override: ['[', ']'] }] # Global scoped method and vars globals: diff --git a/tools/eslint-rules/no-useless-regex-char-class-escape.js b/tools/eslint-rules/no-useless-regex-char-class-escape.js deleted file mode 100644 index 934a3fa193b506..00000000000000 --- a/tools/eslint-rules/no-useless-regex-char-class-escape.js +++ /dev/null @@ -1,190 +0,0 @@ -/** - * @fileoverview Disallow useless escape in regex character class - * Based on 'no-useless-escape' rule - */ -'use strict'; - -//------------------------------------------------------------------------------ -// Rule Definition -//------------------------------------------------------------------------------ - -const REGEX_CHARCLASS_ESCAPES = new Set('\\bcdDfnrsStvwWxu0123456789]'); - -/** - * Parses a regular expression into a list of regex character class list. - * @param {string} regExpText raw text used to create the regular expression - * @returns {Object[]} A list of character classes tokens with index and - * escape info - * @example - * - * parseRegExpCharClass('a\\b[cd-]') - * - * returns: - * [ - * { - * empty: false, - * start: 4, - * end: 6, - * chars: [ - * {text: 'c', index: 4, escaped: false}, - * {text: 'd', index: 5, escaped: false}, - * {text: '-', index: 6, escaped: false} - * ] - * } - * ] - */ - -function parseRegExpCharClass(regExpText) { - const charList = []; - let charListIdx = -1; - const initState = { - escapeNextChar: false, - inCharClass: false, - startingCharClass: false - }; - - regExpText.split('').reduce((state, char, index) => { - if (!state.escapeNextChar) { - if (char === '\\') { - return Object.assign(state, { escapeNextChar: true }); - } - if (char === '[' && !state.inCharClass) { - charListIdx += 1; - charList.push({ start: index + 1, chars: [], end: -1 }); - return Object.assign(state, { - inCharClass: true, - startingCharClass: true - }); - } - if (char === ']' && state.inCharClass) { - const charClass = charList[charListIdx]; - charClass.empty = charClass.chars.length === 0; - if (charClass.empty) { - charClass.start = charClass.end = -1; - } else { - charList[charListIdx].end = index - 1; - } - return Object.assign(state, { - inCharClass: false, - startingCharClass: false - }); - } - } - if (state.inCharClass) { - charList[charListIdx].chars.push({ - text: char, - index, escaped: - state.escapeNextChar - }); - } - return Object.assign(state, { - escapeNextChar: false, - startingCharClass: false - }); - }, initState); - - return charList; -} - -module.exports = { - meta: { - docs: { - description: 'disallow unnecessary regex characer class escape sequences', - category: 'Best Practices', - recommended: false - }, - fixable: 'code', - schema: [{ - 'type': 'object', - 'properties': { - 'override': { - 'type': 'array', - 'items': { 'type': 'string' }, - 'uniqueItems': true - } - }, - 'additionalProperties': false - }] - }, - - create(context) { - const overrideSet = new Set(context.options.length - ? context.options[0].override || [] - : []); - - /** - * Reports a node - * @param {ASTNode} node The node to report - * @param {number} startOffset The backslash's offset - * from the start of the node - * @param {string} character The uselessly escaped character - * (not including the backslash) - * @returns {void} - */ - function report(node, startOffset, character) { - context.report({ - node, - loc: { - line: node.loc.start.line, - column: node.loc.start.column + startOffset - }, - message: 'Unnecessary regex escape in character' + - ' class: \\{{character}}', - data: { character }, - fix: (fixer) => { - const start = node.range[0] + startOffset; - return fixer.replaceTextRange([start, start + 1], ''); - } - }); - } - - /** - * Checks if a node has superflous escape character - * in regex character class. - * - * @param {ASTNode} node - node to check. - * @returns {void} - */ - function check(node) { - if (node.regex) { - parseRegExpCharClass(node.regex.pattern) - .forEach((charClass) => { - charClass - .chars - // The '-' character is a special case if is not at - // either edge of the character class. To account for this, - // filter out '-' characters that appear in the middle of a - // character class. - .filter((charInfo) => !(charInfo.text === '-' && - (charInfo.index !== charClass.start && - charInfo.index !== charClass.end))) - - // The '^' character is a special case if it's at the beginning - // of the character class. To account for this, filter out '^' - // characters that appear at the start of a character class. - // - .filter((charInfo) => !(charInfo.text === '^' && - charInfo.index === charClass.start)) - - // Filter out characters that aren't escaped. - .filter((charInfo) => charInfo.escaped) - - // Filter out characters that are valid to escape, based on - // their position in the regular expression. - .filter((charInfo) => !REGEX_CHARCLASS_ESCAPES.has(charInfo.text)) - - // Filter out overridden character list. - .filter((charInfo) => !overrideSet.has(charInfo.text)) - - // Report all the remaining characters. - .forEach((charInfo) => - report(node, charInfo.index, charInfo.text)); - }); - } - } - - return { - Literal: check - }; - } -}; From 8ab455e727a88ae8493b317693e47e17b29a9f6f Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Sat, 17 Jun 2017 22:35:39 -0700 Subject: [PATCH 2/4] tools: fix indentation in required-modules.js In preparation for applying the more strict indentation linting available in ESLint 4.0.0, correct minor indentation issues in tools/eslint-rules/required-modules.js. This is the only file with indentation that does not conform to the stricter checks. PR-URL: https://github.com/nodejs/node/pull/13758 Reviewed-By: Teddy Katz Reviewed-By: Vse Mozhet Byt Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Michael Dawson --- tools/eslint-rules/required-modules.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/eslint-rules/required-modules.js b/tools/eslint-rules/required-modules.js index 3e4a8e8aadfc82..7691d5dd7d91a0 100644 --- a/tools/eslint-rules/required-modules.js +++ b/tools/eslint-rules/required-modules.js @@ -77,13 +77,13 @@ module.exports = function(context) { function(module) { return foundModules.indexOf(module === -1); } - ); + ); missingModules.forEach(function(moduleName) { context.report( node, 'Mandatory module "{{moduleName}}" must be loaded.', { moduleName: moduleName } - ); + ); }); } } From 1ebad50509ac051bdb7debb6ee5478d70a6f7ff1 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Sat, 17 Jun 2017 22:37:09 -0700 Subject: [PATCH 3/4] tools: apply stricter indentation rules to tools ESLint 4.0.0 provides stricter (and more granular) indentation checking than previous versions. Apply the stricter indentation rules to the tools directory. PR-URL: https://github.com/nodejs/node/pull/13758 Reviewed-By: Teddy Katz Reviewed-By: Vse Mozhet Byt Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Michael Dawson --- tools/.eslintrc.yaml | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 tools/.eslintrc.yaml diff --git a/tools/.eslintrc.yaml b/tools/.eslintrc.yaml new file mode 100644 index 00000000000000..e1405dd718bf0f --- /dev/null +++ b/tools/.eslintrc.yaml @@ -0,0 +1,12 @@ +## Tools-specific linter rules + +rules: + # Stylistic Issues + # http://eslint.org/docs/rules/#stylistic-issues + indent: [2, 2, {ArrayExpression: first, + CallExpression: {arguments: first}, + FunctionDeclaration: {parameters: first}, + FunctionExpression: {parameters: first}, + MemberExpression: off, + ObjectExpression: first, + SwitchCase: 1}] From 1088e81fc6b50333d8bc27411307c607b8e473e5 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Mon, 19 Jun 2017 11:13:30 -0700 Subject: [PATCH 4/4] tools: fix error in custom ESLint rule Fix previously-unnoticed typo in `required-modules.js`. Refs: https://github.com/nodejs/node/pull/13758#discussion_r122582786 PR-URL: https://github.com/nodejs/node/pull/13758 Reviewed-By: Teddy Katz Reviewed-By: Vse Mozhet Byt Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Michael Dawson --- tools/eslint-rules/required-modules.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/eslint-rules/required-modules.js b/tools/eslint-rules/required-modules.js index 7691d5dd7d91a0..4a444809b7115c 100644 --- a/tools/eslint-rules/required-modules.js +++ b/tools/eslint-rules/required-modules.js @@ -75,7 +75,7 @@ module.exports = function(context) { if (foundModules.length < requiredModules.length) { var missingModules = requiredModules.filter( function(module) { - return foundModules.indexOf(module === -1); + return foundModules.indexOf(module) === -1; } ); missingModules.forEach(function(moduleName) {