-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(devex): Add types for getFromPackage and pfPackageMatcher helpe…
…rs (#577) * chore(devex): Add types for getFromPackage and pfPackageMatcher helpers * chore(helpers): add helper imports to helper.js file * chore(helpers): Refactor to use .getSourceCode() instead of .sourceCode
- Loading branch information
1 parent
3d190be
commit cda443e
Showing
7 changed files
with
113 additions
and
55 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
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
72 changes: 72 additions & 0 deletions
72
packages/eslint-plugin-pf-codemods/src/rules/helpers/getFromPackage.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,72 @@ | ||
import { Rule } from "eslint"; | ||
import { | ||
ImportDeclaration, | ||
ModuleDeclaration, | ||
Statement, | ||
Directive, | ||
ExportNamedDeclaration, | ||
ImportSpecifier, | ||
ExportSpecifier, | ||
} from "estree-jsx"; | ||
import { pfPackageMatches } from "./pfPackageMatches"; | ||
|
||
type Declarations = ImportDeclaration | ExportNamedDeclaration; | ||
type Specifiers = ImportSpecifier | ExportSpecifier; | ||
|
||
function filterByPackageName(nodes: Declarations[], packageName: string) { | ||
return nodes.filter((node) => { | ||
const nodeValue = node?.source?.value; | ||
|
||
if (typeof nodeValue !== "string") { | ||
return false; | ||
} | ||
if (packageName.startsWith("@patternfly")) { | ||
return pfPackageMatches(packageName, nodeValue); | ||
} | ||
return nodeValue === packageName; | ||
}); | ||
} | ||
|
||
function getSpecifiers< | ||
NodeType extends Declarations, | ||
SpecifierType extends Specifiers | ||
>( | ||
astBody: (ModuleDeclaration | Statement | Directive)[], | ||
nodeType: "ImportDeclaration" | "ExportNamedDeclaration", | ||
packageName: string | ||
): SpecifierType[] { | ||
const nodesOfSpecifiedType = astBody.filter( | ||
(node) => node?.type === nodeType | ||
) as NodeType[]; | ||
const pfNodes = filterByPackageName(nodesOfSpecifiedType, packageName); | ||
const specifiers = pfNodes.map((node) => node.specifiers); | ||
|
||
const specifierType = | ||
nodeType === "ImportDeclaration" ? "ImportSpecifier" : "ExportSpecifier"; | ||
|
||
const filteredSpecifiersByType = specifiers.filter((specifierArray) => | ||
specifierArray.every((specifier) => specifier.type === specifierType) | ||
) as unknown as SpecifierType[]; | ||
|
||
return filteredSpecifiersByType.reduce( | ||
(acc, val) => acc.concat(val), | ||
[] as SpecifierType[] | ||
); | ||
} | ||
|
||
export function getFromPackage(context: Rule.RuleContext, packageName: string) { | ||
const astBody = context.getSourceCode().ast.body; | ||
|
||
const imports = getSpecifiers<ImportDeclaration, ImportSpecifier>( | ||
astBody, | ||
"ImportDeclaration", | ||
packageName | ||
); | ||
const exports = getSpecifiers<ExportNamedDeclaration, ExportSpecifier>( | ||
astBody, | ||
"ExportNamedDeclaration", | ||
packageName | ||
); | ||
|
||
return { imports, exports }; | ||
} |
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
3 changes: 3 additions & 0 deletions
3
packages/eslint-plugin-pf-codemods/src/rules/helpers/index.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,3 @@ | ||
export * from './helpers'; | ||
export * from './pfPackageMatches'; | ||
export * from './getFromPackage'; |
22 changes: 22 additions & 0 deletions
22
packages/eslint-plugin-pf-codemods/src/rules/helpers/pfPackageMatches.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,22 @@ | ||
import { ImportDeclaration } from "estree-jsx"; | ||
|
||
export function pfPackageMatches( | ||
packageName: string, | ||
nodeSrc: ImportDeclaration["source"]["value"] | ||
) { | ||
if (typeof nodeSrc !== "string") { | ||
return false; | ||
} | ||
|
||
const parts = packageName.split("/"); | ||
const regex = new RegExp( | ||
"^" + | ||
parts[0] + | ||
"/" + | ||
parts[1] + | ||
"(/dist/(esm|js))?" + | ||
(parts[2] ? "/" + parts[2] : "") + | ||
"(/(components|helpers)/.*)?$" | ||
); | ||
return regex.test(nodeSrc); | ||
} |
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