Skip to content

Commit 8b7268a

Browse files
committed
Fix bundle issue [publish]
1 parent 9594a20 commit 8b7268a

7 files changed

+760
-447
lines changed

.eslintrc.cjs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
root: true,
3+
extends: ["@arnaud-barre"],
4+
};

bun.lockb

18.9 KB
Binary file not shown.

package.json

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
{
22
"name": "eslint-plugin-react-refresh",
33
"version": "0.4.5",
4+
"type": "module",
45
"license": "MIT",
56
"scripts": {
67
"build": "scripts/bundle.ts",
8+
"lint": "bun lint-ci --fix --cache",
9+
"lint-ci": "eslint ./ --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
710
"prettier": "bun prettier-ci --write",
811
"prettier-ci": "prettier --ignore-path=.gitignore --check '**/*.{ts,json,md,yml}'",
9-
"ci": "tsc && bun prettier-ci && bun test && bun run build"
12+
"ci": "tsc && bun lint-ci && bun prettier-ci && bun test && bun run build"
1013
},
1114
"peerDependencies": {
1215
"eslint": ">=7"
1316
},
1417
"devDependencies": {
18+
"@arnaud-barre/eslint-config": "^4.0.0",
1519
"@arnaud-barre/tnode": "^0.19.2",
16-
"@types/eslint": "^8.44.6",
17-
"@types/node": "^20.8.7",
18-
"@typescript-eslint/parser": "^6.9.0",
19-
"@typescript-eslint/utils": "^6.9.0",
20-
"bun-types": "^1.0.7",
21-
"eslint": "^8.52.0",
22-
"prettier": "^3.0.3",
23-
"typescript": "^5.2.2"
20+
"@types/eslint": "^8.44.8",
21+
"@types/node": "^20.10.2",
22+
"@typescript-eslint/parser": "^6.13.1",
23+
"@typescript-eslint/utils": "^6.13.1",
24+
"bun-types": "^1.0.15",
25+
"eslint": "^8.55.0",
26+
"prettier": "3.0.3",
27+
"typescript": "~5.3"
2428
}
2529
}

scripts/bundle.ts

+31-31
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
11
#!/usr/bin/env tnode
2-
import { rmSync, writeFileSync } from "fs";
3-
import { execSync } from "child_process";
2+
import { rmSync, writeFileSync } from "node:fs";
3+
import { execSync } from "node:child_process";
44
import { build } from "esbuild";
55

66
import packageJSON from "../package.json";
77

88
rmSync("dist", { force: true, recursive: true });
99

