Skip to content

Commit

Permalink
fix: correctly disable max-lines
Browse files Browse the repository at this point in the history
  • Loading branch information
Stan Stanislaus authored and amanda-mitchell committed Aug 26, 2024
1 parent 4cc6443 commit ad049ce
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions transforms/suppress-eslint-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,18 @@ module.exports = async function codeMod(file, api, options) {
const ruleIdWhitelist = (options.rules || '').split(',').filter((x) => x);
const ruleIdWhitelistSet = ruleIdWhitelist.length ? new Set(ruleIdWhitelist) : null;

let hasMaxLinesRule = false;

for (const { targetLine, ruleId } of targets) {
if (ruleIdWhitelistSet && !ruleIdWhitelistSet.has(ruleId)) {
continue;
}

if (ruleId === 'max-lines') {
hasMaxLinesRule = true;
continue;
}

const pathsStartingOnLine = result
.find('Node', (node) => node.loc && node.loc.start.line === targetLine)
.paths();
Expand All @@ -72,6 +79,27 @@ module.exports = async function codeMod(file, api, options) {
addDisableComment(file.path, api, commentText, targetLine, ruleId, firstPathOnLine);
}

// Gotta put the disable max-lines comment near the top of the file
if (hasMaxLinesRule) {
const disableComment = `/* eslint-disable max-lines */\n`;
const todoComment = `/* ${commentText} */\n`;

const useClientMatch = file.source.match(/^(\s*['"]use client['"];?\s*)/);

let updatedSource;
if (useClientMatch) {
// If 'use client' exists, insert comments just after it
const [fullMatch, useClientDirective] = useClientMatch;
updatedSource =
useClientDirective + todoComment + disableComment + file.source.slice(fullMatch.length);
} else {
// If 'use client' doesn't exist, insert comments at the beginning
updatedSource = todoComment + disableComment + file.source;
}

return api.j(updatedSource).toSource();
}

return result.toSource();
};

Expand Down

0 comments on commit ad049ce

Please sign in to comment.