Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix adding a disable comment in template literal #369

Merged
merged 5 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ module.exports = {
],
},
],
// 煩い上にこれに怒られてもリファクタリングしようという気持ちにならないので off
'max-params': 'off',
},
overrides: [
// for typescript
Expand Down
4 changes: 4 additions & 0 deletions fixtures/lib/add-disable-comment.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ const jsx = (
<div>{2 ** 10}</div>
</>
);
const foo = `
This is a template literal
${2 ** 10}
`;
2 changes: 2 additions & 0 deletions src/fix/disable-per-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ function generateFix(
fixer,
sourceCode,
line: lineToInsert,
column: 0,
description,
}),
);
Expand All @@ -76,6 +77,7 @@ function generateFix(
fixer,
sourceCode,
line: lineToInsert,
column: 0,
scope: 'file',
ruleIds: ruleIdsToDisable,
description: isPreviousLine ? undefined : description,
Expand Down
42 changes: 42 additions & 0 deletions src/fix/disable-per-line.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,48 @@ describe('disable-per-line', () => {
var val3"
`);
});
test('add a disable comment in template literal', async () => {
expect(
await tester.test({
code: [
'`',
// eslint-disable-next-line no-template-curly-in-string
'${void 1}',
// eslint-disable-next-line no-template-curly-in-string
'${0 + void 1}',
'${',
'void 1',
'}',
// eslint-disable-next-line no-template-curly-in-string
'${`${void 1}`}',
'`;',
// MEMO: Code that includes indents can be fixed, but it will not be formatted prettily. This is a limitation of eslint-interactive.
'const withIndent = `',
// eslint-disable-next-line no-template-curly-in-string
' ${void 1}',
'`;',
],
rules: { 'no-void': 'error' },
}),
).toMatchInlineSnapshot(`
"\`
\${// eslint-disable-next-line no-void
void 1}
\${// eslint-disable-next-line no-void
0 + void 1}
\${
// eslint-disable-next-line no-void
void 1
}
\${\`\${// eslint-disable-next-line no-void
void 1}\`}
\`;
const withIndent = \`
\${ // eslint-disable-next-line no-void
void 1}
\`;"
`);
});
describe('add a disable comment for JSX', () => {
test('when descriptionPosition is sameLine', async () => {
expect(
Expand Down
19 changes: 16 additions & 3 deletions src/fix/disable-per-line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { mergeFixes } from '../eslint/report-translator.js';
import { groupBy, unique } from '../util/array.js';
import {
DisableComment,
getStartColumnOfTemplateExpression,
insertDescriptionCommentStatementBeforeLine,
insertDisableCommentStatementBeforeLine,
isLineInTemplateLiteral,
mergeDescription,
mergeRuleIds,
parseDisableComment,
Expand All @@ -30,6 +32,7 @@ function generateFixesPerLine(
description: string | undefined,
descriptionPosition: DescriptionPosition | undefined,
line: number,
column: number,
messagesInLine: Linter.LintMessage[],
): Rule.Fix | null {
const { fixer, sourceCode } = context;
Expand All @@ -48,6 +51,7 @@ function generateFixesPerLine(
fixer,
sourceCode,
line: disableCommentPerLine ? disableCommentPerLine.loc.start.line : line,
column,
description,
}),
);
Expand All @@ -69,6 +73,7 @@ function generateFixesPerLine(
fixer,
sourceCode,
line,
column,
scope: 'next-line',
ruleIds: ruleIdsToDisable,
description: isPreviousLine ? undefined : description,
Expand All @@ -82,10 +87,18 @@ function generateFixesPerLine(
* Create fix to add disable comment per line.
*/
export function createFixToDisablePerLine(context: FixContext, args: FixToDisablePerLineArgs): Rule.Fix[] {
const lineToMessages = groupBy(context.messages, (message) => message.line);
const groupedMessages = groupBy(context.messages, (message) => {
if (isLineInTemplateLiteral(context.sourceCode, message.line)) {
const column = getStartColumnOfTemplateExpression(context.sourceCode, message);
return `${message.line}:${column}`;
} else {
return `${message.line}:0`;
}
});
const fixes: Rule.Fix[] = [];
for (const [line, messagesInLine] of lineToMessages) {
const fix = generateFixesPerLine(context, args.description, args.descriptionPosition, line, messagesInLine);
for (const [lineAndColumn, messagesInLine] of groupedMessages) {
const [line, column] = lineAndColumn.split(':').map(Number) as [number, number];
const fix = generateFixesPerLine(context, args.description, args.descriptionPosition, line, column, messagesInLine);
if (fix) fixes.push(fix);
}
return fixes;
Expand Down
31 changes: 27 additions & 4 deletions src/util/eslint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,27 @@ function isLineInJSXText(sourceCode: SourceCode, line: number): boolean {
return headNode?.type === 'JSXText';
}

export function isLineInTemplateLiteral(sourceCode: SourceCode, line: number): boolean {
const headNodeIndex = sourceCode.getIndexFromLoc({ line, column: 0 });
const headNode = sourceCode.getNodeByRangeIndex(headNodeIndex);
return headNode?.type === 'TemplateElement';
}

export function getStartColumnOfTemplateExpression(sourceCode: SourceCode, message: Linter.LintMessage): number {
for (let i = message.column; i >= 1; i--) {
const index = sourceCode.getIndexFromLoc({
line: message.line,
// Convert 1-indexed to 0-indexed
column: i - 1,
});
const node = sourceCode.getNodeByRangeIndex(index);
if (node?.type === 'TemplateElement') {
return i;
}
}
throw new Error(`unreachable: The line ${message.line} does not have a template element.`);
}

/**
* Merge the ruleIds of the disable comments.
* @param a The ruleIds of first disable comment
Expand Down Expand Up @@ -136,11 +157,12 @@ export function insertDescriptionCommentStatementBeforeLine(args: {
fixer: Rule.RuleFixer;
sourceCode: SourceCode;
line: number;
column: number;
description: string;
}): Rule.Fix {
const { fixer, sourceCode, line, description } = args;
const { fixer, sourceCode, line, column, description } = args;
const indent = getIndentFromLine(sourceCode, line);
const headNodeIndex = sourceCode.getIndexFromLoc({ line, column: 0 });
const headNodeIndex = sourceCode.getIndexFromLoc({ line, column });

if (isLineInJSXText(sourceCode, line)) {
const commentText = toCommentText({ type: 'Block', text: description });
Expand Down Expand Up @@ -175,13 +197,14 @@ export function insertDisableCommentStatementBeforeLine(args: {
fixer: Rule.RuleFixer;
sourceCode: SourceCode;
line: number;
column: number;
scope: 'file' | 'next-line';
ruleIds: string[];
description: string | undefined;
}) {
const { fixer, sourceCode, line, scope, ruleIds, description } = args;
const { fixer, sourceCode, line, column, scope, ruleIds, description } = args;
const indent = getIndentFromLine(sourceCode, line);
const headNodeIndex = sourceCode.getIndexFromLoc({ line, column: 0 });
const headNodeIndex = sourceCode.getIndexFromLoc({ line, column });
const isInJSXText = isLineInJSXText(sourceCode, line);
const type = isInJSXText || scope === 'file' ? 'Block' : 'Line';
const disableCommentText = toDisableCommentText({
Expand Down
Loading