From d26c3958219a87168d21afbca53656fb0082d109 Mon Sep 17 00:00:00 2001 From: Craig Spence Date: Sat, 14 Sep 2024 08:53:27 +1200 Subject: [PATCH] =?UTF-8?q?fix(betterer=20=F0=9F=90=9B):=20handle=20-1=20c?= =?UTF-8?q?olumn=20fixes=20#1202=20(#1214)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/betterer/src/test/file-test/file.ts | 8 ++- .../eslint-no-column-rule.spec.ts.snap | 40 +++++++++++ test/eslint-no-column-rule.spec.ts | 69 +++++++++++++++++++ 3 files changed, 114 insertions(+), 3 deletions(-) create mode 100644 test/__snapshots__/eslint-no-column-rule.spec.ts.snap create mode 100644 test/eslint-no-column-rule.spec.ts diff --git a/packages/betterer/src/test/file-test/file.ts b/packages/betterer/src/test/file-test/file.ts index fd142e23d..3940f8208 100644 --- a/packages/betterer/src/test/file-test/file.ts +++ b/packages/betterer/src/test/file-test/file.ts @@ -103,10 +103,12 @@ function getIssueFromPositions( return null; } const [line, column, endLine, endColumn, message, overrideHash] = issueOverride; - const start = lc.indexForLocation({ line, column: Math.max(0, column) }) ?? 0; - const end = lc.indexForLocation({ line: endLine, column: Math.max(0, endColumn) }) ?? 0; + const absStartColumn = Math.max(0, column); + const absEndColumn = Math.max(0, endColumn); + const start = lc.indexForLocation({ line, column: absStartColumn }) ?? 0; + const end = lc.indexForLocation({ line: endLine, column: absEndColumn }) ?? 0; const length = end - start; - return [line, column, length, message, overrideHash]; + return [line, absStartColumn, length, message, overrideHash]; } function isPositions(issueOverride: BettererIssueOverride): issueOverride is BettererIssuePositions { diff --git a/test/__snapshots__/eslint-no-column-rule.spec.ts.snap b/test/__snapshots__/eslint-no-column-rule.spec.ts.snap new file mode 100644 index 000000000..be8c27e52 --- /dev/null +++ b/test/__snapshots__/eslint-no-column-rule.spec.ts.snap @@ -0,0 +1,40 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`betterer > should handle eslint rules that mark the entire line of code 1`] = ` +"// BETTERER RESULTS V2. +// +// If this file contains merge conflicts, use \`betterer merge\` to automatically resolve them: +// https://phenomnomnominal.github.io/betterer/docs/results-file/#merge +// +exports[\`test\`] = { + value: \`{ + "src/index.ts:3395480044": [ + [0, 0, 27, "Unexpected unlimited \\'eslint-disable-next-line\\' comment. Specify some rule names to disable.", "1912322876"], + [0, 0, 27, "Unexpected undescribed directive comment. Include descriptions to explain why the comment is necessary.", "1912322876"] + ] + }\` +}; +" +`; + +exports[`betterer > should handle eslint rules that mark the entire line of code 2`] = ` +[ + "🌟 Betterer (0ms): +", + "🌟 Betterer (0ms): 1 test running... +🤔 test: running "test"! +", + "🌟 Betterer (0ms): 1 test running... +✅ test: "test" got checked for the first time! (2 issues) 🎉 +", + "🎉 Betterer (0ms): 1 test done! +✅ test: "test" got checked for the first time! (2 issues) 🎉 +", + "🎉 Betterer (0ms): 1 test done! +✅ test: "test" got checked for the first time! (2 issues) 🎉 + +1 test got checked. 🤔 +1 test got checked for the first time! 🎉 +", +] +`; diff --git a/test/eslint-no-column-rule.spec.ts b/test/eslint-no-column-rule.spec.ts new file mode 100644 index 000000000..96902ca7d --- /dev/null +++ b/test/eslint-no-column-rule.spec.ts @@ -0,0 +1,69 @@ +import { describe, expect, it } from 'vitest'; + +import { createFixture } from './fixture.js'; + +describe('betterer', () => { + // See: https://github.com/phenomnomnominal/betterer/issues/1202 + it('should handle eslint rules that mark the entire line of code', async () => { + const { betterer } = await import('@betterer/betterer'); + + const { logs, paths, readFile, cleanup, resolve, writeFile } = await createFixture('eslint-no-column-rule', { + '.betterer.ts': ` +import { eslint } from '@betterer/eslint'; + +export default { + test: () => eslint({ + rules: { + '@eslint-community/eslint-comments/require-description': 'error' + } + }) + .include('./src/**/*.ts') +}; + `, + 'eslint.config.js': ` +import eslint from '@eslint/js'; +import tslint from 'typescript-eslint'; +import comments from '@eslint-community/eslint-plugin-eslint-comments/configs'; + +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +export default tslint.config( + eslint.configs.recommended, + ...tslint.configs.recommended, + comments.recommended, + { + languageOptions: { + parserOptions: { + project: "./tsconfig.json", + tsconfigRootDir: path.dirname(fileURLToPath(import.meta.url)) + }, + }, + }, + { rules: { '@eslint-community/eslint-comments/require-description': 'off' } } +); + `, + 'tsconfig.json': ` +{ + "include": ["./src/**/*"] +} + ` + }); + + const configPaths = [paths.config]; + const resultsPath = paths.results; + const indexPath = resolve('./src/index.ts'); + + await writeFile(indexPath, `// eslint-disable-next-line\ndebugger;`); + + await betterer({ configPaths, resultsPath, workers: false }); + + const result = await readFile(resultsPath); + + expect(result).toMatchSnapshot(); + + expect(logs).toMatchSnapshot(); + + await cleanup(); + }); +});