-
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(buttonMoveIconsIconProp): make elements self-closing
- also fixes Icon imported with alias - supports Icon / react-icons being used in children attribute
- Loading branch information
1 parent
ff8c798
commit cbb89f7
Showing
7 changed files
with
148 additions
and
30 deletions.
There are no files selected for viewing
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
11 changes: 11 additions & 0 deletions
11
packages/eslint-plugin-pf-codemods/src/rules/helpers/childrenIsEmpty.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,11 @@ | ||
import { JSXElement } from "estree-jsx"; | ||
|
||
/** Checks whether children is empty (no children or only whitespaces) */ | ||
export function childrenIsEmpty(children: JSXElement["children"]) { | ||
return ( | ||
!children.length || | ||
(children.length === 1 && | ||
children[0].type === "JSXText" && | ||
children[0].value.trim() === "") | ||
); | ||
} |
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
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
47 changes: 47 additions & 0 deletions
47
packages/eslint-plugin-pf-codemods/src/rules/helpers/makeJSXElementSelfClosing.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,47 @@ | ||
import { Rule } from "eslint"; | ||
import { JSXElement, JSXText } from "estree-jsx"; | ||
import { childrenIsEmpty } from "."; | ||
|
||
/** Transforms JSXElement to a self-closing tag. | ||
* Works only on elements without children by default, but you can overwrite this behaviour with the removeChildren parameter. | ||
*/ | ||
export function makeJSXElementSelfClosing( | ||
node: JSXElement, | ||
context: Rule.RuleContext, | ||
fixer: Rule.RuleFixer, | ||
removeChildren: boolean = false | ||
): Rule.Fix[] { | ||
if (!node.closingElement) { | ||
return []; | ||
} | ||
|
||
const fixes = []; | ||
|
||
const emptyChildren = childrenIsEmpty(node.children); | ||
|
||
if (removeChildren || emptyChildren) { | ||
const closingSymbol = context | ||
.getSourceCode() | ||
.getLastToken(node.openingElement)!; | ||
|
||
fixes.push( | ||
fixer.replaceText(closingSymbol, " />"), | ||
fixer.remove(node.closingElement) | ||
); | ||
|
||
if (node.children.length) { | ||
if (removeChildren) { | ||
fixes.push( | ||
fixer.removeRange([ | ||
node.children[0].range![0], | ||
node.children[node.children.length - 1].range![1], | ||
]) | ||
); | ||
} else if (emptyChildren) { | ||
fixes.push(fixer.remove(node.children[0] as JSXText)); | ||
} | ||
} | ||
} | ||
|
||
return fixes; | ||
} |
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
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