eslint-plugin-enforce-extensions
v0.1.8
Published
A simple eslint plugin that enforces imports and exports for file locations have a file extension.
Downloads
4
Maintainers
Readme
eslint-plugin-enforce-extensions
A simple eslint plugin that enforces imports and exports for file locations have a '.js' file extension.
TypeScript doesn't transform extensions and doesn't enforce file extensions. This can be a problem when the type
specified in the package.json
is module
, as the compiler will not complain about the lack of an extension, because an error will get thrown during runtime saying that it can't find that module.
This plugin can not only identify those problematic imports, but also automatically fix them if configured to do so.
Other plugins exist for this, but it seemed that they only accounted for relative imports stating with .
. This is a problem if you have special import scenarios, for example, importing from a lambda layer in an AWS Lambda function, where the import is an absolute file location starting with /opt/
.
This pluginallows you to specify exactly what import prefixes to look out for.
- Install
npm install --save-dev eslint-plugin-enforce-extensions
- Edit
.eslintrc
{
"plugins": [
"enforce-extensions"
],
"rules": {
"enforce-extensions/extensions": ["error", {"prefixes": ["/opt/"]}
}
}
- Code
// source.js
import Target from './target';
import HigherTarget from '../higherTarget';
import OtherTarget from '/opt/other';
- Lint
eslint .
source.js
1:1 error Location-based imports and exports must end with .js enforce-extensions/extensions
- Fix
eslint --fix .
// source.js
import Target from './target.js';
import HigherTarget from '../higherTarget.js';
import OtherTarget from '/opt/other.js';
If, for whatever reason, you wish to exclude the default .
prefix, you can provide the following option:
{
"plugins": [
"enforce-extensions"
],
"rules": {
"enforce-extensions/extensions": ["error", {"includeDefault": false}
}
}