From cd27b84024aa41589752c16cab31dfe573bf1fa4 Mon Sep 17 00:00:00 2001 From: Eric Olkowski <70952936+thatblindgeye@users.noreply.github.com> Date: Wed, 10 Jul 2024 16:46:26 -0400 Subject: [PATCH] fix(Switch): removed labelOff prop for a11y (#686) --- .../switch-remove-labelOff.md | 17 ++++++ .../switch-remove-labelOff.test.ts | 55 +++++++++++++++++++ .../switch-remove-labelOff.ts | 38 +++++++++++++ .../switchRemoveLabelOffInput.tsx | 3 + .../switchRemoveLabelOffOutput.tsx | 3 + 5 files changed, 116 insertions(+) create mode 100644 packages/eslint-plugin-pf-codemods/src/rules/v6/switchRemoveLabelOff/switch-remove-labelOff.md create mode 100644 packages/eslint-plugin-pf-codemods/src/rules/v6/switchRemoveLabelOff/switch-remove-labelOff.test.ts create mode 100644 packages/eslint-plugin-pf-codemods/src/rules/v6/switchRemoveLabelOff/switch-remove-labelOff.ts create mode 100644 packages/eslint-plugin-pf-codemods/src/rules/v6/switchRemoveLabelOff/switchRemoveLabelOffInput.tsx create mode 100644 packages/eslint-plugin-pf-codemods/src/rules/v6/switchRemoveLabelOff/switchRemoveLabelOffOutput.tsx diff --git a/packages/eslint-plugin-pf-codemods/src/rules/v6/switchRemoveLabelOff/switch-remove-labelOff.md b/packages/eslint-plugin-pf-codemods/src/rules/v6/switchRemoveLabelOff/switch-remove-labelOff.md new file mode 100644 index 000000000..d84bd1dfe --- /dev/null +++ b/packages/eslint-plugin-pf-codemods/src/rules/v6/switchRemoveLabelOff/switch-remove-labelOff.md @@ -0,0 +1,17 @@ +### switch-remove-labelOff [(#10646)](https://github.com/patternfly/patternfly-react/pull/10646) + +The `labelOff` prop has been removed from Switch. The label for a Switch should not dynamically update based on the on/off state. + +#### Examples + +In: + +```jsx +%inputExample% +``` + +Out: + +```jsx +%outputExample% +``` diff --git a/packages/eslint-plugin-pf-codemods/src/rules/v6/switchRemoveLabelOff/switch-remove-labelOff.test.ts b/packages/eslint-plugin-pf-codemods/src/rules/v6/switchRemoveLabelOff/switch-remove-labelOff.test.ts new file mode 100644 index 000000000..90a4840cb --- /dev/null +++ b/packages/eslint-plugin-pf-codemods/src/rules/v6/switchRemoveLabelOff/switch-remove-labelOff.test.ts @@ -0,0 +1,55 @@ +const ruleTester = require("../../ruletester"); +import * as rule from "./switch-remove-labelOff"; + +ruleTester.run("switch-remove-labelOff", rule, { + valid: [ + { + code: ``, + }, + { + code: `import { Switch } from '@patternfly/react-core'; `, + }, + ], + invalid: [ + { + code: `import { Switch } from '@patternfly/react-core'; `, + output: `import { Switch } from '@patternfly/react-core'; `, + errors: [ + { + message: `The \`labelOff\` prop has been removed from Switch. The label for a Switch should not dynamically update based on the on/off state.`, + type: "JSXOpeningElement", + }, + ], + }, + { + code: `import { Switch } from '@patternfly/react-core/dist/esm/components/Switch/index.js'; `, + output: `import { Switch } from '@patternfly/react-core/dist/esm/components/Switch/index.js'; `, + errors: [ + { + message: `The \`labelOff\` prop has been removed from Switch. The label for a Switch should not dynamically update based on the on/off state.`, + type: "JSXOpeningElement", + }, + ], + }, + { + code: `import { Switch } from '@patternfly/react-core/dist/js/components/Switch/index.js'; `, + output: `import { Switch } from '@patternfly/react-core/dist/js/components/Switch/index.js'; `, + errors: [ + { + message: `The \`labelOff\` prop has been removed from Switch. The label for a Switch should not dynamically update based on the on/off state.`, + type: "JSXOpeningElement", + }, + ], + }, + { + code: `import { Switch } from '@patternfly/react-core/dist/dynamic/components/Switch/index.js'; `, + output: `import { Switch } from '@patternfly/react-core/dist/dynamic/components/Switch/index.js'; `, + errors: [ + { + message: `The \`labelOff\` prop has been removed from Switch. The label for a Switch should not dynamically update based on the on/off state.`, + type: "JSXOpeningElement", + }, + ], + }, + ], +}); diff --git a/packages/eslint-plugin-pf-codemods/src/rules/v6/switchRemoveLabelOff/switch-remove-labelOff.ts b/packages/eslint-plugin-pf-codemods/src/rules/v6/switchRemoveLabelOff/switch-remove-labelOff.ts new file mode 100644 index 000000000..198e329a9 --- /dev/null +++ b/packages/eslint-plugin-pf-codemods/src/rules/v6/switchRemoveLabelOff/switch-remove-labelOff.ts @@ -0,0 +1,38 @@ +import { Rule } from "eslint"; +import { JSXOpeningElement } from "estree-jsx"; +import { getFromPackage, getAttribute } from "../../helpers"; + +// https://github.com/patternfly/patternfly-react/pull/10646 +module.exports = { + meta: { fixable: "code" }, + create: function (context: Rule.RuleContext) { + const { imports } = getFromPackage(context, "@patternfly/react-core"); + + const switchImport = imports.find( + (specifier) => specifier.imported.name === "Switch" + ); + + return !switchImport + ? {} + : { + JSXOpeningElement(node: JSXOpeningElement) { + if ( + node.name.type === "JSXIdentifier" && + switchImport.local.name === node.name.name + ) { + const attribute = getAttribute(node, "labelOff"); + if (attribute) { + context.report({ + node, + message: + "The `labelOff` prop has been removed from Switch. The label for a Switch should not dynamically update based on the on/off state.", + fix(fixer) { + return fixer.replaceText(attribute, ""); + }, + }); + } + } + }, + }; + }, +}; diff --git a/packages/eslint-plugin-pf-codemods/src/rules/v6/switchRemoveLabelOff/switchRemoveLabelOffInput.tsx b/packages/eslint-plugin-pf-codemods/src/rules/v6/switchRemoveLabelOff/switchRemoveLabelOffInput.tsx new file mode 100644 index 000000000..5d0235392 --- /dev/null +++ b/packages/eslint-plugin-pf-codemods/src/rules/v6/switchRemoveLabelOff/switchRemoveLabelOffInput.tsx @@ -0,0 +1,3 @@ +import { Switch } from "@patternfly/react-core"; + +export const SwitchRemoveLabelOffInput = () => ; diff --git a/packages/eslint-plugin-pf-codemods/src/rules/v6/switchRemoveLabelOff/switchRemoveLabelOffOutput.tsx b/packages/eslint-plugin-pf-codemods/src/rules/v6/switchRemoveLabelOff/switchRemoveLabelOffOutput.tsx new file mode 100644 index 000000000..308a33427 --- /dev/null +++ b/packages/eslint-plugin-pf-codemods/src/rules/v6/switchRemoveLabelOff/switchRemoveLabelOffOutput.tsx @@ -0,0 +1,3 @@ +import { Switch } from "@patternfly/react-core"; + +export const SwitchRemoveLabelOffInput = () => \ No newline at end of file