2.3.0 ā€¢ Published 2 months ago

eslint-plugin-export-scope v2.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

eslint-plugin-export-scope

Don't leak LOCAL utils, states, contexts, components into the global scope.

Basics

Demo

Demo

Scopes

scopeimportable from
.current directory and childrendefault for all exports
..parent directory and childrendefault for index files
../..two directories above and children
src/consumerwithin specified directory and children
src/consumer.tswithin specified file
*anywhere

Scoped Exports

/** @scopeDefault ../.. */
/** ā˜ Applies to all exports in the file unless overriden with a local `@scope` */

/** @scope * */
export const helper1 = ""; // šŸ‘ˆ Available everywhere

export const helper2 = ""; // šŸ‘ˆ inherits scope `../..` from `@scopeDefault`

/** @scope src/components */
export default "";

Default folder scope with .scope.ts files

ā””ā”€ā”€ src
  ā””ā”€ā”€ `common`
    ā”œā”€ā”€ utils.ts
    ā”œā”€ā”€ context.ts
    ā””ā”€ā”€ `.scope.ts`
             ā”‚
             ā”‚
  ā•­ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā•®
  ā”‚ export default '*' ā”‚
  ā•°ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā•Æ
// ā¬† this will make all exports within `common`
// importable from anywhere unless a
// specific export is overriden on a lower level

Exceptions

Export scope exceptions

// schema.ts
/**
 * @scope ..
 * @scopeException src/schemaConsumer šŸ‘ˆ whole folder has access
 * @scopeException src/schemaConsumer/index.ts šŸ‘ˆ whole file has access
 */
export default "";

Folder scope exceptions in .scope.ts files

ā””ā”€ā”€ src
  ā””ā”€ā”€ `generated`
    ā”œā”€ā”€ schema.ts
    ā””ā”€ā”€ `.scope.ts`
             ā”‚
             ā”‚
  ā•­ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā•®
  ā”‚ export default '.';              ā”‚
  ā”‚                                  ā”‚
  ā”‚ export const exceptions = [      ā”‚
  ā”‚   'src/schemaConsumer',          ā”‚
  ā”‚   'src/scripts/schemaParser.ts', ā”‚
  ā”‚ ]                                ā”‚
  ā•°ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā•Æ
// ā¬† by default exports are only importable
// within `generated` folder, but
// folders/files in `exceptions` are exempt.

Installation

Install ESLint and the export-scope package. This package includes both an ESLint plugin and a TS Language Server plugin.

npm i -D eslint @typescript-eslint/parser eslint-plugin-export-scope
                    # ā¬† v6 or above

ESLint plugin will highlight imports outside the scope

// .eslintrc.js
module.exports = {
  // ...
  extends: ["plugin:eslint-plugin-export-scope/recommended"],
  parser: "@typescript-eslint/parser",
  parserOptions: { project: true, tsconfigRootDir: __dirname },
  ignorePatterns: ["!.scope.ts"],
};
// .eslintrc.js
module.exports = {
  // ...
  eĢ¶xĢ¶tĢ¶eĢ¶nĢ¶dĢ¶sĢ¶:Ģ¶ Ģ¶[Ģ¶"Ģ¶pĢ¶lĢ¶uĢ¶gĢ¶iĢ¶nĢ¶:Ģ¶eĢ¶sĢ¶lĢ¶iĢ¶nĢ¶tĢ¶-Ģ¶pĢ¶lĢ¶uĢ¶gĢ¶iĢ¶nĢ¶-Ģ¶eĢ¶xĢ¶pĢ¶oĢ¶rĢ¶tĢ¶-Ģ¶sĢ¶cĢ¶oĢ¶pĢ¶eĢ¶/Ģ¶rĢ¶eĢ¶cĢ¶oĢ¶mĢ¶mĢ¶eĢ¶nĢ¶dĢ¶eĢ¶dĢ¶"Ģ¶]Ģ¶,Ģ¶
  plugins: ["export-scope"],
  rules: { "export-scope/no-imports-outside-export-scope": "error" },
};

TS plugin will disable autocompletion for exports outside the scope

// tsconfig.json
"compilerOptions": {
  "plugins": [{ "name": "eslint-plugin-export-scope" }],
},
"include": ["**/*", "**/.scope.ts"]
//                  "../../**/.scope.ts" for monorepos

Tell VSCode to Use Workspace Version of TypeScript. Otherwise TS plugin won't work.

  • tsconfig.json file is still required for the plugin to work
  • replace .scope.ts in both configs with .scope.js
  • set compilerOptions.allowJsto true in tsconfig.json

Upgrading from v1 to v2

  • Replace all // comments with jsDocs /** */
  • Replace @scope default with @scopeDefault
  • Relace @.. file/folder prefixes with .scope.ts files.
  • Make sure .eslintrc.js and tsconfig.json configs are updated

Hints

  • Type @ above exports for automatic jsDoc generation.
  • Use autocompletion provided within jsDocs and .scope.ts files.
  • Root .scope.ts file (next to package.json) sets the default for the whole project. Having export default '*' there will make all exports global by default if you prefer a less strict approach.

Issues

āš ļø To re-lint an import in VSCode after updating a scope declaration either touch this import or restart the ESLint Server (ESLint limitation).

2.3.0

2 months ago

2.2.0

2 months ago

2.1.0

2 months ago

2.0.5

3 months ago

2.0.4

3 months ago

2.0.3

3 months ago

2.0.2

3 months ago

2.0.1

3 months ago

2.0.0-beta.4

3 months ago

2.0.0

3 months ago

2.0.0-beta.2

4 months ago

2.0.0-beta.3

4 months ago

2.0.0-beta.1

4 months ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago