You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ESLint Flat配置中.eslintignore失效,JS文件仍被TypeScript规则检测

问题描述

我使用ESLint Flat配置文件,已指定仅检测**/*.ts文件,且在根目录的.eslintignore中设置忽略所有**/*.js文件,但运行lint任务时仍有部分JS文件被TypeScript规则报错。自定义规则部分仅检测.ts文件,功能正常,请帮忙排查问题原因。

配置文件内容
'use strict';

const customEslintRulesPlugin = require("./custom-lint-rules/eslint-custom-rules-plugin"); 
const typeScriptEsLintPlugin = require('@typescript-eslint/eslint-plugin'); 
const { FlatCompat } = require('@eslint/eslintrc');

// 转换ESLintRC风格配置为Flat配置
// @typescript-eslint暂不支持Flat配置,因此需要引入@eslint/eslintrc
const compat = new FlatCompat({   
  baseDirectory: __dirname,   
  recommendedConfig: typeScriptEsLintPlugin.configs['recommended'], 
});

module.exports = [   
  'eslint:recommended',

  // 自定义规则(位于/custom-lint-rules目录)
  {
    files: ["**/*.ts"],
    languageOptions: {
      sourceType: "module",
      ecmaVersion: "latest",
    },
    plugins: { customRule: customEslintRulesPlugin },
    rules: {
      "customRule/do-not-use-should": "error",
      "customRule/async-describe": "error",
    },   
  },

  // TypeScript与Playwright规则的Flat兼容配置
  ...compat.config({
    overrides: [
      {
        files: ["**/*.ts"],
        env: { node: true },
        extends: ['plugin:@typescript-eslint/recommended', "plugin:playwright/recommended"],
        parser: '@typescript-eslint/parser',
        parserOptions: {
          ecmaVersion: 'latest',
          sourceType: 'module',
        },
        plugins: ['@typescript-eslint'],
        rules: {
          '@typescript-eslint/no-unused-vars': 'off',
          '@typescript-eslint/no-empty-interface': 'error',
          "playwright/max-nested-describe": ["error", { "max": 1 }],
          "playwright/prefer-lowercase-title": ["error", { "ignoreTopLevelDescribe": true }],
          "playwright/require-top-level-describe": ["error", { "maxTopLevelDescribes": 1 }],
          "playwright/expect-expect": ["off", { "additionalAssertFunctionNames": ["assertCustomCondition"] }],
          "playwright/no-conditional-in-test": "off",
        },
      },
    ],   
  }), 
];

注:原配置存在大量不必要的字符串转义(如"\".**/*.ts\""),已修正为正确写法,这是配置失效的核心原因之一。

.eslintignore内容

**/*.js

问题排查与解决方案

核心问题分析

  1. 字符串转义错误导致文件匹配失效
    原配置中所有字符串被错误添加了额外转义引号,导致ESLint无法正确识别**/*.ts的文件匹配规则,本该仅作用于TS文件的规则被错误应用到所有文件(包括JS文件)。

  2. FlatCompat转换的规则范围遗漏
    即便修正文件匹配规则,FlatCompat转换ESLintRC风格的overrides时,可能无法完全限制TypeScript规则的作用范围——@typescript-eslint/recommended中的部分规则默认会对所有文件生效,除非显式通过配置限定。

  3. 全局规则无范围限制
    第一个配置项'eslint:recommended'是全局生效的,未限定文件范围,可能间接导致规则溢出到JS文件。

修复步骤

  1. 修正所有错误字符串转义
    将所有类似"\".xxx\""的写法改为正常字符串(如"xxx"),确保文件匹配、规则名、插件名等配置项能被ESLint正确解析。

  2. 为全局规则添加文件范围
    将全局的'eslint:recommended'改为带文件限制的配置,避免规则干扰:

{
  files: ["**/*.ts"],
  ...require('eslint/use-at-your-own-risk').configs.recommended
}
  1. Flat配置中显式忽略JS文件
    在配置顶部添加ignorePatterns,彻底排除JS文件,避免.eslintignore的优先级问题:
module.exports = [
  {
    ignorePatterns: ["**/*.js"]
  },
  // 其他配置项...
];
  1. 验证规则作用范围
    运行eslint --print-config 示例文件.ts和eslint --print-config 示例文件.js对比输出,确认TypeScript规则仅作用于TS文件。

内容的提问来源于stack exchange,提问作者Doctor Who

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 12:57:33