-
Notifications
You must be signed in to change notification settings - Fork 20
TabTitleText #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
TabTitleText #44
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1194c4d
add tabtitletext
jschuler 5426174
merge
jschuler e537d64
tab title
jschuler 1185136
add back in commented out rule
jschuler c112c7b
undo test fix to Title
jschuler 5076f77
rule test
jschuler 20ce30d
merge
jschuler a6cd784
move addImport to helpers
redallen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
56 changes: 56 additions & 0 deletions
56
packages/eslint-plugin-pf-codemods/lib/rules/tab-title-text.js
This file contains hidden or 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,56 @@ | ||
| const { getPackageImports, addImport } = require('../helpers'); | ||
|
|
||
| // https://github.com/patternfly/patternfly-react/pull/4146 | ||
| module.exports = { | ||
| create: function(context) { | ||
| const tabImport = getPackageImports(context, '@patternfly/react-core') | ||
| .filter(specifier => specifier.imported.name === 'Tab'); | ||
|
|
||
| return tabImport.length ? { | ||
| JSXOpeningElement(node) { | ||
| if (tabImport.map(imp => imp.local.name).includes(node.name.name)) { | ||
| const attribute = node.attributes.find(node => node.name.name === 'title'); | ||
| if (attribute) { | ||
| const { value } = attribute; | ||
| let replacement; | ||
| if (value.type === 'Literal') { | ||
| // i.e. title="Title" | ||
| replacement = fixer => [ | ||
| fixer.replaceText(attribute.value, `{<TabTitleText>${attribute.value.value}</TabTitleText>}`) | ||
| ]; | ||
| } else if (value.type === 'JSXExpressionContainer') { | ||
| if (value.expression.type === 'Literal') { | ||
| // i.e. title={'Title'} or title={100} | ||
| replacement = fixer => [ | ||
| fixer.replaceText(attribute.value, `{<TabTitleText>${attribute.value.expression.value}</TabTitleText>}`) | ||
| ]; | ||
| } else if (value.expression.type === 'Identifier') { | ||
| // i.e. title={myVariable} | ||
| replacement = fixer => [ | ||
| fixer.replaceText(attribute.value, `{<TabTitleText>{${attribute.value.expression.name}}</TabTitleText>}`) | ||
| ]; | ||
| } else if (value.expression.type === 'JSXElement' && value.expression.openingElement.name.name !== 'TabTitleText') { | ||
| // i.e. title={<Mycomp2 />} | ||
| replacement = fixer => [ | ||
| fixer.insertTextBefore(attribute.value.expression, `<TabTitleText>`), | ||
| fixer.insertTextAfter(attribute.value.expression, `</TabTitleText>`) | ||
| ]; | ||
| } | ||
| } | ||
| if (replacement) { | ||
| context.report({ | ||
| node, | ||
| message: `title needs to be wrapped with the TabTitleText and/or TabTitleIcon component`, | ||
| fix(fixer) { | ||
| return replacement(fixer).concat( | ||
| addImport(context, fixer, '@patternfly/react-core', 'Tab', 'TabTitleText') | ||
| ); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } : {}; | ||
| } | ||
| }; | ||
24 changes: 24 additions & 0 deletions
24
packages/eslint-plugin-pf-codemods/test/rules/tab-title-text.js
This file contains hidden or 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,24 @@ | ||
| const ruleTester = require('./ruletester'); | ||
| const rule = require('../../lib/rules/tab-title-text'); | ||
|
|
||
| ruleTester.run("tab-title-text", rule, { | ||
| valid: [ | ||
| { | ||
| code: `import { Tab, TabTitleText } from '@patternfly/react-core'; <Tab title={<TabTitleText>Title</TabTitleText>}>Content</Tab>`, | ||
| }, | ||
| { | ||
| // No @patternfly/react-core import | ||
| code: `<Tab title="Non-PF component">Content</Tab>`, | ||
| } | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: `import { Tab } from '@patternfly/react-core'; <Tab title="Title">Content</Tab>`, | ||
| output: `import { Tab, TabTitleText } from '@patternfly/react-core'; <Tab title={<TabTitleText>Title</TabTitleText>}>Content</Tab>`, | ||
| errors: [{ | ||
| message: `title needs to be wrapped with the TabTitleText and/or TabTitleIcon component`, | ||
| type: "JSXOpeningElement", | ||
| }] | ||
| }, | ||
| ] | ||
| }); |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we not target
ImportDeclarations here? I'm going to check this out locally, but I think that'd be pretty nice...