|
| 1 | +import {isLiteral} from './ast/index.js'; |
| 2 | + |
| 3 | +const MODE_ALWAYS = 'always'; |
| 4 | +const MODE_NEVER = 'never'; |
| 5 | + |
| 6 | +const MESSAGE_ID_MISSING_DELAY = 'missing-delay'; |
| 7 | +const MESSAGE_ID_REDUNDANT_DELAY = 'redundant-delay'; |
| 8 | + |
| 9 | +const messages = { |
| 10 | + [MESSAGE_ID_MISSING_DELAY]: '`{{name}}` should have an explicit delay argument.', |
| 11 | + [MESSAGE_ID_REDUNDANT_DELAY]: '`{{name}}` should not have an explicit delay of `0`.', |
| 12 | +}; |
| 13 | + |
| 14 | +const timerFunctions = new Set(['setTimeout', 'setInterval']); |
| 15 | + |
| 16 | +/** |
| 17 | +Check if a call expression is a timer function call. |
| 18 | +@param {import('estree').CallExpression} node - The call expression node. |
| 19 | +@param {import('eslint').SourceCode} sourceCode |
| 20 | +@returns {{isTimer: boolean, name?: string}} Object with isTimer flag and function name. |
| 21 | +*/ |
| 22 | +const checkTimerCall = (node, sourceCode) => { |
| 23 | + const {callee} = node; |
| 24 | + |
| 25 | + if ( |
| 26 | + callee.type === 'Identifier' |
| 27 | + && timerFunctions.has(callee.name) |
| 28 | + && sourceCode.isGlobalReference(callee) |
| 29 | + ) { |
| 30 | + return {isTimer: true, name: callee.name}; |
| 31 | + } |
| 32 | + |
| 33 | + if ( |
| 34 | + callee.type === 'MemberExpression' |
| 35 | + && !callee.computed |
| 36 | + && callee.property.type === 'Identifier' |
| 37 | + && timerFunctions.has(callee.property.name) |
| 38 | + ) { |
| 39 | + const {object} = callee; |
| 40 | + |
| 41 | + if ( |
| 42 | + object.type === 'Identifier' |
| 43 | + && sourceCode.isGlobalReference(object) |
| 44 | + ) { |
| 45 | + return {isTimer: true, name: callee.property.name}; |
| 46 | + } |
| 47 | + |
| 48 | + return {isTimer: false}; |
| 49 | + } |
| 50 | + |
| 51 | + return {isTimer: false}; |
| 52 | +}; |
| 53 | + |
| 54 | +/** |
| 55 | + Check if the delay argument is explicitly zero. |
| 56 | + @param {import('estree').Node} node - The argument node. |
| 57 | + @returns {boolean} True if the argument is zero. |
| 58 | + */ |
| 59 | +const isZeroDelay = node => |
| 60 | + isLiteral(node, 0) |
| 61 | + || (node.type === 'UnaryExpression' && node.operator === '-' && isLiteral(node.argument, 0)); |
| 62 | + |
| 63 | +/** @param {import('eslint').Rule.RuleContext} context */ |
| 64 | +const create = context => { |
| 65 | + const mode = context.options[0] || MODE_ALWAYS; |
| 66 | + const {sourceCode} = context; |
| 67 | + |
| 68 | + context.on('CallExpression', node => { |
| 69 | + if (node.optional) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + const {isTimer, name} = checkTimerCall(node, sourceCode); |
| 74 | + |
| 75 | + if (!isTimer) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + const {arguments: arguments_} = node; |
| 80 | + const hasDelayArgument = arguments_.length >= 2; |
| 81 | + |
| 82 | + if (mode === MODE_ALWAYS && !hasDelayArgument) { |
| 83 | + if (arguments_.length === 0) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + const problem = { |
| 88 | + node, |
| 89 | + messageId: MESSAGE_ID_MISSING_DELAY, |
| 90 | + data: {name}, |
| 91 | + }; |
| 92 | + |
| 93 | + const [firstArgument] = arguments_; |
| 94 | + if (firstArgument && firstArgument.type !== 'SpreadElement') { |
| 95 | + problem.fix = fixer => fixer.insertTextAfter(firstArgument, ', 0'); |
| 96 | + } |
| 97 | + |
| 98 | + return problem; |
| 99 | + } |
| 100 | + |
| 101 | + if (mode === MODE_NEVER && hasDelayArgument) { |
| 102 | + const delayArgument = arguments_[1]; |
| 103 | + |
| 104 | + if (isZeroDelay(delayArgument)) { |
| 105 | + return { |
| 106 | + node: delayArgument, |
| 107 | + messageId: MESSAGE_ID_REDUNDANT_DELAY, |
| 108 | + data: {name}, |
| 109 | + fix(fixer) { |
| 110 | + const firstArgument = arguments_[0]; |
| 111 | + const [, firstArgumentEnd] = sourceCode.getRange(firstArgument); |
| 112 | + const [delayArgumentStart] = sourceCode.getRange(delayArgument); |
| 113 | + |
| 114 | + return fixer.removeRange([firstArgumentEnd, delayArgumentStart + sourceCode.getText(delayArgument).length]); |
| 115 | + }, |
| 116 | + }; |
| 117 | + } |
| 118 | + } |
| 119 | + }); |
| 120 | +}; |
| 121 | + |
| 122 | +const schema = [ |
| 123 | + { |
| 124 | + enum: [MODE_ALWAYS, MODE_NEVER], |
| 125 | + }, |
| 126 | +]; |
| 127 | + |
| 128 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 129 | +const config = { |
| 130 | + create, |
| 131 | + meta: { |
| 132 | + type: 'suggestion', |
| 133 | + docs: { |
| 134 | + description: 'Enforce or disallow explicit `delay` argument for `setTimeout()` and `setInterval()`.', |
| 135 | + recommended: 'unopinionated', |
| 136 | + }, |
| 137 | + fixable: 'code', |
| 138 | + schema, |
| 139 | + defaultOptions: [MODE_ALWAYS], |
| 140 | + messages, |
| 141 | + }, |
| 142 | +}; |
| 143 | + |
| 144 | +export default config; |
0 commit comments