Skip to content

ā¬…ļø Back to Table of Contents

šŸ“„ triple-slash-reference

šŸ“Š Analysis Summary

Metric Count
šŸ”§ Functions 2
šŸ“¦ Imports 4
šŸ“‘ Type Aliases 2

šŸ“š Table of Contents

šŸ› ļø File Location:

šŸ“‚ packages/eslint-plugin/src/rules/triple-slash-reference.ts

šŸ“¤ Default Export

export default createRule<Options, MessageIds>({ ... })
Property Value
name 'triple-slash-reference'
meta.type 'suggestion'
meta.docs.description 'Disallow certain triple slash directives in favor of ES6-style import declarations'
meta.docs.recommended 'recommended'
meta.messages.tripleSlashReference 'Do not use a triple slash reference for {{module}}, use import style instead.'
meta.schema [ { type: 'object', additionalProperties: false, properties: { lib: { type: 'string', description: 'What to enforce f...
defaultOptions [ { lib: 'always', path: 'never', types: 'prefer-import', }, ]

Entry point: create — documented under Functions.


šŸ“¦ Imports

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

Functions

create(context: any, [{ lib, path, types }]: any): { ImportDeclaration(node: any): void; Program(node: any): v…

Parameters:

  • context any
  • [{ lib, path, types }] any

Returns: { ImportDeclaration(node: any): void; Program(node: any): void; TSImportEqualsDeclaration(node: any): void; }

Calls:

  • references.forEach
  • context.report
  • hasMatchingReference
  • context.sourceCode.getCommentsBefore
  • commentsBefore.forEach
  • referenceRegExp.exec
  • references.push
Code
create(context, [{ lib, path, types }]) {
    let programNode: TSESTree.Node | undefined;

    const references: {
      comment: TSESTree.Comment;
      importName: string;
    }[] = [];

    function hasMatchingReference(source: TSESTree.Literal): void {
      references.forEach(reference => {
        if (reference.importName === source.value) {
          context.report({
            node: reference.comment,
            messageId: 'tripleSlashReference',
            data: {
              module: reference.importName,
            },
          });
        }
      });
    }
    return {
      ImportDeclaration(node): void {
        if (programNode) {
          hasMatchingReference(node.source);
        }
      },
      Program(node): void {
        if (lib === 'always' && path === 'always' && types === 'always') {
          return;
        }
        programNode = node;
        const referenceRegExp =
          /^\/\s*<reference\s*(types|path|lib)\s*=\s*["|'](.*)["|']/;
        const commentsBefore =
          context.sourceCode.getCommentsBefore(programNode);

        commentsBefore.forEach(comment => {
          if (comment.type !== AST_TOKEN_TYPES.Line) {
            return;
          }
          const referenceResult = referenceRegExp.exec(comment.value);

          if (referenceResult) {
            if (
              (referenceResult[1] === 'types' && types === 'never') ||
              (referenceResult[1] === 'path' && path === 'never') ||
              (referenceResult[1] === 'lib' && lib === 'never')
            ) {
              context.report({
                node: comment,
                messageId: 'tripleSlashReference',
                data: {
                  module: referenceResult[2],
                },
              });
              return;
            }
            if (referenceResult[1] === 'types' && types === 'prefer-import') {
              references.push({ comment, importName: referenceResult[2] });
            }
          }
        });
      },
      TSImportEqualsDeclaration(node): void {
        if (programNode) {
          const reference = node.moduleReference;

          if (reference.type === AST_NODE_TYPES.TSExternalModuleReference) {
            hasMatchingReference(reference.expression);
          }
        }
      },
    };
  }

Internal helpers

Declared inside another function in this file.

hasMatchingReference(source: TSESTree.Literal): void

Parameters:

  • source TSESTree.Literal

Returns: void

Calls:

  • references.forEach
  • context.report
Code
function hasMatchingReference(source: TSESTree.Literal): void {
      references.forEach(reference => {
        if (reference.importName === source.value) {
          context.report({
            node: reference.comment,
            messageId: 'tripleSlashReference',
            data: {
              module: reference.importName,
            },
          });
        }
      });
    }

Type Aliases

Options

type Options = [
  {
    lib?: 'always' | 'never';
    path?: 'always' | 'never';
    types?: 'always' | 'never' | 'prefer-import';
  },
];

MessageIds

type MessageIds = 'tripleSlashReference';

Generated by Syntax Scribe