Skip to content

Commit f76346a

Browse files
committed
perf: make 'no-create-ref' smaller and faster
1 parent ae1d3ed commit f76346a

File tree

3 files changed

+6
-53
lines changed

3 files changed

+6
-53
lines changed

packages/plugins/eslint-plugin-react-x/src/rules/no-create-ref.spec.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -123,32 +123,6 @@ ruleTester.run(RULE_NAME, rule, {
123123
/* tsx */ `
124124
import { createRef } from 'react';
125125
126-
const ref = createRef();
127-
`,
128-
/* tsx */ `
129-
import { createRef } from 'react';
130-
131-
function notComponent() {
132-
const ref = createRef();
133-
}
134-
`,
135-
/* tsx */ `
136-
import { createRef } from 'react';
137-
138-
function NotComponent() {
139-
const ref = createRef();
140-
}
141-
`,
142-
/* tsx */ `
143-
import { createRef } from 'react';
144-
145-
function NotComponent() {
146-
return () => createRef();
147-
}
148-
`,
149-
/* tsx */ `
150-
import { createRef } from 'react';
151-
152126
function Component() {
153127
const ref = useRef();
154128

packages/plugins/eslint-plugin-react-x/src/rules/no-create-ref.ts

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import type { TSESTreeFunction } from "@eslint-react/ast";
2-
import { isCreateRefCall, useComponentCollector } from "@eslint-react/core";
1+
import { traverseUp } from "@eslint-react/ast";
2+
import { isClassComponent, isCreateRefCall } from "@eslint-react/core";
33
import { O } from "@eslint-react/tools";
4-
import type { TSESTree } from "@typescript-eslint/types";
54
import type { ESLintUtils } from "@typescript-eslint/utils";
65
import type { ConstantCase } from "string-ts";
76

@@ -18,37 +17,17 @@ export default createRule<[], MessageID>({
1817
description: "disallow 'createRef' in function components",
1918
},
2019
messages: {
21-
NO_CREATE_REF: "A 'createRef' is not allowed in function components. Use 'useRef' instead.",
20+
NO_CREATE_REF: "[Deprecated] Use 'useRef' instead.",
2221
},
2322
schema: [],
2423
},
2524
name: RULE_NAME,
2625
create(context) {
27-
const { ctx, listeners } = useComponentCollector(context);
28-
const possibleCreateRefCalls = new WeakMap<TSESTreeFunction, TSESTree.CallExpression[]>();
29-
3026
return {
31-
...listeners,
3227
CallExpression(node) {
3328
if (!isCreateRefCall(node, context)) return;
34-
O.map(
35-
ctx.getCurrentFunction(),
36-
([_, currentFn]) =>
37-
possibleCreateRefCalls.set(currentFn, [...possibleCreateRefCalls.get(currentFn) ?? [], node]),
38-
);
39-
},
40-
"Program:exit"(node) {
41-
const components = ctx.getAllComponents(node).values();
42-
for (const { node: component } of components) {
43-
const createRefCalls = possibleCreateRefCalls.get(component);
44-
if (!createRefCalls) continue;
45-
for (const createRefCall of createRefCalls) {
46-
context.report({
47-
messageId: "NO_CREATE_REF",
48-
node: createRefCall,
49-
});
50-
}
51-
}
29+
if (O.isSome(traverseUp(node, isClassComponent))) return;
30+
context.report({ messageId: "NO_CREATE_REF", node });
5231
},
5332
};
5433
},

website/pages/docs/rules/no-create-ref.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import React, { createRef } from "react";
2222
function Example() {
2323
const ref = React.createRef<HTMLDivElement>();
2424
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
25-
// - 'createRef' is not allowed in function components. Use 'useRef' instead.
25+
// - [Deprecated] Use 'useRef' instead.
2626

2727
return <div ref={ref} />;
2828
}

0 commit comments

Comments
 (0)