Skip to content

Commit

Permalink
fix: eslint-config cypress and storybook definitions fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
SimeonC committed Dec 5, 2023
1 parent 74f4ff3 commit 699d82a
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 64 deletions.
5 changes: 3 additions & 2 deletions packages/eslint-config/README.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ eslint --cache --ext .js,.jsx,.ts,.tsx --format=pretty ./

#### Specific Presets

- `@tablecheck/eslint-config/basic` - Rules for vanilla javascript only repositories.
- `@tablecheck/eslint-config/basic` - Rules for vanilla javascript only repositories. (Recommend using Typescript over this)
- `@tablecheck/eslint-config/typescript` - Rules for vanilla typescript projects
- `@tablecheck/eslint-config/react` - Rules for react javascript project
- `@tablecheck/eslint-config/react-typescript` - Rules for react typescript project
- `@tablecheck/eslint-config/cypress` - Rules for Cypress projects in typescript

The following cypress presets should be used with one of the above Specific Presets

- `@tablecheck/eslint-config/cypress` - Rules for Cypress projects (applies to all files)
- `@tablecheck/eslint-config/cypress-component` - Use this when adding component testing to a project, it adds an override for `*.cypress.*` and `*.cy.*` files
- `@tablecheck/eslint-config/storybook` - This adds an override for story/stories files, use with typescript package

For example, a react typescript project with cypress component testing should use the following;

Expand Down
5 changes: 5 additions & 0 deletions packages/eslint-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
"main": "./dist/presets/reactTs.js",
"default": "./dist/presets/reactTs.js"
},
"./storybook": {
"types": "./dist/presets/storybook.d.ts",
"main": "./dist/presets/storybook.js",
"default": "./dist/presets/storybook.js"
},
"./package.json": "./package.json"
},
"main": "./dist/index.js",
Expand Down
10 changes: 2 additions & 8 deletions packages/eslint-config/src/overrides/buildBaseTypescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,13 @@ export const baseTypescriptRules: Linter.RulesRecord = {
'@tablecheck/prefer-shortest-import': 'error',
};

