|
| 1 | +import { |
| 2 | + ASTUtils, |
| 3 | + TSESTree, |
| 4 | + TSESLint, |
| 5 | +} from '@typescript-eslint/experimental-utils'; |
| 6 | +import { Definition, DefinitionType } from '@typescript-eslint/scope-manager'; |
| 7 | +import * as util from '../util'; |
| 8 | + |
| 9 | +function hasAssignmentBeforeNode( |
| 10 | + variable: TSESLint.Scope.Variable, |
| 11 | + node: TSESTree.Node, |
| 12 | +): boolean { |
| 13 | + return ( |
| 14 | + variable.references.some( |
| 15 | + ref => ref.isWrite() && ref.identifier.range[1] < node.range[1], |
| 16 | + ) || |
| 17 | + variable.defs.some( |
| 18 | + def => |
| 19 | + isDefinitionWithAssignment(def) && def.node.range[1] < node.range[1], |
| 20 | + ) |
| 21 | + ); |
| 22 | +} |
| 23 | + |
| 24 | +function isDefinitionWithAssignment(definition: Definition): boolean { |
| 25 | + if (definition.type !== DefinitionType.Variable) { |
| 26 | + return false; |
| 27 | + } |
| 28 | + |
| 29 | + const variableDeclarator = definition.node; |
| 30 | + return ( |
| 31 | + variableDeclarator.definite === true || variableDeclarator.init !== null |
| 32 | + ); |
| 33 | +} |
| 34 | + |
| 35 | +export default util.createRule({ |
| 36 | + name: 'no-non-null-asserted-nullish-coalescing', |
| 37 | + meta: { |
| 38 | + type: 'problem', |
| 39 | + docs: { |
| 40 | + description: |
| 41 | + 'Disallows using a non-null assertion in the left operand of the nullish coalescing operator', |
| 42 | + category: 'Possible Errors', |
| 43 | + recommended: false, |
| 44 | + suggestion: true, |
| 45 | + }, |
| 46 | + messages: { |
| 47 | + noNonNullAssertedNullishCoalescing: |
| 48 | + 'The nullish coalescing operator is designed to handle undefined and null - using a non-null assertion is not needed.', |
| 49 | + suggestRemovingNonNull: 'Remove the non-null assertion.', |
| 50 | + }, |
| 51 | + schema: [], |
| 52 | + }, |
| 53 | + defaultOptions: [], |
| 54 | + create(context) { |
| 55 | + return { |
| 56 | + 'LogicalExpression[operator = "??"] > TSNonNullExpression.left'( |
| 57 | + node: TSESTree.TSNonNullExpression, |
| 58 | + ): void { |
| 59 | + if (node.expression.type === TSESTree.AST_NODE_TYPES.Identifier) { |
| 60 | + const scope = context.getScope(); |
| 61 | + const identifier = node.expression; |
| 62 | + const variable = ASTUtils.findVariable(scope, identifier.name); |
| 63 | + if (variable && !hasAssignmentBeforeNode(variable, node)) { |
| 64 | + return; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + const sourceCode = context.getSourceCode(); |
| 69 | + |
| 70 | + context.report({ |
| 71 | + node, |
| 72 | + messageId: 'noNonNullAssertedNullishCoalescing', |
| 73 | + /* |
| 74 | + Use a suggestion instead of a fixer, because this can break type checks. |
| 75 | + The resulting type of the nullish coalesce is only influenced by the right operand if the left operand can be `null` or `undefined`. |
| 76 | + After removing the non-null assertion the type of the left operand might contain `null` or `undefined` and then the type of the right operand |
| 77 | + might change the resulting type of the nullish coalesce. |
| 78 | + See the following example: |
| 79 | +
|
| 80 | + function test(x?: string): string { |
| 81 | + const bar = x! ?? false; // type analysis reports `bar` has type `string` |
| 82 | + // x ?? false; // type analysis reports `bar` has type `string | false` |
| 83 | + return bar; |
| 84 | + } |
| 85 | + */ |
| 86 | + suggest: [ |
| 87 | + { |
| 88 | + messageId: 'suggestRemovingNonNull', |
| 89 | + fix(fixer): TSESLint.RuleFix { |
| 90 | + const exclamationMark = util.nullThrows( |
| 91 | + sourceCode.getLastToken( |
| 92 | + node, |
| 93 | + ASTUtils.isNonNullAssertionPunctuator, |
| 94 | + ), |
| 95 | + util.NullThrowsReasons.MissingToken( |
| 96 | + '!', |
| 97 | + 'Non-null Assertion', |
| 98 | + ), |
| 99 | + ); |
| 100 | + return fixer.remove(exclamationMark); |
| 101 | + }, |
| 102 | + }, |
| 103 | + ], |
| 104 | + }); |
| 105 | + }, |
| 106 | + }; |
| 107 | + }, |
| 108 | +}); |
0 commit comments