Skip to content

Commit

Permalink
Updated generator for default imports
Browse files Browse the repository at this point in the history
  • Loading branch information
thatblindgeye committed Jul 30, 2024
1 parent 5c9d1cf commit 9268de2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion generators/src/helpers.js → generators/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function betterStringSort(a, b) {
function betterStringSort(a: string, b: string) {
return a.toLowerCase().localeCompare(b.toLowerCase());
}

Expand Down
15 changes: 6 additions & 9 deletions generators/src/write-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, "");
},
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down

0 comments on commit 9268de2

Please sign in to comment.