├── .github └── workflows │ ├── jest.yml │ ├── pretier.yml │ └── tsc.yml ├── .gitignore ├── .npmignore ├── .nvmrc ├── .prettierrc ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── docs └── no-re-exports.md ├── jest.config.js ├── package-lock.json ├── package.json ├── src ├── index.d.ts ├── index.ts ├── rules.d.ts ├── rules │ └── no-re-export.ts └── tests │ └── no-re-export.test.ts └── tsconfig.json /.github/workflows/jest.yml: -------------------------------------------------------------------------------- 1 | name: Jest 2 | 3 | on: 4 | pull_request: 5 | branches: 6 | - main 7 | merge_group: 8 | push: 9 | branches: 10 | - main 11 | 12 | jobs: 13 | jest: 14 | runs-on: ubuntu-latest 15 | steps: 16 | - name: Checkout 17 | uses: actions/checkout@v3 18 | with: 19 | persist-credentials: false 20 | - uses: actions/setup-node@v3 21 | with: 22 | node-version-file: ".nvmrc" 23 | cache: "npm" 24 | - name: Install dependencies 25 | run: | 26 | npm install 27 | - name: Run jest 28 | run: | 29 | npm run test 30 | -------------------------------------------------------------------------------- /.github/workflows/pretier.yml: -------------------------------------------------------------------------------- 1 | name: Prettier 2 | 3 | on: 4 | pull_request: 5 | branches: 6 | - main 7 | merge_group: 8 | push: 9 | branches: 10 | - main 11 | 12 | jobs: 13 | prettier: 14 | runs-on: ubuntu-latest 15 | steps: 16 | - name: Checkout 17 | uses: actions/checkout@v3 18 | with: 19 | persist-credentials: false 20 | - uses: actions/setup-node@v3 21 | with: 22 | node-version-file: ".nvmrc" 23 | cache: "npm" 24 | - name: Install dependencies 25 | run: | 26 | npm install 27 | - name: Run prettier 28 | run: | 29 | npx prettier --check . 30 | -------------------------------------------------------------------------------- /.github/workflows/tsc.yml: -------------------------------------------------------------------------------- 1 | name: TypeScript Compiler 2 | 3 | on: 4 | pull_request: 5 | branches: 6 | - main 7 | merge_group: 8 | push: 9 | branches: 10 | - main 11 | 12 | jobs: 13 | tsc: 14 | runs-on: ubuntu-latest 15 | steps: 16 | - name: Checkout 17 | uses: actions/checkout@v3 18 | with: 19 | persist-credentials: false 20 | - uses: actions/setup-node@v3 21 | with: 22 | node-version-file: ".nvmrc" 23 | cache: "npm" 24 | - name: Install dependencies 25 | run: | 26 | npm install 27 | - name: Run tsc 28 | run: | 29 | npx tsc 30 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | dist/ 2 | node_modules/ 3 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | dist/tests 2 | -------------------------------------------------------------------------------- /.nvmrc: -------------------------------------------------------------------------------- 1 | 20 2 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "printWidth": 100 3 | } 4 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # 0.0.6 2 | 3 | - Use `module.exports` to fix `rule.create is not a function` 4 | 5 | # 0.0.5 6 | 7 | - Convert to CommonJS for compatibility 8 | 9 | # 0.0.4 10 | 11 | - Fix main entry point in `package.json` 12 | 13 | # 0.0.3 14 | 15 | - Fix rule name from `no-re-exports` to `no-re-export` (part 2) 16 | 17 | # 0.0.2 18 | 19 | - Fix rule name from `no-re-exports` to `no-re-export` 20 | 21 | # 0.0.1 22 | 23 | - Initial release 24 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2023 Christian Vuerings. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # `eslint-plugin-no-re-export` 2 | 3 | > [!CAUTION] 4 | > Use [canonical/no-re-export](https://github.com/gajus/eslint-plugin-canonical?tab=readme-ov-file#no-re-export) instead of `no-re-export/no-re-export`. See https://github.com/gajus/eslint-plugin-canonical/pull/27 for more info 5 | 6 | [![npm](https://img.shields.io/npm/v/eslint-plugin-no-re-export)](https://www.npmjs.com/package/eslint-plugin-no-re-export) [![GitHub Build Status](https://img.shields.io/github/actions/workflow/status/christianvuerings/eslint-plugin-no-re-export/tsc.yml)](https://github.com/christianvuerings/eslint-plugin-no-re-export/actions/workflows/tsc.yml) ![GitHub Tests](https://img.shields.io/github/actions/workflow/status/christianvuerings/eslint-plugin-no-re-export/jest.yml?label=tests) 7 | 8 | Disallow re-exporting in TypeScript/JavaScript. 9 | 10 | ## Installation 11 | 12 | ```sh 13 | # npm 14 | npm install eslint-plugin-no-re-export --save-dev 15 | 16 | # yarn 17 | yarn add eslint-plugin-no-re-export --dev 18 | 19 | # bun 20 | bun install eslint-plugin-no-re-export --save-dev 21 | ``` 22 | 23 | ## Usage 24 | 25 | Add `no-re-export` to the plugins section of your `.eslintrc` configuration file. You can omit the `eslint-plugin-` prefix: 26 | 27 | ```json 28 | { 29 | "plugins": ["no-re-export"] 30 | } 31 | ``` 32 | 33 | Then configure the rules you want to use under the rules section. 34 | 35 | ```json 36 | { 37 | "rules": { 38 | "no-re-export/no-re-export": "error" 39 | } 40 | } 41 | ``` 42 | 43 | ## Rules 44 | 45 | | Rule ID | Description | 46 | | :----------------------------------------- | :--------------------------------------------- | 47 | | [no-re-export](./src/docs/no-re-export.md) | disallow re-exporting in TypeScript/JavaScript | 48 | 49 | ## References 50 | 51 | - [Speeding up the JavaScript ecosystem - The barrel file debacle](https://marvinh.dev/blog/speeding-up-javascript-ecosystem-part-7/) by [@marvinhagemeister](https://github.com/marvinhagemeister) 52 | - [Burn the Barrel!](https://uglow.medium.com/burn-the-barrel-c282578f21b6#:~:text=%E2%80%9CThe%20problem%20is%20that%20Jest,like%20%40mui%2Fmaterial%20.%E2%80%9D) by [@uglow](https://github.com/uglow) 53 | - [Your Next.js Bundle Will Thank You](https://renatopozzi.me/articles/your-nextjs-bundle-will-thank-you) by [@askides](https://github.com/askides) 54 | - [Barrel files in JavaScript](https://flaming.codes/posts/barrel-files-in-javascript) by [@flaming](https://github.com/flaming-codes) 55 | - Comment by [@ljharb](https://github.com/ljharb) at [eslint-plugin-import/issues/1920](https://github.com/import-js/eslint-plugin-import/issues/1920) 56 | 57 | > Barrel exports increase bundle size and memory footprint, and are the only reason treeshaking is needed (to only-partially clean up sloppy importing), and in my experience, are best avoided, especially in any codebase of significant scale/size. 58 | -------------------------------------------------------------------------------- /docs/no-re-exports.md: -------------------------------------------------------------------------------- 1 | ### Disallow re-exports `no-re-export/no-re-export` 2 | 3 | Re-exporting can cause the following problems: 4 | 5 | - Increase bundle size 6 | - Decrease build and test runner performance 7 | - Makes it harder to find the origin of a function 8 | 9 | ### ✅ Correct 10 | 11 | ```ts 12 | export default function foo() {} 13 | 14 | export function bar() {} 15 | ``` 16 | 17 | ### ❌ Incorrect 18 | 19 | ```ts 20 | export { foo } from "./foo"; 21 | ``` 22 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | // @ts-check 2 | 3 | /** @type {import('jest').Config} */ 4 | module.exports = { 5 | projects: [ 6 | { 7 | moduleDirectories: ["node_modules", "src"], 8 | transform: { 9 | "^.+\\.[jt]sx?$": [ 10 | "esbuild-jest", 11 | { 12 | sourcemap: true, 13 | }, 14 | ], 15 | }, 16 | testEnvironment: "node", 17 | testMatch: ["/src/**/*.test.ts"], 18 | }, 19 | ], 20 | clearMocks: true, 21 | restoreMocks: true, 22 | resetMocks: true, 23 | verbose: true, 24 | watchman: false, 25 | }; 26 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "eslint-plugin-no-re-export", 3 | "version": "0.0.6", 4 | "description": "ESLint plugin to disallow re-exporting in TypeScript/JavaScript", 5 | "main": "dist/index.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/christianvuerings/eslint-plugin-no-re-export" 9 | }, 10 | "scripts": { 11 | "build": "tsc", 12 | "format": "prettier --write .", 13 | "test": "jest" 14 | }, 15 | "author": "Christian Vuerings", 16 | "license": "MIT", 17 | "files": [ 18 | "docs", 19 | "dist", 20 | "index.d.ts", 21 | "index.js", 22 | "package.json" 23 | ], 24 | "prepublishOnly": "npm run build", 25 | "dependencies": { 26 | "@typescript-eslint/utils": "5.62.0" 27 | }, 28 | "devDependencies": { 29 | "@typescript-eslint/parser": "5.62.0", 30 | "esbuild-jest": "^0.5.0", 31 | "eslint": "^8.51.0", 32 | "jest": "^29.7.0", 33 | "prettier": "^3.0.3" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/index.d.ts: -------------------------------------------------------------------------------- 1 | import type rules from "./rules"; 2 | 3 | declare const cjsExport: { 4 | rules: typeof rules; 5 | }; 6 | export = cjsExport; 7 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import noReExport from "./rules/no-re-export"; 2 | 3 | module.exports = { 4 | rules: { 5 | "no-re-export": noReExport, 6 | }, 7 | }; 8 | -------------------------------------------------------------------------------- /src/rules.d.ts: -------------------------------------------------------------------------------- 1 | import type { RuleModule } from "@typescript-eslint/utils/ts-eslint"; 2 | 3 | export interface TypeScriptESLintRules { 4 | [ruleName: string]: RuleModule; 5 | } 6 | declare const rules: TypeScriptESLintRules; 7 | export = rules; 8 | -------------------------------------------------------------------------------- /src/rules/no-re-export.ts: -------------------------------------------------------------------------------- 1 | import { AST_NODE_TYPES, ESLintUtils, TSESTree } from "@typescript-eslint/utils"; 2 | 3 | const createRule = ESLintUtils.RuleCreator( 4 | (name) => 5 | `https://github.com/christianvuerings/eslint-plugin-no-re-export/blob/main/docs/${name}.md`, 6 | ); 7 | 8 | interface ReportInfo { 9 | location: TSESTree.SourceLocation; 10 | type: "DefaultReExport" | "ExportDefault" | "ExportObject" | "ObjectProperty" | "Variable"; 11 | } 12 | 13 | export default createRule({ 14 | name: "no-re-export", 15 | defaultOptions: [], 16 | meta: { 17 | docs: { 18 | description: 19 | "Disallow re-exports of imports. Avoids losing type safety & ensures bundle size does not increase by importing too much.", 20 | recommended: "error", 21 | }, 22 | messages: { 23 | noReExports: "Do not re-export '{{name}}'", 24 | }, 25 | type: "problem", 26 | schema: [], 27 | }, 28 | 29 | create(context) { 30 | const imports = new Map(); 31 | const exports: Record = {}; 32 | 33 | const appendToExports = (name: string, reportInfo: ReportInfo) => { 34 | exports[name] = [...(exports[name] ? exports[name] : []), reportInfo]; 35 | }; 36 | 37 | return { 38 | ImportDefaultSpecifier(node) { 39 | imports.set(node.local.name, { node }); 40 | }, 41 | ImportSpecifier(node) { 42 | imports.set(node.local.name, { node }); 43 | }, 44 | ImportNamespaceSpecifier(node) { 45 | imports.set(node.local.name, { node }); 46 | }, 47 | // export * from 'app/CustomCustomButton' 48 | ExportAllDeclaration(node) { 49 | context.report({ 50 | loc: node.loc, 51 | messageId: "noReExports", 52 | data: { 53 | name: node.source.value, 54 | }, 55 | }); 56 | }, 57 | ExportNamedDeclaration(node) { 58 | if ( 59 | node.declaration && 60 | node.declaration?.type === AST_NODE_TYPES.VariableDeclaration && 61 | node.declaration.declarations 62 | ) { 63 | node.declaration.declarations.forEach((declaration) => { 64 | if (declaration.init?.type === AST_NODE_TYPES.Identifier && declaration.init?.name) { 65 | // export const CustomButtom = Button; 66 | appendToExports(declaration.init.name, { 67 | location: declaration.init.loc, 68 | type: "Variable", 69 | }); 70 | } else if ( 71 | declaration.init?.type === AST_NODE_TYPES.ObjectExpression && 72 | declaration.init.properties 73 | ) { 74 | declaration.init.properties.forEach((property) => { 75 | if ( 76 | property?.type === AST_NODE_TYPES.Property && 77 | property.value.type === AST_NODE_TYPES.Identifier && 78 | property.value?.name 79 | ) { 80 | // export const CustomButton = { Button } 81 | appendToExports(property.value.name, { 82 | location: property.loc, 83 | type: "ObjectProperty", 84 | }); 85 | } 86 | }); 87 | } 88 | }); 89 | } else if (node.specifiers) { 90 | node.specifiers.forEach((specifier) => { 91 | if (node.source && specifier.local.name === "default") { 92 | // export { default as Button } from 'app/CustomButtom'; 93 | appendToExports(specifier.exported.name, { 94 | location: specifier.exported.loc, 95 | type: "DefaultReExport", 96 | }); 97 | } else { 98 | // export { Button } 99 | appendToExports(specifier.local.name, { 100 | location: specifier.local.loc, 101 | type: "ExportObject", 102 | }); 103 | } 104 | }); 105 | } 106 | }, 107 | ExportDefaultDeclaration(node) { 108 | if (node.declaration.type === AST_NODE_TYPES.Identifier) { 109 | // export default Button 110 | appendToExports(node.declaration.name, { 111 | location: node.declaration.loc, 112 | type: "ExportDefault", 113 | }); 114 | } 115 | }, 116 | "Program:exit": function programExit() { 117 | Object.keys(exports).forEach((exportName) => { 118 | const reportInfoList = exports[exportName]; 119 | reportInfoList.forEach(({ location, type }) => { 120 | if (imports.has(exportName) || type === "DefaultReExport") { 121 | context.report({ 122 | loc: location, 123 | messageId: "noReExports", 124 | data: { 125 | name: exportName, 126 | }, 127 | }); 128 | } 129 | }); 130 | }); 131 | }, 132 | }; 133 | }, 134 | }); 135 | -------------------------------------------------------------------------------- /src/tests/no-re-export.test.ts: -------------------------------------------------------------------------------- 1 | import { ESLintUtils } from "@typescript-eslint/utils"; 2 | import rule from "../rules/no-re-export"; 3 | 4 | const ruleTester = new ESLintUtils.RuleTester({ 5 | parser: "@typescript-eslint/parser", 6 | }); 7 | 8 | ruleTester.run("no-re-export", rule, { 9 | valid: [ 10 | ` 11 | import Button from 'app/CustomButton'; 12 | export const CustomButtom = 'Button'; 13 | `, 14 | ], 15 | invalid: [ 16 | { 17 | code: ` 18 | import Button1 from 'app/CustomButton'; 19 | export const CustomButtom = Button1; 20 | `, 21 | errors: [{ messageId: "noReExports" as const }], 22 | }, 23 | { 24 | code: ` 25 | import { Button as CustomButtom2 } from 'app/CustomButton'; 26 | export const CustomButtom = CustomButtom2; 27 | `, 28 | errors: [{ messageId: "noReExports" as const }], 29 | }, 30 | { 31 | code: ` 32 | import * as Button3 from "app/Button" 33 | export const CustomButtom = Button3; 34 | `, 35 | errors: [{ messageId: "noReExports" as const }], 36 | }, 37 | { 38 | code: ` 39 | import Button4 from 'app/CustomButtom'; 40 | export default Button4; 41 | `, 42 | errors: [{ messageId: "noReExports" as const }], 43 | }, 44 | { 45 | code: ` 46 | export { default as Button5 } from 'app/CustomButtom'; 47 | `, 48 | errors: [{ messageId: "noReExports" as const }], 49 | }, 50 | { 51 | code: ` 52 | import Button6 from 'app/CustomButtom'; 53 | export { 54 | Button6 55 | }; 56 | `, 57 | errors: [{ messageId: "noReExports" as const }], 58 | }, 59 | { 60 | code: ` 61 | import Button7 from 'app/CustomButtom'; 62 | export const Buttons = { 63 | Button: Button7 64 | }; 65 | `, 66 | errors: [{ messageId: "noReExports" as const }], 67 | }, 68 | { 69 | code: ` 70 | import Button8 from 'app/CustomButtom'; 71 | export default Button8; 72 | export { Button8 } 73 | `, 74 | errors: [{ messageId: "noReExports" as const }, { messageId: "noReExports" as const }], 75 | }, 76 | { 77 | code: ` 78 | export * from 'app/CustomButtom'; 79 | `, 80 | errors: [{ messageId: "noReExports" as const }], 81 | }, 82 | ], 83 | }); 84 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "allowSyntheticDefaultImports": true, 4 | "allowUnreachableCode": false, 5 | "declaration": false, 6 | "declarationMap": false, 7 | "emitDecoratorMetadata": false, 8 | "experimentalDecorators": false, 9 | "lib": ["es2023"], 10 | "module": "commonjs", 11 | "outDir": "./dist", 12 | "rootDir": "./src", 13 | "skipLibCheck": true, 14 | "sourceMap": true, 15 | "strict": true, 16 | "target": "ES2015", 17 | "typeRoots": ["node_modules/@types"] 18 | }, 19 | "include": ["src"], 20 | "exclude": ["dist"] 21 | } 22 | --------------------------------------------------------------------------------