2.6.0 ⢠Published 4 months ago
eslint-plugin-export-scope v2.6.0
eslint-plugin-export-scope
Don't leak LOCAL utils, states, contexts, components into the global scope.
Demo
Scopes
scope | importable from | |
---|---|---|
. | current directory and children | default for all exports |
.. | parent directory and children | default for index files |
../.. | two directories above and children | |
src/consumer | within specified directory and children | |
src/consumer.ts | within 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 "";
/** @scope .. */
const helper3 = "";
export { helper3 }; // š inherits the scope from the variable declaration
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.
ESLint plugin will highlight imports outside the scope
npm i -D eslint typescript-eslint eslint-plugin-export-scope
// package.json
{
"type": "module"
}
// eslint.config.js
// @ts-check
import tseslint from "typescript-eslint";
import exportScope from "eslint-plugin-export-scope";
export default tseslint.config(
// other configs,
exportScope.configs.flatConfigRecommended,
);
// eslint.config.js
// @ts-check
import tseslint from "typescript-eslint";
import exportScope from "eslint-plugin-export-scope";
export default tseslint.config(
// other configs,
{
files: ["**/*.ts", "**/*.tsx", "**/*.js", "**/*.jsx", "**/*.mts", "**/*.mjs", "**/*.cjs"],
plugins: { "export-scope": exportScope.plugin },
rules: { "export-scope/no-imports-outside-export-scope": "error" },
languageOptions: { parser: tseslint.parser, parserOptions: { projectService: true }, sourceType: "module" },
},
);
npm i -D eslint @typescript-eslint/parser eslint-plugin-export-scope
# ⬠^8.0.0
// .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 = {
// ...
parser: "@typescript-eslint/parser",
parserOptions: { project: true, tsconfigRootDir: __dirname },
plugins: ["export-scope"],
rules: { "export-scope/no-imports-outside-export-scope": "error" },
ignorePatterns: ["!.scope.ts"],
};
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.allowJs
totrue
intsconfig.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
andtsconfig.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. Havingexport 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.6.0
4 months ago
2.5.0
5 months ago
2.4.0
1 year ago
2.3.0
1 year ago
2.2.0
1 year ago
2.1.0
1 year ago
2.0.5
1 year ago
2.0.4
1 year ago
2.0.3
1 year ago
2.0.2
1 year ago
2.0.1
1 year ago
2.0.0-beta.4
1 year ago
2.0.0
1 year ago
2.0.0-beta.2
1 year ago
2.0.0-beta.3
1 year ago
2.0.0-beta.1
1 year ago
1.0.9
2 years ago
1.0.8
3 years ago
1.0.7
3 years ago
1.0.6
3 years ago
1.0.5
3 years ago
1.0.4
3 years ago
1.0.3
3 years ago
1.0.2
3 years ago
1.0.1
3 years ago
1.0.0
3 years ago