-
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.
refactor(EmptyState): extract helpers, fix(EmptyState): getElementChi…
…ldText fix (#602) * fix(EmptyState): getChildrenText works on all kinds of children - previously the switch would unintentionally handle most cases under JSXText, as the first few whitespaces count as JSXText of children * refactor(EmptyState): extract small helpers * refactor(imports): export helpers and clean imports * refactor(EmptyState): extract getChildrenAsAttributeValueText helper * refactor: getAttribute helper Co-authored-by: Dominik Petřík <77832970+Dominik-Petrik@users.noreply.github.com> --------- Co-authored-by: Dominik Petřík <77832970+Dominik-Petrik@users.noreply.github.com>
- Loading branch information
1 parent
f42bf72
commit 0cbb6b1
Showing
7 changed files
with
260 additions
and
119 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
packages/eslint-plugin-pf-codemods/src/rules/helpers/JSXAttributes.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,17 @@ | ||
import { JSXElement, JSXAttribute } from "estree-jsx"; | ||
|
||
export function getAttribute(node: JSXElement, attributeName: string) { | ||
return node.openingElement.attributes.find( | ||
(attr) => attr.type === "JSXAttribute" && attr.name.name === attributeName | ||
) as JSXAttribute | undefined; | ||
} | ||
|
||
export function getExpression(node?: JSXAttribute["value"]) { | ||
if (!node) { | ||
return; | ||
} | ||
|
||
if (node.type === "JSXExpressionContainer") { | ||
return node.expression; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
packages/eslint-plugin-pf-codemods/src/rules/helpers/JSXElements.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,18 @@ | ||
import { JSXElement } from "estree-jsx"; | ||
|
||
export function getChildElementByName(node: JSXElement, name: string) { | ||
return node.children?.find( | ||
(child) => | ||
child.type === "JSXElement" && | ||
child.openingElement.name.type === "JSXIdentifier" && | ||
child.openingElement.name.name === name | ||
) as JSXElement | undefined; | ||
} | ||
|
||
export function nodeIsComponentNamed(node: JSXElement, componentName: string) { | ||
if (node.openingElement.name.type === "JSXIdentifier") { | ||
return node.openingElement.name.name === componentName; | ||
} | ||
|
||
return false; | ||
} |
74 changes: 74 additions & 0 deletions
74
packages/eslint-plugin-pf-codemods/src/rules/helpers/getText.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,74 @@ | ||
import { Rule } from "eslint"; | ||
|
||
import { | ||
JSXAttribute, | ||
JSXElement, | ||
JSXExpressionContainer, | ||
JSXFragment, | ||
Node, | ||
} from "estree-jsx"; | ||
|
||
export function getAttributeText( | ||
context: Rule.RuleContext, | ||
attribute?: JSXAttribute | ||
) { | ||
if (!attribute) { | ||
return ""; | ||
} | ||
|
||
return context.getSourceCode().getText(attribute); | ||
} | ||
|
||
export function getAttributeValueText( | ||
context: Rule.RuleContext, | ||
attribute?: JSXAttribute | ||
) { | ||
if (!attribute || !attribute.value) { | ||
return ""; | ||
} | ||
|
||
return context.getSourceCode().getText(attribute.value); | ||
} | ||
|
||
export function getNodesText(context: Rule.RuleContext, nodes: Node[]) { | ||
return nodes.map((node) => context.getSourceCode().getText(node)).join(""); | ||
} | ||
|
||
export function getChildrenAsAttributeValueText( | ||
context: Rule.RuleContext, | ||
children: JSXElement["children"] | ||
) { | ||
if (!children.length) { | ||
return `""`; | ||
} | ||
|
||
// is a single text-only child | ||
if (children.length === 1 && children[0].type === "JSXText") { | ||
const childText = children[0].value.trim(); | ||
|
||
if (childText.includes(`"`)) { | ||
return `{<>${childText}</>}`; | ||
} | ||
|
||
return `"${childText}"`; | ||
} | ||
|
||
const nonEmptyChildrenNodes = children.filter( | ||
(child) => !(child.type === "JSXText" && child.value.trim() === "") | ||
); | ||
|
||
if (nonEmptyChildrenNodes.length === 1) { | ||
const singleChild = nonEmptyChildrenNodes[0]; | ||
const singleChildText = context | ||
.getSourceCode() | ||
.getText( | ||
singleChild as JSXExpressionContainer | JSXElement | JSXFragment | ||
); | ||
|
||
return singleChild.type === "JSXExpressionContainer" | ||
? singleChildText | ||
: `{${singleChildText}}`; | ||
} | ||
|
||
return `{<>${getNodesText(context, children as Node[])}</>}`; | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/eslint-plugin-pf-codemods/src/rules/helpers/includesImport.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,10 @@ | ||
import { ImportSpecifier } from "estree-jsx"; | ||
|
||
export function includesImport( | ||
importSpecifiers: ImportSpecifier[], | ||
targetImport: string | ||
) { | ||
return importSpecifiers.some( | ||
(specifier) => specifier.imported.name === targetImport | ||
); | ||
} |
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 |
---|---|---|
@@ -1,4 +1,8 @@ | ||
export * from "./findAncestor"; | ||
export * from "./helpers"; | ||
export * from "./pfPackageMatches"; | ||
export * from "./getFromPackage"; | ||
export * from "./getText"; | ||
export * from "./includesImport"; | ||
export * from "./JSXAttributes"; | ||
export * from "./JSXElements"; | ||
export * from "./pfPackageMatches"; |
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
Oops, something went wrong.