Skip to content

Commit eb02528

Browse files
committed
Remove unnecessary comments from source code
This simplifies the code by removing redundant inline comments that just repeat what the code is doing.
1 parent 7cd34e4 commit eb02528

11 files changed

+51
-80
lines changed

packages/plugins/eslint-plugin-react-x/src/rules/jsx-key-before-spread.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,12 @@ export default createRule<[], MessageID>({
3333
export function create(context: RuleContext<MessageID, []>): RuleListener {
3434
return {
3535
JSXOpeningElement(node) {
36-
// Find the index of the first spread prop
3736
let firstSpreadPropIndex: null | number = null;
3837
for (const [index, prop] of node.attributes.entries()) {
39-
// Check if the prop is a spread prop
4038
if (prop.type === T.JSXSpreadAttribute) {
41-
// Store the index of the first spread prop found
4239
firstSpreadPropIndex ??= index;
4340
continue;
4441
}
45-
// If no spread prop has been found yet, continue to the next prop
4642
if (firstSpreadPropIndex == null) {
4743
continue;
4844
}

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,22 @@ export default createRule<[], MessageID>({
3232
});
3333

3434
export function create(context: RuleContext<MessageID, []>): RuleListener {
35-
// Checks if a JSX text node or a literal appears to be a comment
3635
function hasCommentLike(node: TSESTree.JSXText | TSESTree.Literal) {
3736
// If the node is within a JSX attribute or expression container, it's not a text node comment
3837
if (AST.isOneOf([T.JSXAttribute, T.JSXExpressionContainer])(node.parent)) {
3938
return false;
4039
}
4140
// Examines the node's raw text to see if it starts with '//' or '/*'
42-
const rawValue = context.sourceCode.getText(node);
43-
return /^\s*\/(?:\/|\*)/mu.test(rawValue);
41+
return /^\s*\/(?:\/|\*)/mu.test(context.sourceCode.getText(node));
4442
}
45-
// This function serves as the visitor for both JSXText and Literal nodes
4643
const visitorFunction = (node: TSESTree.JSXText | TSESTree.Literal): void => {
4744
// Ensures the node is a direct child of a JSX element or fragment
4845
if (!AST.isOneOf([T.JSXElement, T.JSXFragment])(node.parent)) {
4946
return;
5047
}
51-
// Skips nodes that do not appear to be comments
5248
if (!hasCommentLike(node)) {
5349
return;
5450
}
55-
// Reports an issue if a comment-like text node is found
5651
context.report({
5752
messageId: "jsxNoCommentTextnodes",
5853
node,

packages/plugins/eslint-plugin-react-x/src/rules/jsx-shorthand-fragment.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ export default createRule<Options, MessageID>({
5353
export function create(context: RuleContext<MessageID, Options>): RuleListener {
5454
// Get the rule policy from options, default to 1 (enforce shorthand)
5555
const policy = context.options[0] ?? defaultOptions[0];
56-
// Get JSX configuration from context and comments
5756
const jsxConfig = {
5857
...getJsxConfigFromContext(context),
5958
...getJsxConfigFromAnnotation(context),

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export default createRule<[], MessageID>({
3030
});
3131

3232
export function create(context: RuleContext<MessageID, []>): RuleListener {
33-
// Get JSX configuration from context and annotations
3433
const jsxConfig = {
3534
...getJsxConfigFromContext(context),
3635
...getJsxConfigFromAnnotation(context),

packages/plugins/eslint-plugin-react-x/src/rules/no-access-state-in-setstate.ts

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -54,50 +54,42 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
5454
return {};
5555
}
5656
// Stack to track class declarations and whether they are React components
57-
const classEntries: [
58-
node: TSESTree.ClassDeclaration | TSESTree.ClassExpression,
59-
isComponent: boolean,
60-
][] = [];
57+
const classStack: [node: TSESTree.ClassDeclaration | TSESTree.ClassExpression, isComponent: boolean][] = [];
6158
// Stack to track method definitions and whether they are static
62-
const methodEntries: [
63-
node: AST.TSESTreeMethodOrProperty,
64-
isStatic: boolean,
65-
][] = [];
59+
const methodStack: [node: AST.TSESTreeMethodOrProperty, isStatic: boolean][] = [];
6660
// Stack to track `setState` call expressions
67-
const setStateEntries: [
68-
node: TSESTree.CallExpression,
69-
hasThisState: boolean,
70-
][] = [];
61+
const setStateStack: [node: TSESTree.CallExpression, hasThisState: boolean][] = [];
62+
7163
return {
7264
// Push `setState` calls to the stack upon entry
7365
CallExpression(node) {
7466
if (!isThisSetState(node)) {
7567
return;
7668
}
77-
setStateEntries.push([node, false]);
69+
setStateStack.push([node, false]);
7870
},
7971
// Pop `setState` calls from the stack upon exit
8072
"CallExpression:exit"(node) {
8173
if (!isThisSetState(node)) {
8274
return;
8375
}
84-
setStateEntries.pop();
76+
setStateStack.pop();
8577
},
8678
// Push class declarations to the stack upon entry
8779
ClassDeclaration(node) {
88-
classEntries.push([node, isClassComponent(node)]);
80+
classStack.push([node, isClassComponent(node)]);
8981
},
9082
// Pop class declarations from the stack upon exit
9183
"ClassDeclaration:exit"() {
92-
classEntries.pop();
84+
classStack.pop();
9385
},
9486
// Push class expressions to the stack upon entry
9587
ClassExpression(node) {
96-
classEntries.push([node, isClassComponent(node)]);
88+
classStack.push([node, isClassComponent(node)]);
9789
},
9890
// Pop class expressions from the stack upon exit
9991
"ClassExpression:exit"() {
100-
classEntries.pop();
92+
classStack.pop();
10193
},
10294
// Main logic for detecting `this.state` access
10395
MemberExpression(node) {
@@ -106,17 +98,17 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
10698
return;
10799
}
108100
// Ensure we are inside a React class component
109-
const [currClass, isComponent = false] = classEntries.at(-1) ?? [];
101+
const [currClass, isComponent = false] = classStack.at(-1) ?? [];
110102
if (currClass == null || !isComponent) {
111103
return;
112104
}
113105
// Ensure we are inside a non-static method
114-
const [currMethod, isStatic = false] = methodEntries.at(-1) ?? [];
106+
const [currMethod, isStatic = false] = methodStack.at(-1) ?? [];
115107
if (currMethod == null || isStatic) {
116108
return;
117109
}
118110
// Ensure we are inside a `setState` call
119-
const [setState, hasThisState = false] = setStateEntries.at(-1) ?? [];
111+
const [setState, hasThisState = false] = setStateStack.at(-1) ?? [];
120112
if (setState == null || hasThisState) {
121113
return;
122114
}
@@ -129,33 +121,33 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
129121
},
130122
// Push method definitions to the stack upon entry
131123
MethodDefinition(node) {
132-
methodEntries.push([node, node.static]);
124+
methodStack.push([node, node.static]);
133125
},
134126
// Pop method definitions from the stack upon exit
135127
"MethodDefinition:exit"() {
136-
methodEntries.pop();
128+
methodStack.pop();
137129
},
138130
// Push property definitions to the stack upon entry
139131
PropertyDefinition(node) {
140-
methodEntries.push([node, node.static]);
132+
methodStack.push([node, node.static]);
141133
},
142134
// Pop property definitions from the stack upon exit
143135
"PropertyDefinition:exit"() {
144-
methodEntries.pop();
136+
methodStack.pop();
145137
},
146138
// Logic for detecting destructuring of `this.state`
147139
VariableDeclarator(node) {
148140
// Get current class and method context
149-
const [currClass, isComponent = false] = classEntries.at(-1) ?? [];
141+
const [currClass, isComponent = false] = classStack.at(-1) ?? [];
150142
if (currClass == null || !isComponent) {
151143
return;
152144
}
153-
const [currMethod, isStatic = false] = methodEntries.at(-1) ?? [];
145+
const [currMethod, isStatic = false] = methodStack.at(-1) ?? [];
154146
if (currMethod == null || isStatic) {
155147
return;
156148
}
157149
// Ensure we are inside a `setState` call
158-
const [setState, hasThisState = false] = setStateEntries.at(-1) ?? [];
150+
const [setState, hasThisState = false] = setStateStack.at(-1) ?? [];
159151
if (setState == null || hasThisState) {
160152
return;
161153
}

packages/plugins/eslint-plugin-react-x/src/rules/no-context-provider.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,8 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
4444
// Get the full name of the JSX element: "Foo.MyContext.Provider"
4545
const fullName = getJsxElementType(context, node);
4646
const parts = fullName.split(".");
47-
// Get the last part of the name: "Provider"
4847
const selfName = parts.pop();
49-
// Reconstruct the context name: "Foo.MyContext"
5048
const contextFullName = parts.join(".");
51-
// Get the context name itself: "MyContext"
5249
const contextSelfName = parts.pop();
5350
// Exit if the element is not a "Provider"
5451
if (selfName !== "Provider") return;
@@ -57,7 +54,6 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
5754
context.report({
5855
messageId: "noContextProvider",
5956
node,
60-
// Provide an auto-fix
6157
fix(fixer) {
6258
// Ensure the context name is a valid component name before applying the fix
6359
if (!isComponentNameLoose(contextSelfName)) return null;

packages/plugins/eslint-plugin-react-x/src/rules/no-missing-component-display-name.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
3333
// Fast path: skip if `memo` or `forwardRef` is not present in the file
3434
if (!context.sourceCode.text.includes("memo") && !context.sourceCode.text.includes("forwardRef")) return {};
3535

36-
// Initialize the component collector
3736
const {
3837
ctx,
3938
listeners,
@@ -48,10 +47,8 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
4847

4948
return {
5049
...listeners,
51-
// After the program is parsed, check all collected components
5250
"Program:exit"(program) {
5351
const components = ctx.getAllComponents(program);
54-
// Iterate over all found components
5552
for (const { node, displayName, flag } of components.values()) {
5653
// Check if the component is wrapped with `forwardRef` or `memo`
5754
const isMemoOrForwardRef = (flag & (ComponentFlag.ForwardRef | ComponentFlag.Memo)) > 0n;

packages/plugins/eslint-plugin-react-x/src/rules/no-missing-context-display-name.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
5252
if (!isCreateContextCall(context, node)) return;
5353
createCalls.push(node);
5454
},
55-
// After traversing the whole program, check for missing displayNames
5655
"Program:exit"() {
5756
for (const call of createCalls) {
5857
// Get the variable identifier for the context

packages/plugins/eslint-plugin-react-x/src/rules/no-prop-types.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ export default createRule<[], MessageID>({
3434

3535
export function create(context: RuleContext<MessageID, []>): RuleListener {
3636
// Fast path: skip if `propTypes` is not present in the file for performance
37-
if (!context.sourceCode.text.includes("propTypes")) {
38-
return {};
39-
}
37+
if (!context.sourceCode.text.includes("propTypes")) return {};
4038
return {
4139
// Handles cases like: `MyComponent.propTypes = ...`
4240
AssignmentExpression(node) {

packages/plugins/eslint-plugin-react-x/src/rules/no-unused-class-component-members.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,17 @@ export default createRule<[], MessageID>({
7474

7575
export function create(context: RuleContext<MessageID, []>): RuleListener {
7676
// A stack to keep track of class nodes, to handle nested classes
77-
const classEntries: AST.TSESTreeClass[] = [];
77+
const classStack: AST.TSESTreeClass[] = [];
7878
// A stack to keep track of method/property nodes
79-
const methodEntries: AST.TSESTreeMethodOrProperty[] = [];
79+
const methodStack: AST.TSESTreeMethodOrProperty[] = [];
8080
// Stores all defined properties and methods for each class component
8181
const propertyDefs = new WeakMap<AST.TSESTreeClass, Set<Property>>();
8282
// Stores all used properties and methods for each class component
8383
const propertyUsages = new WeakMap<AST.TSESTreeClass, Set<string>>();
8484

8585
// Called when the AST traversal enters a class declaration or expression
8686
function classEnter(node: AST.TSESTreeClass) {
87-
classEntries.push(node);
87+
classStack.push(node);
8888
if (!isClassComponent(node)) {
8989
return;
9090
}
@@ -95,7 +95,7 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
9595

9696
// Called when the AST traversal exits a class declaration or expression
9797
function classExit() {
98-
const currentClass = classEntries.pop();
98+
const currentClass = classStack.pop();
9999
if (currentClass == null || !isClassComponent(currentClass)) {
100100
return;
101101
}
@@ -129,8 +129,8 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
129129

130130
// Called when the AST traversal enters a method or property definition
131131
function methodEnter(node: AST.TSESTreeMethodOrProperty) {
132-
methodEntries.push(node);
133-
const currentClass = classEntries.at(-1);
132+
methodStack.push(node);
133+
const currentClass = classStack.at(-1);
134134
if (currentClass == null || !isClassComponent(currentClass)) {
135135
return;
136136
}
@@ -146,7 +146,7 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
146146

147147
// Called when the AST traversal exits a method or property definition
148148
function methodExit() {
149-
methodEntries.pop();
149+
methodStack.pop();
150150
}
151151

152152
return {
@@ -156,8 +156,8 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
156156
"ClassExpression:exit": classExit,
157157
// Visitor for MemberExpression to track property usages and definitions
158158
MemberExpression(node) {
159-
const currentClass = classEntries.at(-1);
160-
const currentMethod = methodEntries.at(-1);
159+
const currentClass = classStack.at(-1);
160+
const currentMethod = methodStack.at(-1);
161161
if (currentClass == null || currentMethod == null) {
162162
return;
163163
}
@@ -185,8 +185,8 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
185185
"PropertyDefinition:exit": methodExit,
186186
// Visitor for VariableDeclarator to track property usages via destructuring
187187
VariableDeclarator(node) {
188-
const currentClass = classEntries.at(-1);
189-
const currentMethod = methodEntries.at(-1);
188+
const currentClass = classStack.at(-1);
189+
const currentMethod = methodStack.at(-1);
190190
if (currentClass == null || currentMethod == null) {
191191
return;
192192
}

0 commit comments

Comments
 (0)