Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function getFromPackage(
specifierNames.includes(specifier.imported.name)
);
const specifiedExports = exports.filter((specifier) =>
specifierNames.includes(specifier.exported.name)
specifierNames.includes(specifier.local.name)
);

return { imports: specifiedImports, exports: specifiedExports };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
import { Rule } from "eslint";
import { TSESTree } from "@typescript-eslint/utils";
import { Identifier, ImportDeclaration, ImportSpecifier } from "estree-jsx";
import {
ExportSpecifier,
Identifier,
ImportDeclaration,
ImportSpecifier,
} from "estree-jsx";
import {
checkMatchingImportSpecifier,
getFromPackage,
pfPackageMatches,
} from ".";

interface Renames {
interface InterfaceRenames {
[currentName: string]:
| string
| {
newName: string;
message?: string;
};
}

interface ComponentRenames {
[currentName: string]: string;
}

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

export function renameInterface(
interfaceRenames: Renames,
componentRenames: Renames,
interfaceRenames: InterfaceRenames,
componentRenames: ComponentRenames = {},
packageName = "@patternfly/react-core"
) {
return function (context: Rule.RuleContext) {
const oldNames = Object.keys(interfaceRenames);
const { imports } = getFromPackage(context, packageName, oldNames);
const { imports, exports } = getFromPackage(context, packageName, oldNames);

if (imports.length === 0) {
if (!imports.length && !exports.length) {
return {};
}

const shouldRenameIdentifier = (identifier: Identifier) => {
const matchingImport = imports.find(
(specifier) => specifier.local.name === identifier.name
(specifier) =>
specifier.imported.name === identifier.name &&
specifier.imported.name === specifier.local.name
);

if (!matchingImport) {
return false;
return !!matchingImport;
};

const getRenameInfo = (oldName: string) => {
const newName = interfaceRenames[oldName];

if (typeof newName === "string") {
return { newName };
}

return matchingImport.local.name === matchingImport.imported.name;
return newName;
};

const replaceIdentifier = (identifier: Identifier) => {
const oldName = identifier.name;
const newName = interfaceRenames[oldName];
const { newName, message } = getRenameInfo(oldName);

context.report({
node: identifier,
message: formatDefaultMessage(oldName, newName),
message: message ?? formatDefaultMessage(oldName, newName),
fix(fixer) {
return fixer.replaceText(identifier, newName);
},
Expand Down Expand Up @@ -79,16 +101,17 @@ export function renameInterface(
return;
}

const oldName = node.imported.name;
const newName = interfaceRenames[oldName];

context.report({
node,
message: formatDefaultMessage(oldName, newName),
fix(fixer) {
return fixer.replaceText(node.imported, newName);
},
});
replaceIdentifier(node.imported);
},
ExportSpecifier(node: ExportSpecifier) {
if (
exports.some(
(specifier) => specifier.local.name === node.local.name
) ||
shouldRenameIdentifier(node.local)
) {
replaceIdentifier(node.local);
}
},
TSTypeReference(node: TSESTree.TSTypeReference) {
if (node.typeName.type === "Identifier") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ ruleTester.run(
errors: [
{
message,
type: "ImportSpecifier",
type: "Identifier",
},
{
message,
Expand All @@ -60,7 +60,7 @@ ruleTester.run(
errors: [
{
message,
type: "ImportSpecifier",
type: "Identifier",
},
],
},
Expand All @@ -75,7 +75,7 @@ ruleTester.run(
},
{
message,
type: "ImportSpecifier",
type: "Identifier",
},
],
},
Expand All @@ -89,7 +89,7 @@ ruleTester.run(
},
{
message,
type: "ImportSpecifier",
type: "Identifier",
},
],
},
Expand All @@ -103,7 +103,7 @@ ruleTester.run(
},
{
message,
type: "ImportSpecifier",
type: "Identifier",
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ ruleTester.run("formFiledGroupHeaderTitleTextObject-renamed", rule, {
errors: [
{
message: errorMessage,
type: "ImportSpecifier",
type: "Identifier",
},
{
message: errorMessage,
type: "TSTypeReference",
type: "Identifier",
},
],
},
Expand All @@ -34,11 +34,11 @@ ruleTester.run("formFiledGroupHeaderTitleTextObject-renamed", rule, {
errors: [
{
message: errorMessage,
type: "ImportSpecifier",
type: "Identifier",
},
{
message: errorMessage,
type: "TSInterfaceHeritage",
type: "Identifier",
},
],
},
Expand All @@ -48,11 +48,11 @@ ruleTester.run("formFiledGroupHeaderTitleTextObject-renamed", rule, {
errors: [
{
message: errorMessage,
type: "ImportSpecifier",
type: "Identifier",
},
{
message: errorMessage,
type: "ExportSpecifier",
type: "Identifier",
},
],
},
Expand All @@ -62,7 +62,7 @@ ruleTester.run("formFiledGroupHeaderTitleTextObject-renamed", rule, {
errors: [
{
message: errorMessage,
type: "ExportSpecifier",
type: "Identifier",
},
],
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,87 +1,13 @@
import { AST, Rule } from "eslint";
import { ImportSpecifier, ExportSpecifier, Identifier, Node } from "estree-jsx";
import { getFromPackage } from "../../helpers";
import { renameInterface } from "../../helpers";

// https://github.com/patternfly/patternfly-react/pull/10016
module.exports = {
meta: { fixable: "code" },
create: function (context: Rule.RuleContext) {
const { imports, exports } = getFromPackage(
context,
"@patternfly/react-core"
);

const previousName = "FormFiledGroupHeaderTitleTextObject";
const newName = "FormFieldGroupHeaderTitleTextObject";

const interfaceImport = imports.find(
(specifier) => specifier.imported.name === previousName
);

const interfaceExport = exports.find(
(specifier) => specifier.local.name === previousName
);

if (!interfaceImport && !interfaceExport) {
return {};
}

const hasAlias = (specifier: ImportSpecifier) =>
specifier.local.name !== specifier.imported.name;

const reportMessage =
"There was a typo in FormFiledGroupHeaderTitleTextObject interface. It was renamed to the intended FormFieldGroupHeaderTitleTextObject.";

const callContextReport = (node: Node, nodeToReplace: Node | AST.Token) => {
context.report({
node,
message: reportMessage,
fix(fixer) {
return fixer.replaceText(nodeToReplace, newName);
},
});
};

return {
ImportSpecifier(node: ImportSpecifier) {
if (
interfaceImport &&
node.imported.name === interfaceImport.imported.name
) {
callContextReport(node, node.imported);
}
},
ExportSpecifier(node: ExportSpecifier) {
if (interfaceExport && node.local.name === interfaceExport.local.name) {
callContextReport(node, node.local);
}

if (
interfaceImport &&
!hasAlias(interfaceImport) &&
node.local.name === previousName
) {
callContextReport(node, node.local);
}
},
TSTypeReference(node: { typeName: Identifier }) {
if (
interfaceImport &&
!hasAlias(interfaceImport) &&
node.typeName.name === previousName
) {
callContextReport(node as unknown as Node, node.typeName);
}
},
TSInterfaceHeritage(node: { expression: Identifier }) {
if (
interfaceImport &&
!hasAlias(interfaceImport) &&
node.expression.name === previousName
) {
callContextReport(node as unknown as Node, node.expression);
}
},
};
},
create: renameInterface({
FormFiledGroupHeaderTitleTextObject: {
newName: "FormFieldGroupHeaderTitleTextObject",
message:
"There was a typo in FormFiledGroupHeaderTitleTextObject interface. It was renamed to the intended FormFieldGroupHeaderTitleTextObject.",
},
}),
};
Loading