Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin): [prefer-nullish-coalescing] add ignoreTernaryTes…
…ts option (#4965) * feat(eslint-plugin): added ignoreTernaryTests option to prefer-nullish-coalescing rule * feat(eslint-plugin): added checking of loose equal ternary cases for prefer-nullish-coalescing rule * feat(eslint-plugin): fixed typo in docs for prefer-nullish-coalescing rule * feat(eslint-plugin): added more test cases for prefer-nullish-coalescing rule * feat(eslint-plugin): added support for MemberExpressions for ignoreTernaryTests option * refactor(eslint-plugin): cleanup prefer-nullish-coalescing rule * test(eslint-plugin): added missing test case for prefer-nullish-coalescing rule * chore: removed currently not supported comment Co-authored-by: Josh Goldberg <me@joshuakgoldberg.com> * refactor: simplified return statement Co-authored-by: Josh Goldberg <me@joshuakgoldberg.com> * refactor: renamed utilities * refactor: used new utilities in prefer-optional-chain.ts * refactor: simplified prefer-nullish-coalescing.ts * test: added test case for prefer-nullish-coalescing * refactor: renamed message id to preferNullishOverOr * fix: covered edge case where we have two loose comparisons * refactor: removed useless .trim * chore: fixed typo * test: added more test cases * fix: fixed tests * chore: fixed typo * test: added more test cases * refactor: inlined getOperator and getNodes * perf: skip type check if both null and undefined are not checked * Apply suggestions from code review Co-authored-by: Josh Goldberg <me@joshuakgoldberg.com>
- Loading branch information
1 parent
78823cc
commit f82727f
Showing
9 changed files
with
609 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
Oops, something went wrong.