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.
Rule: match-package-default-import-name
Section titled “Rule: match-package-default-import-name”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:
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:
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 }], }, },];Rule Details
Section titled “Rule Details”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.
Examples
Section titled “Examples”Correct
Section titled “Correct”import PortletContextUtil from '@sitevision/api/server/PortletContextUtil'; // ✓ Default import name matches the last segment of the pathimport qs from 'qs'; // ✓ Default import name matches the package nameimport debounce from 'lodash/debounce'; // ✓ Default import name matches the last segment of the path (debounce)Incorrect
Section titled “Incorrect”import portletContextUtil from '@sitevision/api/server/PortletContextUtil'; // ✗ Default import should be named PortletContextUtilimport myQs from 'qs'; // ✗ Default import should be named qsimport myDebounce from 'lodash/debounce'; // ✗ Default import should be named debounceIf the expected name is already used in the scope, a conflict warning is reported.
Auto-fix
Section titled “Auto-fix”This rule is auto-fixable. ESLint can automatically rename the import and all its references to the correct name.
Options
Section titled “Options”packages: An array of package name prefixes to enforce (e.g.,['@sitevision/api', 'lodash']).