Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New rule: no-mixed-exports #1

Merged
merged 5 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,34 @@ Examples of invalid code
const myFunction = (parameterA: { foo: string }) => {};
function myFunction(parameterA: { foo: string }) {}
```

### no-mixed-exports

Mixed exports refers to a file having both a default and one or more named exports.

Mixing exports can make the code hard to navigate and unpredictable. This rule forbids mixing exports.

Examples of valid code

```js
// types.ts
export type Options = {
value: number
};

// index.ts
import { Options } from "./types.ts";

export default myFunction(parameters: Options) {...}
```

Examples of invalid code

```js
// index.ts
export type Options = {
value: number
};

export default myFunction(parameters: Options) {...}
```
2 changes: 2 additions & 0 deletions src/rules/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { noInlineFunctionParameterTypeAnnotation } from "./noInlineFunctionParameterTypeAnnotation";
import { noMixedExports } from "./noMixedExports";

const rules = {
"no-inline-function-parameter-type-annotation":
noInlineFunctionParameterTypeAnnotation,
"no-mixed-exports": noMixedExports,
};

export { rules };
40 changes: 40 additions & 0 deletions src/rules/noMixedExports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { ESLintUtils } from "@typescript-eslint/utils";

export const noMixedExports = ESLintUtils.RuleCreator.withoutDocs({
create(context) {
let hasDefaultExport = false;
let hasNamedExport = false;

return {
ExportDefaultDeclaration(node) {
hasDefaultExport = true;

if (hasNamedExport) {
context.report({
node,
messageId: "noMixedExports",
});
}
},
ExportNamedDeclaration(node) {
hasNamedExport = true;

if (hasDefaultExport) {
context.report({
node,
messageId: "noMixedExports",
});
}
},
};
},
meta: {
messages: {
noMixedExports:
"Do not mix exports, a file should either include named exports or a default export.",
},
type: "problem",
schema: [],
},
defaultOptions: [],
});
26 changes: 26 additions & 0 deletions src/tests/noMixedExports.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { RuleTester } from "@typescript-eslint/rule-tester";
import * as vitest from "vitest";
import { noMixedExports } from "../rules/noMixedExports";

RuleTester.afterAll = vitest.afterAll;
RuleTester.it = vitest.it;
RuleTester.itOnly = vitest.it.only;
RuleTester.describe = vitest.describe;

const ruleTester = new RuleTester({
parser: "@typescript-eslint/parser",
});

ruleTester.run("noMixedExports", noMixedExports, {
valid: ["export const myExport = () => {}", "export default () => {}"],
invalid: [
{
code: "export const myExport = () => {}; export default () => {}",
errors: [
{
messageId: "noMixedExports",
},
],
},
],
});