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

feat: V5 icons codemod #2732

Merged
merged 3 commits into from
Apr 26, 2024
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
5 changes: 2 additions & 3 deletions packages/forma-36-codemod/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@contentful/f36-codemod",
"description": "Forma 36 Codemod",
"version": "4.5.0",
"version": "5.0.0-alpha.0",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to update the deps as well here, we're still pointing to icons v4.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, actually I think we don't need the icons dependency here 🤔

"main": "bin/f36-codemod.mjs",
"license": "MIT",
"files": [
Expand All @@ -21,8 +21,7 @@
"devDependencies": {
"@types/semver": "^7.3.12",
"eslint": "^8.43.0",
"jest": "^29.3.1",
"@contentful/f36-icons": "^4.0.0"
"jest": "^29.3.1"
},
"publishConfig": {
"registry": "https://npm.pkg.github.com/",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import {
InfoCircleIcon,
} from '@contentful/f36-icons';

<CloseIcon size="small" />;
<ChevronUpIcon size="small" />;
<CloseIcon size="large" />;
<ChevronUpIcon size="xlarge" />;
<ChevronDownIcon size="small" />;
<DeleteIcon size="small" />;
<InfoCircleIcon />;
<InfoCircleIcon variant="muted" />;

const largeIcon = true;
<CloseIcon size={largeIcon ? 'large' : 'xlarge'} />
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { CaretDownIcon, CaretUpIcon, XIcon, TrashSimpleIcon, InfoFillIcon } from "@contentful/f36-icons-alpha";
import { CaretDownIcon, CaretUpIcon, XIcon, TrashSimpleIcon, InfoIcon } from "@contentful/f36-icons-alpha";

<XIcon size="small" />;
<CaretUpIcon size="small" />;
<XIcon />;
<CaretUpIcon />;
<CaretDownIcon size="small" />;
<TrashSimpleIcon size="small" />;
<InfoFillIcon />;
<InfoIcon />;

const largeIcon = true;
<XIcon />
79 changes: 79 additions & 0 deletions packages/forma-36-codemod/transforms/v5/icons.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ const {
getComponentLocalName,
changeComponentName,
changeImport,
changeProperties,
deleteProperty,
updateTernaryValues,
updatePropertyValue,
getProperty,
hasProperty,
} = require('../../utils');
const { shouldSkipUpdateImport, getImport } = require('../../utils/config');
const { isConditionalExpression } = require('../../utils/updateTernaryValues');

// V4 icon name : V5 icon name
const iconsMap = {
Expand Down Expand Up @@ -134,6 +141,78 @@ module.exports = function (file, api) {
components.forEach(({ localName, v4IconName }) => {
const newComponentName = `${iconsMap[v4IconName]}Icon`;

source = changeProperties(j, source, {
componentName: localName,
fn(attributes) {
let modifiedAttributes = attributes;

// Remove variant prop
modifiedAttributes = deleteProperty(modifiedAttributes, {
propertyName: 'variant',
file,
});

// Update size prop
if (hasProperty(modifiedAttributes, { propertyName: 'size' })) {
let size = getProperty(modifiedAttributes, {
propertyName: 'size',
});

// update conditional expressions
if (isConditionalExpression(size.value, j)) {
modifiedAttributes = updatePropertyValue(modifiedAttributes, {
j,
propertyName: 'size',
propertyValue: (value) => {
const valueMap = {
large: 'medium',
xlarge: 'medium',
};

const updatedValue = updateTernaryValues(value, {
j,
valueMap,
});

return updatedValue;
},
});
}

size = getProperty(modifiedAttributes, {
propertyName: 'size',
});

// If ternary has same value for true and false, simplify
if (size.value.value === undefined) {
const matches = size.value.match(/\{'(\w+?)'\}/);
if (matches[1]) {
modifiedAttributes = updatePropertyValue(modifiedAttributes, {
j,
propertyName: 'size',
propertyValue: () => {
return j.literal(matches[1]);
},
});
}
}
size = getProperty(modifiedAttributes, {
propertyName: 'size',
});

// Remove size prop if value is 'large', 'xlarge' or 'medium'
if (['large', 'xlarge', 'medium'].includes(size.value.value)) {
modifiedAttributes = deleteProperty(modifiedAttributes, {
propertyName: 'size',
file,
});
}
}

return modifiedAttributes;
},
});

source = changeComponentName(j, source, {
componentName: localName,
outputComponentName: newComponentName,
Expand Down
10 changes: 7 additions & 3 deletions packages/forma-36-codemod/utils/updateTernaryValues.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ function updateTernaryValues(value, { j, valueMap = {} }) {
const consequent = getValueFor('consequent', commonArgs);
const alternate = getValueFor('alternate', commonArgs);

j(expression).replaceWith(
j.conditionalExpression(expression.value.test, consequent, alternate),
);
if (consequent.value === alternate.value) {
j(expression).replaceWith(j.literal(consequent.value));
} else {
j(expression).replaceWith(
j.conditionalExpression(expression.value.test, consequent, alternate),
);
}
})
.toSource({ quote: 'single' });
}
Expand Down