typescript-eslint: Support for mixed JS and TS codebases - do not lint JS files

Repro

JavaScript file (gulpfile.js):

/* eslint-env node */

const gulp = require('gulp');
{
	"parser": "@typescript-eslint/parser",
	"parserOptions": {
		"ecmaVersion": 3
	},
	"env": {
		"browser": true
	},
	"plugins": [
		"@typescript-eslint"
	],
	"extends": "plugin:@typescript-eslint/recommended"
}

Expected Result

I have a package with mostly TypeScript, but also a gulpfile.js, which is plain old JavaScript. I was expecting this plugin to only lint the TypeScript files

Actual Result

For the gulpfile: error Require statement not part of import statement @typescript-eslint/no-var-requires

Additional Info

I suppose this applies to any mixed codebase containing JS and TS together

Versions

package version
@typescript-eslint/eslint-plugin 1.0.0
@typescript-eslint/parser 1.0.0
TypeScript 3.2.4
ESLint 5.12.1
node 11.7.0
npm 6.6.0

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 46
  • Comments: 21 (10 by maintainers)

Commits related to this issue

Most upvoted comments

For anyone who wants a working .eslintrc. Here is my current config, I’m using glob patterns overrides for .ts and .tsx, and the default for .js, .jsx I’m using eslint v6.3.0

{
  "env": { "browser": true, "es6": true, "node": true },
  "extends": ["eslint:recommended", "plugin:react/recommended"],
  "globals": { "Atomics": "readonly", "SharedArrayBuffer": "readonly" },
  "parser": "babel-eslint",
  "parserOptions": {
    "ecmaFeatures": { "jsx": true },
    "ecmaVersion": 2018,
    "sourceType": "module"
  },
  "plugins": ["react"],
  "rules": {
    "indent": ["error", 2, { "SwitchCase": 1 }],
    "linebreak-style": ["error", "unix"],
    "quotes": ["error", "single"],
    "comma-dangle": ["error", "always-multiline"],
    "semi": ["error", "always"]
  },
  "settings": { "react": { "version": "detect" } },
  "overrides": [
    {
      "files": ["**/*.ts", "**/*.tsx"],
      "env": { "browser": true, "es6": true, "node": true },
      "extends": [
        "eslint:recommended",
        "plugin:react/recommended",
        "plugin:@typescript-eslint/eslint-recommended",
        "plugin:@typescript-eslint/recommended"
      ],
      "globals": { "Atomics": "readonly", "SharedArrayBuffer": "readonly" },
      "parser": "@typescript-eslint/parser",
      "parserOptions": {
        "ecmaFeatures": { "jsx": true },
        "ecmaVersion": 2018,
        "sourceType": "module",
        "project": "./tsconfig.json"
      },
      "plugins": ["react", "@typescript-eslint"],
      "rules": {
        "indent": ["error", 2, { "SwitchCase": 1 }],
        "linebreak-style": ["error", "unix"],
        "quotes": ["error", "single"],
        "comma-dangle": ["error", "always-multiline"],
        "@typescript-eslint/no-explicit-any": 0
      },
      "settings": { "react": { "version": "detect" } }
    }
  ]
}

The problem is that overrides do not allow extends property. The only workaround I found is to add an override for .js, and .jsx if it’s a React project, files to disable all @typescript-eslint rules in recommended.json. Something like:

{
    "extends": [
        "plugin:@typescript-eslint/recommended",
        // ...
    ],
    // ...
    },
    "overrides": [
        {
            "files": ["*.js", "*.jsx"],
            "rules": {
                // "@typescript-eslint/...": "off",
            }
        }
    ]
}

This is not ideal, because if the recommended.json changes, I have to manually update this again. It’s not a huge issue, but it looks dirty.

It would be nice if the rule would work with checkJs config flag in tsconfig.json, if the config file exists. This approach makes tsconfig.json the single source of truth for the preference, without requiring additional configuration or rules to support this exception.

even tho the solution suggested by @markogresak is a good one, it would be even better if somehow we could just turn off all @typescript-eslint rules like "rules": { "@typescript-eslint/*": "off" }.

It’s not very maintainable having to manually add the rules to this list.

Also of note: ESLint also now supports nested overrides and extends in overrides: https://github.com/eslint/eslint/pull/11554 🎉

facing the same problem. expecting to have the ability for setting different rule set for ts and js files.

by default eslint lint all provided files with all rules, you should use overrides if don’t want specific files to be linted by some of rules

you can also set // eslint-disable to turn off rules in specific places https://eslint.org/docs/user-guide/configuring#disabling-rules-with-inline-comments

example of eslint config with overrides: https://github.com/typescript-eslint/typescript-eslint/blob/master/.eslintrc.json

Woops, this issue fell through the cracks! It should have been closed a while ago.

As @kaicataldo mentioned, you can accomplish this easily using eslint’s overrides functionality.

In eslint v6, there was as whole lot more support added to extends, and it now works really intuitively.

We’re not going to bake in explicit file filters. In 2.0 we removed the instances of us doing this.
The rules that do not work on js code have this documented in their readmes, all other rules will work fine on js code.

A potentially simpler way to do this would be to only turn on those rules for .ts(x) files using glob configuration. That way the rules are only on for TypeScript files and don’t need to be turned off for non-TypeScript files.

This is actually huge!!! Awesome - we have to update the documentation with these changes.

Hi, thanks for the blazing quick reply and for the tip.

I can do that, however, since I am asking, wouldn’t it make more sense for this plugin to just ignore plain old js files? TypeScript does the same thing. (unless called with --allowJS, I know). - I mean this as a suggestion…