Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
robphoenix committed Aug 16, 2024
1 parent 93fad4d commit 99097b1
Showing 1 changed file with 29 additions and 35 deletions.
64 changes: 29 additions & 35 deletions packages/codemod/transforms/web-ui/v1/migrate-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ function transformer(file, api) {
const firstNode = getFirstNode();
const { comments } = firstNode;

const variantTranslation = {
primary: 'solid',
secondary: 'outline',
};

const webUIButtons = root.findJSXElements('Button');

const getVariantPropValue = path => {
Expand All @@ -24,38 +19,22 @@ function transformer(file, api) {
return variant[0];
};

// update simple variant translation
// TODO: update sizes

// update simple variant translations
webUIButtons
.find(j.JSXAttribute, { name: { type: 'JSXIdentifier', name: 'variant' } })
.find(j.Literal)
.forEach(path => {
const variant = path.node.value;
const newVariant = variantTranslation[variant];
const newVariant = {
primary: 'solid',
secondary: 'outline',
}[variant];

if (newVariant) {
path.node.value = newVariant;
}
if (variant === 'tertiary') {
// add Link import
// Finding all Web UI import declarations
const webUiImports = root
.find(j.ImportDeclaration)
.filter(path => path.node.source.value === '@utilitywarehouse/web-ui');

// Build our new import specifier
const importSpecifier = j.importSpecifier(j.identifier('Link'));

// Iterate over Web UI imports
webUiImports.forEach(webUiImport =>
// Replace the existing node with a new one
j(webUiImport).replaceWith(
// Build a new import declaration node based on the existing one
j.importDeclaration(
[...webUiImport.node.specifiers, importSpecifier], // Insert our new import specificer
webUiImport.node.source
)
)
);
}
});

// replace tertiary variant with button component
Expand All @@ -67,12 +46,6 @@ function transformer(file, api) {
if (path.value.closingElement) {
path.value.closingElement.name = 'button';
}
// // add asChild prop
// path.node.openingElement.attributes = [
// ...path.node.openingElement.attributes,
// // build and insert our new prop
// j.jsxAttribute(j.jsxIdentifier('asChild')),
// ];
// remove variant prop
path.value.openingElement.attributes = path.value.openingElement.attributes.filter(
attr => attr === 'variant'
Expand All @@ -88,6 +61,27 @@ function transformer(file, api) {
);
j(path).replaceWith(wrappedButton);

// add Link import
// Finding all Web UI import declarations
const webUiImports = root
.find(j.ImportDeclaration)
.filter(path => path.node.source.value === '@utilitywarehouse/web-ui');

// Build our new import specifier
const importSpecifier = j.importSpecifier(j.identifier('Link'));

// Iterate over Web UI imports
webUiImports.forEach(webUiImport =>
// Replace the existing node with a new one
j(webUiImport).replaceWith(
// Build a new import declaration node based on the existing one
j.importDeclaration(
[...webUiImport.node.specifiers, importSpecifier], // Insert our new import specificer
webUiImport.node.source
)
)
);

return path;
}
});
Expand Down

0 comments on commit 99097b1

Please sign in to comment.