From fa36d22ec7ec9af67e8456763d91d0a81bcbc91c Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 26 May 2022 16:31:10 +0800 Subject: [PATCH 01/17] Add `prefer-logical-operator-over-ternary` rule --- configs/recommended.js | 1 + .../prefer-logical-operator-over-ternary.md | 46 +++++++ readme.md | 1 + rules/prefer-logical-operator-over-ternary.js | 115 ++++++++++++++++++ test/prefer-logical-operator-over-ternary.mjs | 15 +++ 5 files changed, 178 insertions(+) create mode 100644 docs/rules/prefer-logical-operator-over-ternary.md create mode 100644 rules/prefer-logical-operator-over-ternary.js create mode 100644 test/prefer-logical-operator-over-ternary.mjs diff --git a/configs/recommended.js b/configs/recommended.js index c8d4c73bcb..b9f464cc63 100644 --- a/configs/recommended.js +++ b/configs/recommended.js @@ -83,6 +83,7 @@ module.exports = { 'unicorn/prefer-includes': 'error', 'unicorn/prefer-json-parse-buffer': 'off', 'unicorn/prefer-keyboard-event-key': 'error', + 'unicorn/prefer-logical-operator-over-tenery': 'error', 'unicorn/prefer-math-trunc': 'error', 'unicorn/prefer-modern-dom-apis': 'error', 'unicorn/prefer-modern-math-apis': 'error', diff --git a/docs/rules/prefer-logical-operator-over-ternary.md b/docs/rules/prefer-logical-operator-over-ternary.md new file mode 100644 index 0000000000..ee487da1eb --- /dev/null +++ b/docs/rules/prefer-logical-operator-over-ternary.md @@ -0,0 +1,46 @@ +# Prefer using logical operator over ternary + + + +βœ… *This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.* + +πŸ’‘ *This rule provides [suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).* + + + + +## Fail + +```js +foo ? foo : bar; +``` + +```js +foo.bar ? foo.bar : foo.baz +``` + +```js +foo?.bar ? foo.bar : baz +``` + +```js +!bar ? foo : bar; +``` + +## Pass + +```js +foo ?? bar; +``` + +```js +foo || bar; +``` + +```js +foo.bar ?? foo.baz +``` + +```js +foo?.bar ?? baz +``` diff --git a/readme.md b/readme.md index 94a88f6444..6ec13824aa 100644 --- a/readme.md +++ b/readme.md @@ -123,6 +123,7 @@ Each rule has emojis denoting: | [prefer-includes](docs/rules/prefer-includes.md) | Prefer `.includes()` over `.indexOf()` and `Array#some()` when checking for existence or non-existence. | βœ… | πŸ”§ | πŸ’‘ | | [prefer-json-parse-buffer](docs/rules/prefer-json-parse-buffer.md) | Prefer reading a JSON file as a buffer. | | πŸ”§ | | | [prefer-keyboard-event-key](docs/rules/prefer-keyboard-event-key.md) | Prefer `KeyboardEvent#key` over `KeyboardEvent#keyCode`. | βœ… | πŸ”§ | | +| [prefer-logical-operator-over-tenery](docs/rules/prefer-logical-operator-over-tenery.md) | Prefer using logical operator over ternary. | βœ… | | πŸ’‘ | | [prefer-math-trunc](docs/rules/prefer-math-trunc.md) | Enforce the use of `Math.trunc` instead of bitwise operators. | βœ… | πŸ”§ | πŸ’‘ | | [prefer-modern-dom-apis](docs/rules/prefer-modern-dom-apis.md) | Prefer `.before()` over `.insertBefore()`, `.replaceWith()` over `.replaceChild()`, prefer one of `.before()`, `.after()`, `.append()` or `.prepend()` over `insertAdjacentText()` and `insertAdjacentElement()`. | βœ… | πŸ”§ | | | [prefer-modern-math-apis](docs/rules/prefer-modern-math-apis.md) | Prefer modern `Math` APIs over legacy patterns. | βœ… | πŸ”§ | | diff --git a/rules/prefer-logical-operator-over-ternary.js b/rules/prefer-logical-operator-over-ternary.js new file mode 100644 index 0000000000..6c213787c1 --- /dev/null +++ b/rules/prefer-logical-operator-over-ternary.js @@ -0,0 +1,115 @@ +'use strict'; +const {} = require('./selectors/index.js'); +const {} = require('./fix/index.js'); +const {isParenthesized, getParenthesizedText} = require('./utils/parentheses.js'); +const isSameReference = require('./utils/is-same-reference.js'); +const shouldAddParenthesesToLogicalExpressionChild = require('./utils/should-add-parentheses-to-logical-expression-child.js'); + + +const MESSAGE_ID_ERROR = 'prefer-logical-operator-over-ternary/error'; +const MESSAGE_ID_SUGGESTION = 'prefer-logical-operator-over-ternary/suggestion'; +const messages = { + [MESSAGE_ID_ERROR]: 'Prefer using logical operator over ternary.', + [MESSAGE_ID_SUGGESTION]: 'Switch to `{{operator}}` operator.', +}; + +function fix({ + fixer, + sourceCode, + conditionalExpression, + left, + right, + operator, +}) { + const text = [left, right].map((node, index) => { + const isNodeParenthesized = isParenthesized(node, sourceCode); + let text = isNodeParenthesized ? getParenthesizedText(node, sourceCode) : sourceCode.getText(node); + + if ( + !isNodeParenthesized + && shouldAddParenthesesToLogicalExpressionChild(node, {operator, property: index === 0 ? 'left' : 'right'}) + ) { + text = `(${text})`; + } + + return text; + }).join(operator); + + // TODO: Check ASI + // TODO: Check parentheses + + return fixer.replace(conditionalExpression, text); +} + +function getProblem({ + context, + conditionalExpression, + left, + right, +}) { + const sourceCode = context.getSourceCode(); + return { + node: conditionalExpression, + messageId: MESSAGE_ID_ERROR, + suggest: ['??', '||'].map(operator => ({ + messageId: MESSAGE_ID_SUGGESTION, + data: {operator}, + fix: fixer => fix({ + fixer, + sourceCode, + conditionalExpression, + left, + right, + operator, + }) + })) + } +} + +/** @param {import('eslint').Rule.RuleContext} context */ +const create = context => { + return { + ConditionalExpression(conditionalExpression) { + const {test, consequent, alternate} = conditionalExpression; + + // `foo ? foo : bar` + if (isSameReference(test, consequent)) { + return getProblem({ + context, + conditionalExpression, + left: test, + right: alternate, + }); + } + + // `!bar ? foo : bar` + if ( + test.type === 'UnaryExpression' + && test.operator === '!' + && test.prefix + && isSameReference(test.argument, alternate) + ) { + return getProblem({ + context, + conditionalExpression, + left: test.argument, + right: consequent, + }) + } + } + }; +}; + +/** @type {import('eslint').Rule.RuleModule} */ +module.exports = { + create, + meta: { + type: 'suggestion', + docs: { + description: 'Prefer using logical operator over ternary.', + }, + + hasSuggestions: true, + messages, + }, +}; diff --git a/test/prefer-logical-operator-over-ternary.mjs b/test/prefer-logical-operator-over-ternary.mjs new file mode 100644 index 0000000000..7ab8bea0ac --- /dev/null +++ b/test/prefer-logical-operator-over-ternary.mjs @@ -0,0 +1,15 @@ +import outdent from 'outdent'; +import {getTester} from './utils/test.mjs'; + +const {test} = getTester(import.meta); + +test.snapshot({ + valid: [ + ], + invalid: [ + 'foo ? foo : bar;', + 'foo.bar ? foo.bar : foo.baz', + 'foo?.bar ? foo.bar : baz', + '!bar ? foo : bar;', + ], +}); From d1c740c7c6e7f092456937fb4c61f2b3178c78d1 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 26 May 2022 17:11:40 +0800 Subject: [PATCH 02/17] Add suggestion --- rules/prefer-logical-operator-over-ternary.js | 4 +- ...parentheses-to-logical-expression-child.js | 10 ++- ...refer-logical-operator-over-ternary.mjs.md | 77 ++++++++++++++++++ ...fer-logical-operator-over-ternary.mjs.snap | Bin 0 -> 395 bytes 4 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 test/snapshots/prefer-logical-operator-over-ternary.mjs.md create mode 100644 test/snapshots/prefer-logical-operator-over-ternary.mjs.snap diff --git a/rules/prefer-logical-operator-over-ternary.js b/rules/prefer-logical-operator-over-ternary.js index 6c213787c1..dfa75ca8d8 100644 --- a/rules/prefer-logical-operator-over-ternary.js +++ b/rules/prefer-logical-operator-over-ternary.js @@ -33,12 +33,12 @@ function fix({ } return text; - }).join(operator); + }).join(` ${operator} `); // TODO: Check ASI // TODO: Check parentheses - return fixer.replace(conditionalExpression, text); + return fixer.replaceText(conditionalExpression, text); } function getProblem({ diff --git a/rules/utils/should-add-parentheses-to-logical-expression-child.js b/rules/utils/should-add-parentheses-to-logical-expression-child.js index 92d152f646..ac71bdcb9f 100644 --- a/rules/utils/should-add-parentheses-to-logical-expression-child.js +++ b/rules/utils/should-add-parentheses-to-logical-expression-child.js @@ -7,10 +7,12 @@ Check if parentheses should be added to a `node` when it's used as child of `Log @returns {boolean} */ function shouldAddParenthesesToLogicalExpressionChild(node, {operator, property}) { - // When operator or property is different, need check `LogicalExpression` operator precedence, not implemented - /* c8 ignore next 3 */ - if (operator !== '??' || property !== 'left') { - throw new Error('Not supported.'); + if ( + property === 'left' + && node.type === 'LogicalExpression' + && node.operator === operator + ) { + return false; } // Not really needed, but more readable diff --git a/test/snapshots/prefer-logical-operator-over-ternary.mjs.md b/test/snapshots/prefer-logical-operator-over-ternary.mjs.md new file mode 100644 index 0000000000..4de8df4cef --- /dev/null +++ b/test/snapshots/prefer-logical-operator-over-ternary.mjs.md @@ -0,0 +1,77 @@ +# Snapshot report for `test/prefer-logical-operator-over-ternary.mjs` + +The actual snapshot is saved in `prefer-logical-operator-over-ternary.mjs.snap`. + +Generated by [AVA](https://avajs.dev). + +## Invalid #1 + 1 | foo ? foo : bar; + +> Error 1/1 + + `␊ + > 1 | foo ? foo : bar;␊ + | ^^^^^^^^^^^^^^^ Prefer using logical operator over ternary.␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 1/2: Switch to \`??\` operator.␊ + 1 | foo ?? bar;␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 2/2: Switch to \`||\` operator.␊ + 1 | foo || bar;␊ + ` + +## Invalid #2 + 1 | foo.bar ? foo.bar : foo.baz + +> Error 1/1 + + `␊ + > 1 | foo.bar ? foo.bar : foo.baz␊ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using logical operator over ternary.␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 1/2: Switch to \`??\` operator.␊ + 1 | foo.bar ?? foo.baz␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 2/2: Switch to \`||\` operator.␊ + 1 | foo.bar || foo.baz␊ + ` + +## Invalid #3 + 1 | foo?.bar ? foo.bar : baz + +> Error 1/1 + + `␊ + > 1 | foo?.bar ? foo.bar : baz␊ + | ^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using logical operator over ternary.␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 1/2: Switch to \`??\` operator.␊ + 1 | foo?.bar ?? baz␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 2/2: Switch to \`||\` operator.␊ + 1 | foo?.bar || baz␊ + ` + +## Invalid #4 + 1 | !bar ? foo : bar; + +> Error 1/1 + + `␊ + > 1 | !bar ? foo : bar;␊ + | ^^^^^^^^^^^^^^^^ Prefer using logical operator over ternary.␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 1/2: Switch to \`??\` operator.␊ + 1 | bar ?? foo;␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 2/2: Switch to \`||\` operator.␊ + 1 | bar || foo;␊ + ` diff --git a/test/snapshots/prefer-logical-operator-over-ternary.mjs.snap b/test/snapshots/prefer-logical-operator-over-ternary.mjs.snap new file mode 100644 index 0000000000000000000000000000000000000000..6b1efca7d8bf4143594df55ef64bbda2f1f1bca5 GIT binary patch literal 395 zcmV;60d)RBRzVv#(Py@hX9?_dOr$}=-C zTylAKY5j7Gu>YCgC8E-nW;23Cm#{D}yx6sRvgx;o{QX�_q;?d>O%_K5PsO%na;c zlNnhV1etmnx$G1S6>1dH^7HNWk`jv)>_JR0%Ss_Bv5HFp2x=7KZ~%pXqSUn1B8Af8 z%)E4koc#36{DRb?#FG3Xh5Rz0TuEwCUSd(D9v7D`l>t|9X?l8UaY<%=o`Rvi zk(EMld1gs+hC)fcLV~@00^A*XKwqGG(jM$LnmWh`%|SIacpOwy19lKf+>{{24J2B? zQBq`$Jx+#PR6t?@=$*k34UiZBItC>Q%3x8T2+taL<6wwI0yHZCeKiOo0h$fKjzNip pnMnBnqlkjA2o+O9E@t3fwTJs~z#|CerkWbKn*fLu?+>~L0022Wt5N^} literal 0 HcmV?d00001 From 1fe1bfa61d26e52ce09ebb1cf9bfed619d30cec9 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 26 May 2022 17:30:45 +0800 Subject: [PATCH 03/17] Test --- rules/utils/is-same-reference.js | 12 ++ ...parentheses-to-logical-expression-child.js | 6 +- test/prefer-logical-operator-over-ternary.mjs | 10 ++ ...refer-logical-operator-over-ternary.mjs.md | 144 ++++++++++++++++++ ...fer-logical-operator-over-ternary.mjs.snap | Bin 395 -> 806 bytes 5 files changed, 169 insertions(+), 3 deletions(-) diff --git a/rules/utils/is-same-reference.js b/rules/utils/is-same-reference.js index b4317e2a8a..f95ad2724a 100644 --- a/rules/utils/is-same-reference.js +++ b/rules/utils/is-same-reference.js @@ -144,6 +144,18 @@ function isSameReference(left, right) { ); } + case 'AwaitExpression': { + return isSameReference(left.argument, right.argument); + } + + case 'LogicalExpression': { + return ( + left.operator === right.operator + && isSameReference(left.left, right.left) + && isSameReference(left.right, right.right) + ); + } + default: return false; } diff --git a/rules/utils/should-add-parentheses-to-logical-expression-child.js b/rules/utils/should-add-parentheses-to-logical-expression-child.js index ac71bdcb9f..7bcd33acc1 100644 --- a/rules/utils/should-add-parentheses-to-logical-expression-child.js +++ b/rules/utils/should-add-parentheses-to-logical-expression-child.js @@ -8,8 +8,7 @@ Check if parentheses should be added to a `node` when it's used as child of `Log */ function shouldAddParenthesesToLogicalExpressionChild(node, {operator, property}) { if ( - property === 'left' - && node.type === 'LogicalExpression' + node.type === 'LogicalExpression' && node.operator === operator ) { return false; @@ -26,7 +25,8 @@ function shouldAddParenthesesToLogicalExpressionChild(node, {operator, property} // Lower precedence than `LogicalExpression` // see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table if ( - node.type === 'ConditionalExpression' + node.type === 'LogicalExpression' + || node.type === 'ConditionalExpression' || node.type === 'AssignmentExpression' || node.type === 'AssignmentExpression' || node.type === 'YieldExpression' diff --git a/test/prefer-logical-operator-over-ternary.mjs b/test/prefer-logical-operator-over-ternary.mjs index 7ab8bea0ac..d54ab11144 100644 --- a/test/prefer-logical-operator-over-ternary.mjs +++ b/test/prefer-logical-operator-over-ternary.mjs @@ -11,5 +11,15 @@ test.snapshot({ 'foo.bar ? foo.bar : foo.baz', 'foo?.bar ? foo.bar : baz', '!bar ? foo : bar;', + + // Parentheses + 'foo ? foo : a && b', + 'foo ? foo : a || b', + 'foo ? foo : a ?? b', + 'a && b ? a && b : bar', + 'a || b ? a || b : bar', + 'a ?? b ? a ?? b : bar', + 'foo ? foo : await a', + 'await a ? await a : foo', ], }); diff --git a/test/snapshots/prefer-logical-operator-over-ternary.mjs.md b/test/snapshots/prefer-logical-operator-over-ternary.mjs.md index 4de8df4cef..91fa57bf66 100644 --- a/test/snapshots/prefer-logical-operator-over-ternary.mjs.md +++ b/test/snapshots/prefer-logical-operator-over-ternary.mjs.md @@ -75,3 +75,147 @@ Generated by [AVA](https://avajs.dev). Suggestion 2/2: Switch to \`||\` operator.␊ 1 | bar || foo;␊ ` + +## Invalid #5 + 1 | foo ? foo : a && b + +> Error 1/1 + + `␊ + > 1 | foo ? foo : a && b␊ + | ^^^^^^^^^^^^^^^^^^ Prefer using logical operator over ternary.␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 1/2: Switch to \`??\` operator.␊ + 1 | foo ?? (a && b)␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 2/2: Switch to \`||\` operator.␊ + 1 | foo || (a && b)␊ + ` + +## Invalid #6 + 1 | foo ? foo : a || b + +> Error 1/1 + + `␊ + > 1 | foo ? foo : a || b␊ + | ^^^^^^^^^^^^^^^^^^ Prefer using logical operator over ternary.␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 1/2: Switch to \`??\` operator.␊ + 1 | foo ?? (a || b)␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 2/2: Switch to \`||\` operator.␊ + 1 | foo || a || b␊ + ` + +## Invalid #7 + 1 | foo ? foo : a ?? b + +> Error 1/1 + + `␊ + > 1 | foo ? foo : a ?? b␊ + | ^^^^^^^^^^^^^^^^^^ Prefer using logical operator over ternary.␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 1/2: Switch to \`??\` operator.␊ + 1 | foo ?? a ?? b␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 2/2: Switch to \`||\` operator.␊ + 1 | foo || (a ?? b)␊ + ` + +## Invalid #8 + 1 | a && b ? a && b : bar + +> Error 1/1 + + `␊ + > 1 | a && b ? a && b : bar␊ + | ^^^^^^^^^^^^^^^^^^^^^ Prefer using logical operator over ternary.␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 1/2: Switch to \`??\` operator.␊ + 1 | (a && b) ?? bar␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 2/2: Switch to \`||\` operator.␊ + 1 | (a && b) || bar␊ + ` + +## Invalid #9 + 1 | a || b ? a || b : bar + +> Error 1/1 + + `␊ + > 1 | a || b ? a || b : bar␊ + | ^^^^^^^^^^^^^^^^^^^^^ Prefer using logical operator over ternary.␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 1/2: Switch to \`??\` operator.␊ + 1 | (a || b) ?? bar␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 2/2: Switch to \`||\` operator.␊ + 1 | a || b || bar␊ + ` + +## Invalid #10 + 1 | a ?? b ? a ?? b : bar + +> Error 1/1 + + `␊ + > 1 | a ?? b ? a ?? b : bar␊ + | ^^^^^^^^^^^^^^^^^^^^^ Prefer using logical operator over ternary.␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 1/2: Switch to \`??\` operator.␊ + 1 | a ?? b ?? bar␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 2/2: Switch to \`||\` operator.␊ + 1 | (a ?? b) || bar␊ + ` + +## Invalid #11 + 1 | foo ? foo : await a + +> Error 1/1 + + `␊ + > 1 | foo ? foo : await a␊ + | ^^^^^^^^^^^^^^^^^^^ Prefer using logical operator over ternary.␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 1/2: Switch to \`??\` operator.␊ + 1 | foo ?? (await a)␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 2/2: Switch to \`||\` operator.␊ + 1 | foo || (await a)␊ + ` + +## Invalid #12 + 1 | await a ? await a : foo + +> Error 1/1 + + `␊ + > 1 | await a ? await a : foo␊ + | ^^^^^^^^^^^^^^^^^^^^^^^ Prefer using logical operator over ternary.␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 1/2: Switch to \`??\` operator.␊ + 1 | (await a) ?? foo␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 2/2: Switch to \`||\` operator.␊ + 1 | (await a) || foo␊ + ` diff --git a/test/snapshots/prefer-logical-operator-over-ternary.mjs.snap b/test/snapshots/prefer-logical-operator-over-ternary.mjs.snap index 6b1efca7d8bf4143594df55ef64bbda2f1f1bca5..2723496aa25c3b755c337035e30d0650cc910931 100644 GIT binary patch literal 806 zcmV+>1KIpRRzV~F>h_gB-2836FdvHu00000000BC zV`O09Vc;wYyME>T%!bwL&;Col9`Xn%2n5rBl+df|4`K{kB%Yt@dS0|@eh?#A)QFjZ z!L`VY^>Gug#e#V){tC6XXETCD&#^Et#BR-t{=L>+eu37D(B%tkyBNWuNo))Z32##$ zO5bSsrMAicqo2AN8zWfs13Lpl(?Q2QZw33fj;HY7Tli-64o0wOF((7VVFn?C#$*SB znR8cb#~7ZT%Lo=_<_5|f6#TKuNvAO~wo3HfCe=@jVA1J33=F56`CTF^ZD}?mSabtF1H;3%L4RD2i4TzaZpVS*g+_<(hHB3^2E#%V3gRySyl>Z`T6+bW$48W z#Ao2B0r_nZL=DVEz_o>W4J=YnW1|FHYyjiT8hcy}$%p`ZXRt&9*fA(k(1<-lfLa5?E<=EuID;lG;10r7 zX@C+BxY7VKhFPTn^WGq-GBhCW!QKFF#hOPT%`)6EGPL3Yo=c#u&4G^+cs7B#2qjL+ zU~!@dZ}j4=JBCv#(Py@hX9?_dOr$}=-C zTylAKY5j7Gu>YCgC8E-nW;23Cm#{D}yx6sRvgx;o{QX�_q;?d>O%_K5PsO%na;c zlNnhV1etmnx$G1S6>1dH^7HNWk`jv)>_JR0%Ss_Bv5HFp2x=7KZ~%pXqSUn1B8Af8 z%)E4koc#36{DRb?#FG3Xh5Rz0TuEwCUSd(D9v7D`l>t|9X?l8UaY<%=o`Rvi zk(EMld1gs+hC)fcLV~@00^A*XKwqGG(jM$LnmWh`%|SIacpOwy19lKf+>{{24J2B? zQBq`$Jx+#PR6t?@=$*k34UiZBItC>Q%3x8T2+taL<6wwI0yHZCeKiOo0h$fKjzNip pnMnBnqlkjA2o+O9E@t3fwTJs~z#|CerkWbKn*fLu?+>~L0022Wt5N^} From 569017be20fa4af6dc9eebe1ece716b5872df374 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 26 May 2022 17:45:33 +0800 Subject: [PATCH 04/17] Update --- rules/prefer-logical-operator-over-ternary.js | 43 ++++++++++++--- rules/utils/is-same-reference.js | 12 ----- test/prefer-logical-operator-over-ternary.mjs | 10 ++++ ...refer-logical-operator-over-ternary.mjs.md | 50 +++++++++++++++--- ...fer-logical-operator-over-ternary.mjs.snap | Bin 806 -> 897 bytes 5 files changed, 90 insertions(+), 25 deletions(-) diff --git a/rules/prefer-logical-operator-over-ternary.js b/rules/prefer-logical-operator-over-ternary.js index dfa75ca8d8..6c54563c46 100644 --- a/rules/prefer-logical-operator-over-ternary.js +++ b/rules/prefer-logical-operator-over-ternary.js @@ -13,6 +13,36 @@ const messages = { [MESSAGE_ID_SUGGESTION]: 'Switch to `{{operator}}` operator.', }; +function isSameNode(left, right, sourceCode) { + if (isSameReference(left, right)) { + return true; + } + + if (left.type !== right.type) { + return false; + } + + switch (left.type) { + case 'AwaitExpression': + return isSameNode(left.argument, right.argument, sourceCode); + + case 'LogicalExpression': + return ( + left.operator === right.operator + && isSameNode(left.left, right.left, sourceCode) + && isSameNode(left.right, right.right, sourceCode) + ); + + case 'UnaryExpression': + return left.operator === '!' && isSameNode(left.argument, right.argument, sourceCode); + + case 'UpdateExpression': + return false; + } + + return sourceCode.getText(left) === sourceCode.getText(right); +} + function fix({ fixer, sourceCode, @@ -42,12 +72,11 @@ function fix({ } function getProblem({ - context, + sourceCode, conditionalExpression, left, right, }) { - const sourceCode = context.getSourceCode(); return { node: conditionalExpression, messageId: MESSAGE_ID_ERROR, @@ -68,14 +97,16 @@ function getProblem({ /** @param {import('eslint').Rule.RuleContext} context */ const create = context => { + const sourceCode = context.getSourceCode(); + return { ConditionalExpression(conditionalExpression) { const {test, consequent, alternate} = conditionalExpression; // `foo ? foo : bar` - if (isSameReference(test, consequent)) { + if (isSameNode(test, consequent, sourceCode)) { return getProblem({ - context, + sourceCode, conditionalExpression, left: test, right: alternate, @@ -87,10 +118,10 @@ const create = context => { test.type === 'UnaryExpression' && test.operator === '!' && test.prefix - && isSameReference(test.argument, alternate) + && isSameNode(test.argument, alternate, sourceCode) ) { return getProblem({ - context, + sourceCode, conditionalExpression, left: test.argument, right: consequent, diff --git a/rules/utils/is-same-reference.js b/rules/utils/is-same-reference.js index f95ad2724a..b4317e2a8a 100644 --- a/rules/utils/is-same-reference.js +++ b/rules/utils/is-same-reference.js @@ -144,18 +144,6 @@ function isSameReference(left, right) { ); } - case 'AwaitExpression': { - return isSameReference(left.argument, right.argument); - } - - case 'LogicalExpression': { - return ( - left.operator === right.operator - && isSameReference(left.left, right.left) - && isSameReference(left.right, right.right) - ); - } - default: return false; } diff --git a/test/prefer-logical-operator-over-ternary.mjs b/test/prefer-logical-operator-over-ternary.mjs index d54ab11144..4c93cc477b 100644 --- a/test/prefer-logical-operator-over-ternary.mjs +++ b/test/prefer-logical-operator-over-ternary.mjs @@ -5,12 +5,22 @@ const {test} = getTester(import.meta); test.snapshot({ valid: [ + 'foo ? foo1 : bar;', + 'foo.bar ? foo.bar1 : foo.baz', + 'foo.bar ? foo1.bar : foo.baz', + '++foo ? ++foo : bar;', + + // Not checking + '!!bar ? foo : bar;', ], invalid: [ 'foo ? foo : bar;', 'foo.bar ? foo.bar : foo.baz', 'foo?.bar ? foo.bar : baz', '!bar ? foo : bar;', + '!!bar ? foo : !bar;', + + 'foo() ? foo() : bar', // Parentheses 'foo ? foo : a && b', diff --git a/test/snapshots/prefer-logical-operator-over-ternary.mjs.md b/test/snapshots/prefer-logical-operator-over-ternary.mjs.md index 91fa57bf66..7bbc7eb18d 100644 --- a/test/snapshots/prefer-logical-operator-over-ternary.mjs.md +++ b/test/snapshots/prefer-logical-operator-over-ternary.mjs.md @@ -77,6 +77,42 @@ Generated by [AVA](https://avajs.dev). ` ## Invalid #5 + 1 | !!bar ? foo : !bar; + +> Error 1/1 + + `␊ + > 1 | !!bar ? foo : !bar;␊ + | ^^^^^^^^^^^^^^^^^^ Prefer using logical operator over ternary.␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 1/2: Switch to \`??\` operator.␊ + 1 | !bar ?? foo;␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 2/2: Switch to \`||\` operator.␊ + 1 | !bar || foo;␊ + ` + +## Invalid #6 + 1 | foo() ? foo() : bar + +> Error 1/1 + + `␊ + > 1 | foo() ? foo() : bar␊ + | ^^^^^^^^^^^^^^^^^^^ Prefer using logical operator over ternary.␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 1/2: Switch to \`??\` operator.␊ + 1 | foo() ?? bar␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 2/2: Switch to \`||\` operator.␊ + 1 | foo() || bar␊ + ` + +## Invalid #7 1 | foo ? foo : a && b > Error 1/1 @@ -94,7 +130,7 @@ Generated by [AVA](https://avajs.dev). 1 | foo || (a && b)␊ ` -## Invalid #6 +## Invalid #8 1 | foo ? foo : a || b > Error 1/1 @@ -112,7 +148,7 @@ Generated by [AVA](https://avajs.dev). 1 | foo || a || b␊ ` -## Invalid #7 +## Invalid #9 1 | foo ? foo : a ?? b > Error 1/1 @@ -130,7 +166,7 @@ Generated by [AVA](https://avajs.dev). 1 | foo || (a ?? b)␊ ` -## Invalid #8 +## Invalid #10 1 | a && b ? a && b : bar > Error 1/1 @@ -148,7 +184,7 @@ Generated by [AVA](https://avajs.dev). 1 | (a && b) || bar␊ ` -## Invalid #9 +## Invalid #11 1 | a || b ? a || b : bar > Error 1/1 @@ -166,7 +202,7 @@ Generated by [AVA](https://avajs.dev). 1 | a || b || bar␊ ` -## Invalid #10 +## Invalid #12 1 | a ?? b ? a ?? b : bar > Error 1/1 @@ -184,7 +220,7 @@ Generated by [AVA](https://avajs.dev). 1 | (a ?? b) || bar␊ ` -## Invalid #11 +## Invalid #13 1 | foo ? foo : await a > Error 1/1 @@ -202,7 +238,7 @@ Generated by [AVA](https://avajs.dev). 1 | foo || (await a)␊ ` -## Invalid #12 +## Invalid #14 1 | await a ? await a : foo > Error 1/1 diff --git a/test/snapshots/prefer-logical-operator-over-ternary.mjs.snap b/test/snapshots/prefer-logical-operator-over-ternary.mjs.snap index 2723496aa25c3b755c337035e30d0650cc910931..085b381918fc01f6336e151ddc9f87e6d592eca0 100644 GIT binary patch literal 897 zcmV-{1AhELRzVoWw@nSwcnG~Z+F#8?_>mvZen3za4j-pecZ%rv0z?{ze4Tp*^FROKQ;!2OtvXA zKSXDw9`4gCUUXHin-MJfh@F8UU$Woy&J7Ok7{d%t!3h5*MzClBCj&#Fc0qFJnF98s zrZ@j;zDkH=1dIOWVqj=8mEf9k$o2DkA&o^=;%4_5!J_>K%+=Q6WAChPl#R#jk#uEeTJ_xIbSu@d+bXbR9nf!?NusA01#xzPJ3^Elr)CH!B#y zqE>(7(nLk=mGKUc?dO?VR;gZX;vY(SW!|6A~vyM3r734a2MSg8FH|V*^o_ zfFlJpHfmt8p{NKix`6Qk;#d!p=um_fS3sXx4}5GWLdz`;hE_yC%PFvT20Rj=r4-mP*xOgo&Iu@AKv`BGUHF?1KIpRRzV~F>h_gB-2836FdvHu00000000BC zV`O09Vc;wYyME>T%!bwL&;Col9`Xn%2n5rBl+df|4`K{kB%Yt@dS0|@eh?#A)QFjZ z!L`VY^>Gug#e#V){tC6XXETCD&#^Et#BR-t{=L>+eu37D(B%tkyBNWuNo))Z32##$ zO5bSsrMAicqo2AN8zWfs13Lpl(?Q2QZw33fj;HY7Tli-64o0wOF((7VVFn?C#$*SB znR8cb#~7ZT%Lo=_<_5|f6#TKuNvAO~wo3HfCe=@jVA1J33=F56`CTF^ZD}?mSabtF1H;3%L4RD2i4TzaZpVS*g+_<(hHB3^2E#%V3gRySyl>Z`T6+bW$48W z#Ao2B0r_nZL=DVEz_o>W4J=YnW1|FHYyjiT8hcy}$%p`ZXRt&9*fA(k(1<-lfLa5?E<=EuID;lG;10r7 zX@C+BxY7VKhFPTn^WGq-GBhCW!QKFF#hOPT%`)6EGPL3Yo=c#u&4G^+cs7B#2qjL+ zU~!@dZ}j4=JBC Date: Thu, 26 May 2022 17:50:03 +0800 Subject: [PATCH 05/17] ASI --- rules/prefer-logical-operator-over-ternary.js | 6 ++++- test/prefer-logical-operator-over-ternary.mjs | 10 +++++++- ...refer-logical-operator-over-ternary.mjs.md | 22 ++++++++++++++++++ ...fer-logical-operator-over-ternary.mjs.snap | Bin 897 -> 975 bytes 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/rules/prefer-logical-operator-over-ternary.js b/rules/prefer-logical-operator-over-ternary.js index 6c54563c46..53b421b158 100644 --- a/rules/prefer-logical-operator-over-ternary.js +++ b/rules/prefer-logical-operator-over-ternary.js @@ -34,7 +34,11 @@ function isSameNode(left, right, sourceCode) { ); case 'UnaryExpression': - return left.operator === '!' && isSameNode(left.argument, right.argument, sourceCode); + return ( + left.operator === right.operator + && left.prefix === right.prefix + && isSameNode(left.argument, right.argument, sourceCode) + ); case 'UpdateExpression': return false; diff --git a/test/prefer-logical-operator-over-ternary.mjs b/test/prefer-logical-operator-over-ternary.mjs index 4c93cc477b..1d51bf28bd 100644 --- a/test/prefer-logical-operator-over-ternary.mjs +++ b/test/prefer-logical-operator-over-ternary.mjs @@ -22,7 +22,7 @@ test.snapshot({ 'foo() ? foo() : bar', - // Parentheses + // Children parentheses 'foo ? foo : a && b', 'foo ? foo : a || b', 'foo ? foo : a ?? b', @@ -31,5 +31,13 @@ test.snapshot({ 'a ?? b ? a ?? b : bar', 'foo ? foo : await a', 'await a ? await a : foo', + + // Parentheses + + // ASI + outdent` + const foo = [] + !+a ? b : +a + ` ], }); diff --git a/test/snapshots/prefer-logical-operator-over-ternary.mjs.md b/test/snapshots/prefer-logical-operator-over-ternary.mjs.md index 7bbc7eb18d..b5bfb0651f 100644 --- a/test/snapshots/prefer-logical-operator-over-ternary.mjs.md +++ b/test/snapshots/prefer-logical-operator-over-ternary.mjs.md @@ -255,3 +255,25 @@ Generated by [AVA](https://avajs.dev). Suggestion 2/2: Switch to \`||\` operator.␊ 1 | (await a) || foo␊ ` + +## Invalid #15 + 1 | const foo = [] + 2 | !+a ? b : +a + +> Error 1/1 + + `␊ + 1 | const foo = []␊ + > 2 | !+a ? b : +a␊ + | ^^^^^^^^^^^^ Prefer using logical operator over ternary.␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 1/2: Switch to \`??\` operator.␊ + 1 | const foo = []␊ + 2 | +a ?? b␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 2/2: Switch to \`||\` operator.␊ + 1 | const foo = []␊ + 2 | +a || b␊ + ` diff --git a/test/snapshots/prefer-logical-operator-over-ternary.mjs.snap b/test/snapshots/prefer-logical-operator-over-ternary.mjs.snap index 085b381918fc01f6336e151ddc9f87e6d592eca0..2c869cad5ccc7941395fd92f2d22f88fd1e182ab 100644 GIT binary patch delta 891 zcmV->1BCp62hRsGK~_N^Q*L2!b7*gLAa*he0s!k{(8pTenj}9DW8N3Z{QMt_2mk;8 z00003>tSSI;AfFCFMsG>eW}f&d+5uS*X1+TezSVS2o}B0!oc9tWi$7|9qy|Wk6FIm zRWH4h5iFX@#=ziOWXAfqiPvJmycU0j+S{`k!J>>D3=ElUQ)YgM&PYAnr&qk_s$4fC zSad2U14F)Kzv-PD9NaO68J>a>{!NTvQDtrhhC=Ov*x1E8jGyN&F(XTMV3^AZ~3)bnmRpiRxpA^-wH7>Y$^Zy@u^^h z(vi+&{%A?b9DhczXtM|d!zGtzm)0-02>YM;T_P%NX*MHRltYYx;l-}ilTE)xl3J9PSX8OU#idJS zz!hAYo}OAC8KJqT zrUtuSmQ)NO-&&QXOs-l7y)}~FhvN|K`8ODgb}JLIX|zs1gzFpAvzWq zNk%|HMStx?U?e30qevTDju~1JgXTj8un$2V1p08`V+oh*fH9YZ8fEp^vk+L}aLYm< zPYs?dge^9Dkz&JMFDbDI5+z`kl|oWt74Dj5XvPfGllEZ04ThM3I0zIoKnG!O06;Q1 zFlL|(P{b7BiPmfEmND`T!4O R43j_uIS2v@C?h->003%ji`oDH delta 812 zcmV+{1JnG^2Z0ANK~_N^Q*L2!b7*gLAa*he0s#4tFY7Z2u8sVi36q!IC7>UR2mk;8 z00003D`I3|;A4?8FMqgn+01=#hx_WpW0r4s)l2VW1dDEBVPJ4AGGl$*#A~r&UW>m% z?d{o&U{OCd28K+wDKkGrXQUqP(<@$dRj!*6Ec%F@fgxYA-}KH64(=Gk3{Sxb|0YJT zXaOe!L!ov-a_E@?_M@gZ|7yNUh+_nc{^nv}Xfl=HnsUhX^M89GjYU@CX7?GvqWwHT znS+jd-U{|{9Z%uExA4vC9gJX6AwC9%xzb(5uYQ>=2~WwmKVLTS2_sl^9X|uZvh61y z9biemxBS{IO`V=MD;U9|R)P!+Tgv}_d@2~BbfhzxKUz{UhY>7#L5P9jlFPG8>z7-E z{m=X^5tX(yn|~238Y;rT@M72M$)?{T^7m&|3aERm^JN5!{uO0lU}j(khb|*4gCJ8Y zBbS|mp+b#9T7JHQJ(#poNGwmxEKx}0QUHP)g*Z&05KxqwmRh7xTAZ1eu8@r@c|8Fl-TG+iVb_cq{Jdflz>@Q3Q37oxQnf! z88c8%+JA%nHW*?C;vi7W03C$AHc14#7Z@{81}I{R@I}nfiWQjm?7`j|@Ho+cxJMHb zCq+b+ULXy_tMr2UX~1IxQI~)t1vNHmV6ma72rs&T@d4sk50mIngcesopIHxlY$!s@ zDzIyC6;_~p11_w;B5v0sQjhkAe?4h9v$sj Date: Thu, 26 May 2022 17:58:10 +0800 Subject: [PATCH 06/17] Update --- rules/prefer-logical-operator-over-ternary.js | 13 ++++++--- test/prefer-logical-operator-over-ternary.mjs | 8 ++++-- ...refer-logical-operator-over-ternary.mjs.md | 26 ++++++++++++++++-- ...fer-logical-operator-over-ternary.mjs.snap | Bin 975 -> 1025 bytes 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/rules/prefer-logical-operator-over-ternary.js b/rules/prefer-logical-operator-over-ternary.js index 53b421b158..0f699847f9 100644 --- a/rules/prefer-logical-operator-over-ternary.js +++ b/rules/prefer-logical-operator-over-ternary.js @@ -4,7 +4,7 @@ const {} = require('./fix/index.js'); const {isParenthesized, getParenthesizedText} = require('./utils/parentheses.js'); const isSameReference = require('./utils/is-same-reference.js'); const shouldAddParenthesesToLogicalExpressionChild = require('./utils/should-add-parentheses-to-logical-expression-child.js'); - +const needsSemicolon = require('./utils/needs-semicolon.js'); const MESSAGE_ID_ERROR = 'prefer-logical-operator-over-ternary/error'; const MESSAGE_ID_SUGGESTION = 'prefer-logical-operator-over-ternary/suggestion'; @@ -55,7 +55,7 @@ function fix({ right, operator, }) { - const text = [left, right].map((node, index) => { + let text = [left, right].map((node, index) => { const isNodeParenthesized = isParenthesized(node, sourceCode); let text = isNodeParenthesized ? getParenthesizedText(node, sourceCode) : sourceCode.getText(node); @@ -69,8 +69,13 @@ function fix({ return text; }).join(` ${operator} `); - // TODO: Check ASI - // TODO: Check parentheses + // According to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#table + // There should be no cases need add parentheses when switching ternary to logical expression + + // ASI + if (needsSemicolon(sourceCode.getTokenBefore(conditionalExpression), sourceCode, text)) { + text = `;${text}`; + } return fixer.replaceText(conditionalExpression, text); } diff --git a/test/prefer-logical-operator-over-ternary.mjs b/test/prefer-logical-operator-over-ternary.mjs index 1d51bf28bd..5791fdb028 100644 --- a/test/prefer-logical-operator-over-ternary.mjs +++ b/test/prefer-logical-operator-over-ternary.mjs @@ -32,12 +32,14 @@ test.snapshot({ 'foo ? foo : await a', 'await a ? await a : foo', - // Parentheses - // ASI outdent` const foo = [] !+a ? b : +a - ` + `, + outdent` + const foo = [] + a && b ? a && b : 1 + `, ], }); diff --git a/test/snapshots/prefer-logical-operator-over-ternary.mjs.md b/test/snapshots/prefer-logical-operator-over-ternary.mjs.md index b5bfb0651f..fa400e8946 100644 --- a/test/snapshots/prefer-logical-operator-over-ternary.mjs.md +++ b/test/snapshots/prefer-logical-operator-over-ternary.mjs.md @@ -270,10 +270,32 @@ Generated by [AVA](https://avajs.dev). --------------------------------------------------------------------------------␊ Suggestion 1/2: Switch to \`??\` operator.␊ 1 | const foo = []␊ - 2 | +a ?? b␊ + 2 | ;+a ?? b␊ ␊ --------------------------------------------------------------------------------␊ Suggestion 2/2: Switch to \`||\` operator.␊ 1 | const foo = []␊ - 2 | +a || b␊ + 2 | ;+a || b␊ + ` + +## Invalid #16 + 1 | const foo = [] + 2 | a && b ? a && b : 1 + +> Error 1/1 + + `␊ + 1 | const foo = []␊ + > 2 | a && b ? a && b : 1␊ + | ^^^^^^^^^^^^^^^^^^^ Prefer using logical operator over ternary.␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 1/2: Switch to \`??\` operator.␊ + 1 | const foo = []␊ + 2 | ;(a && b) ?? 1␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 2/2: Switch to \`||\` operator.␊ + 1 | const foo = []␊ + 2 | ;(a && b) || 1␊ ` diff --git a/test/snapshots/prefer-logical-operator-over-ternary.mjs.snap b/test/snapshots/prefer-logical-operator-over-ternary.mjs.snap index 2c869cad5ccc7941395fd92f2d22f88fd1e182ab..7619cf0291d63bdf12f8935402a6649cdd4c9f83 100644 GIT binary patch literal 1025 zcmV+c1pfO$RzVzz{)NfcA+w{C@O-e_dMr#-11EOP;6)C!}jjk8T|kI@%-QCypiw{k> z`K7L__;Br4b0>ysTPQ+l<#Ud`coxxm;O^}1hArbRW2icYBD8=@342*OV%Evy+tW$O z)1w%wA;S=I4y`!yF+1OWYj2|KbZhJ`4An!!5n^@wlAk>u7O~Qlzet;vUW%bQDFUJO z@kO@GmZI=G$!$N4?{ijTs9uOfs5Ci7Q*m>_=bll9)8?@$9T=)%P;&`GwJ8drW8=zQ?|-D6S)QBUabi643PaVVMX0jAxwA3U)?W4SiE--g57ii| zJ);n+EB^WEwKi+Q?VYx$Y@MzEL$z!)Lbs}?N6bu*jjWyVyz$!iE;EMe(lH1J+|@Z4ey)vY@E520`%g2Rw@NEz15 zBPPO=JeDOEcr_EIm~pxZqapO-$?E8jHc8+XtISU=6fOR-+OaWnw>>T4}Il^wF z-?{BvA;WD-)Myg^@ugX^!QrsG+)lQTn5HF}$&zBH+m=t}ZltBn20*GBs$w&(U3^x!Mby4ZHX8~J& zXJQcb7)2)BU!@nMa8RXJpzJJt0{}qrros2p(Cg*sqCA8TsX>B6FU1vgmg;{RdMT?w zjjym0`Ub)Z7C{zPy}NqA#Fg|nD}|L?DXe_=hPpsVIR$q74nayO5TiU_Nl%nQUq~yn z@T-BxD|Hc(pPHq-@;{b7MPg=ovnlat4}(!`5c)nyV5y6VWGz6lN>rm1RXh5&58*){ zwhveY-}V6q83BVt7NkU_aliwX{-{jB(l*NH8J_S={zo^6ND9>CjdPw@nSwF?_rV?Rs}qk|zTH(Xy^|3vn##t&;96wH z`nZYLV!^x?e}&rHvl+pnj2sLMnQT*Leu&OUJ=~{Pyy&W2HzQbdDklR&zGT1Yof{n7 zF@_nQf)V~rj9^h^ZU%-z?SkacGX?BNO>h3ye3cN#2o_z(!@$sFD#117kn897LK=&# z#LezAf<>M9fHDUi_q-MC<2s(ge{bQN)jJr$qDT1|80JcM6~Fprwj?|yYM;T_P%NX*MHRltYYx;l-}ilTE)x|&I8(WSU zS`mZhLj|x8K^_G9aNuJJm+OErmxLN+_1LozSmAKXLLg5Ko-BkdHhPg_!(J~bu?P|+ zV3w6aQeqYEnr3Lm4AhhMV80E9n1MJ56f-~vVQ&CHGC442pbSvN6yb}Qp%p7I@7aUB zHQ;fg0dbEeBuoOLW?V) z&#VVNHWZ;{71%Yn3M){)0T)(a#xN_aV72<7DXyTwjIFRL!B$w|&KpB3BB13I*gFFr z3D8ms>=^9rD`mw7eSlXhFCLQf8sXO)XOP(9ncr5FkyU zz#3X{0`=M;DXSo^!B$kYBdQNjHy>PmfEmND`T!4O44No`I|y5>l)>9q$jvJ}t*fCG x8H1{6g|lTf6RDWO=%GVcgnH>iE@t3fwTJs~z#|CerkWbKn*ag|C?h->000U*w*deE From c2246f89d2e034768e8c455f5941ed3764847135 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 26 May 2022 18:12:39 +0800 Subject: [PATCH 07/17] Update docs --- docs/rules/prefer-logical-operator-over-ternary.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/rules/prefer-logical-operator-over-ternary.md b/docs/rules/prefer-logical-operator-over-ternary.md index ee487da1eb..5cd9ff99c9 100644 --- a/docs/rules/prefer-logical-operator-over-ternary.md +++ b/docs/rules/prefer-logical-operator-over-ternary.md @@ -7,7 +7,9 @@ πŸ’‘ *This rule provides [suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).* - +Disallow ternary operators when simpler logical operator alternatives exist. + +Ideally, most cases we report are equivalent to a [`Logical OR(||)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR) expression. We provide suggestions instead of auto-fix intentionally, because in many cases, [`Nullish coalescing operator(??)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator) should be preferred. ## Fail @@ -37,6 +39,10 @@ foo ?? bar; foo || bar; ``` +```js +foo ? bar : baz; +``` + ```js foo.bar ?? foo.baz ``` From 37903698e168d35410e6b91955c2d4cf8dd991ca Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 26 May 2022 18:25:25 +0800 Subject: [PATCH 08/17] Linting --- rules/prefer-logical-operator-over-ternary.js | 14 +++++++------- ...-add-parentheses-to-logical-expression-child.js | 6 ++++++ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/rules/prefer-logical-operator-over-ternary.js b/rules/prefer-logical-operator-over-ternary.js index 0f699847f9..c7b399a634 100644 --- a/rules/prefer-logical-operator-over-ternary.js +++ b/rules/prefer-logical-operator-over-ternary.js @@ -1,6 +1,4 @@ 'use strict'; -const {} = require('./selectors/index.js'); -const {} = require('./fix/index.js'); const {isParenthesized, getParenthesizedText} = require('./utils/parentheses.js'); const isSameReference = require('./utils/is-same-reference.js'); const shouldAddParenthesesToLogicalExpressionChild = require('./utils/should-add-parentheses-to-logical-expression-child.js'); @@ -42,6 +40,8 @@ function isSameNode(left, right, sourceCode) { case 'UpdateExpression': return false; + + // No default } return sourceCode.getText(left) === sourceCode.getText(right); @@ -99,9 +99,9 @@ function getProblem({ left, right, operator, - }) - })) - } + }), + })), + }; } /** @param {import('eslint').Rule.RuleContext} context */ @@ -134,9 +134,9 @@ const create = context => { conditionalExpression, left: test.argument, right: consequent, - }) + }); } - } + }, }; }; diff --git a/rules/utils/should-add-parentheses-to-logical-expression-child.js b/rules/utils/should-add-parentheses-to-logical-expression-child.js index 7bcd33acc1..5bda0c5f5e 100644 --- a/rules/utils/should-add-parentheses-to-logical-expression-child.js +++ b/rules/utils/should-add-parentheses-to-logical-expression-child.js @@ -7,6 +7,12 @@ Check if parentheses should be added to a `node` when it's used as child of `Log @returns {boolean} */ function shouldAddParenthesesToLogicalExpressionChild(node, {operator, property}) { + // We are not using this, but we can improve this function with it + if (!property) { + throw new Error('`property` is required.'); + } + + if ( node.type === 'LogicalExpression' && node.operator === operator From 97bf3a0cd5ce163ddf88237b8004c8ea4fda056a Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 26 May 2022 18:33:00 +0800 Subject: [PATCH 09/17] Fix bug in `prefer-string-starts-ends-with` --- .../prefer-string-starts-ends-with.mjs.md | 6 +++--- .../prefer-string-starts-ends-with.mjs.snap | Bin 2169 -> 2167 bytes 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/snapshots/prefer-string-starts-ends-with.mjs.md b/test/snapshots/prefer-string-starts-ends-with.mjs.md index bb616fbca8..1b84db47ba 100644 --- a/test/snapshots/prefer-string-starts-ends-with.mjs.md +++ b/test/snapshots/prefer-string-starts-ends-with.mjs.md @@ -117,7 +117,7 @@ Generated by [AVA](https://avajs.dev). ␊ --------------------------------------------------------------------------------␊ Suggestion 3/3: Use nullish coalescing \`(… ?? '').startsWith()\`.␊ - 1 | (foo || bar ?? '').startsWith('a')␊ + 1 | ((foo || bar) ?? '').startsWith('a')␊ ` ## Invalid #6 @@ -541,7 +541,7 @@ Generated by [AVA](https://avajs.dev). ␊ --------------------------------------------------------------------------------␊ Suggestion 3/3: Use nullish coalescing \`(… ?? '').startsWith()\`.␊ - 1 | (a || b ?? '').startsWith('a')␊ + 1 | ((a || b) ?? '').startsWith('a')␊ ` ## Invalid #22 @@ -569,7 +569,7 @@ Generated by [AVA](https://avajs.dev). ␊ --------------------------------------------------------------------------------␊ Suggestion 3/3: Use nullish coalescing \`(… ?? '').startsWith()\`.␊ - 1 | (a && b ?? '').startsWith('a')␊ + 1 | ((a && b) ?? '').startsWith('a')␊ ` ## Invalid #23 diff --git a/test/snapshots/prefer-string-starts-ends-with.mjs.snap b/test/snapshots/prefer-string-starts-ends-with.mjs.snap index 898944b7c7db3c4e89c04a831c8e94ffcea08b6d..1f73e419583db9a3260eb11ccc936ebe9241197b 100644 GIT binary patch literal 2167 zcmV--2#EJVRzVNmV4>&#A(9&NSYRzL?N7To*rL3kJ2H zBH0@aKvLU=-3z1lOqhJ4?rf21#dr*AFCi(60U&wa=_z*`VoF9_I`cMZG}FB>+C@lu z#R8Bra>LBT9@lO+r@zqJGH_fe2DPsuIYEp5#?ZKFJJ)w#I%`_vnTZv@V^Ev$oJS`2 z1R$eZ+=ZgBJO7+(OOB1aQThW0wddkIayt=#T++Vd%C7B?4jX!(-?aM5D;U%!yy%f; z9DuQA_p;q9how*Y=KF+!%}17CP~#gW7r|eUkv7 znl|N~TwMR2>t6Z6RmF{T8;o`wlE=vaIOZ*kjlQ(D_0X3eZK@a*weI#oK18{h2$zJN< z;t{>-jyLsNu^h z^^>D(yJv2@8dr-!Z4DA*E&%t+<40C6|LWM+`##t?{qOk27}SzT67y(0>3>EWqw{mm zrCLv!Z(W>-LG5THKji}u4!Xh^3X1^GhrJYq<0gC#UPe)dV!~l?x(J8MIkD1Jq1O?* z)F}VLSREFX3ricP5=MiIbh`AYUMk%TP8+5QKKOVCSw=c=TY<|_X)o{Z*FmZcr)JP5 z0(X$*WG!ApD{zwz*vC#YxB3K z8aOBo$VaFEJ7j^co+xTFgSya3;?!HrZ=wn>sURxt5IGImDh?T8wLV>}nJiYjyQ-?v zS%H^OL>1{QfnXU2<5nxK(@D6Qb+AYw0VTuqsFIzm!3(Hr63vl(Ev!LEWPStEj1FOH z3!I<@j`Klli|l;#s#wovZ9PF3ABGpF8c2Jo6xnBa$xb9Ava{7xXKiou;>1o424eeR zj24ZB0cM zf3{LO&pjH<@7xh+I@xoFqAK&_s41I=2R8ZAm4awt>&XO0yJA2TB%K@v3}EuEY)mf_ z#x$xYg)#J^RLW677>+DfZABi7T2+vxu(dJ<;z^#VOj4-g6mXbRS!o^Wc+BPd=NLB6 zs5`n8R1wUz$!A8Ln#@qe#HuON+cN6b1Q9`-7tvs38sars&@i%)g@FSNIi7J3 z@#xJA5CfyI&^eyb1_WG;>?J^ zZ05`mlkGWkBPgHAohyM|(sw>Oc}dJK;m3Hs$s!=Rph&il^a7h-j7UtnIl~(i0+Wd! zB!UuVsd*FdS5Sb*XK#rRSq?id6QXYZqlBoPj2L9M4*xHOzKOSLc;?%GlcjkEct)m~ z1i;xICbM_BWfX9iorv;0Zc)*j3sc|gxY z*(WW5J`eRip^+wZ$DhrlaM|RxPq$MwcG-6&ZoxY{US;J+EbVxQ$AK__ofMbDP2yI% z_)x`XxtoPagHu}`bm%1xdDjJF>fj*OSn}XtSCu}}*t*PvAHDC@jMBr=ed6ySIV~s# zXbAFM*^R--c>!u|@tzM~74!T1-z%i6xF1|4em|J5bUPS&B$x46FtXbRS6j691FnkE zNm1T2k5=aVpwc10)uMz(RiC{L4m)ryaM}k~Ta@{caVByP%5*P2+0MhPEw*#P t2lV_+Prx$??fD-QtS80p&||ffqk@sm2&=YO%%Ya!@4q}?_JuV^003q?Cl3Gs literal 2169 zcmV-<2!{7TRzVorNv&&Z7e?(E_xATyrzaVgjm4n03CZ|q0MzqNOukzeT`;WagHtE|a3-N&!=XhO)c%TuNCe=`{AH6SWf#}gMt4fw79XB~L2V6^xFi7V z_3MWmt6ue?>t6A`75Tf!HYn{VBoEX8IOZ*kiE8?^<-oU}uP+%H?ZTioy{|{^XaJ}@ z{mY>ZLu(Fy)-wJ<+>r}YF{n+}dZaN0fZ4TY?X9OCuew*L~IM#iJvycH8w62DObyrltX~xl4m))kcTqaLI`un@oqYFsQ9T5^n^c?re0! z(gW7KkyX{pYm#q$ghA~{Bu7cvv*6K;58fKSVdBuej|wl(TaQ8Q9V9CTkbd1%u+zS; zde}=qL^81HirF_~GSCzdLet_s83&{}W%0K`ntKeh2{fQ~!!GMCIn3 z)0vN(nlDYjp!PK+=W_w*1R`Jzg@uC`!(LJ0xDlU&7uoIle8Qo3x(J8MIib{5qSX)@ zoyxy3R)wi@U}?h?!k~9qoh~h^rz4xeNy8M*2OsOO7Fiv*CC}w3wH5dA>p*A0?K4Od zfjg|l)=IpBRN|H?7QIT-1Jzp%B?;dC5+w5!vZ#va`0gd46K6gMrw7 z7+JIy@Y1v8G7nrYQ1jD7Dx1K3h`o?Gg}AKE;15_s3fW_Mxz*;vQw$P`yG1f_Gh369 z#h-p~%YoIBLq`;ekcGbSWoV=z1c7(WV&S1xY4{c!Sayx9!A)co5-1$(;cWciG%Ct_r3$47S16*`NJG9#|Bf%K`@`t0W1R;A| za&ib(9!A9gjGq+pE5$(lG*SIf7wP0hBxx`q!vN|xv}90MlqnsV20{h2Wzsfd8Zu`2 zjM?xKT2XgoDX1c-YopJMIyIW0ii%ZZy0>N2tq~%EHZP(9$TZMv%0$D+Ko$xPG-P|m zJ2bClPB~viqmeNq z9J7frLsYhB%nhe}I&-c#c0u3y=;S3ayM!O(*``bm$vH)`e5B{t{9;64(oJdJpx~H{ z>>%NkFbT~YhrgTxEIxZngvfH(ahVWx^NbRrax!9&-8%fg6#6FKtl*h%`%RYO8Q>Y2 zViEvjd#I-Ga*OPCe9j!4_w^;?Ch8O3SG{yyu?=2`ELqGKzJg8*vOLS4WM%Eae82;G z7D_*73G`X0_xX%4o!kFprZ1CAZrgO5y}~B?p2W?0U&pE}{eYzn@9!88lIbaNIowv< zOcoy``5boxH)Swti-Qimv?1=gU`!kg#L7w?Ozg7KM+#k+dhDb1y_S)BG`gGr9VDX# z$p8&OzDv6?7&*^Dtt{TN;mcxvAO8h6RmR=m3jVvnRH>W6&?C8s$AXdFKDf%FwI6U< zj80N{&pVo_vx8Cx09W!78dZJ%wm0;^mB48qTxC((hdZFn4P8x~&x?fl+|j!mMQ~YB zOjah3WwBV}P3VmC5aN|85SO(SnRRVAPtg(kc6vpzsAr>X_gstErrpljwENc#aPu4J zc0JL@C^@U&62bp*h%bI2nJ<*J^eJ@Z3t-_(Q}G`Aly?#fyC``;IDjk?!o(MJiW1^L z2)wewLTD7SqGSkag>cTa72ri~TLFYbkU=)v0Z%UBTB3p|bx}!!C%w&OhK+_k?kCco z-|<|(hUcOv68VIDOo=#+KHn=k6+swca(TrGxhFiD_Qjmos2IZ%h(Tu5c>jFJ3T25U zYP-}DCAlz#Q0VY3M~Ya4*qD{P_yqJ Date: Thu, 26 May 2022 18:35:34 +0800 Subject: [PATCH 10/17] Typo --- configs/recommended.js | 2 +- readme.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/configs/recommended.js b/configs/recommended.js index b9f464cc63..672c155b54 100644 --- a/configs/recommended.js +++ b/configs/recommended.js @@ -83,7 +83,7 @@ module.exports = { 'unicorn/prefer-includes': 'error', 'unicorn/prefer-json-parse-buffer': 'off', 'unicorn/prefer-keyboard-event-key': 'error', - 'unicorn/prefer-logical-operator-over-tenery': 'error', + 'unicorn/prefer-logical-operator-over-ternary': 'error', 'unicorn/prefer-math-trunc': 'error', 'unicorn/prefer-modern-dom-apis': 'error', 'unicorn/prefer-modern-math-apis': 'error', diff --git a/readme.md b/readme.md index 6ec13824aa..53fd7879a3 100644 --- a/readme.md +++ b/readme.md @@ -123,7 +123,7 @@ Each rule has emojis denoting: | [prefer-includes](docs/rules/prefer-includes.md) | Prefer `.includes()` over `.indexOf()` and `Array#some()` when checking for existence or non-existence. | βœ… | πŸ”§ | πŸ’‘ | | [prefer-json-parse-buffer](docs/rules/prefer-json-parse-buffer.md) | Prefer reading a JSON file as a buffer. | | πŸ”§ | | | [prefer-keyboard-event-key](docs/rules/prefer-keyboard-event-key.md) | Prefer `KeyboardEvent#key` over `KeyboardEvent#keyCode`. | βœ… | πŸ”§ | | -| [prefer-logical-operator-over-tenery](docs/rules/prefer-logical-operator-over-tenery.md) | Prefer using logical operator over ternary. | βœ… | | πŸ’‘ | +| [prefer-logical-operator-over-ternary](docs/rules/prefer-logical-operator-over-ternary.md) | Prefer using logical operator over ternary. | βœ… | | πŸ’‘ | | [prefer-math-trunc](docs/rules/prefer-math-trunc.md) | Enforce the use of `Math.trunc` instead of bitwise operators. | βœ… | πŸ”§ | πŸ’‘ | | [prefer-modern-dom-apis](docs/rules/prefer-modern-dom-apis.md) | Prefer `.before()` over `.insertBefore()`, `.replaceWith()` over `.replaceChild()`, prefer one of `.before()`, `.after()`, `.append()` or `.prepend()` over `insertAdjacentText()` and `insertAdjacentElement()`. | βœ… | πŸ”§ | | | [prefer-modern-math-apis](docs/rules/prefer-modern-math-apis.md) | Prefer modern `Math` APIs over legacy patterns. | βœ… | πŸ”§ | | From f4d5339e70d9311162d389c35014504d8064c94a Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 26 May 2022 18:40:12 +0800 Subject: [PATCH 11/17] Fix --- .../utils/should-add-parentheses-to-logical-expression-child.js | 1 - 1 file changed, 1 deletion(-) diff --git a/rules/utils/should-add-parentheses-to-logical-expression-child.js b/rules/utils/should-add-parentheses-to-logical-expression-child.js index 5bda0c5f5e..73647ab39c 100644 --- a/rules/utils/should-add-parentheses-to-logical-expression-child.js +++ b/rules/utils/should-add-parentheses-to-logical-expression-child.js @@ -12,7 +12,6 @@ function shouldAddParenthesesToLogicalExpressionChild(node, {operator, property} throw new Error('`property` is required.'); } - if ( node.type === 'LogicalExpression' && node.operator === operator From 98ccadfb91815ddaac843d6d2f938abee7b0d364 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 26 May 2022 19:49:46 +0800 Subject: [PATCH 12/17] Coverage --- .../utils/should-add-parentheses-to-logical-expression-child.js | 1 + 1 file changed, 1 insertion(+) diff --git a/rules/utils/should-add-parentheses-to-logical-expression-child.js b/rules/utils/should-add-parentheses-to-logical-expression-child.js index 73647ab39c..91a844efa9 100644 --- a/rules/utils/should-add-parentheses-to-logical-expression-child.js +++ b/rules/utils/should-add-parentheses-to-logical-expression-child.js @@ -8,6 +8,7 @@ Check if parentheses should be added to a `node` when it's used as child of `Log */ function shouldAddParenthesesToLogicalExpressionChild(node, {operator, property}) { // We are not using this, but we can improve this function with it + /* c8 ignore next 3 */ if (!property) { throw new Error('`property` is required.'); } From 6eb02bce7ef38165d80fb10fd90f5cb5a7cdd1fb Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sun, 5 Jun 2022 14:49:15 +0700 Subject: [PATCH 13/17] Update prefer-logical-operator-over-ternary.md --- docs/rules/prefer-logical-operator-over-ternary.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/rules/prefer-logical-operator-over-ternary.md b/docs/rules/prefer-logical-operator-over-ternary.md index 5cd9ff99c9..0d4d3b03f9 100644 --- a/docs/rules/prefer-logical-operator-over-ternary.md +++ b/docs/rules/prefer-logical-operator-over-ternary.md @@ -9,7 +9,7 @@ Disallow ternary operators when simpler logical operator alternatives exist. -Ideally, most cases we report are equivalent to a [`Logical OR(||)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR) expression. We provide suggestions instead of auto-fix intentionally, because in many cases, [`Nullish coalescing operator(??)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator) should be preferred. +Ideally, most reported cases have an equivalent [`Logical OR(||)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR) expression. The rule intentionally provides suggestions instead of auto-fixes, because in many cases, the [nullish coalescing operator (`??`)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator) should be preferred. ## Fail From 475ee5cf418577179421a3802afa7c6429de6741 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 29 Jun 2022 10:24:46 +0800 Subject: [PATCH 14/17] Apply suggestions from code review Co-authored-by: Sindre Sorhus --- rules/prefer-logical-operator-over-ternary.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/prefer-logical-operator-over-ternary.js b/rules/prefer-logical-operator-over-ternary.js index c7b399a634..32c295c56a 100644 --- a/rules/prefer-logical-operator-over-ternary.js +++ b/rules/prefer-logical-operator-over-ternary.js @@ -7,7 +7,7 @@ const needsSemicolon = require('./utils/needs-semicolon.js'); const MESSAGE_ID_ERROR = 'prefer-logical-operator-over-ternary/error'; const MESSAGE_ID_SUGGESTION = 'prefer-logical-operator-over-ternary/suggestion'; const messages = { - [MESSAGE_ID_ERROR]: 'Prefer using logical operator over ternary.', + [MESSAGE_ID_ERROR]: 'Prefer using a logical operator over a ternary.', [MESSAGE_ID_SUGGESTION]: 'Switch to `{{operator}}` operator.', }; From 5d6fc3017c900b671c91ccefce6e80d2884af67c Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 29 Jun 2022 10:28:31 +0800 Subject: [PATCH 15/17] Update files --- readme.md | 2 +- rules/prefer-logical-operator-over-ternary.js | 2 +- ...refer-logical-operator-over-ternary.mjs.md | 32 +++++++++--------- ...fer-logical-operator-over-ternary.mjs.snap | Bin 1025 -> 1029 bytes 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/readme.md b/readme.md index 53fd7879a3..611391227f 100644 --- a/readme.md +++ b/readme.md @@ -123,7 +123,7 @@ Each rule has emojis denoting: | [prefer-includes](docs/rules/prefer-includes.md) | Prefer `.includes()` over `.indexOf()` and `Array#some()` when checking for existence or non-existence. | βœ… | πŸ”§ | πŸ’‘ | | [prefer-json-parse-buffer](docs/rules/prefer-json-parse-buffer.md) | Prefer reading a JSON file as a buffer. | | πŸ”§ | | | [prefer-keyboard-event-key](docs/rules/prefer-keyboard-event-key.md) | Prefer `KeyboardEvent#key` over `KeyboardEvent#keyCode`. | βœ… | πŸ”§ | | -| [prefer-logical-operator-over-ternary](docs/rules/prefer-logical-operator-over-ternary.md) | Prefer using logical operator over ternary. | βœ… | | πŸ’‘ | +| [prefer-logical-operator-over-ternary](docs/rules/prefer-logical-operator-over-ternary.md) | Prefer using a logical operator over a ternary. | βœ… | | πŸ’‘ | | [prefer-math-trunc](docs/rules/prefer-math-trunc.md) | Enforce the use of `Math.trunc` instead of bitwise operators. | βœ… | πŸ”§ | πŸ’‘ | | [prefer-modern-dom-apis](docs/rules/prefer-modern-dom-apis.md) | Prefer `.before()` over `.insertBefore()`, `.replaceWith()` over `.replaceChild()`, prefer one of `.before()`, `.after()`, `.append()` or `.prepend()` over `insertAdjacentText()` and `insertAdjacentElement()`. | βœ… | πŸ”§ | | | [prefer-modern-math-apis](docs/rules/prefer-modern-math-apis.md) | Prefer modern `Math` APIs over legacy patterns. | βœ… | πŸ”§ | | diff --git a/rules/prefer-logical-operator-over-ternary.js b/rules/prefer-logical-operator-over-ternary.js index 32c295c56a..0939f1dd3e 100644 --- a/rules/prefer-logical-operator-over-ternary.js +++ b/rules/prefer-logical-operator-over-ternary.js @@ -146,7 +146,7 @@ module.exports = { meta: { type: 'suggestion', docs: { - description: 'Prefer using logical operator over ternary.', + description: 'Prefer using a logical operator over a ternary.', }, hasSuggestions: true, diff --git a/test/snapshots/prefer-logical-operator-over-ternary.mjs.md b/test/snapshots/prefer-logical-operator-over-ternary.mjs.md index fa400e8946..f19537bc36 100644 --- a/test/snapshots/prefer-logical-operator-over-ternary.mjs.md +++ b/test/snapshots/prefer-logical-operator-over-ternary.mjs.md @@ -11,7 +11,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | foo ? foo : bar;␊ - | ^^^^^^^^^^^^^^^ Prefer using logical operator over ternary.␊ + | ^^^^^^^^^^^^^^^ Prefer using a logical operator over a ternary.␊ ␊ --------------------------------------------------------------------------------␊ Suggestion 1/2: Switch to \`??\` operator.␊ @@ -29,7 +29,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | foo.bar ? foo.bar : foo.baz␊ - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using logical operator over ternary.␊ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using a logical operator over a ternary.␊ ␊ --------------------------------------------------------------------------------␊ Suggestion 1/2: Switch to \`??\` operator.␊ @@ -47,7 +47,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | foo?.bar ? foo.bar : baz␊ - | ^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using logical operator over ternary.␊ + | ^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using a logical operator over a ternary.␊ ␊ --------------------------------------------------------------------------------␊ Suggestion 1/2: Switch to \`??\` operator.␊ @@ -65,7 +65,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | !bar ? foo : bar;␊ - | ^^^^^^^^^^^^^^^^ Prefer using logical operator over ternary.␊ + | ^^^^^^^^^^^^^^^^ Prefer using a logical operator over a ternary.␊ ␊ --------------------------------------------------------------------------------␊ Suggestion 1/2: Switch to \`??\` operator.␊ @@ -83,7 +83,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | !!bar ? foo : !bar;␊ - | ^^^^^^^^^^^^^^^^^^ Prefer using logical operator over ternary.␊ + | ^^^^^^^^^^^^^^^^^^ Prefer using a logical operator over a ternary.␊ ␊ --------------------------------------------------------------------------------␊ Suggestion 1/2: Switch to \`??\` operator.␊ @@ -101,7 +101,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | foo() ? foo() : bar␊ - | ^^^^^^^^^^^^^^^^^^^ Prefer using logical operator over ternary.␊ + | ^^^^^^^^^^^^^^^^^^^ Prefer using a logical operator over a ternary.␊ ␊ --------------------------------------------------------------------------------␊ Suggestion 1/2: Switch to \`??\` operator.␊ @@ -119,7 +119,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | foo ? foo : a && b␊ - | ^^^^^^^^^^^^^^^^^^ Prefer using logical operator over ternary.␊ + | ^^^^^^^^^^^^^^^^^^ Prefer using a logical operator over a ternary.␊ ␊ --------------------------------------------------------------------------------␊ Suggestion 1/2: Switch to \`??\` operator.␊ @@ -137,7 +137,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | foo ? foo : a || b␊ - | ^^^^^^^^^^^^^^^^^^ Prefer using logical operator over ternary.␊ + | ^^^^^^^^^^^^^^^^^^ Prefer using a logical operator over a ternary.␊ ␊ --------------------------------------------------------------------------------␊ Suggestion 1/2: Switch to \`??\` operator.␊ @@ -155,7 +155,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | foo ? foo : a ?? b␊ - | ^^^^^^^^^^^^^^^^^^ Prefer using logical operator over ternary.␊ + | ^^^^^^^^^^^^^^^^^^ Prefer using a logical operator over a ternary.␊ ␊ --------------------------------------------------------------------------------␊ Suggestion 1/2: Switch to \`??\` operator.␊ @@ -173,7 +173,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | a && b ? a && b : bar␊ - | ^^^^^^^^^^^^^^^^^^^^^ Prefer using logical operator over ternary.␊ + | ^^^^^^^^^^^^^^^^^^^^^ Prefer using a logical operator over a ternary.␊ ␊ --------------------------------------------------------------------------------␊ Suggestion 1/2: Switch to \`??\` operator.␊ @@ -191,7 +191,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | a || b ? a || b : bar␊ - | ^^^^^^^^^^^^^^^^^^^^^ Prefer using logical operator over ternary.␊ + | ^^^^^^^^^^^^^^^^^^^^^ Prefer using a logical operator over a ternary.␊ ␊ --------------------------------------------------------------------------------␊ Suggestion 1/2: Switch to \`??\` operator.␊ @@ -209,7 +209,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | a ?? b ? a ?? b : bar␊ - | ^^^^^^^^^^^^^^^^^^^^^ Prefer using logical operator over ternary.␊ + | ^^^^^^^^^^^^^^^^^^^^^ Prefer using a logical operator over a ternary.␊ ␊ --------------------------------------------------------------------------------␊ Suggestion 1/2: Switch to \`??\` operator.␊ @@ -227,7 +227,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | foo ? foo : await a␊ - | ^^^^^^^^^^^^^^^^^^^ Prefer using logical operator over ternary.␊ + | ^^^^^^^^^^^^^^^^^^^ Prefer using a logical operator over a ternary.␊ ␊ --------------------------------------------------------------------------------␊ Suggestion 1/2: Switch to \`??\` operator.␊ @@ -245,7 +245,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | await a ? await a : foo␊ - | ^^^^^^^^^^^^^^^^^^^^^^^ Prefer using logical operator over ternary.␊ + | ^^^^^^^^^^^^^^^^^^^^^^^ Prefer using a logical operator over a ternary.␊ ␊ --------------------------------------------------------------------------------␊ Suggestion 1/2: Switch to \`??\` operator.␊ @@ -265,7 +265,7 @@ Generated by [AVA](https://avajs.dev). `␊ 1 | const foo = []␊ > 2 | !+a ? b : +a␊ - | ^^^^^^^^^^^^ Prefer using logical operator over ternary.␊ + | ^^^^^^^^^^^^ Prefer using a logical operator over a ternary.␊ ␊ --------------------------------------------------------------------------------␊ Suggestion 1/2: Switch to \`??\` operator.␊ @@ -287,7 +287,7 @@ Generated by [AVA](https://avajs.dev). `␊ 1 | const foo = []␊ > 2 | a && b ? a && b : 1␊ - | ^^^^^^^^^^^^^^^^^^^ Prefer using logical operator over ternary.␊ + | ^^^^^^^^^^^^^^^^^^^ Prefer using a logical operator over a ternary.␊ ␊ --------------------------------------------------------------------------------␊ Suggestion 1/2: Switch to \`??\` operator.␊ diff --git a/test/snapshots/prefer-logical-operator-over-ternary.mjs.snap b/test/snapshots/prefer-logical-operator-over-ternary.mjs.snap index 7619cf0291d63bdf12f8935402a6649cdd4c9f83..e88fdf8d6e59417e472267f3de2880387a522b91 100644 GIT binary patch literal 1029 zcmV+g1p50yRzVI@YaWE%|<61Iy4dzKFB13#gLtL-CAa6PCG>^7(Q$km+*ne z+~O88B9UNm-`tWVL^AhV<`RsIkP(USp68sMwRXDsK-$yup?&-GH2MGg%kzJq^ByJu z{lIXl;CA!U3#+fqx;5vQsiBq?*|n3sjof%PQ=ZFNw>jN&pmFFXg4L6~0eHif%qI_e#VxS7X6SQs$_ZAd!~rZz zEp^OkDD8PI^WjhP`}{=&t9Rl7$}^L6JFZUq($e2_z?zu#oM6?}mwj_(%K4A_&AJ-} z3D5R_IDVF3wIujGHmHkFcKQWVF^@;(YJM=^%Bo_4DDu`|} zwNO7TR z7BWy7tY}Q-7$`f#l&aP_+Jp0lONuVDMVHKG_)&$F$N1TU!L3(sCcH5kn@KblInAU- zWLp3cTe_VSBrLeHQaiUQ^xmUgI0-@@8|0fGr8vbK$g(Wt~2Vzz{)NfcA+w{C@O-e_dMr#-11EOP;6)C!}jjk8T|kI@%-QCypiw{k> z`K7L__;Br4b0>ysTPQ+l<#Ud`coxxm;O^}1hArbRW2icYBD8=@342*OV%Evy+tW$O z)1w%wA;S=I4y`!yF+1OWYj2|KbZhJ`4An!!5n^@wlAk>u7O~Qlzet;vUW%bQDFUJO z@kO@GmZI=G$!$N4?{ijTs9uOfs5Ci7Q*m>_=bll9)8?@$9T=)%P;&`GwJ8drW8=zQ?|-D6S)QBUabi643PaVVMX0jAxwA3U)?W4SiE--g57ii| zJ);n+EB^WEwKi+Q?VYx$Y@MzEL$z!)Lbs}?N6bu*jjWyVyz$!iE;EMe(lH1J+|@Z4ey)vY@E520`%g2Rw@NEz15 zBPPO=JeDOEcr_EIm~pxZqapO-$?E8jHc8+XtISU=6fOR-+OaWnw>>T4}Il^wF z-?{BvA;WD-)Myg^@ugX^!QrsG+)lQTn5HF}$&zBH+m=t}ZltBn20*GBs$w&(U3^x!Mby4ZHX8~J& zXJQcb7)2)BU!@nMa8RXJpzJJt0{}qrros2p(Cg*sqCA8TsX>B6FU1vgmg;{RdMT?w zjjym0`Ub)Z7C{zPy}NqA#Fg|nD}|L?DXe_=hPpsVIR$q74nayO5TiU_Nl%nQUq~yn z@T-BxD|Hc(pPHq-@;{b7MPg=ovnlat4}(!`5c)nyV5y6VWGz6lN>rm1RXh5&58*){ zwhveY-}V6q83BVt7NkU_aliwX{-{jB(l*NH8J_S={zo^6ND9>CjdP Date: Wed, 29 Jun 2022 13:39:30 +0800 Subject: [PATCH 16/17] Trigger CI From 00016a3c2cf51cbf798e4ac1841bfd59bf794535 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 29 Jun 2022 13:42:25 +0800 Subject: [PATCH 17/17] Update docs/rules/prefer-logical-operator-over-ternary.md Co-authored-by: Sindre Sorhus --- docs/rules/prefer-logical-operator-over-ternary.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/rules/prefer-logical-operator-over-ternary.md b/docs/rules/prefer-logical-operator-over-ternary.md index 0d4d3b03f9..e4efe87490 100644 --- a/docs/rules/prefer-logical-operator-over-ternary.md +++ b/docs/rules/prefer-logical-operator-over-ternary.md @@ -1,4 +1,4 @@ -# Prefer using logical operator over ternary +# Prefer using a logical operator over a ternary