Skip to content

⬅️ Back to Table of Contents

📄 no-relative-paths-to-internal-packages

📊 Analysis Summary

Metric Count
🔧 Functions 2
📦 Imports 3

📚 Table of Contents

🛠️ File Location:

📂 packages/eslint-plugin-internal/src/rules/no-relative-paths-to-internal-packages.ts

📤 Default Export

export default createRule({ ... })
Property Value
name 'no-relative-paths-to-internal-packages'
meta.type 'problem'
meta.docs.description 'Disallow relative paths to internal packages'
meta.fixable 'code'
meta.messages.noRelativePathsToInternalPackages 'Use absolute paths instead of relative paths to import modules in other internal packages.'
meta.schema []
defaultOptions []

Entry point: create — documented under Functions.


📦 Imports

Name Source
path node:path
fileURLToPath node:url
createRule ../util/index.js

Functions

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

Parameters:

  • context any

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

Calls:

  • importSource.value.startsWith
  • path.relative
  • pathOfFileFromPackagesDir.split
  • path.resolve
  • path.dirname
  • pathOfImportFromPackagesDir.split
  • context.report
  • pathOfImportFromPackagesDir.split(path.sep).join
  • fixer.replaceText

Internal Comments:

// The idea here is to check if the import source resolves to a different (x2)
// package than the package the file is currently in. This lets us not flag (x2)
// relative paths to modules inside a package called 'utils' but still flag (x2)
// if importing the '@typescript-eslint/utils' package with a relative path. (x2)
// this is to allow importing the root package.json
// Force the output path to be separated with '/' to get consistent (x2)
// results on windows. (x2)

Code
create(context) {
    return {
      ImportDeclaration(node): void {
        const importSource = node.source;
        if (
          importSource.value.startsWith('@typescript-eslint') ||
          !importSource.value.startsWith('.')
        ) {
          return;
        }

        // The idea here is to check if the import source resolves to a different
        // package than the package the file is currently in. This lets us not flag
        // relative paths to modules inside a package called 'utils' but still flag
        // if importing the '@typescript-eslint/utils' package with a relative path.

        const pathOfFileFromPackagesDir = path.relative(
          PACKAGES_DIR,
          context.physicalFilename,
        );
        const packageOfFile = pathOfFileFromPackagesDir.split(path.sep)[0];
        const absolutePathOfImport = path.resolve(
          path.dirname(context.physicalFilename),
          importSource.value,
        );
        const pathOfImportFromPackagesDir = path.relative(
          PACKAGES_DIR,
          absolutePathOfImport,
        );
        const packageOfImport = pathOfImportFromPackagesDir.split(path.sep)[0];

        if (path.dirname(absolutePathOfImport) === REPO_ROOT) {
          // this is to allow importing the root package.json
          return;
        }

        if (packageOfImport !== packageOfFile) {
          context.report({
            node: importSource,
            messageId: 'noRelativePathsToInternalPackages',
            fix: fixer => {
              // Force the output path to be separated with '/' to get consistent
              // results on windows.
              const platformIndependentRelativePathOfImportFromPackagesDir =
                pathOfImportFromPackagesDir.split(path.sep).join('/');
              return fixer.replaceText(
                importSource,
                `'@typescript-eslint/${platformIndependentRelativePathOfImportFromPackagesDir}'`,
              );
            },
          });
        }
      },
    };
  }

Internal helpers

Declared inside another function in this file.

fix(fixer: any): any

Parameters:

  • fixer any

Returns: any

Calls:

  • pathOfImportFromPackagesDir.split(path.sep).join
  • fixer.replaceText

Internal Comments:

// Force the output path to be separated with '/' to get consistent (x2)
// results on windows. (x2)

Code
fixer => {
              // Force the output path to be separated with '/' to get consistent
              // results on windows.
              const platformIndependentRelativePathOfImportFromPackagesDir =
                pathOfImportFromPackagesDir.split(path.sep).join('/');
              return fixer.replaceText(
                importSource,
                `'@typescript-eslint/${platformIndependentRelativePathOfImportFromPackagesDir}'`,
              );
            }

Generated by Syntax Scribe