|
| 1 | +function closingBracket(tokens, openingIndex) { |
| 2 | + let depth = 0; |
| 3 | + for (let index = openingIndex; index < tokens.length; index += 1) { |
| 4 | + if (tokens[index].value === "[") depth += 1; |
| 5 | + if (tokens[index].value === "]") depth -= 1; |
| 6 | + if (depth === 0) return index; |
| 7 | + } |
| 8 | + return undefined; |
| 9 | +} |
| 10 | + |
| 11 | +function parseTopLevelEntries(tokens, source, startIndex, endIndex) { |
| 12 | + const entries = []; |
| 13 | + let currentStart = startIndex; |
| 14 | + let parenDepth = 0; |
| 15 | + let bracketDepth = 0; |
| 16 | + let braceDepth = 0; |
| 17 | + |
| 18 | + for (let index = startIndex; index < endIndex; index += 1) { |
| 19 | + const token = tokens[index]; |
| 20 | + if (token.value === "(") parenDepth += 1; |
| 21 | + if (token.value === ")") parenDepth -= 1; |
| 22 | + if (token.value === "[") bracketDepth += 1; |
| 23 | + if (token.value === "]") bracketDepth -= 1; |
| 24 | + if (token.value === "{") braceDepth += 1; |
| 25 | + if (token.value === "}") braceDepth -= 1; |
| 26 | + |
| 27 | + if ( |
| 28 | + token.value === "," |
| 29 | + && parenDepth === 0 |
| 30 | + && bracketDepth === 0 |
| 31 | + && braceDepth === 0 |
| 32 | + ) { |
| 33 | + const start = tokens[currentStart].start; |
| 34 | + const end = token.start; |
| 35 | + entries.push({ start, end, text: source.slice(start, end).trim() }); |
| 36 | + currentStart = index + 1; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + if (currentStart < endIndex) { |
| 41 | + const start = tokens[currentStart].start; |
| 42 | + const end = tokens[endIndex - 1].end; |
| 43 | + entries.push({ start, end, text: source.slice(start, end).trim() }); |
| 44 | + } |
| 45 | + |
| 46 | + return entries.filter((entry) => entry.text.length > 0); |
| 47 | +} |
| 48 | + |
| 49 | +function isRepairCall(text) { |
| 50 | + return /^repair\s*\(/.test(text); |
| 51 | +} |
| 52 | + |
| 53 | +function isSteeringCall(text) { |
| 54 | + return /^steering\s*\(/.test(text); |
| 55 | +} |
| 56 | + |
| 57 | +export function scanTokens(tokens, source = "") { |
| 58 | + const problems = []; |
| 59 | + |
| 60 | + for (let index = 0; index <= tokens.length - 3; index += 1) { |
| 61 | + const [key, colon, openBracket] = tokens.slice(index, index + 3); |
| 62 | + if ( |
| 63 | + key.value !== "addons" |
| 64 | + || colon.value !== ":" |
| 65 | + || openBracket.value !== "[" |
| 66 | + ) { |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + const closeIndex = closingBracket(tokens, index + 2); |
| 71 | + if (closeIndex === undefined) continue; |
| 72 | + |
| 73 | + const entries = parseTopLevelEntries(tokens, source, index + 3, closeIndex); |
| 74 | + if (entries.length !== 2) continue; |
| 75 | + |
| 76 | + const repairIndex = entries.findIndex((entry) => isRepairCall(entry.text)); |
| 77 | + const steeringIndex = entries.findIndex((entry) => isSteeringCall(entry.text)); |
| 78 | + if (repairIndex === -1 || steeringIndex === -1 || repairIndex > steeringIndex) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + const fixedText = `[${entries[steeringIndex].text}, ${entries[repairIndex].text}]`; |
| 83 | + problems.push({ |
| 84 | + start: openBracket.start, |
| 85 | + end: tokens[closeIndex].end, |
| 86 | + message: "Use addons: [steering(), repair()] so steering can wrap repair retries.", |
| 87 | + kind: "addon-order", |
| 88 | + edits: [{ start: openBracket.start, end: tokens[closeIndex].end, text: fixedText }], |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + return problems; |
| 93 | +} |
| 94 | + |
| 95 | +export default { |
| 96 | + meta: { |
| 97 | + type: "problem", |
| 98 | + docs: { |
| 99 | + description: "Enforce addons order as [steering(), repair()]", |
| 100 | + }, |
| 101 | + fixable: "code", |
| 102 | + schema: [], |
| 103 | + messages: { |
| 104 | + order: "Use addons: [steering(), repair()] so steering can wrap repair retries.", |
| 105 | + }, |
| 106 | + }, |
| 107 | + create(context) { |
| 108 | + return { |
| 109 | + Property(node) { |
| 110 | + if ( |
| 111 | + node.key.type !== "Identifier" |
| 112 | + || node.key.name !== "addons" |
| 113 | + || node.value.type !== "ArrayExpression" |
| 114 | + || node.value.elements.length !== 2 |
| 115 | + ) { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + const [first, second] = node.value.elements; |
| 120 | + if ( |
| 121 | + first?.type !== "CallExpression" |
| 122 | + || second?.type !== "CallExpression" |
| 123 | + || first.callee.type !== "Identifier" |
| 124 | + || second.callee.type !== "Identifier" |
| 125 | + || first.callee.name !== "repair" |
| 126 | + || second.callee.name !== "steering" |
| 127 | + ) { |
| 128 | + return; |
| 129 | + } |
| 130 | + |
| 131 | + context.report({ |
| 132 | + node: node.value, |
| 133 | + messageId: "order", |
| 134 | + fix(fixer) { |
| 135 | + const sourceCode = context.sourceCode; |
| 136 | + const steeringText = sourceCode.getText(second); |
| 137 | + const repairText = sourceCode.getText(first); |
| 138 | + return fixer.replaceText(node.value, `[${steeringText}, ${repairText}]`); |
| 139 | + }, |
| 140 | + }); |
| 141 | + }, |
| 142 | + }; |
| 143 | + }, |
| 144 | +}; |
0 commit comments