diff --git a/lib/util/hasTooltipParent.js b/lib/util/hasTooltipParent.js deleted file mode 100644 index e73a704..0000000 --- a/lib/util/hasTooltipParent.js +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -var elementType = require("jsx-ast-utils").elementType; - -function hasToolTipParent(context) { - const ancestors = context.getAncestors(); - - if (ancestors == null || ancestors.length === 0) { - return false; - } - - var toolTip = false; - - ancestors.forEach(item => { - if ( - item.type === "JSXElement" && - item.openingElement != null && - item.openingElement.type === "JSXOpeningElement" && - elementType(item.openingElement) === "Tooltip" - ) { - toolTip = true; - } - }); - - return toolTip; -} - -module.exports = { - hasToolTipParent -}; diff --git a/lib/util/hasTooltipParent.ts b/lib/util/hasTooltipParent.ts new file mode 100644 index 0000000..e41abae --- /dev/null +++ b/lib/util/hasTooltipParent.ts @@ -0,0 +1,24 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +import { elementType } from "jsx-ast-utils"; +import { TSESLint } from "@typescript-eslint/utils"; // Assuming context comes from TSESLint +import { JSXOpeningElement } from "estree-jsx"; + +const hasToolTipParent = (context: TSESLint.RuleContext): boolean => { + const ancestors = context.getAncestors(); + + if (!ancestors || ancestors.length === 0) { + return false; + } + + return ancestors.some( + item => + item.type === "JSXElement" && + item.openingElement && + item.openingElement.type === "JSXOpeningElement" && + elementType(item.openingElement as unknown as JSXOpeningElement) === "Tooltip" + ); +}; + +export { hasToolTipParent }; diff --git a/tests/lib/rules/utils/hasTooltipParent.test.ts b/tests/lib/rules/utils/hasTooltipParent.test.ts new file mode 100644 index 0000000..b048b33 --- /dev/null +++ b/tests/lib/rules/utils/hasTooltipParent.test.ts @@ -0,0 +1,70 @@ +import { hasToolTipParent } from "../../../../lib/util/hasTooltipParent"; +import { TSESLint } from "@typescript-eslint/utils"; + +// Mocking the elementType utility +jest.mock("jsx-ast-utils", () => ({ + elementType: (openingElement: any) => openingElement.name.name +})); + +describe("hasToolTipParent", () => { + let mockContext: TSESLint.RuleContext; + + beforeEach(() => { + mockContext = { + getAncestors: jest.fn() + } as unknown as TSESLint.RuleContext; + }); + + test("should return false when there are no ancestors", () => { + (mockContext.getAncestors as jest.Mock).mockReturnValue([]); + + const result = hasToolTipParent(mockContext); + expect(result).toBe(false); + }); + + test("should return false when no Tooltip ancestor exists", () => { + const mockAncestors = [ + { + type: "JSXElement", + openingElement: { + type: "JSXOpeningElement", + name: { name: "Button" } // Not a Tooltip + } + }, + { + type: "JSXElement", + openingElement: { + type: "JSXOpeningElement", + name: { name: "Div" } // Not a Tooltip + } + } + ]; + (mockContext.getAncestors as jest.Mock).mockReturnValue(mockAncestors); + + const result = hasToolTipParent(mockContext); + expect(result).toBe(false); + }); + + test("should return true when a Tooltip ancestor exists", () => { + const mockAncestors = [ + { + type: "JSXElement", + openingElement: { + type: "JSXOpeningElement", + name: { name: "Div" } // Not a Tooltip + } + }, + { + type: "JSXElement", + openingElement: { + type: "JSXOpeningElement", + name: { name: "Tooltip" } // This is a Tooltip + } + } + ]; + (mockContext.getAncestors as jest.Mock).mockReturnValue(mockAncestors); + + const result = hasToolTipParent(mockContext); + expect(result).toBe(true); + }); +});