|
| 1 | +'use strict'; |
| 2 | +const {getStaticValue} = require('@eslint-community/eslint-utils'); |
| 3 | + |
| 4 | +const MESSAGE_ID = 'consistent-empty-array-spread'; |
| 5 | +const messages = { |
| 6 | + [MESSAGE_ID]: 'Prefer using empty {{replacementDescription}} since the {{anotherNodePosition}} is {{anotherNodeDescription}}.', |
| 7 | +}; |
| 8 | + |
| 9 | +const isEmptyArrayExpression = node => |
| 10 | + node.type === 'ArrayExpression' |
| 11 | + && node.elements.length === 0; |
| 12 | + |
| 13 | +const isEmptyStringLiteral = node => |
| 14 | + node.type === 'Literal' |
| 15 | + && node.value === ''; |
| 16 | + |
| 17 | +const isString = (node, context) => { |
| 18 | + const staticValueResult = getStaticValue(node, context.sourceCode.getScope(node)); |
| 19 | + return typeof staticValueResult?.value === 'string'; |
| 20 | +}; |
| 21 | + |
| 22 | +const isArray = (node, context) => { |
| 23 | + if (node.type === 'ArrayExpression') { |
| 24 | + return true; |
| 25 | + } |
| 26 | + |
| 27 | + const staticValueResult = getStaticValue(node, context.sourceCode.getScope(node)); |
| 28 | + return Array.isArray(staticValueResult?.value); |
| 29 | +}; |
| 30 | + |
| 31 | +const cases = [ |
| 32 | + { |
| 33 | + oneSidePredicate: isEmptyStringLiteral, |
| 34 | + anotherSidePredicate: isArray, |
| 35 | + anotherNodeDescription: 'an array', |
| 36 | + replacementDescription: 'array', |
| 37 | + replacementCode: '[]', |
| 38 | + }, |
| 39 | + { |
| 40 | + oneSidePredicate: isEmptyArrayExpression, |
| 41 | + anotherSidePredicate: isString, |
| 42 | + anotherNodeDescription: 'a string', |
| 43 | + replacementDescription: 'string', |
| 44 | + replacementCode: '\'\'', |
| 45 | + }, |
| 46 | +]; |
| 47 | + |
| 48 | +function createProblem({ |
| 49 | + problemNode, |
| 50 | + anotherNodePosition, |
| 51 | + anotherNodeDescription, |
| 52 | + replacementDescription, |
| 53 | + replacementCode, |
| 54 | +}) { |
| 55 | + return { |
| 56 | + node: problemNode, |
| 57 | + messageId: MESSAGE_ID, |
| 58 | + data: { |
| 59 | + replacementDescription, |
| 60 | + anotherNodePosition, |
| 61 | + anotherNodeDescription, |
| 62 | + }, |
| 63 | + fix: fixer => fixer.replaceText(problemNode, replacementCode), |
| 64 | + }; |
| 65 | +} |
| 66 | + |
| 67 | +function getProblem(conditionalExpression, context) { |
| 68 | + const { |
| 69 | + consequent, |
| 70 | + alternate, |
| 71 | + } = conditionalExpression; |
| 72 | + |
| 73 | + for (const problemCase of cases) { |
| 74 | + const { |
| 75 | + oneSidePredicate, |
| 76 | + anotherSidePredicate, |
| 77 | + } = problemCase; |
| 78 | + |
| 79 | + if (oneSidePredicate(consequent, context) && anotherSidePredicate(alternate, context)) { |
| 80 | + return createProblem({ |
| 81 | + ...problemCase, |
| 82 | + problemNode: consequent, |
| 83 | + anotherNodePosition: 'alternate', |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + if (oneSidePredicate(alternate, context) && anotherSidePredicate(consequent, context)) { |
| 88 | + return createProblem({ |
| 89 | + ...problemCase, |
| 90 | + problemNode: alternate, |
| 91 | + anotherNodePosition: 'consequent', |
| 92 | + }); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/** @param {import('eslint').Rule.RuleContext} context */ |
| 98 | +const create = context => ({ |
| 99 | + * ArrayExpression(arrayExpression) { |
| 100 | + for (const element of arrayExpression.elements) { |
| 101 | + if ( |
| 102 | + element?.type !== 'SpreadElement' |
| 103 | + || element.argument.type !== 'ConditionalExpression' |
| 104 | + ) { |
| 105 | + continue; |
| 106 | + } |
| 107 | + |
| 108 | + yield getProblem(element.argument, context); |
| 109 | + } |
| 110 | + }, |
| 111 | +}); |
| 112 | + |
| 113 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 114 | +module.exports = { |
| 115 | + create, |
| 116 | + meta: { |
| 117 | + type: 'suggestion', |
| 118 | + docs: { |
| 119 | + description: 'Prefer consistent types when spreading a ternary in an array literal.', |
| 120 | + recommended: true, |
| 121 | + }, |
| 122 | + fixable: 'code', |
| 123 | + |
| 124 | + messages, |
| 125 | + }, |
| 126 | +}; |
0 commit comments