eslint-plugin-node
eslint-plugin-node copied to clipboard
Consider adding typescript faq, recommended-typescript config
This is a reopening of https://github.com/mysticatea/eslint-plugin-node/issues/205.
Typescript is usually (IMHO) configured, when used with node, to translate what look like ECMA imports and exports down to CommonJS. I know that's evolving, but only the brave and those without other dependencies are going all in. In this config, what look like ECMA imports and exports in the .ts source really aren't.
The plugin:node/recommended-typescript-commonjs
config should probably include something like:
"rules": {
"node/no-unsupported-features/es-syntax": [
"error",
{
"ignores": [
"modules"
]
}
],
"node/no-missing-import": ["error", {
"tryExtensions": [".js", ".ts"]
}]
}
It probably makes sense to also soon have another plugin:node/recommended-typescript-esm
for when typescript is set up to emit ecmascript modules.
Also, it would be helpful if the doc specified whether node
should come before or after typescript
in extends
.
node/no-missing-import would also need to take into account import type
".d.ts"
will be needed as well in node/no-missing-import
- tryExtensions
I've also run into a couple issues with TypeScript.
// index.ts
import {RequestHandler} from 'express' // node/no-missing-import and node/no-unpublished-import reported
export function logger(): RequestHandler {
return (req, res, next) => {
//...
}
}
// package.json
{
"dependencies": {
"@types/express": "*"
},
"devDependencies": {
"express": "*"
}
}
node/no-missing-import
reports an error when only @types/<module>
is needed. In the example above, RequestHandler
is an interface from @types/express
, but there is no need to add a dependency for express
since the package never needs to import any Express code.
node/no-unpublished-import
reports an error in a similar way when a module is only needed as a devDependency
(e.g. for testing) but only its types are needed for production, and those types come from @types/<module>
.
I've turned off node/no-missing-import
since TypeScript checks this for me, but node/no-unpublished-import
is not something TypeScript checks, so it would be nice if eslint-plugin-node
could do it while supporting TypeScript.
Is there any place that talks about this? I'm trying to sort this out in https://github.com/snowpackjs/prettier-plugin-astro/pull/56/ and I'd love for it to work! Would it be possible to make a community-based recommended config?
here are a couple of commented configs for eslint and tsconfig (eslintrc.json is fine with comments):
eslintrc.json
{
"env": {
/* safely matches tsconfig target, even if tsc might backport/shim */
"es2020": true,
"node": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:node/recommended",
"plugin:import/recommended",
"plugin:import/typescript",
"prettier"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": ["@typescript-eslint", "import", "node"],
/* not really necessary, just safety */
"root": true,
"rules": {
/* allow import/export syntax, tied to package.json engine.node values */
"node/no-unsupported-features/es-syntax": [
"error",
{ "ignores": ["modules"] }
],
/* let the import plugin deal with module checking */
"node/no-missing-import": "off"
},
"settings": {
"import/parsers": {
"@typescript-eslint/parser": [".ts", "tsx"]
},
"import/resolver": {
"typescript": { "alwaysTryTypes": true }
}
}
}
tsconfig.json
{
"extends": "@tsconfig/node14/tsconfig.json",
"include": ["./src"],
"compilerOptions": {
"outDir": "./dist"
}
}