export function buildBaseTypescript<
T extends Linter.RulesRecord,
TForced extends Linter.RulesRecord,
>({
export function buildBaseTypescript<T extends Linter.RulesRecord>({
files,
rules,
forcedRules,
...options
}: {
files: Linter.ConfigOverride['files'];
rules: T;
forcedRules?: TForced;
} & Omit<
Linter.ConfigOverride,
'parser' | 'extends' | 'plugins' | 'settings' | 'rules' | 'files'
Expand Down Expand Up @@ -86,9 +81,8 @@ export function buildBaseTypescript<
},
},
rules: {
...rules,
...baseTypescriptRules,
...forcedRules,
...rules,
},
};
}
4 changes: 4 additions & 0 deletions packages/eslint-config/src/overrides/storybook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export const storybookOverrides: Linter.ConfigOverride = {
'react-hooks/exhaustive-deps': 'off',
'react/function-component-definition': 'off',
'react/jsx-no-constructed-context-values': 'off',
'react/jsx-props-no-spreading': 'off',
'react/react-in-jsx-scope': 'off',
'react/prop-types': 'off',
'react/require-default-props': 'off',
'react-refresh/only-export-components': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/naming-convention': 'off',
Expand Down
2 changes: 0 additions & 2 deletions packages/eslint-config/src/overrides/typescriptDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ export const typescriptDefinitionOverrides = buildBaseTypescript({
...reactRules,
...promiseRules,
...emotionRules,
},
forcedRules: {
'import/no-default-export': 'off',
'vars-on-top': 'off',
'no-unused-vars': 'off',
Expand Down
8 changes: 1 addition & 7 deletions packages/eslint-config/src/presets/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { Linter } from 'eslint';

import { rootConfigsOverrides } from '../overrides/rootConfigs';
import { scriptsOverrides } from '../overrides/scripts';
import { storybookOverrides } from '../overrides/storybook';
import { testOverrides } from '../overrides/tests';
import { emotionRules } from '../rules/emotion';
import { generalRules } from '../rules/general';
Expand Down Expand Up @@ -36,10 +35,5 @@ module.exports = {
...emotionRules,
},

overrides: [
rootConfigsOverrides,
scriptsOverrides,
testOverrides,
storybookOverrides,
],
overrides: [rootConfigsOverrides, scriptsOverrides, testOverrides],
} satisfies Linter.Config;
44 changes: 2 additions & 42 deletions packages/eslint-config/src/presets/cypress.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,3 @@
import type { Linter } from 'eslint';
import { cypressPreset } from './cypressInternal';

import { testOverrides as testRules } from '../overrides/tests';
import { namingRules } from '../rules/namingConvention';

if (!process.env.NODE_ENV) {
// This check allows us to run linters inside IDE's
process.env.NODE_ENV = 'development';
}

const testOverrides = Object.keys(testRules.rules ?? {}).reduce(
(result, ruleKey) => ({ ...result, [ruleKey]: 'off' }),
{},
);

module.exports = {
env: {
'cypress/globals': true,
},
rules: {
...testOverrides,
'promise/catch-or-return': 'off',
'promise/always-return': 'off',
'import/no-import-module-exports': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-namespace': 'off',
'@typescript-eslint/naming-convention': (
['error'] as Linter.RuleLevelAndOptions
).concat(
(
namingRules[
'@typescript-eslint/naming-convention'
] as Linter.RuleLevelAndOptions
).slice(1),
[
{
selector: 'memberLike',
format: null,
},
],
) as Linter.RuleLevelAndOptions,
},
} satisfies Linter.Config;
module.exports = cypressPreset;
6 changes: 5 additions & 1 deletion packages/eslint-config/src/presets/cypressComponent.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import type { Linter } from 'eslint';

import { cypressPreset } from './cypressInternal';

if (!process.env.NODE_ENV) {
// This check allows us to run linters inside IDE's
process.env.NODE_ENV = 'development';
}

const { extends: extendPreset, ...config } = cypressPreset;

module.exports = {
overrides: [
{
files: ['**/cypress/**/*', '**/*.{cy,cypress}.{js,jsx,ts,tsx}'],
extends: ['@tablecheck/eslint-config/cypress'],
...config,
},
],
} satisfies Linter.Config;
45 changes: 45 additions & 0 deletions packages/eslint-config/src/presets/cypressInternal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { Linter } from 'eslint';

import { testOverrides as testRules } from '../overrides/tests';
import { namingRules } from '../rules/namingConvention';

if (!process.env.NODE_ENV) {
// This check allows us to run linters inside IDE's
process.env.NODE_ENV = 'development';
}

const testOverrides = Object.keys(testRules.rules ?? {}).reduce(
(result, ruleKey) => ({ ...result, [ruleKey]: 'off' }),
{},
);

export const cypressPreset = {
plugins: ['cypress'],
extends: ['@tablecheck/eslint-config/typescript'],
env: {
'cypress/globals': true,
},
rules: {
...testOverrides,
'promise/catch-or-return': 'off',
'promise/always-return': 'off',
'import/no-import-module-exports': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-namespace': 'off',
'@typescript-eslint/naming-convention': (
['error'] as Linter.RuleLevelAndOptions
).concat(
(
namingRules[
'@typescript-eslint/naming-convention'
] as Linter.RuleLevelAndOptions
).slice(1),
[
{
selector: 'memberLike',
format: null,
},
],
) as Linter.RuleLevelAndOptions,
},
} satisfies Linter.Config;
17 changes: 17 additions & 0 deletions packages/eslint-config/src/presets/storybook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { Linter } from 'eslint';

import { storybookOverrides } from '../overrides/storybook';

if (!process.env.NODE_ENV) {
// This check allows us to run linters inside IDE's
process.env.NODE_ENV = 'development';
}

module.exports = {
overrides: [
{
extends: ['@tablecheck/eslint-config/typescript'],
...storybookOverrides,
},
],
} satisfies Linter.Config;
2 changes: 0 additions & 2 deletions packages/eslint-config/src/presets/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Linter } from 'eslint';

import { buildBaseTypescript } from '../overrides/buildBaseTypescript';
import { storybookOverrides } from '../overrides/storybook';
import { testOverrides } from '../overrides/tests';
import { emotionRules } from '../rules/emotion';
import { generalRules } from '../rules/general';
Expand Down Expand Up @@ -61,6 +60,5 @@ module.exports = {
node: true,
},
}),
buildBaseTypescript(storybookOverrides as never),
],
} satisfies Linter.Config;

0 comments on commit 699d82a

Please sign in to comment.