Skip to content

Commit 4d17ec9

Browse files
authored
fix(formFiledGroupHeaderTitleTextObject): use renameInterface helper (#820)
1 parent 05f3e13 commit 4d17ec9

File tree

5 files changed

+66
-117
lines changed

5 files changed

+66
-117
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export function getFromPackage(
8181
specifierNames.includes(specifier.imported.name)
8282
);
8383
const specifiedExports = exports.filter((specifier) =>
84-
specifierNames.includes(specifier.exported.name)
84+
specifierNames.includes(specifier.local.name)
8585
);
8686

8787
return { imports: specifiedImports, exports: specifiedExports };

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

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
import { Rule } from "eslint";
22
import { TSESTree } from "@typescript-eslint/utils";
3-
import { Identifier, ImportDeclaration, ImportSpecifier } from "estree-jsx";
3+
import {
4+
ExportSpecifier,
5+
Identifier,
6+
ImportDeclaration,
7+
ImportSpecifier,
8+
} from "estree-jsx";
49
import {
510
checkMatchingImportSpecifier,
611
getFromPackage,
712
pfPackageMatches,
813
} from ".";
914

10-
interface Renames {
15+
interface InterfaceRenames {
16+
[currentName: string]:
17+
| string
18+
| {
19+
newName: string;
20+
message?: string;
21+
};
22+
}
23+
24+
interface ComponentRenames {
1125
[currentName: string]: string;
1226
}
1327

@@ -16,37 +30,45 @@ function formatDefaultMessage(oldName: string, newName: string) {
1630
}
1731

1832
export function renameInterface(
19-
interfaceRenames: Renames,
20-
componentRenames: Renames,
33+
interfaceRenames: InterfaceRenames,
34+
componentRenames: ComponentRenames = {},
2135
packageName = "@patternfly/react-core"
2236
) {
2337
return function (context: Rule.RuleContext) {
2438
const oldNames = Object.keys(interfaceRenames);
25-
const { imports } = getFromPackage(context, packageName, oldNames);
39+
const { imports, exports } = getFromPackage(context, packageName, oldNames);
2640

27-
if (imports.length === 0) {
41+
if (!imports.length && !exports.length) {
2842
return {};
2943
}
3044

3145
const shouldRenameIdentifier = (identifier: Identifier) => {
3246
const matchingImport = imports.find(
33-
(specifier) => specifier.local.name === identifier.name
47+
(specifier) =>
48+
specifier.imported.name === identifier.name &&
49+
specifier.imported.name === specifier.local.name
3450
);
3551

36-
if (!matchingImport) {
37-
return false;
52+
return !!matchingImport;
53+
};
54+
55+
const getRenameInfo = (oldName: string) => {
56+
const newName = interfaceRenames[oldName];
57+
58+
if (typeof newName === "string") {
59+
return { newName };
3860
}
3961

40-
return matchingImport.local.name === matchingImport.imported.name;
62+
return newName;
4163
};
4264

4365
const replaceIdentifier = (identifier: Identifier) => {
4466
const oldName = identifier.name;
45-
const newName = interfaceRenames[oldName];
67+
const { newName, message } = getRenameInfo(oldName);
4668

4769
context.report({
4870
node: identifier,
49-
message: formatDefaultMessage(oldName, newName),
71+
message: message ?? formatDefaultMessage(oldName, newName),
5072
fix(fixer) {
5173
return fixer.replaceText(identifier, newName);
5274
},
@@ -79,16 +101,17 @@ export function renameInterface(
79101
return;
80102
}
81103

82-
const oldName = node.imported.name;
83-
const newName = interfaceRenames[oldName];
84-
85-
context.report({
86-
node,
87-
message: formatDefaultMessage(oldName, newName),
88-
fix(fixer) {
89-
return fixer.replaceText(node.imported, newName);
90-
},
91-
});
104+
replaceIdentifier(node.imported);
105+
},
106+
ExportSpecifier(node: ExportSpecifier) {
107+
if (
108+
exports.some(
109+
(specifier) => specifier.local.name === node.local.name
110+
) ||
111+
shouldRenameIdentifier(node.local)
112+
) {
113+
replaceIdentifier(node.local);
114+
}
92115
},
93116
TSTypeReference(node: TSESTree.TSTypeReference) {
94117
if (node.typeName.type === "Identifier") {

packages/eslint-plugin-pf-codemods/src/rules/v6/componentGroupsInvalidObjectPropsRenameToMissingPageProps/component-groups-invalidObjectProps-rename-to-missingPageProps.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ ruleTester.run(
3535
errors: [
3636
{
3737
message,
38-
type: "ImportSpecifier",
38+
type: "Identifier",
3939
},
4040
{
4141
message,
@@ -60,7 +60,7 @@ ruleTester.run(
6060
errors: [
6161
{
6262
message,
63-
type: "ImportSpecifier",
63+
type: "Identifier",
6464
},
6565
],
6666
},
@@ -75,7 +75,7 @@ ruleTester.run(
7575
},
7676
{
7777
message,
78-
type: "ImportSpecifier",
78+
type: "Identifier",
7979
},
8080
],
8181
},
@@ -89,7 +89,7 @@ ruleTester.run(
8989
},
9090
{
9191
message,
92-
type: "ImportSpecifier",
92+
type: "Identifier",
9393
},
9494
],
9595
},
@@ -103,7 +103,7 @@ ruleTester.run(
103103
},
104104
{
105105
message,
106-
type: "ImportSpecifier",
106+
type: "Identifier",
107107
},
108108
],
109109
},

packages/eslint-plugin-pf-codemods/src/rules/v6/formFiledGroupHeaderTitleTextObjectRenamed/formFiledGroupHeaderTitleTextObject-renamed.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ ruleTester.run("formFiledGroupHeaderTitleTextObject-renamed", rule, {
2020
errors: [
2121
{
2222
message: errorMessage,
23-
type: "ImportSpecifier",
23+
type: "Identifier",
2424
},
2525
{
2626
message: errorMessage,
27-
type: "TSTypeReference",
27+
type: "Identifier",
2828
},
2929
],
3030
},
@@ -34,11 +34,11 @@ ruleTester.run("formFiledGroupHeaderTitleTextObject-renamed", rule, {
3434
errors: [
3535
{
3636
message: errorMessage,
37-
type: "ImportSpecifier",
37+
type: "Identifier",
3838
},
3939
{
4040
message: errorMessage,
41-
type: "TSInterfaceHeritage",
41+
type: "Identifier",
4242
},
4343
],
4444
},
@@ -48,11 +48,11 @@ ruleTester.run("formFiledGroupHeaderTitleTextObject-renamed", rule, {
4848
errors: [
4949
{
5050
message: errorMessage,
51-
type: "ImportSpecifier",
51+
type: "Identifier",
5252
},
5353
{
5454
message: errorMessage,
55-
type: "ExportSpecifier",
55+
type: "Identifier",
5656
},
5757
],
5858
},
@@ -62,7 +62,7 @@ ruleTester.run("formFiledGroupHeaderTitleTextObject-renamed", rule, {
6262
errors: [
6363
{
6464
message: errorMessage,
65-
type: "ExportSpecifier",
65+
type: "Identifier",
6666
},
6767
],
6868
},
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,13 @@
1-
import { AST, Rule } from "eslint";
2-
import { ImportSpecifier, ExportSpecifier, Identifier, Node } from "estree-jsx";
3-
import { getFromPackage } from "../../helpers";
1+
import { renameInterface } from "../../helpers";
42

53
// https://github.com/patternfly/patternfly-react/pull/10016
64
module.exports = {
75
meta: { fixable: "code" },
8-
create: function (context: Rule.RuleContext) {
9-
const { imports, exports } = getFromPackage(
10-
context,
11-
"@patternfly/react-core"
12-
);
13-
14-
const previousName = "FormFiledGroupHeaderTitleTextObject";
15-
const newName = "FormFieldGroupHeaderTitleTextObject";
16-
17-
const interfaceImport = imports.find(
18-
(specifier) => specifier.imported.name === previousName
19-
);
20-
21-
const interfaceExport = exports.find(
22-
(specifier) => specifier.local.name === previousName
23-
);
24-
25-
if (!interfaceImport && !interfaceExport) {
26-
return {};
27-
}
28-
29-
const hasAlias = (specifier: ImportSpecifier) =>
30-
specifier.local.name !== specifier.imported.name;
31-
32-
const reportMessage =
33-
"There was a typo in FormFiledGroupHeaderTitleTextObject interface. It was renamed to the intended FormFieldGroupHeaderTitleTextObject.";
34-
35-
const callContextReport = (node: Node, nodeToReplace: Node | AST.Token) => {
36-
context.report({
37-
node,
38-
message: reportMessage,
39-
fix(fixer) {
40-
return fixer.replaceText(nodeToReplace, newName);
41-
},
42-
});
43-
};
44-
45-
return {
46-
ImportSpecifier(node: ImportSpecifier) {
47-
if (
48-
interfaceImport &&
49-
node.imported.name === interfaceImport.imported.name
50-
) {
51-
callContextReport(node, node.imported);
52-
}
53-
},
54-
ExportSpecifier(node: ExportSpecifier) {
55-
if (interfaceExport && node.local.name === interfaceExport.local.name) {
56-
callContextReport(node, node.local);
57-
}
58-
59-
if (
60-
interfaceImport &&
61-
!hasAlias(interfaceImport) &&
62-
node.local.name === previousName
63-
) {
64-
callContextReport(node, node.local);
65-
}
66-
},
67-
TSTypeReference(node: { typeName: Identifier }) {
68-
if (
69-
interfaceImport &&
70-
!hasAlias(interfaceImport) &&
71-
node.typeName.name === previousName
72-
) {
73-
callContextReport(node as unknown as Node, node.typeName);
74-
}
75-
},
76-
TSInterfaceHeritage(node: { expression: Identifier }) {
77-
if (
78-
interfaceImport &&
79-
!hasAlias(interfaceImport) &&
80-
node.expression.name === previousName
81-
) {
82-
callContextReport(node as unknown as Node, node.expression);
83-
}
84-
},
85-
};
86-
},
6+
create: renameInterface({
7+
FormFiledGroupHeaderTitleTextObject: {
8+
newName: "FormFieldGroupHeaderTitleTextObject",
9+
message:
10+
"There was a typo in FormFiledGroupHeaderTitleTextObject interface. It was renamed to the intended FormFieldGroupHeaderTitleTextObject.",
11+
},
12+
}),
8713
};

0 commit comments

Comments
 (0)