Skip to content

Commit

Permalink
feat: support flat configs (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
43081j authored May 15, 2024
1 parent 87c2be9 commit dd9d8d7
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ Or if you're using (flat) config files, add to your `eslint.config.js`:
import {configs} from 'eslint-plugin-depend';

export default [
configs.recommended,
configs['flat/recommended'],

// or if you want to specify `files`, or other options
{
...configs.recommended,
...configs['flat/recommended'],
files: ['test/**/*.js']
}
];
Expand Down
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = [
rules: {
'comma-dangle': ['error', 'never'],
'indent': 'off',
'quote-props': 'off',
'max-len': ['error', {
ignoreTemplateLiterals: true,
ignoreStrings: true
Expand Down
8 changes: 8 additions & 0 deletions src/configs/legacy-recommended.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type {ESLint} from 'eslint';

export const config: ESLint.ConfigData = {
plugins: ['depend'],
rules: {
'depend/ban-dependencies': 'error'
}
};
15 changes: 11 additions & 4 deletions src/configs/recommended.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
export const recommended = {
plugins: ['depend'],
rules: {}
};
import type {ESLint, Linter} from 'eslint';

export const configFactory = (plugin: ESLint.Plugin): Linter.FlatConfig => ({
plugins: {
depend: plugin
},

rules: {
'depend/ban-dependencies': 'error'
}
});
15 changes: 10 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import {recommended} from './configs/recommended.js';
import type {Rule, ESLint} from 'eslint';
import {configFactory as configRecommended} from './configs/recommended.js';
import {config as configLegacyRecommended} from './configs/legacy-recommended.js';
import {rule as banDependencies} from './rules/ban-dependencies.js';

export const configs = {
recommended
export const rules: Record<string, Rule.RuleModule> = {
'ban-dependencies': banDependencies
};

export const rules = {
'ban-dependencies': banDependencies
const plugin: ESLint.Plugin = {rules};

export const configs = {
recommended: configLegacyRecommended,
'flat/recommended': configRecommended(plugin)
};
19 changes: 19 additions & 0 deletions src/test/configs_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type {ESLint, Linter} from 'eslint';
import * as assert from 'node:assert/strict';
import {test} from 'node:test';
import {configs} from '../main.js';

type ConfigLike = Linter.FlatConfig | ESLint.ConfigData;

const isFlatConfig = (config: ConfigLike): config is Linter.FlatConfig =>
!Array.isArray(config.plugins);

test('configs', async (t) => {
await t.test('should define configs correctly', () => {
assert.ok(configs['recommended']);
assert.ok(configs['flat/recommended']);

assert.ok(isFlatConfig(configs['flat/recommended']));
assert.equal(isFlatConfig(configs['recommended']), false);
});
});

0 comments on commit dd9d8d7

Please sign in to comment.