import { ASTPath, CallExpression, JSCodeshift, MemberExpression, ObjectExpression } from 'jscodeshift'; import { RegisterAPI } from '../types/transformer'; import { CUSTOMIZATION_CLASS } from '../constants'; /** * Returns AST for ObjectExpression containing all the key-value pairs as properties * @param properties Key-Value pairs that needs to form the ObjectExpression * @param j JSCodeshift core * @returns ObjectExpression containing all the properties */ export const getRegisterPropsAST = ( properties: { name: string; value: string; }[], j: JSCodeshift, ): ObjectExpression => { return j.objectExpression( properties.map(({ name, value }) => j.property('init', j.identifier(name), j.literal(value))), ); }; /** * Returns AST for Flex Customisation API * @param j JSCodeshift core * @param registerAPI Customisation Register API * @param flexVariable Flex instance variable * @returns Flex Customisation API AST */ export const flexCustomizationAST = ( j: JSCodeshift, registerAPI: RegisterAPI, flexVariable: string, ): MemberExpression => { return j.memberExpression( j.memberExpression(j.identifier(flexVariable), j.identifier(CUSTOMIZATION_CLASS)), j.identifier(registerAPI), ); }; /** * Adds a comment AST node to the given AST Path * @param j JSCodeshift core * @param path AST Path * @param comment Comment text * @param leading true/false */ export const addComment = ( j: JSCodeshift, path: ASTPath, comment: string, leading: boolean = true, ): void => { const commentBlock = j.commentBlock(` ${comment} `, leading, !leading); if (path.node.comments?.length) { path.node.comments.push(commentBlock); } else { path.node.comments = [commentBlock]; } };