Skip to content

Commit

Permalink
fix: fix option.ignores
Browse files Browse the repository at this point in the history
  • Loading branch information
ASafaeirad committed Oct 19, 2024
1 parent 7596f3f commit db9c603
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 43 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,22 @@ $ npm install --save-dev @fullstacksjs/eslint-config eslint prettier

To use the configuration all you need is exporting generated config by `init` function. The configuration reads the metadata from your root `package.json` file and automatically adds the rules and plugins that are needed.

### ESM

```js
import { init } from '@fullstacksjs/eslint-config';

export default init();
```

### CJS

```js
const { init } = require('@fullstacksjs/eslint-config');

module.exports = init();
```

## Modules API

You can fine tune module detection by overriding it, `init` function accepts options as its first argument to control enabled modules.
Expand Down Expand Up @@ -70,7 +80,13 @@ import { init } from '@fullstacksjs/eslint-config';
export default init(
{
typescript: true,
}, {
// You can pass extends here
rules {
'no-console': 'error'
}
},
// And any number of extra configurations
{
files: ['**/*.ts'],
rules: {},
}, {
Expand Down
3 changes: 1 addition & 2 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { init } from './src/index.js';

export default init(
{
ignores: ['cjs/'],
fp: false,
test: true,
esm: true,
Expand All @@ -16,8 +17,6 @@ export default init(
storybook: true,
typescript: true,
tailwind: false,
},
{
rules: {
'max-lines-per-function': 'off',
'complexity': 'off',
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
"module": "./src/index.js",
"exports": {
".": {
"import": "./cjs/index.js",
"require": "./src/index.js"
"import": "./src/index.js",
"require": "./cjs/index.js"
}
},
"type": "module",
Expand Down
78 changes: 53 additions & 25 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { isPackageExists } from 'local-pkg';
import base from './modules/base.js';
import cypress from './modules/cypress.js';
import fp from './modules/functional.js';
import ignores from './modules/ignore.js';
import imports from './modules/imports.js';
import jest from './modules/jest.js';
import next from './modules/next.js';
Expand All @@ -17,11 +18,15 @@ import tailwind from './modules/tailwind.js';
import tests from './modules/tests.js';
import typescript from './modules/typescript.js';
import vitest from './modules/vitest.js';
import { compose } from './utils/compose.js';

const testPackages = ['jest', 'vitest', 'cypress', 'playwright'];

/** @type {import('./option.d.ts').Options} */
/**
* @typedef {import('eslint').Linter.Config} Config
* @typedef {import('./option.d.ts').Options} Options
* /
/** @type {Options} */
const defaultOptions = {
cypress: isPackageExists('cypress'),
disableExpensiveRules: false,
Expand All @@ -41,18 +46,16 @@ const defaultOptions = {
tailwind: isPackageExists('tailwindcss'),
test: testPackages.some(p => isPackageExists(p)),
typescript: isPackageExists('typescript') ? { projects: true } : false,
unocss: isPackageExists('unocss'),
vitest: isPackageExists('vitest'),
};

/**
* Initialize eslint config
*
* @param {import('./option.d.ts').Options} initOptions
* @param {...(import('eslint').Linter.Config)} extend
* @returns {import('eslint').Linter.Config[]}
* @param {Options} initOptions
* @param {...Config} extend
* @returns {Config[]}
*/
export function init(initOptions, ...extend) {
export function init(initOptions = {}, ...extend) {
const options = merge(defaultOptions, initOptions);
if (options.typescript === true) {
options.typescript = {};
Expand All @@ -61,24 +64,49 @@ export function init(initOptions, ...extend) {
options.import = {};
}

const rules = [compose(base, options)];
const {
sort: enableSort,
cypress: enableCypress,
disableExpensiveRules,
esm: enableEsm,
fp: enableFp,
ignores: enableIgnores,
import: enableImport,
jest: enableJest,
next: enableNext,
node: enableNode,
playwright: enablePlaywright,
prettier: enablePrettier,
react: enableReact,
storybook: enableStorybook,
strict: enableStrict,
tailwind: enableTailwind,
test: enableTest,
typescript: enableTypescript,
vitest: enableVitest,
...eslintOptions
} = options;

const rules = [ignores(options), base(options)];

if (options.fp) rules.push(compose(fp, options));
if (options.sort) rules.push(compose(perfectionist, options));
if (options.import) rules.push(compose(imports, options));
if (options.tailwind) rules.push(compose(tailwind, options));
if (options.test) rules.push(compose(tests, options));
if (options.node) rules.push(compose(node, options));
if (options.jest) rules.push(compose(jest, options));
if (options.vitest) rules.push(compose(vitest, options));
if (options.cypress) rules.push(compose(cypress, options));
if (options.react) rules.push(compose(react, options));
if (options.storybook) rules.push(compose(storybook, options));
if (options.typescript) rules.push(compose(typescript, options));
if (options.playwright) rules.push(compose(playwright, options));
if (options.next && options.next) rules.push(compose(next, options));
if (enableFp) rules.push(fp(options));
if (enableSort) rules.push(perfectionist(options));
if (enableImport) rules.push(imports(options));
if (enableTailwind) rules.push(tailwind(options));
if (enableTest) rules.push(tests(options));
if (enableNode) rules.push(node(options));
if (enableJest) rules.push(jest(options));
if (enableVitest) rules.push(vitest(options));
if (enableCypress) rules.push(cypress(options));
if (enableReact) rules.push(react(options));
if (enableStorybook) rules.push(storybook(options));
if (enableTypescript) rules.push(typescript(options));
if (enablePlaywright) rules.push(playwright(options));
if (enableNext && enableNext) rules.push(next(options));
if (enablePrettier) rules.push(prettier(options));

if (options.prettier) rules.push(compose(prettier, options));
if (Object.keys(eslintOptions).length > 0) rules.push(eslintOptions);
if (extend) Array.prototype.push.apply(rules, extend);

return rules.concat(extend);
return rules;
}
12 changes: 12 additions & 0 deletions src/modules/ignore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ignoreGlobs } from '../utils/globs.js';
/**
* @param { import('../option').Options } options
* @return { import('eslint').Linter.Config }
*/
function ignores(options = {}) {
return {
ignores: [...ignoreGlobs, ...(options.ignores ?? [])],
};
}

export default ignores;
4 changes: 3 additions & 1 deletion src/option.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export interface Options {
import type { Linter } from 'eslint';

export interface Options extends Linter.Config {
react?: boolean;
sort?: boolean;
next?: boolean;
Expand Down
12 changes: 0 additions & 12 deletions src/utils/compose.js

This file was deleted.

0 comments on commit db9c603

Please sign in to comment.