From f4f1cfb7ad93bbe8c3222c8df93d9da1769c7da8 Mon Sep 17 00:00:00 2001 From: adamviktora Date: Tue, 12 Mar 2024 16:40:41 +0100 Subject: [PATCH] feat(FormFiledGroupHeaderTitleTextObject): interface renamed (typo) --- ...FiledGroupHeaderTitleTextObject-renamed.md | 18 ++++++ ...GroupHeaderTitleTextObject-renamed.test.ts | 43 ++++++++++++++ ...FiledGroupHeaderTitleTextObject-renamed.ts | 59 +++++++++++++++++++ ...GroupHeaderTitleTextObjectRenamedInput.tsx | 8 +++ ...roupHeaderTitleTextObjectRenamedOutput.tsx | 8 +++ 5 files changed, 136 insertions(+) create mode 100644 packages/eslint-plugin-pf-codemods/src/rules/v6/formFiledGroupHeaderTitleTextObjectRenamed/formFiledGroupHeaderTitleTextObject-renamed.md create mode 100644 packages/eslint-plugin-pf-codemods/src/rules/v6/formFiledGroupHeaderTitleTextObjectRenamed/formFiledGroupHeaderTitleTextObject-renamed.test.ts create mode 100644 packages/eslint-plugin-pf-codemods/src/rules/v6/formFiledGroupHeaderTitleTextObjectRenamed/formFiledGroupHeaderTitleTextObject-renamed.ts create mode 100644 packages/eslint-plugin-pf-codemods/src/rules/v6/formFiledGroupHeaderTitleTextObjectRenamed/formFiledGroupHeaderTitleTextObjectRenamedInput.tsx create mode 100644 packages/eslint-plugin-pf-codemods/src/rules/v6/formFiledGroupHeaderTitleTextObjectRenamed/formFiledGroupHeaderTitleTextObjectRenamedOutput.tsx diff --git a/packages/eslint-plugin-pf-codemods/src/rules/v6/formFiledGroupHeaderTitleTextObjectRenamed/formFiledGroupHeaderTitleTextObject-renamed.md b/packages/eslint-plugin-pf-codemods/src/rules/v6/formFiledGroupHeaderTitleTextObjectRenamed/formFiledGroupHeaderTitleTextObject-renamed.md new file mode 100644 index 000000000..2c4194673 --- /dev/null +++ b/packages/eslint-plugin-pf-codemods/src/rules/v6/formFiledGroupHeaderTitleTextObjectRenamed/formFiledGroupHeaderTitleTextObject-renamed.md @@ -0,0 +1,18 @@ +### formFiledGroupHeaderTitleTextObject-renamed [(#10016)](https://github.com/patternfly/patternfly-react/pull/10016) + +There was a typo in FormFiledGroupHeaderTitleTextObject interface. It was renamed to the intended FormFieldGroupHeaderTitleTextObject. + +#### Examples + +In: + +```jsx +%inputExample% +``` + +Out: + +```jsx +%outputExample% +``` + diff --git a/packages/eslint-plugin-pf-codemods/src/rules/v6/formFiledGroupHeaderTitleTextObjectRenamed/formFiledGroupHeaderTitleTextObject-renamed.test.ts b/packages/eslint-plugin-pf-codemods/src/rules/v6/formFiledGroupHeaderTitleTextObjectRenamed/formFiledGroupHeaderTitleTextObject-renamed.test.ts new file mode 100644 index 000000000..289785d1c --- /dev/null +++ b/packages/eslint-plugin-pf-codemods/src/rules/v6/formFiledGroupHeaderTitleTextObjectRenamed/formFiledGroupHeaderTitleTextObject-renamed.test.ts @@ -0,0 +1,43 @@ +const ruleTester = require("../../ruletester"); +import * as rule from "./formFiledGroupHeaderTitleTextObject-renamed"; + +const errorMessage = + "There was a typo in FormFiledGroupHeaderTitleTextObject interface. It was renamed to the intended FormFieldGroupHeaderTitleTextObject."; + +ruleTester.run("formFiledGroupHeaderTitleTextObject-renamed", rule, { + valid: [ + { + code: `const titleTextObject: FormFiledGroupHeaderTitleTextObject = {text: "Some title", id: "form-field-group-header-title-text"};`, + }, + ], + invalid: [ + { + code: `import { FormFiledGroupHeaderTitleTextObject } from '@patternfly/react-core'; const titleTextObject: FormFiledGroupHeaderTitleTextObject = {text: "Some title", id: "form-field-group-header-title-text"};`, + output: `import { FormFieldGroupHeaderTitleTextObject } from '@patternfly/react-core'; const titleTextObject: FormFieldGroupHeaderTitleTextObject = {text: "Some title", id: "form-field-group-header-title-text"};`, + errors: [ + { + message: errorMessage, + type: "ImportSpecifier", + }, + { + message: errorMessage, + type: "TSTypeReference", + }, + ], + }, + { + code: `import { FormFiledGroupHeaderTitleTextObject } from '@patternfly/react-core'; interface MyExtension extends FormFiledGroupHeaderTitleTextObject {}`, + output: `import { FormFieldGroupHeaderTitleTextObject } from '@patternfly/react-core'; interface MyExtension extends FormFieldGroupHeaderTitleTextObject {}`, + errors: [ + { + message: errorMessage, + type: "ImportSpecifier", + }, + { + message: errorMessage, + type: "TSInterfaceHeritage", + }, + ], + }, + ], +}); diff --git a/packages/eslint-plugin-pf-codemods/src/rules/v6/formFiledGroupHeaderTitleTextObjectRenamed/formFiledGroupHeaderTitleTextObject-renamed.ts b/packages/eslint-plugin-pf-codemods/src/rules/v6/formFiledGroupHeaderTitleTextObjectRenamed/formFiledGroupHeaderTitleTextObject-renamed.ts new file mode 100644 index 000000000..9ac095368 --- /dev/null +++ b/packages/eslint-plugin-pf-codemods/src/rules/v6/formFiledGroupHeaderTitleTextObjectRenamed/formFiledGroupHeaderTitleTextObject-renamed.ts @@ -0,0 +1,59 @@ +import { AST, Rule } from "eslint"; +import { ImportSpecifier, Identifier, Node } from "estree-jsx"; +import { getFromPackage } from "../../helpers"; + +// https://github.com/patternfly/patternfly-react/pull/10016 +module.exports = { + meta: { fixable: "code" }, + create: function (context: Rule.RuleContext) { + const { imports } = getFromPackage(context, "@patternfly/react-core"); + + const previousName = "FormFiledGroupHeaderTitleTextObject"; + const newName = "FormFieldGroupHeaderTitleTextObject"; + + const interfaceImport = imports.find( + (specifier) => specifier.imported.name === previousName + ); + + if (!interfaceImport) { + 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 (node.imported.name === interfaceImport.imported.name) { + callContextReport(node, node.imported); + } + }, + TSTypeReference(node: { typeName: Identifier }) { + if (!hasAlias(interfaceImport) && node.typeName.name === previousName) { + callContextReport(node as unknown as Node, node.typeName); + } + }, + TSInterfaceHeritage(node: { expression: Identifier }) { + if ( + !hasAlias(interfaceImport) && + node.expression.name === previousName + ) { + callContextReport(node as unknown as Node, node.expression); + } + }, + }; + }, +}; diff --git a/packages/eslint-plugin-pf-codemods/src/rules/v6/formFiledGroupHeaderTitleTextObjectRenamed/formFiledGroupHeaderTitleTextObjectRenamedInput.tsx b/packages/eslint-plugin-pf-codemods/src/rules/v6/formFiledGroupHeaderTitleTextObjectRenamed/formFiledGroupHeaderTitleTextObjectRenamedInput.tsx new file mode 100644 index 000000000..a675d5417 --- /dev/null +++ b/packages/eslint-plugin-pf-codemods/src/rules/v6/formFiledGroupHeaderTitleTextObjectRenamed/formFiledGroupHeaderTitleTextObjectRenamedInput.tsx @@ -0,0 +1,8 @@ +import { FormFiledGroupHeaderTitleTextObject } from "@patternfly/react-core"; + +interface MyExtension extends FormFiledGroupHeaderTitleTextObject {} + +const titleTextObject: FormFiledGroupHeaderTitleTextObject = { + text: "Some title", + id: "form-field-group-header-title-text", +}; diff --git a/packages/eslint-plugin-pf-codemods/src/rules/v6/formFiledGroupHeaderTitleTextObjectRenamed/formFiledGroupHeaderTitleTextObjectRenamedOutput.tsx b/packages/eslint-plugin-pf-codemods/src/rules/v6/formFiledGroupHeaderTitleTextObjectRenamed/formFiledGroupHeaderTitleTextObjectRenamedOutput.tsx new file mode 100644 index 000000000..7d21b15fa --- /dev/null +++ b/packages/eslint-plugin-pf-codemods/src/rules/v6/formFiledGroupHeaderTitleTextObjectRenamed/formFiledGroupHeaderTitleTextObjectRenamedOutput.tsx @@ -0,0 +1,8 @@ +import { FormFieldGroupHeaderTitleTextObject } from "@patternfly/react-core"; + +interface MyExtension extends FormFieldGroupHeaderTitleTextObject {} + +const titleTextObject: FormFieldGroupHeaderTitleTextObject = { + text: "Some title", + id: "form-field-group-header-title-text", +};