eslint-plugin-enforce-extensions
TypeScript icon, indicating that this package has built-in type declarations

0.1.8 • Public • Published

eslint-plugin-enforce-extensions

codecov Build Package status License

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.

  1. Install
npm install --save-dev eslint-plugin-enforce-extensions
  1. Edit .eslintrc
{
    "plugins": [
        "enforce-extensions"
    ],
    "rules": {
        "enforce-extensions/extensions": ["error", {"prefixes": ["/opt/"]}
    }
}
  1. Code
// source.js

import Target from './target';
import HigherTarget from '../higherTarget';
import OtherTarget from '/opt/other';
  1. Lint
eslint .
source.js
  1:1  error  Location-based imports and exports must end with .js  enforce-extensions/extensions
  1. 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}
    }
}

Package Sidebar

Install

npm i eslint-plugin-enforce-extensions

Weekly Downloads

1

Version

0.1.8

License

MIT

Unpacked Size

20.3 kB

Total Files

15

Last publish

Collaborators

  • salmonmode