Skip to content

Commit 4c7d9da

Browse files
chore(generators): made enhancements to generator files (#719)
* chore(generators): made enhancements to generator files * Updated generator for default imports
1 parent 578475a commit 4c7d9da

File tree

9 files changed

+126
-114
lines changed

9 files changed

+126
-114
lines changed

generators/src/helpers.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

generators/src/helpers.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function betterStringSort(a: string, b: string) {
2+
return a.toLowerCase().localeCompare(b.toLowerCase());
3+
}
4+
5+
module.exports = { betterStringSort };

generators/src/plop-interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export interface Answers {
22
componentName: string;
33
propName: string;
44
ruleName: string;
5+
referenceRepo: string;
56
referencePR: string;
67
message?: string;
78
}

generators/src/write-readme.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,17 @@ import { outputFile } from "fs-extra";
33
import { camelCase } from "case-anything";
44
import { Answers } from "./plop-interfaces";
55

6-
async function baseReadme({ referencePR, ruleName, message }: Answers) {
6+
export enum RepoNames {
7+
react = "patternfly-react",
8+
componentGroups = "react-component-groups",
9+
}
10+
11+
async function baseReadme({
12+
referenceRepo,
13+
referencePR,
14+
ruleName,
15+
message,
16+
}: Answers) {
717
const camelCaseRuleName = camelCase(ruleName);
818
const readMePath = join(
919
require
@@ -15,7 +25,9 @@ async function baseReadme({ referencePR, ruleName, message }: Answers) {
1525
`${ruleName}.md`
1626
);
1727

18-
const readMeContent = `### ${ruleName} [(#${referencePR})](https://github.com/patternfly/patternfly-react/pull/${referencePR})
28+
const prLinkTextPrefix =
29+
referenceRepo === RepoNames.react ? "" : `${referenceRepo}/`;
30+
const readMeContent = `### ${ruleName} [(${prLinkTextPrefix}#${referencePR})](https://github.com/patternfly/${referenceRepo}/pull/${referencePR})
1931
2032
${message}
2133

generators/src/write-rule.ts

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,44 +22,34 @@ export async function genericRule({
2222
componentName,
2323
propName,
2424
ruleName,
25+
referenceRepo,
2526
referencePR,
2627
message,
2728
}: Answers) {
2829
// the formatting for content here looks weird, but that's to preserve indentation in the written file
2930
const content = `import { Rule } from "eslint";
3031
import { JSXOpeningElement } from "estree-jsx";
31-
import { getFromPackage } from "../../helpers";
32+
import { getAllImportsFromPackage, checkMatchingJSXOpeningElement, getAttribute } from "../../helpers";
3233
33-
// https://github.com/patternfly/patternfly-react/pull/${referencePR}
34+
// https://github.com/patternfly/${referenceRepo}/pull/${referencePR}
3435
module.exports = {
3536
meta: { fixable: "code" },
3637
create: function (context: Rule.RuleContext) {
37-
const { imports } = getFromPackage(context, "@patternfly/react-core");
38-
39-
const componentImports = imports.filter(
40-
(specifier) => specifier.imported.name === "${componentName}"
41-
);
38+
const basePackage = "@patternfly/react-core";
39+
const componentImports = getAllImportsFromPackage(context, basePackage, ["${componentName}"]);
4240
4341
return !componentImports.length
4442
? {}
4543
: {
4644
JSXOpeningElement(node: JSXOpeningElement) {
47-
if (
48-
node.name.type === "JSXIdentifier" &&
49-
componentImports
50-
.map((imp) => imp.local.name)
51-
.includes(node.name.name)
52-
) {
53-
const attribute = node.attributes.find(
54-
(attr) =>
55-
attr.type === "JSXAttribute" && attr.name.name === "${propName}"
56-
);
57-
if (attribute) {
45+
if (checkMatchingJSXOpeningElement(node, componentImports)) {
46+
const ${propName}Prop = getAttribute(node, "${propName}");
47+
if (${propName}Prop) {
5848
context.report({
5949
node,
6050
message: "${message}",
6151
fix(fixer) {
62-
return fixer.replaceText(attribute, "");
52+
return fixer.replaceText(${propName}Prop, "");
6353
},
6454
});
6555
}
@@ -76,11 +66,12 @@ export async function addEventCBRule({
7666
componentName,
7767
propName,
7868
ruleName,
69+
referenceRepo,
7970
referencePR,
8071
}: Answers) {
8172
const content = `const { addCallbackParam } = require("../../helpers");
8273
83-
// https://github.com/patternfly/patternfly-react/pull/${referencePR}
74+
// https://github.com/patternfly/${referenceRepo}/pull/${referencePR}
8475
module.exports = {
8576
meta: { fixable: "code" },
8677
create: addCallbackParam(["${componentName}"], { ${propName}: "_event" }),
@@ -93,11 +84,12 @@ export async function swapCBRule({
9384
componentName,
9485
propName,
9586
ruleName,
87+
referenceRepo,
9688
referencePR,
9789
}: Answers) {
9890
const content = `const { addCallbackParam } = require("../../helpers");
9991
100-
// https://github.com/patternfly/patternfly-react/pull/${referencePR}
92+
// https://github.com/patternfly/${referenceRepo}/pull/${referencePR}
10193
module.exports = {
10294
meta: { fixable: "code" },
10395
create: addCallbackParam(["${componentName}"], { ${propName}: { defaultParamName: "_event", previousParamIndex: 1, otherMatchers: /^_?(ev\\w*|e$)/ } }),

packages/eslint-plugin-pf-codemods/src/rules/helpers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export * from "./includesImport";
1010
export * from "./interfaces";
1111
export * from "./JSXAttributes";
1212
export * from "./JSXElements";
13+
export * from "./nodeMatches";
1314
export * from "./pfPackageMatches";
1415
export * from "./removeElement";
1516
export * from "./removeEmptyLineAfter";
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {
2+
JSXOpeningElement,
3+
ImportSpecifier,
4+
ImportDefaultSpecifier,
5+
} from "estree-jsx";
6+
7+
export function checkMatchingJSXOpeningElement(
8+
node: JSXOpeningElement,
9+
imports:
10+
| ImportSpecifier
11+
| ImportDefaultSpecifier
12+
| (ImportSpecifier | ImportDefaultSpecifier)[]
13+
) {
14+
if (Array.isArray(imports)) {
15+
return (
16+
node.name.type === "JSXIdentifier" &&
17+
imports.map((imp) => imp.local.name).includes(node.name.name)
18+
);
19+
}
20+
21+
return (
22+
node.name.type === "JSXIdentifier" && imports.local.name === node.name.name
23+
);
24+
}

packages/eslint-plugin-pf-codemods/src/rules/v6/accordionItemWarnUpdateMarkup/accordionItem-warn-update-markup.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getFromPackage } from "../../helpers";
1+
import { getFromPackage, checkMatchingJSXOpeningElement } from "../../helpers";
22
import { Rule } from "eslint";
33
import { JSXOpeningElement } from "estree-jsx";
44

@@ -17,10 +17,7 @@ module.exports = {
1717
? {}
1818
: {
1919
JSXOpeningElement(node: JSXOpeningElement) {
20-
if (
21-
node.name.type === "JSXIdentifier" &&
22-
accordionItemImport.local.name === node.name.name
23-
) {
20+
if (checkMatchingJSXOpeningElement(node, accordionItemImport)) {
2421
context.report({
2522
node,
2623
message:

plopFile.cjs

Lines changed: 66 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,49 @@ const {
99
swapCBTest,
1010
} = require("./generators/dist/js/write-test");
1111
const {
12+
RepoNames,
1213
genericReadme,
1314
addEventCBReadme,
14-
swapCBReadme
15+
swapCBReadme,
1516
} = require("./generators/dist/js/write-readme");
1617
const {
1718
genericTestSingle,
1819
addEventCBTestSingle,
1920
swapCBTestSingle,
2021
} = require("./generators/dist/js/write-test-single");
2122

23+
const componentNamePrompt = {
24+
type: "input",
25+
name: "componentName",
26+
message: "What is the name of the component being changed? (PascalCase)",
27+
};
28+
const propNamePrompt = {
29+
type: "input",
30+
name: "propName",
31+
message: "What is the name of the prop being changed? (camelCase)",
32+
};
33+
const ruleNamePrompt = {
34+
type: "input",
35+
name: "ruleName",
36+
message: "What should the name of this rule be? (kebab-case)",
37+
};
38+
const referenceRepoPrompt = {
39+
type: "list",
40+
name: "referenceRepo",
41+
message: "What is the repo the PR was made in",
42+
choices: Object.values(RepoNames),
43+
};
44+
const referencePrPrompt = {
45+
type: "input",
46+
name: "referencePR",
47+
message: "What is the PR reference number",
48+
};
49+
const messagePrompt = {
50+
type: "input",
51+
name: "message",
52+
message: "What message should the codemod send? (Sentence case)",
53+
};
54+
2255
module.exports = function (plop) {
2356
plop.setActionType("generateRule", async function (answers, config, plop) {
2457
console.log("Generating rule file", answers.ruleName);
@@ -62,49 +95,32 @@ module.exports = function (plop) {
6295
}
6396
});
6497

65-
plop.setActionType("generateTestSingle", async function (answers, config, plop) {
66-
console.log("Generating tsx files for", answers.ruleName);
67-
switch (config.generatorSelection) {
68-
case "addEventCB":
69-
await addEventCBTestSingle(answers);
70-
break;
71-
case "swapCB":
72-
await swapCBTestSingle(answers);
73-
break;
74-
default:
75-
await genericTestSingle(answers);
98+
plop.setActionType(
99+
"generateTestSingle",
100+
async function (answers, config, plop) {
101+
console.log("Generating tsx files for", answers.ruleName);
102+
switch (config.generatorSelection) {
103+
case "addEventCB":
104+
await addEventCBTestSingle(answers);
105+
break;
106+
case "swapCB":
107+
await swapCBTestSingle(answers);
108+
break;
109+
default:
110+
await genericTestSingle(answers);
111+
}
76112
}
77-
});
113+
);
78114

79115
plop.setGenerator("generic", {
80116
description: "create a generic new rule",
81117
prompts: [
82-
{
83-
type: "input",
84-
name: "componentName",
85-
message:
86-
"What is the name of the component being changed? (PascalCase)",
87-
},
88-
{
89-
type: "input",
90-
name: "propName",
91-
message: "What is the name of the prop being changed? (camelCase)",
92-
},
93-
{
94-
type: "input",
95-
name: "ruleName",
96-
message: "What should the name of this rule be? (kebab-case)",
97-
},
98-
{
99-
type: "input",
100-
name: "referencePR",
101-
message: "What is the PR reference number",
102-
},
103-
{
104-
type: "input",
105-
name: "message",
106-
message: "What message should the codemod send? (Sentence case)",
107-
},
118+
componentNamePrompt,
119+
propNamePrompt,
120+
ruleNamePrompt,
121+
referenceRepoPrompt,
122+
referencePrPrompt,
123+
messagePrompt,
108124
],
109125
actions: [
110126
{
@@ -125,27 +141,12 @@ module.exports = function (plop) {
125141
plop.setGenerator("add event parameter", {
126142
description: "add an event parameter to the front of a callback",
127143
prompts: [
128-
{
129-
type: "input",
130-
name: "componentName",
131-
message:
132-
"What is the name of the component being changed? (PascalCase)",
133-
},
134-
{
135-
type: "input",
136-
name: "propName",
137-
message: "What is the name of the prop being changed? (camelCase)",
138-
},
139-
{
140-
type: "input",
141-
name: "ruleName",
142-
message: "What should the name of this rule be? (kebab-case)",
143-
},
144-
{
145-
type: "input",
146-
name: "referencePR",
147-
message: "What is the PR reference number",
148-
},
144+
componentNamePrompt,
145+
propNamePrompt,
146+
ruleNamePrompt,
147+
referenceRepoPrompt,
148+
referencePrPrompt,
149+
,
149150
],
150151
actions: [
151152
{
@@ -170,27 +171,11 @@ module.exports = function (plop) {
170171
plop.setGenerator("swap parameter", {
171172
description: "move the position of a parameter in a callback to the front",
172173
prompts: [
173-
{
174-
type: "input",
175-
name: "componentName",
176-
message:
177-
"What is the name of the component being changed? (PascalCase)",
178-
},
179-
{
180-
type: "input",
181-
name: "propName",
182-
message: "What is the name of the prop being changed? (camelCase)",
183-
},
184-
{
185-
type: "input",
186-
name: "ruleName",
187-
message: "What should the name of this rule be? (kebab-case)",
188-
},
189-
{
190-
type: "input",
191-
name: "referencePR",
192-
message: "What is the PR reference number",
193-
},
174+
componentNamePrompt,
175+
propNamePrompt,
176+
ruleNamePrompt,
177+
referenceRepoPrompt,
178+
referencePrPrompt,
194179
],
195180
actions: [
196181
{

0 commit comments

Comments
 (0)