10-
build({
10+
await build({
1111
bundle: true,
1212
entryPoints: ["src/index.ts"],
1313
outdir: "dist",
1414
platform: "node",
1515
target: "node14",
1616
external: Object.keys(packageJSON.peerDependencies),
17-
}).then(() => {
18-
execSync("cp LICENSE README.md dist/");
19-
20-
writeFileSync(
21-
"dist/package.json",
22-
JSON.stringify(
23-
{
24-
name: packageJSON.name,
25-
description:
26-
"Validate that your components can safely be updated with fast refresh",
27-
version: packageJSON.version,
28-
author: "Arnaud Barré (https://github.com/ArnaudBarre)",
29-
license: packageJSON.license,
30-
repository: "github:ArnaudBarre/eslint-plugin-react-refresh",
31-
main: "index.js",
32-
keywords: [
33-
"eslint",
34-
"eslint-plugin",
35-
"react",
36-
"react-refresh",
37-
"fast refresh",
38-
],
39-
peerDependencies: packageJSON.peerDependencies,
40-
},
41-
null,
42-
2,
43-
),
44-
);
4517
});
18+
19+
execSync("cp LICENSE README.md dist/");
20+
21+
writeFileSync(
22+
"dist/package.json",
23+
JSON.stringify(
24+
{
25+
name: packageJSON.name,
26+
description:
27+
"Validate that your components can safely be updated with fast refresh",
28+
version: packageJSON.version,
29+
author: "Arnaud Barré (https://github.com/ArnaudBarre)",
30+
license: packageJSON.license,
31+
repository: "github:ArnaudBarre/eslint-plugin-react-refresh",
32+
main: "index.js",
33+
keywords: [
34+
"eslint",
35+
"eslint-plugin",
36+
"react",
37+
"react-refresh",
38+
"fast refresh",
39+
],
40+
peerDependencies: packageJSON.peerDependencies,
41+
},
42+
null,
43+
2,
44+
),
45+
);

src/only-export-components.test.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ const valid = [
117117
},
118118
{
119119
name: "Component and template literal with allowConstantExport",
120+
// eslint-disable-next-line no-template-curly-in-string
120121
code: "const foo = 'world'; export const CONSTANT = `Hello ${foo}`; export const Foo = () => {};",
121122
options: [{ allowConstantExport: true }],
122123
},
@@ -216,7 +217,7 @@ const invalid = [
216217
},
217218
{
218219
name: "export default compose",
219-
code: `export default compose()(MainView);`,
220+
code: "export default compose()(MainView);",
220221
filename: "Test.jsx",
221222
errorId: "anonymousExport",
222223
},
@@ -232,21 +233,21 @@ const it = (name: string, cases: Parameters<typeof ruleTester.run>[2]) => {
232233
test(name, () => {
233234
ruleTester.run(
234235
"only-export-components",
235-
// @ts-ignore Mismatch between typescript-eslint and eslint
236+
// @ts-expect-error Mismatch between typescript-eslint and eslint
236237
onlyExportComponents,
237238
cases,
238239
);
239240
});
240241
};
241242

242-
valid.forEach(({ name, code, filename, options = [] }) => {
243+
for (const { name, code, filename, options = [] } of valid) {
243244
it(name, {
244245
valid: [{ filename: filename ?? "Test.jsx", code, options }],
245246
invalid: [],
246247
});
247-
});
248+
}
248249

249-
invalid.forEach(({ name, code, errorId, filename, options = [] }) => {
250+
for (const { name, code, errorId, filename, options = [] } of invalid) {
250251
it(name, {
251252
valid: [],
252253
invalid: [
@@ -258,4 +259,4 @@ invalid.forEach(({ name, code, errorId, filename, options = [] }) => {
258259
},
259260
],
260261
});
261-
});
262+
}

src/only-export-components.ts

+15-20
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { TSESLint } from "@typescript-eslint/utils";
2-
import { TSESTree } from "@typescript-eslint/types";
1+
import type { TSESLint } from "@typescript-eslint/utils";
2+
import type { TSESTree } from "@typescript-eslint/types";
33

4-
const possibleReactExportRE = /^[A-Z][a-zA-Z0-9]*$/;
4+
const possibleReactExportRE = /^[A-Z][a-zA-Z0-9]*$/u;
55
// Starts with uppercase and at least one lowercase
66
// This can lead to some false positive (ex: `const CMS = () => <></>; export default CMS`)
77
// But allow to catch `export const CONSTANT = 3`
88
// and the false positive can be avoided with direct name export
9-
const strictReactExportRE = /^[A-Z][a-zA-Z0-9]*[a-z]+[a-zA-Z0-9]*$/;
9+
const strictReactExportRE = /^[A-Z][a-zA-Z0-9]*[a-z]+[a-zA-Z0-9]*$/u;
1010

1111
export const onlyExportComponents: TSESLint.RuleModule<
1212
| "exportAll"
@@ -55,7 +55,7 @@ export const onlyExportComponents: TSESLint.RuleModule<
5555
allowConstantExport = false,
5656
checkJS = false,
5757
allowExportNames,
58-
} = context.options[0] || {};
58+
} = context.options[0] ?? {};
5959
const filename = context.getFilename();
6060
// Skip tests & stories files
6161
if (
@@ -97,9 +97,7 @@ export const onlyExportComponents: TSESLint.RuleModule<
9797
nonComponentExports.push(identifierNode);
9898
return;
9999
}
100-
if (allowExportNames?.includes(identifierNode.name)) {
101-
return;
102-
}
100+
if (allowExportNames?.includes(identifierNode.name)) return;
103101
if (
104102
allowConstantExport &&
105103
init &&
@@ -184,10 +182,7 @@ export const onlyExportComponents: TSESLint.RuleModule<
184182
if (node.declaration.type === "Identifier") {
185183
handleExportIdentifier(node.declaration);
186184
}
187-
if (
188-
node.declaration.type === "ArrowFunctionExpression" &&
189-
!node.declaration.id
190-
) {
185+
if (node.declaration.type === "ArrowFunctionExpression") {
191186
context.report({ messageId: "anonymousExport", node });
192187
}
193188
} else if (node.type === "ExportNamedDeclaration") {
@@ -202,16 +197,18 @@ export const onlyExportComponents: TSESLint.RuleModule<
202197
}
203198
} else if (node.type === "FunctionDeclaration") {
204199
handleLocalIdentifier(node.id);
205-
} else if (node.type === "ImportDeclaration") {
206-
if (node.source.value === "react") {
207-
reactIsInScope = true;
208-
}
200+
} else if (
201+
node.type === "ImportDeclaration" &&
202+
node.source.value === "react"
203+
) {
204+
reactIsInScope = true;
209205
}
210206
}
211207

212208
if (checkJS && !reactIsInScope) return;
213209

214210
if (hasExports) {
211+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
215212
if (mayHaveReactExport) {
216213
for (const node of nonComponentExports) {
217214
context.report({ messageId: "namedExport", node });
@@ -235,10 +232,8 @@ const reactHOCs = ["memo", "forwardRef"];
235232
const canBeReactFunctionComponent = (init: TSESTree.Expression | null) => {
236233
if (!init) return false;
237234
if (init.type === "ArrowFunctionExpression") return true;
238-
if (init.type === "CallExpression") {
239-
if (init.callee.type === "Identifier") {
240-
return reactHOCs.includes(init.callee.name);
241-
}
235+
if (init.type === "CallExpression" && init.callee.type === "Identifier") {
236+
return reactHOCs.includes(init.callee.name);
242237
}
243238
return false;
244239
};

0 commit comments

Comments
 (0)