From 2d7da66b451b14fa5cbec0fd245309d665b794ab Mon Sep 17 00:00:00 2001 From: Manuel Blum Date: Tue, 26 Nov 2024 15:59:17 +0100 Subject: [PATCH 1/2] chore: add mui datagrid remove error prop codmod --- src/v8/mui-datagrid-remove-error-prop.ts | 51 ++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/v8/mui-datagrid-remove-error-prop.ts diff --git a/src/v8/mui-datagrid-remove-error-prop.ts b/src/v8/mui-datagrid-remove-error-prop.ts new file mode 100644 index 0000000..e48cb8a --- /dev/null +++ b/src/v8/mui-datagrid-remove-error-prop.ts @@ -0,0 +1,51 @@ +import { Project, SyntaxKind } from "ts-morph"; + +export default async function removeMuiDataGridErrorProp() { + console.log("Remove error prop from DataGrid and DataGridPro components"); + + const project = new Project({ tsConfigFilePath: "./admin/tsconfig.json" }); + + const sourceFiles = project.getSourceFiles("admin/src/**/*.tsx"); + + sourceFiles.forEach((sourceFile) => { + const importDeclarations = sourceFile.getImportDeclarations(); + const dataGridImports = importDeclarations.filter((importDeclaration) => { + const moduleSpecifier = importDeclaration.getModuleSpecifier().getLiteralValue(); + return moduleSpecifier === "@mui/x-data-grid" || moduleSpecifier === "@mui/x-data-grid-pro"; + }); + + if (dataGridImports.length > 0) { + const jsxElements = sourceFile.getDescendantsOfKind(SyntaxKind.JsxSelfClosingElement); + + jsxElements.forEach((jsxElement) => { + const tagName = jsxElement.getTagNameNode().getText(); + if (tagName.includes("DataGrid") || tagName.includes("DataGridPro")) { + const errorProp = jsxElement.getAttributes().find((attribute) => { + if (attribute.getKind() === SyntaxKind.JsxAttribute) { + const jsxAttribute = attribute.asKind(SyntaxKind.JsxAttribute); + return jsxAttribute && jsxAttribute.getNameNode().getText() === "error"; + } + }); + + if (errorProp) { + console.log(`✅ Removed error prop on ${tagName} at line: ${errorProp.getStartLineNumber()} in `, sourceFile.getFilePath()); + errorProp.replaceWithText(`/* + @comet/upgrade + TODO: DataGrid's error prop got removed in @mui/x-data-grid(-pro) > v5. + + Recommended usage of errors is using parents ErrorBoundary: https://mui.com/x/migration/migration-data-grid-v5/#removed-props. + + \`\`\` + if (error) { + throw error + } + \`\`\` +*/`); + } + } + }); + + sourceFile.save(); + } + }); +} From 391b55e94b9911a4e4ab0aebe53cdc4a13ec4c7f Mon Sep 17 00:00:00 2001 From: Manuel Blum Date: Tue, 26 Nov 2024 19:35:30 +0100 Subject: [PATCH 2/2] rename due to cspell error --- ...id-remove-error-prop.ts => mui-data-grid-remove-error-prop.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/v8/{mui-datagrid-remove-error-prop.ts => mui-data-grid-remove-error-prop.ts} (100%) diff --git a/src/v8/mui-datagrid-remove-error-prop.ts b/src/v8/mui-data-grid-remove-error-prop.ts similarity index 100% rename from src/v8/mui-datagrid-remove-error-prop.ts rename to src/v8/mui-data-grid-remove-error-prop.ts