Skip to content

Commit

Permalink
fix(betterer 🐛): handle -1 column fixes #1202 (#1214)
Browse files Browse the repository at this point in the history
  • Loading branch information
phenomnomnominal committed Sep 13, 2024
1 parent da704f2 commit d26c395
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/betterer/src/test/file-test/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
40 changes: 40 additions & 0 deletions test/__snapshots__/eslint-no-column-rule.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -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! 🎉
",
]
`;
69 changes: 69 additions & 0 deletions test/eslint-no-column-rule.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit d26c395

Please sign in to comment.