Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add mui datagrid remove error prop codmod #36

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
51 changes: 51 additions & 0 deletions src/v8/mui-data-grid-remove-error-prop.ts
Original file line number Diff line number Diff line change
@@ -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();
}
});
}