eslint-plugin-file-extension-in-import-ts
v2.1.1
Published
The plugin check and fix file extensions in Node.js application type module in Typescript
Downloads
13,625
Readme
eslint-plugin-file-extension-in-import-ts
Disclaimer
This is small patch to the great rule:
file-extension-in-import
from eslint-plugin-node module.
Author of code:
I've just added mapping functionality.
Motivation
When we use type module we must add extension to the file
import foo from "./path/to/a/file"
It's a bad practice
import eslint from "eslint"
import foo from "./path/to/a/file.js"
It's a good practice.
node/file-extension-in-import rule raise error when we forgot to add extension to the import. But it doesn't work with Typescript.
Typescript usage
When we use Typescript we can't add .ts extension to the file. It won't be process with Typescript compiler. In this case we need to add .js extension.
In this case we need to replace .ts extension to .js.
In this plugin I will add possibility to make mapping for extensions:
'file-extension-in-import-ts/file-extension-in-import-ts': [
'error',
'always',
{ extMapping: {'.ts': '.js' } }
]
It means, this rule will fix our ts files to .js extension in import statement.
import module from './my-module';
will be changed to:
import module from './my-module/index.js';
Installation:
- Install the package:
npm i -D eslint-plugin-file-extension-in-import-ts
- Add to .eslintrc.js:
{
"plugins": [
"file-extension-in-import-ts"
],
"rules": {
"file-extension-in-import-ts/file-extension-in-import-ts": "error"
}
}
By default, the plugin will process mapping:
- .ts -> .js
- .tsx -> .js
Troubleshooting
This plugin has conflict with eslint-import-plugin, the rule: import/no-unresolved rule
To quick fix it we can skip '.js' (in the folder with TS sources JS files are not existing).
'import/no-unresolved': ['error', { ignore: [ '\\.js$' ] }],
The MIT License
Copyright (c)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.