Skip to content

Latest commit

 

History

History
129 lines (105 loc) · 2.75 KB

named-export.md

File metadata and controls

129 lines (105 loc) · 2.75 KB

filenames-simple/named-export

This rule checks the export name is same as filename.

NOTE: this rule is only enabled when parserOptions.ecmaVersion in .eslintrc is set to 6 (2015) or later.

Configuration example

{
  "plugins": ["filenames-simple"],
  "rules": {
    "filenames-simple/named-export": ["error", "singular"]
  }
}

Available options

rule: (the first option)

Specify one of the following as the file naming convention to which this rule applies.

Naming convention presets

  • always (default): Always check the export name.
  • singular: Check the export name only if filename is singular.
  • plural: Check the export name only if filename is plural.

Rule details

Basic

  • module.js

    // OK
    export const module = 1;
  • module.js

    // NG: You can use `mod.js` or `Mod.js` as filename.
    export const mod = 1;
  • module.js

    // default export is ignored
    const module = 1;
    export default module;
  • modules.js

    // Multiple named exports are ignored too.
    const module1 = 1;
    const module2 = 2;
    export { module1, module2 };
  • module.js

    // It is ignored when the file includes both default export and named export.
    const module = 1;
    export default module;
    export const extraModule = { key: 'value' };

When the name of exported module contains two or more words

  • my-class.js
    /*
     * When the name of exported module is written in camelCase or PascalCase,
     * the filename can be written in `kebab-case`, `camelCase` or `PascalCase`
     */
    export class MyClass {}
  • myFunction.js
    // `my-function.js`, `MyFunction.js` is also OK.
    export function myFunction() {}

When the filename is index.js (index.ts)

  • src/rules/index.js
    // The rule checks the parent directory name with the name of exported module.
    export const rules = {};
  • src/config/index.js
    // Multiple named export are ignored.
    export { all } from './all';
    export { recommended } from './recommended';

You can also lint TypeScript notations

  • fruits.ts
    export enum fruits {
      orange = 'orange',
      apple = 'apple',
      banana = 'banana',
    }
  • identifier.ts
    export interface Identifier {
      name: string;
    }
  • class-expression.d.ts
    export type ClassExpression = { id: Identifier };

NOTE: This rule skips the detection of named exports in TypeScript module blocks.

  • @types/espree.d.ts
    declare module 'espree' {
      export function parse(code: string, options?: any): Node;
    }

See also