Eslint 9 在现有配置中添加规则

问题描述 投票:0回答:1

我的 ES9 配置如下所示 (

eslint.config.js
):

import globals from 'globals';
import pluginJs from '@eslint/js';
import tseslint from 'typescript-eslint';
import stylistic from '@stylistic/eslint-plugin';

export default [
  { 
    files: ['src/js/**/*.{js,mjs,cjs,ts}'], 
  },
  stylistic.configs.customize({
    indent: 2,
    quotes: 'single',
    semi: true,
    jsx: true,
    braceStyle: '1tbs'
  }),
  { languageOptions: { globals: globals.browser } },
  pluginJs.configs.recommended,
  ...tseslint.configs.recommended
];

它有效,但我还需要添加一条限制最大行长度的规则。

Stylistic 有一条规则 @stylistic/js/max-len,但我在添加它时遇到了麻烦。

我尝试了各种方法来添加这个规则,例如:

export default [
  { 
    files: ['src/js/**/*.{js,mjs,cjs,ts}'], 
  },
  stylistic.configs.customize({
    // the following options are the default values
    indent: 2,
    quotes: 'single',
    semi: true,
    jsx: true,
    braceStyle: '1tbs'
  }),
  { languageOptions: { globals: globals.browser } },
  pluginJs.configs.recommended,
  ...tseslint.configs.recommended,
  {
    rules: {
      "@stylistic/js/max-len": ["error", { "code": 40 }],
    },
  },
]

但它不起作用,不仅如此 - 当我添加时它还打破了以前的规则:

  {
    rules: {
      "@stylistic/js/max-len": ["error", { "code": 40 }],
    },
  },
javascript typescript eslint typescript-eslint
1个回答
0
投票

在类似情况下对我有帮助的是在最后

files
部分中明确指定
rules

export default [
  { 
    files: ['src/js/**/*.{js,mjs,cjs,ts}'], 
  },
  stylistic.configs.customize({
    // the following options are the default values
    indent: 2,
    quotes: 'single',
    semi: true,
    jsx: true,
    braceStyle: '1tbs'
  }),
  { languageOptions: { globals: globals.browser } },
  pluginJs.configs.recommended,
  ...tseslint.configs.recommended,
  {
    files: ['src/js/**/*.{js,mjs,cjs,ts}'],
    rules: {
      "@stylistic/js/max-len": ["error", { "code": 40 }],
    },
  },
]
© www.soinside.com 2019 - 2024. All rights reserved.