From c88882e9e4c22dc96b9ccfe61a7e93c9923cdd46 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 15 Apr 2021 12:58:56 +0800 Subject: [PATCH] `no-useless-undefined`: Handle parenthesized `undefined` (#1178) --- rules/no-useless-undefined.js | 14 +- test/no-useless-undefined.mjs | 39 +++++- test/snapshots/no-useless-undefined.mjs.md | 132 +++++++++++++++++++ test/snapshots/no-useless-undefined.mjs.snap | Bin 392 -> 686 bytes 4 files changed, 175 insertions(+), 10 deletions(-) diff --git a/rules/no-useless-undefined.js b/rules/no-useless-undefined.js index b1b7a0b170..ce446b458e 100644 --- a/rules/no-useless-undefined.js +++ b/rules/no-useless-undefined.js @@ -1,6 +1,7 @@ 'use strict'; const {isCommaToken} = require('eslint-utils'); const getDocumentationUrl = require('./utils/get-documentation-url'); +const replaceNodeOrTokenAndSpacesBefore = require('./utils/replace-node-or-token-and-spaces-before'); const messageId = 'no-useless-undefined'; const messages = { @@ -97,19 +98,14 @@ const create = context => { }); }; - const code = context.getSourceCode().text; + const sourceCode = context.getSourceCode(); const options = { checkArguments: true, ...context.options[0] }; - const removeNodeAndLeadingSpace = (node, fixer) => { - const textBefore = code.slice(0, node.range[0]); - return fixer.removeRange([ - node.range[0] - (textBefore.length - textBefore.trim().length), - node.range[1] - ]); - }; + const removeNodeAndLeadingSpace = (node, fixer) => + replaceNodeOrTokenAndSpacesBefore(node, '', fixer, sourceCode); const listeners = { [returnSelector]: listener( @@ -118,7 +114,7 @@ const create = context => { ), [yieldSelector]: listener(removeNodeAndLeadingSpace), [arrowFunctionSelector]: listener( - (node, fixer) => fixer.replaceText(node, '{}'), + (node, fixer) => replaceNodeOrTokenAndSpacesBefore(node, ' {}', fixer, sourceCode), /* CheckFunctionReturnType */ true ), [variableInitSelector]: listener( diff --git a/test/no-useless-undefined.mjs b/test/no-useless-undefined.mjs index 765d2e5493..b4c71f5c37 100644 --- a/test/no-useless-undefined.mjs +++ b/test/no-useless-undefined.mjs @@ -367,6 +367,43 @@ test.snapshot({ `, 'function foo([bar = undefined] = []) {}', 'foo(bar, undefined, undefined);', - 'let a = undefined, b = 2;' + 'let a = undefined, b = 2;', + outdent` + function foo() { + return /* */ ( + /* */ + ( + /* */ + undefined + /* */ + ) + /* */ + ) /* */ ; + } + `, + outdent` + function * foo() { + yield /* */ ( + /* */ + ( + /* */ + undefined + /* */ + ) + /* */ + ) /* */ ; + } + `, + outdent` + const foo = () => /* */ ( + /* */ + ( + /* */ + undefined + /* */ + ) + /* */ + ); + ` ] }); diff --git a/test/snapshots/no-useless-undefined.mjs.md b/test/snapshots/no-useless-undefined.mjs.md index f108525da4..eb0f109a60 100644 --- a/test/snapshots/no-useless-undefined.mjs.md +++ b/test/snapshots/no-useless-undefined.mjs.md @@ -87,3 +87,135 @@ Generated by [AVA](https://avajs.dev). > 1 | let a = undefined, b = 2;␊ | ^^^^^^^^^ Do not use useless \`undefined\`.␊ ` + +## Invalid #5 + 1 | function foo() { + 2 | return /* */ ( + 3 | /* */ + 4 | ( + 5 | /* */ + 6 | undefined + 7 | /* */ + 8 | ) + 9 | /* */ + 10 | ) /* */ ; + 11 | } + +> Output + + `␊ + 1 | function foo() {␊ + 2 | return /* */␊ + 3 | /* */␊ + 4 |␊ + 5 | /* */␊ + 6 |␊ + 7 | /* */␊ + 8 |␊ + 9 | /* */␊ + 10 | /* */ ;␊ + 11 | }␊ + ` + +> Error 1/1 + + `␊ + 1 | function foo() {␊ + 2 | return /* */ (␊ + 3 | /* */␊ + 4 | (␊ + 5 | /* */␊ + > 6 | undefined␊ + | ^^^^^^^^^ Do not use useless \`undefined\`.␊ + 7 | /* */␊ + 8 | )␊ + 9 | /* */␊ + 10 | ) /* */ ;␊ + 11 | }␊ + ` + +## Invalid #6 + 1 | function * foo() { + 2 | yield /* */ ( + 3 | /* */ + 4 | ( + 5 | /* */ + 6 | undefined + 7 | /* */ + 8 | ) + 9 | /* */ + 10 | ) /* */ ; + 11 | } + +> Output + + `␊ + 1 | function * foo() {␊ + 2 | yield /* */␊ + 3 | /* */␊ + 4 |␊ + 5 | /* */␊ + 6 |␊ + 7 | /* */␊ + 8 |␊ + 9 | /* */␊ + 10 | /* */ ;␊ + 11 | }␊ + ` + +> Error 1/1 + + `␊ + 1 | function * foo() {␊ + 2 | yield /* */ (␊ + 3 | /* */␊ + 4 | (␊ + 5 | /* */␊ + > 6 | undefined␊ + | ^^^^^^^^^ Do not use useless \`undefined\`.␊ + 7 | /* */␊ + 8 | )␊ + 9 | /* */␊ + 10 | ) /* */ ;␊ + 11 | }␊ + ` + +## Invalid #7 + 1 | const foo = () => /* */ ( + 2 | /* */ + 3 | ( + 4 | /* */ + 5 | undefined + 6 | /* */ + 7 | ) + 8 | /* */ + 9 | ); + +> Output + + `␊ + 1 | const foo = () => /* */␊ + 2 | /* */␊ + 3 |␊ + 4 | /* */␊ + 5 | {}␊ + 6 | /* */␊ + 7 |␊ + 8 | /* */␊ + 9 | ;␊ + ` + +> Error 1/1 + + `␊ + 1 | const foo = () => /* */ (␊ + 2 | /* */␊ + 3 | (␊ + 4 | /* */␊ + > 5 | undefined␊ + | ^^^^^^^^^ Do not use useless \`undefined\`.␊ + 6 | /* */␊ + 7 | )␊ + 8 | /* */␊ + 9 | );␊ + ` diff --git a/test/snapshots/no-useless-undefined.mjs.snap b/test/snapshots/no-useless-undefined.mjs.snap index d7f2d45fff1390ec34cffafd50117e9d36601cc6..573e569bf586b0e6d05dc58f5fb56ec74b950f28 100644 GIT binary patch literal 686 zcmV;f0#W@zRzV?<7fWS&9p3lg@;H9g=TK{7D#2Gx_UKY7&?_vUr zdNM(<12Y4IPiARa^PkLX`z+? ze?xH+3j@QVw@;q;9Y`!NQYZ;sbkCri2`oApibYr%7>?xyh~{jn42@gc;x$K>DTWCw zIu(ke*ccd2rZK)}jtz=r)(hea%{_6E2`oAbio4kv7?^=R1_4G^20^A-3|tBd3Wf?b z3TdTz$t9Wjc?xOy`5KxE)gW;rpg3nyYDsBPo`SxXf|fo=&=@4h3Fn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