Skip to content

Commit ad049ce

Browse files
Stan-Staniamanda-mitchell
authored andcommitted
fix: correctly disable max-lines
1 parent 4cc6443 commit ad049ce

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

transforms/suppress-eslint-errors.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,18 @@ module.exports = async function codeMod(file, api, options) {
4848
const ruleIdWhitelist = (options.rules || '').split(',').filter((x) => x);
4949
const ruleIdWhitelistSet = ruleIdWhitelist.length ? new Set(ruleIdWhitelist) : null;
5050

51+
let hasMaxLinesRule = false;
52+
5153
for (const { targetLine, ruleId } of targets) {
5254
if (ruleIdWhitelistSet && !ruleIdWhitelistSet.has(ruleId)) {
5355
continue;
5456
}
5557

58+
if (ruleId === 'max-lines') {
59+
hasMaxLinesRule = true;
60+
continue;
61+
}
62+
5663
const pathsStartingOnLine = result
5764
.find('Node', (node) => node.loc && node.loc.start.line === targetLine)
5865
.paths();
@@ -72,6 +79,27 @@ module.exports = async function codeMod(file, api, options) {
7279
addDisableComment(file.path, api, commentText, targetLine, ruleId, firstPathOnLine);
7380
}
7481

82+
// Gotta put the disable max-lines comment near the top of the file
83+
if (hasMaxLinesRule) {
84+
const disableComment = `/* eslint-disable max-lines */\n`;
85+
const todoComment = `/* ${commentText} */\n`;
86+
87+
const useClientMatch = file.source.match(/^(\s*['"]use client['"];?\s*)/);
88+
89+
let updatedSource;
90+
if (useClientMatch) {
91+
// If 'use client' exists, insert comments just after it
92+
const [fullMatch, useClientDirective] = useClientMatch;
93+
updatedSource =
94+
useClientDirective + todoComment + disableComment + file.source.slice(fullMatch.length);
95+
} else {
96+
// If 'use client' doesn't exist, insert comments at the beginning
97+
updatedSource = todoComment + disableComment + file.source;
98+
}
99+
100+
return api.j(updatedSource).toSource();
101+
}
102+
75103
return result.toSource();
76104
};
77105

0 commit comments

Comments
 (0)