Skip to content

jaydenseric/eslint-plugin-optimal-modules

Repository files navigation

eslint-plugin-optimal-modules

An ESLint plugin to enforce optimal JavaScript module design.

Installation

To install eslint-plugin-optimal-modules with npm, run:

npm install eslint-plugin-optimal-modules --save-dev

To use the recommended config, add the following ESLint config:

{
  "extends": ["plugin:optimal-modules/recommended"]
}

Alternatively, manually configure the plugin and the desired rules:

{
  "plugins": ["optimal-modules"],
  "rules": {
    "optimal-modules/no-named-exports": "error"
  }
}

To allow named exports in Storybook story modules that have a Component Story Format (CSF), add this extra ESLint config:

{
  "overrides": [
    {
      "files": ["*.stories.{mjs,cjs,js,mts,cts,ts,tsx}"],
      "rules": {
        "optimal-modules/no-named-exports": "off"
      }
    }
  ]
}

Rules

Rule no-named-exports

Prohibits using named exports for optimal module design.

Valid examples:

// No exports.
const a = true;
// Default export.
export default true;
// Default export.
const a = true;
export { a as default };
// TypeScript type default export.
type A = boolean;
export type { A as default };

Invalid examples:

// Named export.
export const a = true;
// Named export.
const a = true;
export { a };
// TypeScript type named export.
export type A = boolean;

To fix the above errors, move the thing being exported to its own module as a default export.

Configs

Config recommended

Enabled rules:

Requirements

Supported runtime environments:

Projects must configure TypeScript to use types from the CommonJS modules that have a // @ts-check comment:

Exports

These CommonJS modules are exported via the package.json field exports: