Repository navigation
feat(eslint-config): add opt-in canonicalPluginNames for bun lint native rules #61
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
31f7367
feat(eslint-config): add opt-in canonicalPluginNames for bun lint nat…
amondnet 3124429
fix(eslint-config): accept flat config keys in pleaseai() options
amondnet d1d585c
chore(eslint-config): apply AI code review suggestions
amondnet 4a8d10a
fix(eslint-config): look up type specifier styles in a Map
amondnet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
packages/eslint-config/test/fixtures/without-eslint-plugin-import.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // Resolves the config in a process where `eslint-plugin-import` cannot be | ||
| // loaded. Pass `imports=false` to turn the import rules off. | ||
| import process from 'node:process' | ||
| import { plugin } from 'bun' | ||
|
|
||
| plugin({ | ||
| name: 'hide eslint-plugin-import', | ||
| setup(build) { | ||
| build.onLoad({ filter: /[\\/]eslint-plugin-import[\\/]lib[\\/]index\.js$/ }, () => { | ||
| throw new Error('eslint-plugin-import is not installed') | ||
| }) | ||
| }, | ||
| }) | ||
|
|
||
| async function main(): Promise<void> { | ||
| // Imported after the plugin is registered, so the hook sees its loads. | ||
| const { pleaseai } = await import('../../src/index') | ||
| await pleaseai({ canonicalPluginNames: true, imports: process.argv[2] !== 'imports=false' }) | ||
| } | ||
|
|
||
| main().catch((error: unknown) => { | ||
|
Check warning on line 21 in packages/eslint-config/test/fixtures/without-eslint-plugin-import.ts
|
||
| console.error(error) | ||
| process.exitCode = 1 | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| import type { TypedFlatConfigItem } from '../src/index' | ||
| import process from 'node:process' | ||
| import { fileURLToPath } from 'node:url' | ||
| import { describe, expect, test } from 'bun:test' | ||
| import { ESLint } from 'eslint' | ||
| import pluginImport from 'eslint-plugin-import' | ||
| import pleaseai from '../src/index' | ||
|
|
||
| function pluginNames(configs: TypedFlatConfigItem[]): Set<string> { | ||
| return new Set(configs.flatMap(config => Object.keys(config.plugins ?? {}))) | ||
| } | ||
|
|
||
| function ruleIds(configs: TypedFlatConfigItem[]): string[] { | ||
| return configs.flatMap(config => Object.keys(config.rules ?? {})) | ||
| } | ||
|
|
||
| /** Resolves the canonical config in a child process where `eslint-plugin-import` cannot be loaded. */ | ||
| function resolveWithoutEslintPluginImport(...args: string[]) { | ||
| const fixture = fileURLToPath(new URL('./fixtures/without-eslint-plugin-import.ts', import.meta.url)) | ||
| return Bun.spawnSync([process.execPath, fixture, ...args], { stdout: 'ignore', stderr: 'pipe' }) | ||
| } | ||
|
|
||
| function ruleEntry(configs: TypedFlatConfigItem[], id: string): unknown { | ||
| return configs.filter(config => config.rules?.[id] !== undefined).at(-1)?.rules?.[id] | ||
| } | ||
|
|
||
| describe('pleaseai', () => { | ||
| test('keeps antfu\'s plugin names by default', async () => { | ||
| const configs = await pleaseai() | ||
|
|
||
| expect(pluginNames(configs)).toContain('ts') | ||
| expect(pluginNames(configs)).not.toContain('@typescript-eslint') | ||
| expect(configs.find(config => config.name === 'antfu/imports/rules')?.plugins?.import).not.toBe(pluginImport) | ||
| }) | ||
|
|
||
| test('merges rules from options with the PleaseAI defaults', async () => { | ||
| const configs = await pleaseai({ rules: { 'no-console': 'error', 'antfu/top-level-function': 'off' } }) | ||
|
|
||
| expect(ruleEntry(configs, 'no-console')).toBe('error') | ||
| expect(ruleEntry(configs, 'antfu/top-level-function')).toBe('off') | ||
| expect(ruleEntry(configs, 'test/prefer-lowercase-title')).toBe('off') | ||
| }) | ||
| }) | ||
|
|
||
| describe('canonicalPluginNames', () => { | ||
| test('registers typescript-eslint as @typescript-eslint and keeps the other renames', async () => { | ||
| const configs = await pleaseai({ canonicalPluginNames: true }) | ||
| const plugins = pluginNames(configs) | ||
|
|
||
| expect(plugins).toContain('@typescript-eslint') | ||
| expect(plugins).not.toContain('ts') | ||
| expect(plugins).toContain('style') | ||
| expect(plugins).toContain('node') | ||
| expect(ruleIds(configs).filter(id => id.startsWith('ts/'))).toEqual([]) | ||
| }) | ||
|
|
||
| test('renames ts/* rule settings from options and user configs', async () => { | ||
| const configs = await pleaseai( | ||
| { canonicalPluginNames: true, typescript: { overrides: { 'ts/no-explicit-any': 'warn' } } }, | ||
| { rules: { 'ts/array-type': 'error' } }, | ||
| ) | ||
|
|
||
| expect(ruleEntry(configs, '@typescript-eslint/no-explicit-any')).toBe('warn') | ||
| expect(ruleEntry(configs, '@typescript-eslint/array-type')).toBe('error') | ||
| }) | ||
|
|
||
| test('replaces eslint-plugin-import-lite with eslint-plugin-import', async () => { | ||
| const configs = await pleaseai({ canonicalPluginNames: true }) | ||
|
|
||
| expect(configs.find(config => config.name === 'antfu/imports/rules')?.plugins?.import).toBe(pluginImport) | ||
| expect(ruleEntry(configs, 'import/consistent-type-specifier-style')).toEqual(['error', 'prefer-top-level']) | ||
| }) | ||
|
|
||
| test('passes through type specifier styles that need no translation', async () => { | ||
| const rule = 'import/consistent-type-specifier-style' | ||
| const preferred = await pleaseai({ canonicalPluginNames: true, rules: { [rule]: ['error', 'prefer-top-level'] } }) | ||
| // A prototype key must not resolve to an inherited member such as `Object.prototype.toString`. | ||
| const inherited = await pleaseai({ canonicalPluginNames: true, rules: { [rule]: ['error', 'toString' as 'inline'] } }) | ||
|
|
||
| expect(ruleEntry(preferred, rule)).toEqual(['error', 'prefer-top-level']) | ||
| expect(ruleEntry(inherited, rule)).toEqual(['error', 'toString']) | ||
| }) | ||
|
|
||
| test('registers no import plugin when imports are disabled', async () => { | ||
| const configs = await pleaseai({ canonicalPluginNames: true, imports: false }) | ||
|
|
||
| expect(pluginNames(configs)).not.toContain('import') | ||
|
amondnet marked this conversation as resolved.
|
||
| }) | ||
|
|
||
| test('does not load eslint-plugin-import when imports are disabled', () => { | ||
| const result = resolveWithoutEslintPluginImport('imports=false') | ||
|
|
||
| expect(result.stderr.toString()).toBe('') | ||
| expect(result.exitCode).toBe(0) | ||
| }) | ||
|
|
||
| test('explains how to install eslint-plugin-import when it is missing', () => { | ||
| const result = resolveWithoutEslintPluginImport() | ||
|
|
||
| expect(result.exitCode).not.toBe(0) | ||
| expect(result.stderr.toString()).toContain('`canonicalPluginNames` requires `eslint-plugin-import`') | ||
| }) | ||
|
|
||
| test('reports under the canonical rule IDs when linting', async () => { | ||
| const eslint = new ESLint({ | ||
| overrideConfigFile: true, | ||
| overrideConfig: await pleaseai({ canonicalPluginNames: true }), | ||
| }) | ||
| const code = [ | ||
| 'import { type A } from \'./a\'', | ||
| '', | ||
| 'const b = 1', | ||
| '', | ||
| 'import { c } from \'./c\'', | ||
| '', | ||
| 'type D = { d: A }', | ||
| '', | ||
| 'export { b, c }', | ||
| 'export type { D }', | ||
| '', | ||
| ].join('\n') | ||
|
|
||
| const [result] = await eslint.lintText(code, { filePath: 'src/example.ts' }) | ||
| const reported = result!.messages.map(message => message.ruleId) | ||
|
|
||
| expect(result!.messages.filter(message => message.fatal)).toEqual([]) | ||
| expect(reported).toContain('@typescript-eslint/consistent-type-definitions') | ||
| expect(reported).toContain('import/consistent-type-specifier-style') | ||
| expect(reported).toContain('import/first') | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.