|
| 1 | +'use strict'; |
| 2 | +const isShadowed = require('./utils/is-shadowed.js'); |
| 3 | +const {matches} = require('./selectors/index.js'); |
| 4 | +const { |
| 5 | + addParenthesizesToReturnOrThrowExpression, |
| 6 | +} = require('./fix/index.js'); |
| 7 | +const {removeSpacesAfter} = require('./fix/index.js'); |
| 8 | +const isOnSameLine = require('./utils/is-on-same-line.js'); |
| 9 | +const needsSemicolon = require('./utils/needs-semicolon.js'); |
| 10 | +const { |
| 11 | + isParenthesized, |
| 12 | +} = require('./utils/parentheses.js'); |
| 13 | + |
| 14 | +const MESSAGE_ID_ERROR = 'no-typeof-undefined/error'; |
| 15 | +const MESSAGE_ID_SUGGESTION = 'no-typeof-undefined/suggestion'; |
| 16 | +const messages = { |
| 17 | + [MESSAGE_ID_ERROR]: 'Compare with `undefined` directly instead of using `typeof`.', |
| 18 | + [MESSAGE_ID_SUGGESTION]: 'Switch to `… {{operator}} undefined`.', |
| 19 | +}; |
| 20 | + |
| 21 | +const selector = [ |
| 22 | + 'BinaryExpression', |
| 23 | + matches(['===', '!==', '==', '!='].map(operator => `[operator="${operator}"]`)), |
| 24 | + '[left.type="UnaryExpression"]', |
| 25 | + '[left.operator="typeof"]', |
| 26 | + '[left.prefix]', |
| 27 | + '[right.type="Literal"]', |
| 28 | +].join(''); |
| 29 | + |
| 30 | +/** @param {import('eslint').Rule.RuleContext} context */ |
| 31 | +const create = context => { |
| 32 | + const { |
| 33 | + checkGlobalVariables, |
| 34 | + } = { |
| 35 | + checkGlobalVariables: false, |
| 36 | + ...context.options[0], |
| 37 | + }; |
| 38 | + |
| 39 | + return { |
| 40 | + [selector](binaryExpression) { |
| 41 | + const {left: typeofNode, right: undefinedString, operator} = binaryExpression; |
| 42 | + if (undefinedString.value !== 'undefined') { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + const valueNode = typeofNode.argument; |
| 47 | + const isGlobalVariable = valueNode.type === 'Identifier' |
| 48 | + && !isShadowed(context.getScope(), valueNode); |
| 49 | + |
| 50 | + if (!checkGlobalVariables && isGlobalVariable) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + const sourceCode = context.getSourceCode(); |
| 55 | + const [typeofToken, secondToken] = sourceCode.getFirstTokens(typeofNode, 2); |
| 56 | + |
| 57 | + const fix = function * (fixer) { |
| 58 | + // Change `==`/`!=` to `===`/`!==` |
| 59 | + if (operator === '==' || operator === '!=') { |
| 60 | + const operatorToken = sourceCode.getTokenAfter( |
| 61 | + typeofNode, |
| 62 | + token => token.type === 'Punctuator' && token.value === operator, |
| 63 | + ); |
| 64 | + |
| 65 | + yield fixer.insertTextAfter(operatorToken, '='); |
| 66 | + } |
| 67 | + |
| 68 | + yield fixer.replaceText(undefinedString, 'undefined'); |
| 69 | + |
| 70 | + yield fixer.remove(typeofToken); |
| 71 | + yield removeSpacesAfter(typeofToken, sourceCode, fixer); |
| 72 | + |
| 73 | + const {parent} = binaryExpression; |
| 74 | + if ( |
| 75 | + (parent.type === 'ReturnStatement' || parent.type === 'ThrowStatement') |
| 76 | + && parent.argument === binaryExpression |
| 77 | + && !isOnSameLine(typeofToken, secondToken) |
| 78 | + && !isParenthesized(binaryExpression, sourceCode) |
| 79 | + && !isParenthesized(typeofNode, sourceCode) |
| 80 | + ) { |
| 81 | + yield * addParenthesizesToReturnOrThrowExpression(fixer, parent, sourceCode); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + const tokenBefore = sourceCode.getTokenBefore(binaryExpression); |
| 86 | + if (needsSemicolon(tokenBefore, sourceCode, secondToken.value)) { |
| 87 | + yield fixer.insertTextBefore(binaryExpression, ';'); |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + const problem = { |
| 92 | + node: binaryExpression, |
| 93 | + loc: typeofToken.loc, |
| 94 | + messageId: MESSAGE_ID_ERROR, |
| 95 | + }; |
| 96 | + |
| 97 | + if (isGlobalVariable) { |
| 98 | + problem.suggest = [ |
| 99 | + { |
| 100 | + messageId: MESSAGE_ID_SUGGESTION, |
| 101 | + data: {operator: operator.startsWith('!') ? '!==' : '==='}, |
| 102 | + fix, |
| 103 | + }, |
| 104 | + ]; |
| 105 | + } else { |
| 106 | + problem.fix = fix; |
| 107 | + } |
| 108 | + |
| 109 | + return problem; |
| 110 | + }, |
| 111 | + }; |
| 112 | +}; |
| 113 | + |
| 114 | +const schema = [ |
| 115 | + { |
| 116 | + type: 'object', |
| 117 | + additionalProperties: false, |
| 118 | + properties: { |
| 119 | + checkGlobalVariables: { |
| 120 | + type: 'boolean', |
| 121 | + default: false, |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | +]; |
| 126 | + |
| 127 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 128 | +module.exports = { |
| 129 | + create, |
| 130 | + meta: { |
| 131 | + type: 'suggestion', |
| 132 | + docs: { |
| 133 | + description: 'Disallow comparing `undefined` using `typeof`.', |
| 134 | + }, |
| 135 | + fixable: 'code', |
| 136 | + hasSuggestions: true, |
| 137 | + schema, |
| 138 | + messages, |
| 139 | + }, |
| 140 | +}; |
0 commit comments