Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .pkgs/function-rules/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
"devDependencies": {
"eslint": "^9.39.1",
"tsdown": "^0.17.0-beta.4"
"tsdown": "^0.17.0-beta.5"
},
"peerDependencies": {
"eslint": "^9.39.1",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"sort-package-json": "^3.5.0",
"tinyglobby": "^0.2.15",
"ts-pattern": "^5.9.0",
"tsdown": "^0.17.0-beta.4",
"tsdown": "^0.17.0-beta.5",
"tsx": "^4.21.0",
"type-fest": "^5.2.0",
"typedoc": "^0.28.15",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
},
"devDependencies": {
"@local/configs": "workspace:*",
"tsdown": "^0.17.0-beta.4"
"tsdown": "^0.17.0-beta.5"
},
"engines": {
"node": ">=20.19.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/eslint-plugin-react-debug/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"@local/configs": "workspace:*",
"@types/react": "^19.2.7",
"@types/react-dom": "^19.2.3",
"tsdown": "^0.17.0-beta.4"
"tsdown": "^0.17.0-beta.5"
},
"peerDependencies": {
"eslint": "^8.57.0 || ^9.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/eslint-plugin-react-dom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"@local/configs": "workspace:*",
"@types/react": "^19.2.7",
"@types/react-dom": "^19.2.3",
"tsdown": "^0.17.0-beta.4"
"tsdown": "^0.17.0-beta.5"
},
"peerDependencies": {
"eslint": "^8.57.0 || ^9.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"@local/configs": "workspace:*",
"@types/react": "^19.2.7",
"@types/react-dom": "^19.2.3",
"tsdown": "^0.17.0-beta.4"
"tsdown": "^0.17.0-beta.5"
},
"peerDependencies": {
"eslint": "^8.57.0 || ^9.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"@local/configs": "workspace:*",
"@types/react": "^19.2.7",
"@types/react-dom": "^19.2.3",
"tsdown": "^0.17.0-beta.4"
"tsdown": "^0.17.0-beta.5"
},
"peerDependencies": {
"eslint": "^8.57.0 || ^9.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/eslint-plugin-react-web-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"@local/configs": "workspace:*",
"@types/react": "^19.2.7",
"@types/react-dom": "^19.2.3",
"tsdown": "^0.17.0-beta.4"
"tsdown": "^0.17.0-beta.5"
},
"peerDependencies": {
"eslint": "^8.57.0 || ^9.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/eslint-plugin-react-x/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"@local/configs": "workspace:*",
"@types/react": "^19.2.7",
"@types/react-dom": "^19.2.3",
"tsdown": "^0.17.0-beta.4"
"tsdown": "^0.17.0-beta.5"
},
"peerDependencies": {
"eslint": "^8.57.0 || ^9.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ ruleTester.run(RULE_NAME, rule, {
export const App = memo(
forwardRef<HTMLDivElement, Props>(
function App({ day }, ref) {
const onClick = () => { console.log(ref.current) };
return <div ref={ref}>{day}</div>;
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default createRule<[], MessageID>({
export function create(context: RuleContext<MessageID, []>): RuleListener {
const { ctx, listeners } = useComponentCollector(context);
// Store all member expressions with their scope
const memberExpressionWithNames: [Scope, MemberExpressionWithObjectName][] = [];
const exprs: [Scope, MemberExpressionWithObjectName][] = [];

return {
...listeners,
Expand All @@ -50,17 +50,13 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
if (isMemberExpressionWithObjectName(node)) {
const scope = context.sourceCode.getScope(node);

memberExpressionWithNames.push([scope, node]);
exprs.push([scope, node]);
}
},

// After traversing the whole AST, check the collected member expressions
"Program:exit"(program) {
const components = [
...ctx
.getAllComponents(program)
.values(),
];
const components = [...ctx.getAllComponents(program).values()];
// Check if a node is a function component collected by `useComponentCollector`
function isFunctionComponent(block: TSESTree.Node): block is AST.TSESTreeFunction {
if (!AST.isFunction(block)) {
Expand All @@ -73,15 +69,11 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
}

// For each member expression, find its parent component
for (const [initialScope, memberExpression] of memberExpressionWithNames) {
for (const [initialScope, expr] of exprs) {
let scope = initialScope;
let isComponent = isFunctionComponent(scope.block);
// Traverse up the scope chain to find the component scope
while (
!isComponent
&& scope.upper != null
&& scope.upper !== scope
) {
while (!isComponent && scope.upper != null && scope.upper !== scope) {
scope = scope.upper;
isComponent = isFunctionComponent(scope.block);
}
Expand All @@ -93,32 +85,22 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
if (!("params" in component)) {
continue;
}
const [props, ctx] = component.params;
const props = component.params.at(0);
// Check if a node is an identifier with the same name as the member expression's object
const isMatch = (node: null | TSESTree.Node | undefined) =>
node != null
&& node.type === T.Identifier
&& node.name === memberExpression.object.name;
&& node.name === expr.object.name;
// If the member expression's object is `props`, report an error
if (isMatch(props)) {
context.report({
messageId: "preferDestructuringAssignment",
node: memberExpression,
node: expr,
data: {
name: "props",
},
});
}
// If the member expression's object is `context`, report an error
if (isMatch(ctx)) {
context.report({
messageId: "preferDestructuringAssignment",
node: memberExpression,
data: {
name: "context",
},
});
}
}
},
};
Expand Down
4 changes: 2 additions & 2 deletions packages/plugins/eslint-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ This project is and will continue to maintain that 90% of the code is written by

Contributions are welcome!

Please follow our [contributing guidelines](https://github.com/Rel1cx/eslint-react/tree/main/.github/CONTRIBUTING.md).
Please follow our [contributing guidelines](https://github.com/Rel1cx/eslint-react/tree/prefer-destructuring-assignment/.github/CONTRIBUTING.md).

## License

This project is licensed under the MIT License - see the [LICENSE](https://github.com/Rel1cx/eslint-react/tree/main/LICENSE) file for details.
This project is licensed under the MIT License - see the [LICENSE](https://github.com/Rel1cx/eslint-react/tree/prefer-destructuring-assignment/LICENSE) file for details.
2 changes: 1 addition & 1 deletion packages/plugins/eslint-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
},
"devDependencies": {
"@local/configs": "workspace:*",
"tsdown": "^0.17.0-beta.4"
"tsdown": "^0.17.0-beta.5"
},
"peerDependencies": {
"eslint": "^8.57.0 || ^9.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"@local/configs": "workspace:*",
"@tsconfig/node22": "^22.0.5",
"@types/picomatch": "^4.0.2",
"tsdown": "^0.17.0-beta.4"
"tsdown": "^0.17.0-beta.5"
},
"engines": {
"node": ">=20.19.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/utilities/ast/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
},
"devDependencies": {
"@local/configs": "workspace:*",
"tsdown": "^0.17.0-beta.4"
"tsdown": "^0.17.0-beta.5"
},
"engines": {
"node": ">=20.19.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/utilities/eff/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
"devDependencies": {
"@local/configs": "workspace:*",
"tsdown": "^0.17.0-beta.4"
"tsdown": "^0.17.0-beta.5"
},
"engines": {
"node": ">=20.19.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/utilities/var/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
},
"devDependencies": {
"@local/configs": "workspace:*",
"tsdown": "^0.17.0-beta.4"
"tsdown": "^0.17.0-beta.5"
},
"engines": {
"node": ">=20.19.0"
Expand Down
Loading