-
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.
feat(Banner): added color and status props (#649)
* feat(Banner): added color and status props * PR feedback
- Loading branch information
1 parent
dc98e0c
commit 67f0dfb
Showing
5 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
...pf-codemods/src/rules/v6/bannerReplaceVariantProp/banner-replace-variantProp.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,19 @@ | ||
### banner-replace-variantProp [(#9891)](https://github.com/patternfly/patternfly-react/pull/9891) | ||
|
||
The `variant` property has been removed from Banner. We recommend using our new `color` or `status` properties, depending on the original intent of the `variant` property. | ||
|
||
Running the fix for this rule will either replace the `variant` property with the `color` property, or remove the `variant` property entirely, but additional updates may need to be made. | ||
|
||
#### Examples | ||
|
||
In: | ||
|
||
```jsx | ||
%inputExample% | ||
``` | ||
|
||
Out: | ||
|
||
```jsx | ||
%outputExample% | ||
``` |
82 changes: 82 additions & 0 deletions
82
...ugin-pf-codemods/src/rules/v6/bannerReplaceVariantProp/banner-replace-variantProp.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,82 @@ | ||
const ruleTester = require("../../ruletester"); | ||
import * as rule from "./banner-replace-variantProp"; | ||
|
||
const createErrorMessage = (hasValueOfDefault?: boolean) => | ||
`The variant property has been removed from Banner. We recommend using our new color or status properties, depending on the original intent of the variant property. Running the fix for this rule will ${ | ||
hasValueOfDefault | ||
? "remove the variant property" | ||
: "replace the variant property with the color property" | ||
}, but additional manual updates may need to be made.`; | ||
|
||
ruleTester.run("banner-replace-variantProp", rule, { | ||
valid: [ | ||
{ | ||
code: `<Banner variant />`, | ||
}, | ||
{ | ||
code: `import { Banner } from '@patternfly/react-core'; <Banner />`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: `import { Banner } from '@patternfly/react-core'; <Banner variant="default" />`, | ||
output: `import { Banner } from '@patternfly/react-core'; <Banner />`, | ||
errors: [ | ||
{ | ||
message: createErrorMessage(true), | ||
type: "JSXOpeningElement", | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `import { Banner } from '@patternfly/react-core'; <Banner variant="red" />`, | ||
output: `import { Banner } from '@patternfly/react-core'; <Banner color="red" />`, | ||
errors: [ | ||
{ | ||
message: createErrorMessage(), | ||
type: "JSXOpeningElement", | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `import { Banner as CustomBanner } from '@patternfly/react-core'; <CustomBanner variant="default" />`, | ||
output: `import { Banner as CustomBanner } from '@patternfly/react-core'; <CustomBanner />`, | ||
errors: [ | ||
{ | ||
message: createErrorMessage(true), | ||
type: "JSXOpeningElement", | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `import { Banner } from '@patternfly/react-core/dist/esm/components/Banner/index.js'; <Banner variant="default" />`, | ||
output: `import { Banner } from '@patternfly/react-core/dist/esm/components/Banner/index.js'; <Banner />`, | ||
errors: [ | ||
{ | ||
message: createErrorMessage(true), | ||
type: "JSXOpeningElement", | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `import { Banner } from '@patternfly/react-core/dist/js/components/Banner/index.js'; <Banner variant="default" />`, | ||
output: `import { Banner } from '@patternfly/react-core/dist/js/components/Banner/index.js'; <Banner />`, | ||
errors: [ | ||
{ | ||
message: createErrorMessage(true), | ||
type: "JSXOpeningElement", | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `import { Banner } from '@patternfly/react-core/dist/dynamic/components/Banner/index.js'; <Banner variant="default" />`, | ||
output: `import { Banner } from '@patternfly/react-core/dist/dynamic/components/Banner/index.js'; <Banner />`, | ||
errors: [ | ||
{ | ||
message: createErrorMessage(true), | ||
type: "JSXOpeningElement", | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
51 changes: 51 additions & 0 deletions
51
...nt-plugin-pf-codemods/src/rules/v6/bannerReplaceVariantProp/banner-replace-variantProp.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,51 @@ | ||
import { Rule } from "eslint"; | ||
import { JSXOpeningElement } from "estree-jsx"; | ||
import { getFromPackage, getAttribute, getAttributeValue } from "../../helpers"; | ||
|
||
// https://github.com/patternfly/patternfly-react/pull/9891 | ||
module.exports = { | ||
meta: { fixable: "code" }, | ||
create: function (context: Rule.RuleContext) { | ||
const { imports } = getFromPackage(context, "@patternfly/react-core"); | ||
|
||
const bannerImport = imports.find( | ||
(specifier) => specifier.imported.name === "Banner" | ||
); | ||
|
||
return !bannerImport | ||
? {} | ||
: { | ||
JSXOpeningElement(node: JSXOpeningElement) { | ||
if ( | ||
node.name.type === "JSXIdentifier" && | ||
bannerImport.local.name === node.name.name | ||
) { | ||
const attribute = getAttribute(node, "variant"); | ||
if (!attribute) { | ||
return; | ||
} | ||
const attributeValue = getAttributeValue( | ||
context, | ||
attribute.value | ||
); | ||
const isValueDefault = attributeValue === "default"; | ||
const fixMessage = isValueDefault | ||
? "remove the variant property" | ||
: "replace the variant property with the color property"; | ||
|
||
context.report({ | ||
node, | ||
message: `The variant property has been removed from Banner. We recommend using our new color or status properties, depending on the original intent of the variant property. Running the fix for this rule will ${fixMessage}, but additional manual updates may need to be made.`, | ||
fix(fixer) { | ||
if (isValueDefault) { | ||
return fixer.replaceText(attribute, ""); | ||
} | ||
|
||
return fixer.replaceText(attribute.name, "color"); | ||
}, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
8 changes: 8 additions & 0 deletions
8
...lugin-pf-codemods/src/rules/v6/bannerReplaceVariantProp/bannerReplaceVariantPropInput.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,8 @@ | ||
import { Banner } from "@patternfly/react-core"; | ||
|
||
export const BannerReplaceVariantPropInput = () => ( | ||
<> | ||
<Banner variant='default' /> | ||
<Banner variant='red' /> | ||
</> | ||
); |
8 changes: 8 additions & 0 deletions
8
...ugin-pf-codemods/src/rules/v6/bannerReplaceVariantProp/bannerReplaceVariantPropOutput.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,8 @@ | ||
import { Banner } from "@patternfly/react-core"; | ||
|
||
export const BannerReplaceVariantPropInput = () => ( | ||
<> | ||
<Banner /> | ||
<Banner color='red' /> | ||
</> | ||
); |