Skip to content

⬅️ Back to Table of Contents

📄 prefer-namespace-keyword

📊 Analysis Summary

Metric Count
🔧 Functions 1
📦 Imports 3

📚 Table of Contents

🛠️ File Location:

📂 packages/eslint-plugin/src/rules/prefer-namespace-keyword.ts

📤 Default Export

export default createRule({ ... })
Property Value
name 'prefer-namespace-keyword'
meta.type 'suggestion'
meta.docs.description 'Require using namespace keyword over module keyword to declare custom TypeScript modules'
meta.docs.recommended 'recommended'
meta.fixable 'code'
meta.messages.useNamespace "Use 'namespace' instead of 'module' to declare custom TypeScript modules."
meta.schema []
defaultOptions []

Entry point: create — documented under Functions.


📦 Imports

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

Functions

create(context: any): { TSModuleDeclaration(node: any): void; }

Parameters:

  • context any

Returns: { TSModuleDeclaration(node: any): void; }

Calls:

  • context.sourceCode.getTokenBefore
  • context.report
  • fixer.replaceText

Internal Comments:

// Do nothing if the name is a string.
// Get tokens of the declaration header. (x2)

Code
create(context) {
    return {
      TSModuleDeclaration(node): void {
        // Do nothing if the name is a string.
        if (node.id.type === AST_NODE_TYPES.Literal) {
          return;
        }
        // Get tokens of the declaration header.
        const moduleType = context.sourceCode.getTokenBefore(node.id);

        if (
          moduleType?.type === AST_TOKEN_TYPES.Identifier &&
          moduleType.value === 'module'
        ) {
          context.report({
            node,
            messageId: 'useNamespace',
            fix(fixer) {
              return fixer.replaceText(moduleType, 'namespace');
            },
          });
        }
      },
    };
  }

Generated by Syntax Scribe