Skip to content

Commit 0f4a76a

Browse files
committed
fix: fixed 'react-x/jsx-no-undef' false positive on JSX attribute names
1 parent 200c249 commit 0f4a76a

File tree

5 files changed

+16
-29
lines changed

5 files changed

+16
-29
lines changed

apps/website/content/docs/rules/overview.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Linter rules can have false positives, false negatives, and some rules are depen
2626
| Rule || 🌟 | Description | `react` |
2727
| :----------------------------------------------------------------------------------- | :- | :------: | :---------------------------------------------------------------------------------------------------- | :-----: |
2828
| [`jsx-no-duplicate-props`](./jsx-no-duplicate-props) | 1️⃣ | | Disallow duplicate props in JSX elements | |
29-
| [`jsx-no-undef`](./jsx-no-undef) | 2️⃣ | | Disallow undefined variables in JSX elements | |
29+
| [`jsx-no-undef`](./jsx-no-undef) | 0️⃣ | | Disallow undefined variables in JSX elements | |
3030
| [`jsx-uses-vars`](./jsx-uses-vars) | 1️⃣ | | Marks variables used in JSX elements as used | |
3131
| [`no-access-state-in-setstate`](./no-access-state-in-setstate) | 2️⃣ | | Disallow accessing `this.state` inside `setState` calls | |
3232
| [`no-array-index-key`](./no-array-index-key) | 1️⃣ | | Disallow an item's index in the array as its key | |

packages/plugins/eslint-plugin-react-x/src/configs/recommended.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ export const name = "react-x/recommended";
55

66
export const rules = {
77
"react-x/jsx-no-duplicate-props": "warn",
8-
"react-x/jsx-no-undef": "error",
98
"react-x/jsx-uses-vars": "warn",
109
"react-x/no-access-state-in-setstate": "error",
1110
"react-x/no-array-index-key": "warn",

packages/plugins/eslint-plugin-react-x/src/rules/jsx-no-undef.md

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22
title: jsx-no-undef
33
---
44

5-
<Callout type="warn" title="CAUTION">
6-
7-
**This rule hasn’t been released yet. If you want to try it, you can install the `beta` version of the plugin.**
8-
9-
</Callout>
10-
115
**Full Name in `eslint-plugin-react-x`**
126

137
```plain copy
@@ -20,11 +14,6 @@ react-x/jsx-no-undef
2014
@eslint-react/jsx-no-undef
2115
```
2216

23-
**Presets**
24-
25-
- `core`
26-
- `recommended`
27-
2817
## Description
2918

3019
This rule is used to prevent the use of undefined variables in JSX. It checks for any undefined variables in the JSX code and reports them as errors.

packages/plugins/eslint-plugin-react-x/src/rules/jsx-no-undef.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import type { RuleContext, RuleFeature } from "@eslint-react/kit";
22
import type { RuleListener } from "@typescript-eslint/utils/ts-eslint";
33
import type { CamelCase } from "string-ts";
4-
import * as VAR from "@eslint-react/var";
4+
import { _ } from "@eslint-react/eff";
55

6+
import * as VAR from "@eslint-react/var";
67
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
8+
import { match } from "ts-pattern";
79
import { createRule } from "../utils";
810

911
export const RULE_NAME = "jsx-no-undef";
@@ -31,24 +33,22 @@ export default createRule<[], MessageID>({
3133

3234
export function create(context: RuleContext<MessageID, []>): RuleListener {
3335
return {
34-
JSXIdentifier(node) {
35-
if (node.name === "this") {
36-
return;
37-
}
36+
JSXOpeningElement(node) {
37+
const name = match(node.name)
38+
.with({ type: T.JSXIdentifier }, (n) => n.name)
39+
.with({ type: T.JSXMemberExpression, object: { type: T.JSXIdentifier } }, (n) => n.object.name)
40+
.otherwise(() => _);
41+
if (name == null) return;
42+
if (name === "this") return;
3843
// Skip JsxIntrinsicElements
39-
if (/^[a-z]/u.test(node.name)) {
40-
return;
41-
}
42-
// Skip JSXMemberExpression property
43-
if (node.parent.type === T.JSXMemberExpression && node.parent.property === node) {
44-
return;
45-
}
46-
const initialScope = context.sourceCode.getScope(node);
47-
if (VAR.findVariable(node.name, initialScope) == null) {
44+
if (/^[a-z]/u.test(name)) return;
45+
if (VAR.findVariable(name, context.sourceCode.getScope(node)) == null) {
4846
context.report({
4947
messageId: "jsxNoUndef",
5048
node,
51-
data: { name: node.name },
49+
data: {
50+
name,
51+
},
5252
});
5353
}
5454
},

packages/plugins/eslint-plugin/src/configs/core.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ export const name = "@eslint-react/core";
66

77
export const rules = {
88
"@eslint-react/jsx-no-duplicate-props": "warn",
9-
"@eslint-react/jsx-no-undef": "error",
109
"@eslint-react/jsx-uses-vars": "warn",
1110
"@eslint-react/no-access-state-in-setstate": "error",
1211
"@eslint-react/no-array-index-key": "warn",

0 commit comments

Comments
 (0)