Skip to content

Commit 56944a8

Browse files
Remove comments
1 parent 88e7e01 commit 56944a8

File tree

3 files changed

+5
-35
lines changed

3 files changed

+5
-35
lines changed

lib/util/hasLabeledChild.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,30 @@ import { hasAssociatedAriaText } from "./labelUtils";
2424
*/
2525
export const hasLabeledChild = (openingElement: TSESTree.JSXOpeningElement, context: TSESLint.RuleContext<string, unknown[]>): boolean => {
2626
try {
27-
// Find the parent JSX element - it should be the immediate parent of the opening element
2827
let node: TSESTree.JSXElement | null = null;
2928

30-
// The openingElement's parent should be the JSXElement
3129
if (openingElement.parent && openingElement.parent.type === "JSXElement") {
3230
node = openingElement.parent as TSESTree.JSXElement;
3331
}
3432

35-
// If no parent node or no children, return false
3633
if (!node?.children || node.children.length === 0) {
3734
return false;
3835
}
3936

40-
// Get all child elements (flattened to handle nested structures)
4137
const allChildren = flattenChildren(node);
4238

43-
// Check if any child has accessible labeling
4439
return allChildren.some(child => {
4540
if (child.type === "JSXElement") {
4641
const childOpeningElement = child.openingElement;
4742
const childName = childOpeningElement.name;
4843

49-
// Check if child is an element that can provide accessible names
5044
if (childName.type === "JSXIdentifier") {
5145
const tagName = childName.name.toLowerCase();
5246

53-
// Image elements with alt text
5447
if ((tagName === "img" || tagName === "image") && hasNonEmptyProp(childOpeningElement.attributes, "alt")) {
5548
return true;
5649
}
5750

58-
// SVG elements with title or aria-label
5951
if (tagName === "svg") {
6052
return (
6153
hasNonEmptyProp(childOpeningElement.attributes, "title") ||
@@ -64,13 +56,11 @@ export const hasLabeledChild = (openingElement: TSESTree.JSXOpeningElement, cont
6456
);
6557
}
6658

67-
// Elements with role="img" and aria-label (like icons, emojis)
6859
if (hasNonEmptyProp(childOpeningElement.attributes, "role")) {
6960
const roleProp = childOpeningElement.attributes.find(
7061
attr => attr.type === "JSXAttribute" && attr.name?.type === "JSXIdentifier" && attr.name.name === "role"
7162
);
7263

73-
// Type guard to ensure we have a JSXAttribute with a value
7464
if (roleProp?.type === "JSXAttribute" && roleProp.value?.type === "Literal" && roleProp.value.value === "img") {
7565
return (
7666
hasNonEmptyProp(childOpeningElement.attributes, "aria-label") ||
@@ -79,7 +69,6 @@ export const hasLabeledChild = (openingElement: TSESTree.JSXOpeningElement, cont
7969
}
8070
}
8171

82-
// FluentUI Icon components or any element with aria-label/title/aria-labelledby
8372
if (
8473
tagName.toLowerCase().includes("icon") ||
8574
hasNonEmptyProp(childOpeningElement.attributes, "aria-label") ||
@@ -94,7 +83,6 @@ export const hasLabeledChild = (openingElement: TSESTree.JSXOpeningElement, cont
9483
return false;
9584
});
9685
} catch (error) {
97-
// Fail safely - if we can't determine, assume no labeled children
9886
return false;
9987
}
10088
};

tests/lib/rules/utils/hasLabeledChild.test.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/types";
55
import { TSESLint } from "@typescript-eslint/utils";
66
import { hasLabeledChild } from "../../../../lib/util/hasLabeledChild";
77

8-
// Helper for creating mock loc/range objects
98
const mockLocRange = () => ({
109
loc: {
1110
start: { line: 0, column: 0 },
@@ -14,7 +13,6 @@ const mockLocRange = () => ({
1413
range: [0, 0] as [number, number]
1514
});
1615

17-
// Mock context helper
1816
const mockContext = (sourceText = ""): TSESLint.RuleContext<string, unknown[]> => {
1917
return {
2018
getSourceCode: () => ({
@@ -24,7 +22,6 @@ const mockContext = (sourceText = ""): TSESLint.RuleContext<string, unknown[]> =
2422
} as unknown as TSESLint.RuleContext<string, unknown[]>;
2523
};
2624

27-
// Helper to create JSX attributes
2825
const createJSXAttribute = (name: string, value?: string | number): TSESTree.JSXAttribute => ({
2926
type: AST_NODE_TYPES.JSXAttribute,
3027
name: {
@@ -44,7 +41,6 @@ const createJSXAttribute = (name: string, value?: string | number): TSESTree.JSX
4441
...mockLocRange()
4542
});
4643

47-
// Helper to create JSX opening element
4844
const createJSXOpeningElement = (tagName: string, attributes: TSESTree.JSXAttribute[] = []): TSESTree.JSXOpeningElement => ({
4945
type: AST_NODE_TYPES.JSXOpeningElement,
5046
name: {
@@ -57,7 +53,6 @@ const createJSXOpeningElement = (tagName: string, attributes: TSESTree.JSXAttrib
5753
...mockLocRange()
5854
});
5955

60-
// Helper to create JSX element
6156
const createJSXElement = (
6257
tagName: string,
6358
attributes: TSESTree.JSXAttribute[] = [],
@@ -78,7 +73,6 @@ const createJSXElement = (
7873
...mockLocRange()
7974
});
8075

81-
// Helper to create opening element with parent context
8276
const createOpeningElementWithParent = (tagName: string, children: TSESTree.JSXElement[] = []): TSESTree.JSXOpeningElement => {
8377
const parentElement = createJSXElement("Container", [], children);
8478
const openingElement = createJSXOpeningElement(tagName);
@@ -441,11 +435,9 @@ describe("hasLabeledChild", () => {
441435

442436
describe("Integration with rule factory", () => {
443437
it("integrates correctly with makeLabeledControlRule", () => {
444-
// This test ensures the function works as expected when called from the rule factory
445438
const imgChild = createJSXElement("img", [createJSXAttribute("alt", "Integration test")]);
446439
const openingElement = createOpeningElementWithParent("Button", [imgChild]);
447440

448-
// Simulate the call from hasAccessibleLabel in ruleFactory
449441
expect(hasLabeledChild(openingElement, defaultContext)).toBe(true);
450442
});
451443
});

tests/lib/rules/utils/hasLabelledChilImage.test.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ describe("hasLabelledChildImage", () => {
158158
});
159159

160160
describe("hasLabelledChildImage - missing coverage", () => {
161-
it("covers line 34 - handles non-JSXElement children", () => {
162-
// This test covers the case where children exist but are not JSXElement types
161+
it("handles non-JSXElement children", () => {
163162
const mockTextChild: TSESTree.JSXText = {
164163
type: AST_NODE_TYPES.JSXText,
165164
value: "Some text content",
@@ -188,16 +187,14 @@ describe("hasLabelledChildImage - missing coverage", () => {
188187
...createMockLocRange()
189188
},
190189
closingElement: null,
191-
children: [mockTextChild, mockExpressionChild], // Non-JSXElement children
190+
children: [mockTextChild, mockExpressionChild],
192191
...createMockLocRange()
193192
};
194193

195-
// This should execute line 34 and return false since no JSXElement children are found
196194
expect(hasLabelledChildImage(node)).toBe(false);
197195
});
198196

199-
it("covers line 34 - JSXElement child with non-image component name", () => {
200-
// This covers the case where we have JSXElement children but they're not image components
197+
it("JSXElement child with non-image component name", () => {
201198
const mockDivChild: TSESTree.JSXElement = {
202199
type: AST_NODE_TYPES.JSXElement,
203200
openingElement: {
@@ -233,14 +230,12 @@ describe("hasLabelledChildImage - missing coverage", () => {
233230
...createMockLocRange()
234231
};
235232

236-
// This should execute line 34 and return false since child is not an image component
237233
expect(hasLabelledChildImage(node)).toBe(false);
238234
});
239235
});
240236

241237
describe("isImageHidden - missing coverage for aria-label handling", () => {
242-
it("covers lines 70-72 - returns false when aria-label has non-empty value", () => {
243-
// This covers the case where aria-label is present and has a value (lines 70-72)
238+
it("returns false when aria-label has non-empty value", () => {
244239
const attributes: TSESTree.JSXOpeningElement["attributes"] = [
245240
{
246241
type: AST_NODE_TYPES.JSXAttribute,
@@ -255,12 +250,10 @@ describe("isImageHidden - missing coverage for aria-label handling", () => {
255250
}
256251
];
257252

258-
// Should return false because aria-label has a value, meaning image is not hidden
259253
expect(isImageHidden(attributes)).toBe(false);
260254
});
261255

262-
it("covers lines 70-72 - returns false when aria-labelledby has non-empty value", () => {
263-
// This covers the case where aria-labelledby is present and has a value
256+
it("returns false when aria-labelledby has non-empty value", () => {
264257
const attributes: TSESTree.JSXOpeningElement["attributes"] = [
265258
{
266259
type: AST_NODE_TYPES.JSXAttribute,
@@ -275,12 +268,10 @@ describe("isImageHidden - missing coverage for aria-label handling", () => {
275268
}
276269
];
277270

278-
// Should return false because aria-labelledby has a value, meaning image is not hidden
279271
expect(isImageHidden(attributes)).toBe(false);
280272
});
281273

282274
it("handles case with both aria-label and aria-labelledby present", () => {
283-
// Additional test to ensure proper handling when both attributes are present
284275
const attributes: TSESTree.JSXOpeningElement["attributes"] = [
285276
{
286277
type: AST_NODE_TYPES.JSXAttribute,
@@ -306,7 +297,6 @@ describe("isImageHidden - missing coverage for aria-label handling", () => {
306297
}
307298
];
308299

309-
// Should return false because aria-label has a value
310300
expect(isImageHidden(attributes)).toBe(false);
311301
});
312302
});

0 commit comments

Comments
 (0)