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)
For anyone who wants a working
.eslintrc. Here is my current config, I’m using glob patterns overrides for.tsand.tsx, and the default for.js,.jsxI’m usingeslint v6.3.0The problem is that
overridesdo not allowextendsproperty. The only workaround I found is to add an override for.js, and.jsxif it’s a React project, files to disable all@typescript-eslintrules inrecommended.json. Something like:This is not ideal, because if the
recommended.jsonchanges, 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
checkJsconfig flag intsconfig.json, if the config file exists. This approach makestsconfig.jsonthe 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
overridesandextendsinoverrides: 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-disableto turn off rules in specific places https://eslint.org/docs/user-guide/configuring#disabling-rules-with-inline-commentsexample 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
overridesfunctionality.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…