Hoppa till innehåll

eslint-plugin-import-naming

This ESLint plugin enforces consistent naming for default imports from specified packages. It ensures that the default import name matches the last segment of the imported package path, helping maintain clarity and consistency in your codebase.

Enforces that the default import name matches the module name for specified packages.

Mainly because of the @sitevision/api package, which has many modules that are imported with default imports.
This rule helps ensure that the default import name matches the module name, improving readability and maintainability.
But it works with any package, so it can be used to enforce consistent naming for default imports across your codebase.

This rule is included in the main config with the following setup, so you can use it by enabling the rule and specifying the packages to enforce in your ESLint configuration:

eslint.config.js
import config from '@soleil-se/eslint-config';
export default [
...config,
{
rules: {
'import-naming/match-package-default-import-name': [ 'warn', {
packages: ['@sitevision/api'], // Specify packages to enforce
}],
},
},
];

If you want to use the plugin separately, you can import it and add it to your configuration:

eslint.config.js
import importNamingPlugin from '@soleil-se/eslint-config/plugins/import-naming';
export default [
{
plugins: {
'import-naming': importNamingPlugin,
},
rules: {
'import-naming/match-package-default-import-name': [ 'warn', {
packages: ['@sitevision/api', 'lodash', 'qs'], // Specify packages to enforce
}],
},
},
];

This rule checks default imports from the specified packages. The default import name must match the package name or last segment of the import path.

import PortletContextUtil from '@sitevision/api/server/PortletContextUtil'; // ✓ Default import name matches the last segment of the path
import qs from 'qs'; // ✓ Default import name matches the package name
import debounce from 'lodash/debounce'; // ✓ Default import name matches the last segment of the path (debounce)
import portletContextUtil from '@sitevision/api/server/PortletContextUtil'; // ✗ Default import should be named PortletContextUtil
import myQs from 'qs'; // ✗ Default import should be named qs
import myDebounce from 'lodash/debounce'; // ✗ Default import should be named debounce

If the expected name is already used in the scope, a conflict warning is reported.

This rule is auto-fixable. ESLint can automatically rename the import and all its references to the correct name.

  • packages: An array of package name prefixes to enforce (e.g., ['@sitevision/api', 'lodash']).