-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(Switch): removed labelOff prop for a11y (#686)
- Loading branch information
1 parent
eff02ee
commit cd27b84
Showing
5 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
...-plugin-pf-codemods/src/rules/v6/switchRemoveLabelOff/switch-remove-labelOff.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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% | ||
``` |
55 changes: 55 additions & 0 deletions
55
...slint-plugin-pf-codemods/src/rules/v6/switchRemoveLabelOff/switch-remove-labelOff.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
const ruleTester = require("../../ruletester"); | ||
import * as rule from "./switch-remove-labelOff"; | ||
|
||
ruleTester.run("switch-remove-labelOff", rule, { | ||
valid: [ | ||
{ | ||
code: `<Switch labelOff />`, | ||
}, | ||
{ | ||
code: `import { Switch } from '@patternfly/react-core'; <Switch someOtherProp />`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: `import { Switch } from '@patternfly/react-core'; <Switch labelOff="Some label" />`, | ||
output: `import { Switch } from '@patternfly/react-core'; <Switch />`, | ||
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'; <Switch labelOff="Some label" />`, | ||
output: `import { Switch } from '@patternfly/react-core/dist/esm/components/Switch/index.js'; <Switch />`, | ||
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'; <Switch labelOff="Some label" />`, | ||
output: `import { Switch } from '@patternfly/react-core/dist/js/components/Switch/index.js'; <Switch />`, | ||
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'; <Switch labelOff="Some label" />`, | ||
output: `import { Switch } from '@patternfly/react-core/dist/dynamic/components/Switch/index.js'; <Switch />`, | ||
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", | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
38 changes: 38 additions & 0 deletions
38
...ges/eslint-plugin-pf-codemods/src/rules/v6/switchRemoveLabelOff/switch-remove-labelOff.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, ""); | ||
}, | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
3 changes: 3 additions & 0 deletions
3
...eslint-plugin-pf-codemods/src/rules/v6/switchRemoveLabelOff/switchRemoveLabelOffInput.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { Switch } from "@patternfly/react-core"; | ||
|
||
export const SwitchRemoveLabelOffInput = () => <Switch labelOff='Some label' />; |
3 changes: 3 additions & 0 deletions
3
...slint-plugin-pf-codemods/src/rules/v6/switchRemoveLabelOff/switchRemoveLabelOffOutput.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { Switch } from "@patternfly/react-core"; | ||
|
||
export const SwitchRemoveLabelOffInput = () => <Switch /> |