Skip to content

⬅️ Back to Table of Contents

πŸ“„ no-useless-empty-export

πŸ“Š Analysis Summary

Metric Count
πŸ”§ Functions 3
πŸ“¦ Imports 4
πŸ“Š Variables & Constants 1

πŸ“š Table of Contents

πŸ› οΈ File Location:

πŸ“‚ packages/eslint-plugin/src/rules/no-useless-empty-export.ts

πŸ“€ Default Export

export default createRule({ ... })
Property Value
name 'no-useless-empty-export'
meta.type 'suggestion'
meta.docs.description "Disallow empty exports that don't change anything in a module file"
meta.fixable 'code'
meta.hasSuggestions false
meta.messages.uselessExport 'Empty export does nothing and can be removed.'
meta.schema []
defaultOptions []

Entry point: create β€” documented under Functions.


πŸ“¦ Imports

Name Source
TSESTree @typescript-eslint/utils
AST_NODE_TYPES @typescript-eslint/utils
createRule ../util
isDefinitionFile ../util

Variables & Constants

Name Type Kind Value Exported
exportOrImportNodeTypes Set<any> const new Set([ AST_NODE_TYPES.ExportAllDeclaration, AST_NODE_TYPES.ExportDefaultDe... βœ—

Functions

create(context: any): { Program?: undefined; TSModuleDeclaration?: undefined; } |…

Parameters:

  • context any

Returns: { Program?: undefined; TSModuleDeclaration?: undefined; } | { Program: (node: TSESTree.Program | TSESTree.TSModuleDeclaration) => void; TSModuleDeclaration: (node: TSESTree.Program | TSESTree.TSModuleDeclaration) => void; }

Calls:

  • isDefinitionFile (from ../util)
  • Array.isArray
  • isEmptyExport
  • emptyExports.push
  • exportOrImportNodeTypes.has
  • context.report
  • fixer.remove

Internal Comments:

// In a definition file, export {} is necessary to make the module properly
// encapsulated, even when there are other exports
// https://github.com/typescript-eslint/typescript-eslint/issues/4975

Code
create(context) {
    // In a definition file, export {} is necessary to make the module properly
    // encapsulated, even when there are other exports
    // https://github.com/typescript-eslint/typescript-eslint/issues/4975
    if (isDefinitionFile(context.filename)) {
      return {};
    }
    function checkNode(
      node: TSESTree.Program | TSESTree.TSModuleDeclaration,
    ): void {
      if (!Array.isArray(node.body)) {
        return;
      }

      const emptyExports: TSESTree.ExportNamedDeclaration[] = [];
      let foundOtherExport = false;

      for (const statement of node.body) {
        if (isEmptyExport(statement)) {
          emptyExports.push(statement);
        } else if (exportOrImportNodeTypes.has(statement.type)) {
          foundOtherExport = true;
        }
      }

      if (foundOtherExport) {
        for (const emptyExport of emptyExports) {
          context.report({
            node: emptyExport,
            messageId: 'uselessExport',
            fix: fixer => fixer.remove(emptyExport),
          });
        }
      }
    }

    return {
      Program: checkNode,
      TSModuleDeclaration: checkNode,
    };
  }

isEmptyExport(node: TSESTree.Node): node is TSESTree.ExportNamedDeclaration

Parameters:

  • node TSESTree.Node

Returns: node is TSESTree.ExportNamedDeclaration

Code
function isEmptyExport(
  node: TSESTree.Node,
): node is TSESTree.ExportNamedDeclaration {
  return (
    node.type === AST_NODE_TYPES.ExportNamedDeclaration &&
    node.specifiers.length === 0 &&
    !node.declaration
  );
}

Internal helpers

Declared inside another function in this file.

checkNode(node: TSESTree.Program | TSESTree.TSModuleDec…): void

Parameters:

  • node TSESTree.Program | TSESTree.TSModuleDeclaration

Returns: void

Calls:

  • Array.isArray
  • isEmptyExport
  • emptyExports.push
  • exportOrImportNodeTypes.has
  • context.report
  • fixer.remove
Code
function checkNode(
      node: TSESTree.Program | TSESTree.TSModuleDeclaration,
    ): void {
      if (!Array.isArray(node.body)) {
        return;
      }

      const emptyExports: TSESTree.ExportNamedDeclaration[] = [];
      let foundOtherExport = false;

      for (const statement of node.body) {
        if (isEmptyExport(statement)) {
          emptyExports.push(statement);
        } else if (exportOrImportNodeTypes.has(statement.type)) {
          foundOtherExport = true;
        }
      }

      if (foundOtherExport) {
        for (const emptyExport of emptyExports) {
          context.report({
            node: emptyExport,
            messageId: 'uselessExport',
            fix: fixer => fixer.remove(emptyExport),
          });
        }
      }
    }

Generated by Syntax Scribe