typescript-eslint / typescript-eslint

:sparkles: Monorepo for all the tooling which enables ESLint to support TypeScript
https://typescript-eslint.io
Other
15.3k stars 2.73k forks source link

Bug: [`@typescript-eslint/no-unused-expressions` in `8.0.0-alpha.34`] complain in json files #9439

Closed AhmedBaset closed 4 months ago

AhmedBaset commented 4 months ago

Before You File a Bug Report Please Confirm You Have Done The Following...

Playground Link

I can't write json in the playground

Repro Code

{
  "key": "value"
}

لقطة شاشة 2024-06-26 212817

ESLint Config

export const base = defineConfig(
  {
    ignores: [".next", ".turbo", "node_modules"],
  },

  js.configs.recommended,
  tseslint.configs.eslintRecommended,
  tseslint.configs.base,
  ...tseslint.configs.recommended,
  ...tseslint.configs.stylistic,
});

// In another file: 
export const i18n = defineConfig(
  // TODO: FixMe
  // ...fixupConfigRules(compat.extends("plugin:rtl-friendly/recommended")),
  ...fixupConfigRules(compat.extends("plugin:i18next/recommended")),
  rtlFriendly.configs.recommended,
  {
    files: ["**/en.json", "**/ar.json"],
    plugins: { "i18n-json": i18nJson },
    rules: {
      "i18n-json/valid-message-syntax": "off",
      "i18n-json/identical-keys": [
        2,
        {
          filePath: path.join(process.cwd(), "/src/translations/en.json"),
        },
      ],
    },
  }
);

tsconfig

{
  "extends": "@fleetx/typescript-config/next.json",
  "compilerOptions": {
    "typeRoots": ["./src/types"],
    "paths": {
      "@/*": ["./src/*"],
      "#/*": ["./public/*"],
      "@shadcn/*": ["./src/components/ui/*"],
      "@env": ["./env.mjs"]
    },
    "lib": ["ESNext", "DOM", "DOM.Iterable", "WebWorker"]
  },
  "include": [
    "next-env.d.ts",
    "src/**/*.ts",
    "src/**/*.tsx",
    "src/translations/*.json",
    "*.mjs",
    "*.js",
    "*.ts",
    "*.mts",
    ".next/types/**/*.ts",
    "postcss.config.cjs"
  ],
  "exclude": ["node_modules"]
}

// next.json
{
  "$schema": "https://json.schemastore.org/tsconfig",
  "display": "Next.js",
  "extends": "./base.json",
  "compilerOptions": {
    "plugins": [{ "name": "next" }],
    "module": "ESNext",
    "moduleResolution": "Bundler",
    "allowJs": true,
    "jsx": "preserve",
    "noEmit": true
  }
}
// base.json
{
  "$schema": "https://json.schemastore.org/tsconfig",
  "display": "Default",
  "compilerOptions": {
    "esModuleInterop": true,
    "incremental": true,
    "tsBuildInfoFile": "node_modules/.cache/typescript/tsbuildinfo.json",
    "isolatedModules": true,
    "lib": ["ESNext", "DOM", "DOM.Iterable"],
    "module": "NodeNext",
    "moduleDetection": "force",
    "moduleResolution": "NodeNext",
    "noUncheckedIndexedAccess": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "strict": true,
    "target": "ES2022",
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "noErrorTruncation": true
  }
}

Expected Result

I expect to ignore json files

Actual Result

It runs on json files

Additional Info

No response

bradzacher commented 4 months ago

You've misunderstood how flat config files work.

By default eslint applies all configurations to all files being linted. When you use files it makes eslint apply just that specific config object to the matched files. It does not prevent other config objects from matching.

So your config is setup has

  js.configs.recommended,
  tseslint.configs.eslintRecommended,
  tseslint.configs.base,
  ...tseslint.configs.recommended,
  ...tseslint.configs.stylistic,

None of those configs have a files - So that means that eslint applies all of those to every single file. You then have files: ["**/en.json", "**/ar.json"] which does two things: first it tells eslint that json files should be included in the lint run, and second it restricts that specific config object to json files.

The first part is the kicker - all the above configs are applied to all files being linted. And you just added json files to the files being linted.

There's no bug here. The issue is your config is wrong.