From a38fd08f02c1f62017682df02255ac74aaa7246d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=AF=E7=84=B6?= Date: Sun, 16 May 2021 13:38:18 +0800 Subject: [PATCH 1/2] Chore: upgrade eslint-plugin-eslint-plugin --- .eslintrc.js | 17 +---------------- lib/rules/dot-notation.js | 28 ++++++++++++++++------------ lib/rules/prefer-arrow-callback.js | 27 +++++++++++++++------------ package.json | 2 +- 4 files changed, 33 insertions(+), 41 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 1457d9b553c..668bc2747c5 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -71,22 +71,7 @@ module.exports = { "eslint-plugin/report-message-format": ["error", "[^a-z].*\\.$"], "eslint-plugin/require-meta-docs-description": "error", "eslint-plugin/require-meta-type": "error", - "eslint-plugin/test-case-property-ordering": [ - "error", - - // https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/issues/79 - [ - "filename", - "code", - "output", - "options", - "parser", - "parserOptions", - "globals", - "env", - "errors" - ] - ], + "eslint-plugin/test-case-property-ordering": "error", "eslint-plugin/test-case-shorthand-strings": "error", "internal-rules/multiline-comment-style": "error" }, diff --git a/lib/rules/dot-notation.js b/lib/rules/dot-notation.js index 751b4628edc..cbfd854da15 100644 --- a/lib/rules/dot-notation.js +++ b/lib/rules/dot-notation.js @@ -87,27 +87,28 @@ module.exports = { data: { key: formattedValue }, - *fix(fixer) { + fix(fixer) { + const fixes = []; const leftBracket = sourceCode.getTokenAfter(node.object, astUtils.isOpeningBracketToken); const rightBracket = sourceCode.getLastToken(node); const nextToken = sourceCode.getTokenAfter(node); // Don't perform any fixes if there are comments inside the brackets. if (sourceCode.commentsExistBetween(leftBracket, rightBracket)) { - return; // eslint-disable-line eslint-plugin/fixer-return -- false positive + return null; } // Replace the brackets by an identifier. if (!node.optional) { - yield fixer.insertTextBefore( + fixes.push(fixer.insertTextBefore( leftBracket, astUtils.isDecimalInteger(node.object) ? " ." : "." - ); + )); } - yield fixer.replaceTextRange( + fixes.push(fixer.replaceTextRange( [leftBracket.range[0], rightBracket.range[1]], value - ); + )); // Insert a space after the property if it will be connected to the next token. if ( @@ -115,8 +116,9 @@ module.exports = { rightBracket.range[1] === nextToken.range[0] && !astUtils.canTokensBeAdjacent(String(value), nextToken) ) { - yield fixer.insertTextAfter(node, " "); + fixes.push(fixer.insertTextAfter(node, " ")); } + return fixes; } }); } @@ -149,24 +151,26 @@ module.exports = { data: { key: node.property.name }, - *fix(fixer) { + fix(fixer) { + const fixes = []; const dotToken = sourceCode.getTokenBefore(node.property); // A statement that starts with `let[` is parsed as a destructuring variable declaration, not a MemberExpression. if (node.object.type === "Identifier" && node.object.name === "let" && !node.optional) { - return; // eslint-disable-line eslint-plugin/fixer-return -- false positive + return null; } // Don't perform any fixes if there are comments between the dot and the property name. if (sourceCode.commentsExistBetween(dotToken, node.property)) { - return; // eslint-disable-line eslint-plugin/fixer-return -- false positive + return null; } // Replace the identifier to brackets. if (!node.optional) { - yield fixer.remove(dotToken); + fixes.push(fixer.remove(dotToken)); } - yield fixer.replaceText(node.property, `["${node.property.name}"]`); + fixes.push(fixer.replaceText(node.property, `["${node.property.name}"]`)); + return fixes; } }); } diff --git a/lib/rules/prefer-arrow-callback.js b/lib/rules/prefer-arrow-callback.js index ee5cfe3c8c7..060e7633163 100644 --- a/lib/rules/prefer-arrow-callback.js +++ b/lib/rules/prefer-arrow-callback.js @@ -286,7 +286,9 @@ module.exports = { context.report({ node, messageId: "preferArrowCallback", - *fix(fixer) { + fix(fixer) { + const fixes = []; + if ((!callbackInfo.isLexicalThis && scopeInfo.this) || hasDuplicateParams(node.params)) { /* @@ -295,7 +297,7 @@ module.exports = { * If the callback function has duplicates in its list of parameters (possible in sloppy mode), * don't replace it with an arrow function, because this is a SyntaxError with arrow functions. */ - return; // eslint-disable-line eslint-plugin/fixer-return -- false positive + return null; } // Remove `.bind(this)` if exists. @@ -307,7 +309,7 @@ module.exports = { * E.g. `(foo || function(){}).bind(this)` */ if (memberNode.type !== "MemberExpression") { - return; // eslint-disable-line eslint-plugin/fixer-return -- false positive + return null; } const callNode = memberNode.parent; @@ -320,15 +322,15 @@ module.exports = { * ^^^^^^^^^^^^ */ if (astUtils.isParenthesised(sourceCode, memberNode)) { - return; // eslint-disable-line eslint-plugin/fixer-return -- false positive + return null; } // If comments exist in the `.bind(this)`, don't remove those. if (sourceCode.commentsExistBetween(firstTokenToRemove, lastTokenToRemove)) { - return; // eslint-disable-line eslint-plugin/fixer-return -- false positive + return null; } - yield fixer.removeRange([firstTokenToRemove.range[0], lastTokenToRemove.range[1]]); + fixes.push(fixer.removeRange([firstTokenToRemove.range[0], lastTokenToRemove.range[1]])); } // Convert the function expression to an arrow function. @@ -338,16 +340,16 @@ module.exports = { if (sourceCode.commentsExistBetween(functionToken, leftParenToken)) { // Remove only extra tokens to keep comments. - yield fixer.remove(functionToken); + fixes.push(fixer.remove(functionToken)); if (node.id) { - yield fixer.remove(node.id); + fixes.push(fixer.remove(node.id)); } } else { // Remove extra tokens and spaces. - yield fixer.removeRange([functionToken.range[0], leftParenToken.range[0]]); + fixes.push(fixer.removeRange([functionToken.range[0], leftParenToken.range[0]])); } - yield fixer.insertTextBefore(node.body, "=> "); + fixes.push(fixer.insertTextBefore(node.body, "=> ")); // Get the node that will become the new arrow function. let replacedNode = callbackInfo.isLexicalThis ? node.parent.parent : node; @@ -367,9 +369,10 @@ module.exports = { !astUtils.isParenthesised(sourceCode, replacedNode) && !astUtils.isParenthesised(sourceCode, node) ) { - yield fixer.insertTextBefore(replacedNode, "("); - yield fixer.insertTextAfter(replacedNode, ")"); + fixes.push(fixer.insertTextBefore(replacedNode, "(")); + fixes.push(fixer.insertTextAfter(replacedNode, ")")); } + return fixes; } }); } diff --git a/package.json b/package.json index 51a76ec31c8..2e12782f294 100644 --- a/package.json +++ b/package.json @@ -94,7 +94,7 @@ "escape-string-regexp": "^3.0.0", "eslint": "file:.", "eslint-config-eslint": "file:packages/eslint-config-eslint", - "eslint-plugin-eslint-plugin": "^2.2.1", + "eslint-plugin-eslint-plugin": "^3.0.3", "eslint-plugin-internal-rules": "file:tools/internal-rules", "eslint-plugin-jsdoc": "^25.4.3", "eslint-plugin-node": "^11.1.0", From abdf83173638361b53112546a90a7b6bd7d5ef4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=AF=E7=84=B6?= Date: Tue, 25 May 2021 11:56:17 +0800 Subject: [PATCH 2/2] chore: revert generator fixers --- lib/rules/dot-notation.js | 28 ++++++++++++---------------- lib/rules/prefer-arrow-callback.js | 27 ++++++++++++--------------- 2 files changed, 24 insertions(+), 31 deletions(-) diff --git a/lib/rules/dot-notation.js b/lib/rules/dot-notation.js index cbfd854da15..751b4628edc 100644 --- a/lib/rules/dot-notation.js +++ b/lib/rules/dot-notation.js @@ -87,28 +87,27 @@ module.exports = { data: { key: formattedValue }, - fix(fixer) { - const fixes = []; + *fix(fixer) { const leftBracket = sourceCode.getTokenAfter(node.object, astUtils.isOpeningBracketToken); const rightBracket = sourceCode.getLastToken(node); const nextToken = sourceCode.getTokenAfter(node); // Don't perform any fixes if there are comments inside the brackets. if (sourceCode.commentsExistBetween(leftBracket, rightBracket)) { - return null; + return; // eslint-disable-line eslint-plugin/fixer-return -- false positive } // Replace the brackets by an identifier. if (!node.optional) { - fixes.push(fixer.insertTextBefore( + yield fixer.insertTextBefore( leftBracket, astUtils.isDecimalInteger(node.object) ? " ." : "." - )); + ); } - fixes.push(fixer.replaceTextRange( + yield fixer.replaceTextRange( [leftBracket.range[0], rightBracket.range[1]], value - )); + ); // Insert a space after the property if it will be connected to the next token. if ( @@ -116,9 +115,8 @@ module.exports = { rightBracket.range[1] === nextToken.range[0] && !astUtils.canTokensBeAdjacent(String(value), nextToken) ) { - fixes.push(fixer.insertTextAfter(node, " ")); + yield fixer.insertTextAfter(node, " "); } - return fixes; } }); } @@ -151,26 +149,24 @@ module.exports = { data: { key: node.property.name }, - fix(fixer) { - const fixes = []; + *fix(fixer) { const dotToken = sourceCode.getTokenBefore(node.property); // A statement that starts with `let[` is parsed as a destructuring variable declaration, not a MemberExpression. if (node.object.type === "Identifier" && node.object.name === "let" && !node.optional) { - return null; + return; // eslint-disable-line eslint-plugin/fixer-return -- false positive } // Don't perform any fixes if there are comments between the dot and the property name. if (sourceCode.commentsExistBetween(dotToken, node.property)) { - return null; + return; // eslint-disable-line eslint-plugin/fixer-return -- false positive } // Replace the identifier to brackets. if (!node.optional) { - fixes.push(fixer.remove(dotToken)); + yield fixer.remove(dotToken); } - fixes.push(fixer.replaceText(node.property, `["${node.property.name}"]`)); - return fixes; + yield fixer.replaceText(node.property, `["${node.property.name}"]`); } }); } diff --git a/lib/rules/prefer-arrow-callback.js b/lib/rules/prefer-arrow-callback.js index 060e7633163..ee5cfe3c8c7 100644 --- a/lib/rules/prefer-arrow-callback.js +++ b/lib/rules/prefer-arrow-callback.js @@ -286,9 +286,7 @@ module.exports = { context.report({ node, messageId: "preferArrowCallback", - fix(fixer) { - const fixes = []; - + *fix(fixer) { if ((!callbackInfo.isLexicalThis && scopeInfo.this) || hasDuplicateParams(node.params)) { /* @@ -297,7 +295,7 @@ module.exports = { * If the callback function has duplicates in its list of parameters (possible in sloppy mode), * don't replace it with an arrow function, because this is a SyntaxError with arrow functions. */ - return null; + return; // eslint-disable-line eslint-plugin/fixer-return -- false positive } // Remove `.bind(this)` if exists. @@ -309,7 +307,7 @@ module.exports = { * E.g. `(foo || function(){}).bind(this)` */ if (memberNode.type !== "MemberExpression") { - return null; + return; // eslint-disable-line eslint-plugin/fixer-return -- false positive } const callNode = memberNode.parent; @@ -322,15 +320,15 @@ module.exports = { * ^^^^^^^^^^^^ */ if (astUtils.isParenthesised(sourceCode, memberNode)) { - return null; + return; // eslint-disable-line eslint-plugin/fixer-return -- false positive } // If comments exist in the `.bind(this)`, don't remove those. if (sourceCode.commentsExistBetween(firstTokenToRemove, lastTokenToRemove)) { - return null; + return; // eslint-disable-line eslint-plugin/fixer-return -- false positive } - fixes.push(fixer.removeRange([firstTokenToRemove.range[0], lastTokenToRemove.range[1]])); + yield fixer.removeRange([firstTokenToRemove.range[0], lastTokenToRemove.range[1]]); } // Convert the function expression to an arrow function. @@ -340,16 +338,16 @@ module.exports = { if (sourceCode.commentsExistBetween(functionToken, leftParenToken)) { // Remove only extra tokens to keep comments. - fixes.push(fixer.remove(functionToken)); + yield fixer.remove(functionToken); if (node.id) { - fixes.push(fixer.remove(node.id)); + yield fixer.remove(node.id); } } else { // Remove extra tokens and spaces. - fixes.push(fixer.removeRange([functionToken.range[0], leftParenToken.range[0]])); + yield fixer.removeRange([functionToken.range[0], leftParenToken.range[0]]); } - fixes.push(fixer.insertTextBefore(node.body, "=> ")); + yield fixer.insertTextBefore(node.body, "=> "); // Get the node that will become the new arrow function. let replacedNode = callbackInfo.isLexicalThis ? node.parent.parent : node; @@ -369,10 +367,9 @@ module.exports = { !astUtils.isParenthesised(sourceCode, replacedNode) && !astUtils.isParenthesised(sourceCode, node) ) { - fixes.push(fixer.insertTextBefore(replacedNode, "(")); - fixes.push(fixer.insertTextAfter(replacedNode, ")")); + yield fixer.insertTextBefore(replacedNode, "("); + yield fixer.insertTextAfter(replacedNode, ")"); } - return fixes; } }); }