Package detail

eslint-plugin-import-access

uhyo100.5kMIT3.0.0

ESLint rule for prohibiting package-private imports

eslint, eslintplugin

readme

eslint-plugin-import-access

What?

This package provides a typescript-eslint rule that restricts importing variables marked as @package from a file outside the same directory. Also, this package serves as a TypeScript Language Service Plugin that prevents auto-completion of such imports.

Illustration of how `@package` works.

Why?

The largest encapsulation unit available for a TypeScript project is a file. That is, variables not exported from a file is only visible to code in the same file. Once a variable is exported, it is visible from the entire project.

Sometimes this is insufficient. A rational effort for proper encapsulation may result in a large file that is hard to maintain.

This package solves this problem by providing a new directory-level layer and enabling a “package-private” export that is only visible to files in the same directory.

Installation

npm i -D eslint-plugin-import-access

Depending on how you configure ESLint, use either Flat Config or eslintrc to configure eslint-plugin-import-access.

Also, you can enable the TypeScript Language Service Plugin by adding it to the plugins array in tsconfig.json.

Flat Config

In eslint.config.js:

import typescriptEslintParser from "@typescript-eslint/parser";
import importAccess from "eslint-plugin-import-access/flat-config";

export default [
  // other settings...
  {
    // set up typescript-eslint
    languageOptions: {
      parser: typescriptEslintParser,
      parserOptions: {
        project: true,
        sourceType: "module",
      },
    },
  },
  {
    plugins: {
      "import-access": importAccess,
    },
  },
  {
    rules: {
      "import-access/jsdoc": ["error"],
    },
  },
];

Note: currently you need to import the plugin from the /flat-config subpath. In a future version, this will be simplified.

eslintrc

In .eslintrc.js:

  // set up typescript-eslint
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "project": true,
    "sourceType": "module"
  },
  "plugins": [
    "import-access",
    // ...
  ],
  "rules": {
    "import-access/jsdoc": ["error"],
  }

TypeScript Language Service Plugin

In tsconfig.json:

{
  "compilerOptions": {
    // ...
    "plugins": [
      // ...
      {
        "name": "eslint-plugin-import-access"
      }
    ]
  }
}

_Note: to enable TypeScript language service plugins installed locally, you must use TypeScript in node_modules, not the one bundled with VSCode._

Example

// ----- sub/foo.ts -----

/**
 * @package
 */
export const fooPackageVariable = "I am package-private export";

// ----- sub/bar.ts -----
// This is correct because foo.ts is in the same directory
import { fooPackageVariable } from "./foo";

// ----- baz.ts -----
// This is INCORRECT because package-private exports
// cannot be imported from outside the sub directory
import { fooPackageVariable } from "./sub/foo";

Rule References

Contributing

Welcome

License

MIT

changelog

Changelog

3.0.0

[!WARNING]

Breaking Change: minimum supported versions have been updated to:

  • Node.js 20
  • TypeScript 5.0
  • TypeScript-ESLint 8
  • feat: add excludeSourcePatterns option to import-access/jsdoc rule (#125)

2.2.2

  • fix typing incompatibility with typescript-eslint (#49)

2.2.1

  • Support ESLint V9 (#33)

2.2.0

  • Support Flat Config (#20)

2.1.2

  • fix bug check package incorrect on Windows (#15)

2.1.1

  • Fixed the treatSelfReferenceAs option to support scoped packages. (#13)

2.1.0

  • Added a treatSelfReferenceAs option which allows you to treat self-references either like internal file or like external module. (#12)

2.0.0

:warning: BREAKING CHANGES

  • Minimum supported TypeScript version is now 4.7 (previously 4.4)
  • Minimum supported Node.js version is now 16 (previously 14)

Other Changes

  • Added check for the export { ... } from syntax. Previously you could export a private symbol by using this syntax, but now it is also checked. (#9)
  • Fixed the handling of symlinked external modules. (#10)

1.1.2

  • Fixed the issue where one could not import from type-only packages. (#7)
  • Fixed the issue where one could not import from Node.js built-in modules. (#8)

1.1.1

  • Removed check against imports from inside node_modules. (#6)

1.1.0

  • Added a defaultImportability option with which you can use @package or @private as default importability of all exports in the project. (#5)

1.0.2

  • Fix a bug that export default is not checked. (#2)
  • Extend the file name pattern to consider as an index file. Previously only index.ts and index.tsx were considered as an index file. Now index.js, index.mts, etc. are also considered.

1.0.1

1.0.0

No change from 1.0.0-beta.

1.0.0-beta

Initial release.