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

updated styledictionary/4/asynchronous-api codemod #1329

Open
wants to merge 1 commit 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: 34 additions & 17 deletions packages/codemods/styledictionary/4/asynchronous-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import type {
API,
ArrowFunctionExpression,
FileInfo,
Options,
} from 'jscodeshift';
import type { API, FileInfo, Options } from 'jscodeshift';

export default function transform(
file: FileInfo,
Expand All @@ -25,6 +20,32 @@ export default function transform(

const root = j(file.source);

// Keep track of variables declared using StyleDictionary
const styleDictionaryVars: Set<string> = new Set();

// Identify variables assigned using StyleDictionary.extend or new StyleDictionary
root.find(j.VariableDeclarator).forEach((path) => {
const init = path.node.init;

// Handle StyleDictionary.extend() pattern
if (
j.CallExpression.check(init) &&
init.callee.type === 'MemberExpression' &&
init.callee.object.name === 'StyleDictionary' &&
init.callee.property.name === 'extend'
) {
styleDictionaryVars.add(path.node.id.name);
}

// Handle new StyleDictionary() pattern
if (
j.NewExpression.check(init) &&
init.callee.name === 'StyleDictionary'
) {
styleDictionaryVars.add(path.node.id.name);
}
});

// Find all function declarations, function expressions, and arrow functions
root.find(j.FunctionDeclaration).forEach((path) =>
addAsyncIfNeeded(path, j),
Expand All @@ -38,21 +59,17 @@ export default function transform(
addAsyncIfNeeded(path, j),
);

// Add 'await' before the relevant method calls, except for StyleDictionary.extend()
// Add 'await' before the relevant method calls, only for variables declared using StyleDictionary
asyncMethods.forEach((method) => {
root.find(j.CallExpression, {
callee: { property: { name: method } },
}).forEach((path) => {
// Check if the method is already awaited
if (!j.AwaitExpression.check(path.parent.node)) {
// Skip if it's StyleDictionary.extend()
if (
!(
path.node.callee.object &&
path.node.callee.object.name === 'StyleDictionary' &&
path.node.callee.property.name === 'extend'
)
) {
const objectName = path.node.callee.object.name;

// Ensure the method is called on a variable initialized with StyleDictionary
if (styleDictionaryVars.has(objectName)) {
// Check if the method is already awaited
if (!j.AwaitExpression.check(path.parent.node)) {
j(path).replaceWith(j.awaitExpression(path.node));
}
}
Expand Down
Loading