Skip to content

Commit 82343f4

Browse files
committed
feat(rule): prefer named exports over default exports
For detailed explanation see: https://humanwhocodes.com/blog/2019/01/stop-using-default-exports-javascript-module/
1 parent e448af9 commit 82343f4

File tree

11 files changed

+23
-28
lines changed

11 files changed

+23
-28
lines changed

samples/all/Component.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import React from "react";
22

3-
import Image from "./Image";
3+
import { Image } from "./Image";
44

55
export interface ComponentProps {
66
className?: string;
77
children?: React.ReactNode;
88
}
99

10-
const Component: React.VoidFunctionComponent<ComponentProps> = (props) => {
10+
export const Component: React.VoidFunctionComponent<ComponentProps> = (
11+
props
12+
) => {
1113
const { className, children } = props;
1214

1315
return (
@@ -31,5 +33,3 @@ Component.defaultProps = {
3133
className: undefined,
3234
children: null,
3335
};
34-
35-
export default Component;

samples/all/Image.jsx

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
import PropTypes from "prop-types";
33
import React from "react";
44

5-
const Image = ({ alt, ...rest }) => <img alt={alt} {...rest} />;
5+
export const Image = ({ alt, ...rest }) => <img alt={alt} {...rest} />;
66

77
Image.propTypes = {
88
alt: PropTypes.string.isRequired,
99
src: PropTypes.string.isRequired,
1010
};
11-
12-
export default Image;

samples/all/a.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
const a = (b, c, d) => {
1+
export const a = (b, c, d) => {
22
return b * c - d;
33
};
44

55
export function e() {
66
return "f";
77
}
8-
9-
export default a;

samples/all/b.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import a, { e } from "./a";
1+
import { a, e } from "./a";
22

3-
export default function b() {
3+
export function b() {
44
a(1, 2, 3);
55

66
e();

samples/js/a.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
const a = (b, c, d) => {
1+
export const a = (b, c, d) => {
22
return b * c - d;
33
};
44

55
export function e() {
66
return "f";
77
}
8-
9-
export default a;

samples/js/b.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import a, { e } from "./a";
1+
import { a, e } from "./a";
22

3-
export default function b() {
3+
export function b() {
44
a(1, 2, 3);
55

66
e();

samples/jsx/Component.jsx

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import PropTypes from "prop-types";
33
import React from "react";
44

5-
const Component = (props) => {
5+
export const Component = (props) => {
66
const { className, children } = props;
77

88
return (
@@ -31,5 +31,3 @@ Component.propTypes = {
3131
className: PropTypes.string,
3232
children: PropTypes.element,
3333
};
34-
35-
export default Component;

samples/ts/a.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
const a = (b: number, c: number, d: number) => {
1+
export const a = (b: number, c: number, d: number) => {
22
return b * c - d;
33
};
44

55
export function e() {
66
return "f";
77
}
8-
9-
export default a;

samples/ts/b.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import a, { e } from "./a";
1+
import { a, e } from "./a";
22

3-
export default function b() {
3+
export function b() {
44
a(1, 2, 3);
55

66
e();

samples/tsx/Component.tsx

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export interface ComponentProps {
55
className?: string;
66
}
77

8-
const Component: React.FunctionComponent<ComponentProps> = (props) => {
8+
export const Component: React.FunctionComponent<ComponentProps> = (props) => {
99
const { className, children } = props;
1010

1111
return (
@@ -28,5 +28,3 @@ const Component: React.FunctionComponent<ComponentProps> = (props) => {
2828
Component.defaultProps = {
2929
className: undefined,
3030
};
31-
32-
export default Component;

src/internal/final.js

+7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ module.exports = {
1717
"arrow-body-style": "off",
1818
"prefer-arrow-callback": "off",
1919

20+
// Prefer named exports over default exports
21+
// https://humanwhocodes.com/blog/2019/01/stop-using-default-exports-javascript-module/
22+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/prefer-default-export.md
23+
// https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-default-export.md
24+
"import/prefer-default-export": "off",
25+
"import/no-default-export": "error",
26+
2027
// https://github.com/benmosher/eslint-plugin-import/tree/master/docs/rules
2128
"import/extensions": [
2229
"error",

0 commit comments

Comments
 (0)