Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
feat(eslint-plugin): [prefer-nullish-coalescing] add ignoreTernaryTests option#4965
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
a07ac2d
0487bb7
33a6053
b7b0758
42e9737
61b9680
40609b1
e9626e9
81f1a1d
060002a
a20e741
ee6bbf6
0726c9d
c4146c0
65415e6
dda02a3
cafa047
8ee505c
119f14d
31966ff
3d0eab1
73034b0
4ef1ef7
3b36d89
67e7ab5
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -5,14 +5,20 @@ import { | ||
TSESTree, | ||
} from '@typescript-eslint/utils'; | ||
import * as util from '../util'; | ||
import * as ts from 'typescript'; | ||
export type Options = [ | ||
{ | ||
ignoreConditionalTests?: boolean; | ||
ignoreTernaryTests?: boolean; | ||
ignoreMixedLogicalExpressions?: boolean; | ||
}, | ||
]; | ||
export type MessageIds = | ||
| 'preferNullishOverOr' | ||
| 'preferNullishOverTernary' | ||
| 'suggestNullish'; | ||
export default util.createRule<Options, MessageIds>({ | ||
name: 'prefer-nullish-coalescing', | ||
@@ -27,8 +33,10 @@ export default util.createRule<Options, MessageIds>({ | ||
}, | ||
hasSuggestions: true, | ||
messages: { | ||
preferNullishOverOr: | ||
'Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator.', | ||
preferNullishOverTernary: | ||
'Prefer using nullish coalescing operator (`??`) instead of a ternary expression, as it is simpler to read.', | ||
suggestNullish: 'Fix to nullish coalescing operator (`??`).', | ||
}, | ||
schema: [ | ||
@@ -38,6 +46,9 @@ export default util.createRule<Options, MessageIds>({ | ||
ignoreConditionalTests: { | ||
type: 'boolean', | ||
}, | ||
ignoreTernaryTests: { | ||
type: 'boolean', | ||
}, | ||
ignoreMixedLogicalExpressions: { | ||
type: 'boolean', | ||
}, | ||
@@ -52,15 +63,182 @@ export default util.createRule<Options, MessageIds>({ | ||
defaultOptions: [ | ||
{ | ||
ignoreConditionalTests: true, | ||
ignoreTernaryTests: true, | ||
ignoreMixedLogicalExpressions: true, | ||
}, | ||
], | ||
create( | ||
context, | ||
[ | ||
{ | ||
ignoreConditionalTests, | ||
ignoreTernaryTests, | ||
ignoreMixedLogicalExpressions, | ||
}, | ||
], | ||
) { | ||
const parserServices = util.getParserServices(context); | ||
const sourceCode = context.getSourceCode(); | ||
const checker = parserServices.program.getTypeChecker(); | ||
return { | ||
ConditionalExpression(node: TSESTree.ConditionalExpression): void { | ||
if (ignoreTernaryTests) { | ||
return; | ||
} | ||
let operator: '==' | '!=' | '===' | '!==' | undefined; | ||
let nodesInsideTestExpression: TSESTree.Node[] = []; | ||
if (node.test.type === AST_NODE_TYPES.BinaryExpression) { | ||
nodesInsideTestExpression = [node.test.left, node.test.right]; | ||
if ( | ||
node.test.operator === '==' || | ||
node.test.operator === '!=' || | ||
node.test.operator === '===' || | ||
node.test.operator === '!==' | ||
) { | ||
operator = node.test.operator; | ||
} | ||
} else if ( | ||
node.test.type === AST_NODE_TYPES.LogicalExpression && | ||
node.test.left.type === AST_NODE_TYPES.BinaryExpression && | ||
node.test.right.type === AST_NODE_TYPES.BinaryExpression | ||
) { | ||
nodesInsideTestExpression = [ | ||
node.test.left.left, | ||
node.test.left.right, | ||
node.test.right.left, | ||
node.test.right.right, | ||
]; | ||
if (node.test.operator === '||') { | ||
if ( | ||
node.test.left.operator === '===' && | ||
node.test.right.operator === '===' | ||
) { | ||
operator = '==='; | ||
} else if ( | ||
((node.test.left.operator === '===' || | ||
node.test.right.operator === '===') && | ||
(node.test.left.operator === '==' || | ||
node.test.right.operator === '==')) || | ||
(node.test.left.operator === '==' && | ||
node.test.right.operator === '==') | ||
) { | ||
operator = '=='; | ||
} | ||
} else if (node.test.operator === '&&') { | ||
if ( | ||
node.test.left.operator === '!==' && | ||
node.test.right.operator === '!==' | ||
) { | ||
operator = '!=='; | ||
} else if ( | ||
((node.test.left.operator === '!==' || | ||
node.test.right.operator === '!==') && | ||
(node.test.left.operator === '!=' || | ||
node.test.right.operator === '!=')) || | ||
(node.test.left.operator === '!=' && | ||
node.test.right.operator === '!=') | ||
) { | ||
operator = '!='; | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. The other way to solve this is to put a bunch of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Yeah I tried refactoring a bit and couldn't find anything significantly cleaner. 🤷 | ||
} | ||
if (!operator) { | ||
return; | ||
} | ||
let identifier: TSESTree.Node | undefined; | ||
let hasUndefinedCheck = false; | ||
let hasNullCheck = false; | ||
// we check that the test only contains null, undefined and the identifier | ||
for (const testNode of nodesInsideTestExpression) { | ||
if (util.isNullLiteral(testNode)) { | ||
hasNullCheck = true; | ||
} else if (util.isUndefinedIdentifier(testNode)) { | ||
hasUndefinedCheck = true; | ||
} else if ( | ||
(operator === '!==' || operator === '!=') && | ||
util.isNodeEqual(testNode, node.consequent) | ||
) { | ||
identifier = testNode; | ||
} else if ( | ||
(operator === '===' || operator === '==') && | ||
util.isNodeEqual(testNode, node.alternate) | ||
) { | ||
identifier = testNode; | ||
} else { | ||
return; | ||
} | ||
} | ||
if (!identifier) { | ||
return; | ||
} | ||
const isFixable = ((): boolean => { | ||
// it is fixable if we check for both null and undefined, or not if neither | ||
if (hasUndefinedCheck === hasNullCheck) { | ||
return hasUndefinedCheck; | ||
} | ||
// it is fixable if we loosely check for either null or undefined | ||
if (operator === '==' || operator === '!=') { | ||
return true; | ||
} | ||
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(identifier); | ||
const type = checker.getTypeAtLocation(tsNode); | ||
const flags = util.getTypeFlags(type); | ||
if (flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) { | ||
return false; | ||
} | ||
const hasNullType = (flags & ts.TypeFlags.Null) !== 0; | ||
// it is fixable if we check for undefined and the type is not nullable | ||
if (hasUndefinedCheck && !hasNullType) { | ||
return true; | ||
} | ||
const hasUndefinedType = (flags & ts.TypeFlags.Undefined) !== 0; | ||
// it is fixable if we check for null and the type can't be undefined | ||
return hasNullCheck && !hasUndefinedType; | ||
})(); | ||
if (isFixable) { | ||
context.report({ | ||
node, | ||
messageId: 'preferNullishOverTernary', | ||
suggest: [ | ||
{ | ||
messageId: 'suggestNullish', | ||
fix(fixer: TSESLint.RuleFixer): TSESLint.RuleFix { | ||
const [left, right] = | ||
operator === '===' || operator === '==' | ||
? [node.alternate, node.consequent] | ||
: [node.consequent, node.alternate]; | ||
return fixer.replaceText( | ||
node, | ||
`${sourceCode.text.slice( | ||
left.range[0], | ||
left.range[1], | ||
)} ?? ${sourceCode.text.slice( | ||
right.range[0], | ||
right.range[1], | ||
)}`, | ||
); | ||
}, | ||
}, | ||
], | ||
}); | ||
} | ||
}, | ||
'LogicalExpression[operator = "||"]'( | ||
node: TSESTree.LogicalExpression, | ||
): void { | ||
@@ -110,7 +288,7 @@ export default util.createRule<Options, MessageIds>({ | ||
context.report({ | ||
node: barBarOperator, | ||
messageId: 'preferNullishOverOr', | ||
suggest: [ | ||
{ | ||
messageId: 'suggestNullish', | ||
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,31 @@ | ||||||
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils'; | ||||||
export function isNodeEqual(a: TSESTree.Node, b: TSESTree.Node): boolean { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Nit: this name is a little non-specific. Maybe... Suggested change
There's almost certainly a better name out there than There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I will think about this one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I have thought about this, I don't like I however also have no idea for a better name 🤷♂️ | ||||||
if (a.type !== b.type) { | ||||||
return false; | ||||||
} | ||||||
if ( | ||||||
a.type === AST_NODE_TYPES.ThisExpression && | ||||||
b.type === AST_NODE_TYPES.ThisExpression | ||||||
) { | ||||||
return true; | ||||||
} | ||||||
if (a.type === AST_NODE_TYPES.Literal && b.type === AST_NODE_TYPES.Literal) { | ||||||
return a.value === b.value; | ||||||
} | ||||||
if ( | ||||||
a.type === AST_NODE_TYPES.Identifier && | ||||||
b.type === AST_NODE_TYPES.Identifier | ||||||
) { | ||||||
return a.name === b.name; | ||||||
} | ||||||
if ( | ||||||
a.type === AST_NODE_TYPES.MemberExpression && | ||||||
b.type === AST_NODE_TYPES.MemberExpression | ||||||
) { | ||||||
return ( | ||||||
isNodeEqual(a.property, b.property) && isNodeEqual(a.object, b.object) | ||||||
); | ||||||
} | ||||||
return false; | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils'; | ||
export function isNullLiteral(i: TSESTree.Node): boolean { | ||
return i.type === AST_NODE_TYPES.Literal && i.value === null; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils'; | ||
export function isUndefinedIdentifier(i: TSESTree.Node): boolean { | ||
return i.type === AST_NODE_TYPES.Identifier && i.name === 'undefined'; | ||
} |
Uh oh!
There was an error while loading.Please reload this page.