Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add clarification on stylistic overrides and Svelte overrides #446

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ export default antfu({
stylistic: {
indent: 2, // 4, or 'tab'
quotes: 'single', // or 'double'
overrides: {
'style/brace-style': ['error', '1tbs', { allowSingleLine: true }],
},
},

// TypeScript and Vue are auto-detected, you can also explicitly enable them:
Expand Down Expand Up @@ -457,6 +460,26 @@ export default antfu({
})
```

For more control over which rules apply to your Svelte files, you apply overrides like this:

```js
// eslint.config.js
import { antfu, svelte, typescript } from '@antfu/eslint-config'

export default antfu(
{},
typescript({
componentExts: ['svelte'],
}),
svelte({
typescript: true,
overrides: {
'svelte/no-dom-manipulating': 'error',
},
}),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not suggest users to directly use those sub configs manually to overrides,

  • It's easy to duplicate configs, for example, here typescript will be created twice with the order messed up
  • You need to know very well how each config work, and duplicated the information like having svelte in ts, and ts in svelte. Could be hard to keep track on it.

Instead, I'd recommend to use the generalized composer: https://github.com/antfu/eslint-config#config-composer

Where you can override, or replace an entire config item as you want.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I updated it to accomplish my overrides via the config composer option so now the Typescript stuff is not duplicated. The Svelte stuff was never duplicated the old way but perhaps it's best to keep it uniform by doing all the overrides this way. Did I do it right this time? 馃槃

So you can see my full example, using the strategy suggested in my pr here, I now have this:

import { antfu } from '@antfu/eslint-config'
import svelteStylistic from 'eslint-plugin-svelte-stylistic'

export default antfu(
  {
    ignores: [
      '**/.svelte-kit**',
      '.eslintcache',
      '**/route/kitbook/**',
    ],
    stylistic: {
      overrides: {
        'style/brace-style': ['error', '1tbs', { allowSingleLine: true }],
      },
    },
    svelte: true,
  },
  {
    name: 'jacob/svelte/stylistic',
    files: ['**/*.svelte', '**/*.composition'],
    plugins: {
      'svelte-stylistic': svelteStylistic,
    },
    rules: {
      'svelte-stylistic/brackets-same-line': 'error',
      'svelte-stylistic/consistent-attribute-lines': 'error',
    },
  },
  {
    name: 'jacob/test/rules',
    files: ['**/*.test.ts'],
    rules: {
      'test/consistent-test-it': ['error', { fn: 'test' }],
      'test/no-commented-out-tests': 'error',
      'test/no-disabled-tests': 'error',
      'test/consistent-test-filename': 'error',
      'test/expect-expect': 'error',
      'test/no-alias-methods': 'error',
      'test/no-conditional-expect': 'error',
      'test/no-conditional-in-test': 'error',
      'test/no-conditional-tests': 'error',
      'test/no-duplicate-hooks': 'error',
      'test/no-focused-tests': 'error',
      'test/no-standalone-expect': 'error',
      'test/no-test-return-statement': 'error',
      'test/prefer-comparison-matcher': 'error',
      'test/prefer-hooks-on-top': 'error',
      'test/prefer-spy-on': 'error',
      'test/prefer-to-be-falsy': 'error',
      'test/prefer-to-be-truthy': 'error',
      'test/prefer-to-contain': 'error',
      'test/prefer-to-have-length': 'error',
      'test/valid-describe-callback': 'error',
      'test/valid-expect': 'error',
    },
  },
).overrides({
  'antfu/typescript/rules': {
    files: ['**/*.svelte', '**/*.composition'],
    rules: {
      'prefer-destructuring': 'error',
      'no-constant-binary-expression': 'error',
      'ts/default-param-last': 'error',
      'require-await': 'error',
      'prefer-object-spread': 'error',
      'no-useless-concat': 'error',
      'no-else-return': 'error',
      'no-console': ['error', { allow: ['warn', 'error', 'info', 'time', 'timeEnd'] }],
      'require-atomic-updates': 'error',
      'style/quotes': ['error', 'single', {
        allowTemplateLiterals: true,
        avoidEscape: true,
      }],
      'ts/no-unused-vars': ['warn', {
        argsIgnorePattern: '^_',
        caughtErrors: 'none',
        ignoreRestSiblings: true,
        varsIgnorePattern: '^\\$\\$Props$',
      }],
      'ts/no-explicit-any': 'warn',
      'prefer-named-capture-group': 'warn',
      'no-undef': 'off',
      'no-unused-vars': 'off',
      'curly': 'off',
      'no-alert': 'off',
      'antfu/if-newline': 'off',
      'ts/ban-ts-comment': 'off',
      'ts/sort-type-constituents': 'off',
    },
  },
  'antfu/svelte/rules': {
    files: ['**/*.composition'],
    overrides: {
      'svelte/valid-compile': ['error', { ignoreWarnings: true }], // because this throws numerous useless a11y errors
      'svelte/no-dom-manipulating': 'error',
      'svelte/no-store-async': 'error',
      'svelte/require-store-reactive-access': 'error',
      'svelte/require-event-dispatcher-types': 'error',
      'svelte/button-has-type': 'error',
      'svelte/no-extra-reactive-curlies': 'error',
      'svelte/mustache-spacing': 'error',
      'svelte/html-closing-bracket-spacing': 'error',
      'svelte/no-reactive-reassign': ['warn', { props: false }],
      'svelte/html-quotes': 'off',
      'svelte/no-at-html-tags': 'off',
      'no-unused-expressions': 'off',
      'no-inner-declarations': 'off',
      'style/space-infix-ops': 'off',
      'no-undef-init': 'off',
      'no-self-assign': 'off',
    },
  },
})

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a bit of general feedback on your docs, it's hard to keep track of and know how to use the different overrides options. For vue and typescript (but not svelte) you can provide them directly in the integrations:

vue: {
  overrides: {
    'vue/operator-linebreak': ['error', 'before'],
  },
},
typescript: {
  overrides: {
    'ts/consistent-type-definitions': ['error', 'interface'],
  },
},

You can provide them as a new ruleset, and then finally using the Config Composer. The section on Config Composer I completely didn't understand the first time reading through. A tip there about getting names from the config inspector would be helpful.

Now finally, after reflecting on everything it seems that if I didn't have my special .composition extension, I could have just done things like this:

export default antfu(
  {
    svelte: true,
  },
  {
    files: ['**/*.svelte'],
    rules: {
      'svelte/no-dom-manipulating': 'error',
    },
  },
).overrides({
  'antfu/typescript/rules': {
    files: ['**/*.svelte'],
    rules: {
      'prefer-destructuring': 'error',
    },
  },
})

Want me to use that method in my doc update instead?

)
```

Running `npx eslint` should prompt you to install the required dependencies, otherwise, you can install them manually:

```bash
Expand Down