diff --git a/generators/src/helpers.js b/generators/src/helpers.ts similarity index 66% rename from generators/src/helpers.js rename to generators/src/helpers.ts index 779cef106..a8636ee9e 100644 --- a/generators/src/helpers.js +++ b/generators/src/helpers.ts @@ -1,4 +1,4 @@ -function betterStringSort(a, b) { +function betterStringSort(a: string, b: string) { return a.toLowerCase().localeCompare(b.toLowerCase()); } diff --git a/generators/src/write-rule.ts b/generators/src/write-rule.ts index 08e04f322..148d0021a 100644 --- a/generators/src/write-rule.ts +++ b/generators/src/write-rule.ts @@ -29,30 +29,27 @@ export async function genericRule({ // the formatting for content here looks weird, but that's to preserve indentation in the written file const content = `import { Rule } from "eslint"; import { JSXOpeningElement } from "estree-jsx"; -import { getFromPackage, checkMatchingJSXOpeningElement, getAttribute } from "../../helpers"; +import { getAllImportsFromPackage, checkMatchingJSXOpeningElement, getAttribute } from "../../helpers"; // https://github.com/patternfly/${referenceRepo}/pull/${referencePR} module.exports = { meta: { fixable: "code" }, create: function (context: Rule.RuleContext) { - const { imports } = getFromPackage(context, "@patternfly/react-core"); - - const componentImports = imports.filter( - (specifier) => specifier.imported.name === "${componentName}" - ); + const basePackage = "@patternfly/react-core"; + const componentImports = getAllImportsFromPackage(context, basePackage, ["${componentName}"]); return !componentImports.length ? {} : { JSXOpeningElement(node: JSXOpeningElement) { if (checkMatchingJSXOpeningElement(node, componentImports)) { - const attribute = getAttribute(node, "${propName}"); - if (attribute) { + const ${propName}Prop = getAttribute(node, "${propName}"); + if (${propName}Prop) { context.report({ node, message: "${message}", fix(fixer) { - return fixer.replaceText(attribute, ""); + return fixer.replaceText(${propName}Prop, ""); }, }); } diff --git a/packages/eslint-plugin-pf-codemods/src/rules/helpers/nodeMatches.ts b/packages/eslint-plugin-pf-codemods/src/rules/helpers/nodeMatches.ts index 874ef0b6b..1d3657947 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/helpers/nodeMatches.ts +++ b/packages/eslint-plugin-pf-codemods/src/rules/helpers/nodeMatches.ts @@ -1,8 +1,15 @@ -import { JSXOpeningElement, ImportSpecifier } from "estree-jsx"; +import { + JSXOpeningElement, + ImportSpecifier, + ImportDefaultSpecifier, +} from "estree-jsx"; export function checkMatchingJSXOpeningElement( node: JSXOpeningElement, - imports: ImportSpecifier | ImportSpecifier[] + imports: + | ImportSpecifier + | ImportDefaultSpecifier + | (ImportSpecifier | ImportDefaultSpecifier)[] ) { if (Array.isArray(imports)) { return (