|
| 1 | +import core, { API, FileInfo, Options, Collection } from 'jscodeshift'; |
| 2 | + |
| 3 | +import { hasImportDeclaration, hasImportSpecifier } from '@hypermod/utils'; |
| 4 | + |
| 5 | +export default function transformer( |
| 6 | + file: FileInfo, |
| 7 | + { jscodeshift: j }: API, |
| 8 | + options: Options, |
| 9 | +) { |
| 10 | + const source = j(file.source); |
| 11 | + |
| 12 | + if (!hasTestUtilsImport(j, source)) { |
| 13 | + return file.source; |
| 14 | + } |
| 15 | + |
| 16 | + // Replace the import of defineInlineTest with applyTransform from '@hypermod/utils' |
| 17 | + replaceImport(j, source); |
| 18 | + |
| 19 | + // Transform defineInlineTest calls to it function with applyTransform |
| 20 | + transformDefineInlineTestCalls(j, source); |
| 21 | + |
| 22 | + return source.toSource(options); |
| 23 | +} |
| 24 | + |
| 25 | +function replaceImport(j: core.JSCodeshift, source: Collection<any>) { |
| 26 | + // Remove defineInlineTest import or require |
| 27 | + source |
| 28 | + .find(j.ImportDeclaration) |
| 29 | + .filter(path => path.node.source.value === 'jscodeshift/dist/testUtils') |
| 30 | + .remove(); |
| 31 | + |
| 32 | + source |
| 33 | + .find(j.VariableDeclarator) |
| 34 | + .filter(path => { |
| 35 | + return ( |
| 36 | + path.node.init && |
| 37 | + // @ts-expect-error |
| 38 | + path.node.init.object && |
| 39 | + // @ts-expect-error |
| 40 | + path.node.init.object.callee && |
| 41 | + // @ts-expect-error |
| 42 | + path.node.init.object.callee.name === 'require' && |
| 43 | + // @ts-expect-error |
| 44 | + path.node.init.object.arguments.length && |
| 45 | + // @ts-expect-error |
| 46 | + path.node.init.object.arguments[0].value === |
| 47 | + 'jscodeshift/dist/testUtils' |
| 48 | + ); |
| 49 | + }) |
| 50 | + .remove(); |
| 51 | + |
| 52 | + if (hasImportSpecifier(j, source, 'applyTransform', '@hypermod/utils')) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + // Insert the applyTransform import |
| 57 | + const applyTransformImport = j.importDeclaration( |
| 58 | + [j.importSpecifier(j.identifier('applyTransform'))], |
| 59 | + j.stringLiteral('@hypermod/utils'), |
| 60 | + ); |
| 61 | + |
| 62 | + // Insert the new import at the top of the file |
| 63 | + source.get().node.program.body.unshift(applyTransformImport); |
| 64 | +} |
| 65 | + |
| 66 | +function hasTestUtilsImport(j: core.JSCodeshift, source: Collection<any>) { |
| 67 | + const hasRequireStatement = source.find(j.VariableDeclarator).filter(path => { |
| 68 | + return ( |
| 69 | + path.node.init && |
| 70 | + // @ts-expect-error |
| 71 | + path.node.init.object && |
| 72 | + // @ts-expect-error |
| 73 | + path.node.init.object.callee && |
| 74 | + // @ts-expect-error |
| 75 | + path.node.init.object.callee.name === 'require' && |
| 76 | + // @ts-expect-error |
| 77 | + path.node.init.object.arguments.length && |
| 78 | + // @ts-expect-error |
| 79 | + path.node.init.object.arguments[0].value === 'jscodeshift/dist/testUtils' |
| 80 | + ); |
| 81 | + }).length; |
| 82 | + |
| 83 | + return ( |
| 84 | + hasImportSpecifier(j, source, 'applyTransform', '@hypermod/utils') || |
| 85 | + hasRequireStatement |
| 86 | + ); |
| 87 | +} |
| 88 | + |
| 89 | +function transformDefineInlineTestCalls( |
| 90 | + j: core.JSCodeshift, |
| 91 | + source: Collection<any>, |
| 92 | +) { |
| 93 | + source |
| 94 | + .find(j.CallExpression) |
| 95 | + .filter( |
| 96 | + path => |
| 97 | + // @ts-expect-error |
| 98 | + path.node.callee.name === 'defineInlineTest' || |
| 99 | + // @ts-expect-error |
| 100 | + (path.node.callee.property && |
| 101 | + // @ts-expect-error |
| 102 | + path.node.callee.property.name === 'defineInlineTest'), |
| 103 | + ) |
| 104 | + .forEach(path => { |
| 105 | + const [transformerOpts, _configOpts, input, output, description] = |
| 106 | + path.node.arguments; |
| 107 | + |
| 108 | + const transformer = j(transformerOpts).find(j.ObjectProperty, { |
| 109 | + key: { name: 'default' }, |
| 110 | + }); |
| 111 | + |
| 112 | + const parser = j(transformerOpts).find(j.ObjectProperty, { |
| 113 | + key: { name: 'parser' }, |
| 114 | + }); |
| 115 | + |
| 116 | + if (!parser.length || !transformer.length) { |
| 117 | + throw new Error('Parser or transformer not found'); |
| 118 | + } |
| 119 | + |
| 120 | + // Create the async arrow function for the test body |
| 121 | + const testBody = j.blockStatement([ |
| 122 | + j.variableDeclaration('const', [ |
| 123 | + j.variableDeclarator( |
| 124 | + j.identifier('result'), |
| 125 | + j.awaitExpression( |
| 126 | + j.callExpression(j.identifier('applyTransform'), [ |
| 127 | + transformer.get().value.value, |
| 128 | + input, |
| 129 | + j.objectExpression([parser.get().value]), |
| 130 | + ]), |
| 131 | + ), |
| 132 | + ), |
| 133 | + ]), |
| 134 | + j.expressionStatement( |
| 135 | + j.callExpression( |
| 136 | + j.memberExpression( |
| 137 | + j.callExpression(j.identifier('expect'), [ |
| 138 | + j.identifier('result'), |
| 139 | + ]), |
| 140 | + j.identifier('toMatchInlineSnapshot'), |
| 141 | + ), |
| 142 | + [output], |
| 143 | + ), |
| 144 | + ), |
| 145 | + ]); |
| 146 | + |
| 147 | + const asyncArrowFunction = j.arrowFunctionExpression([], testBody, true); |
| 148 | + // https://github.com/benjamn/ast-types/issues/264#issuecomment-384392685 |
| 149 | + asyncArrowFunction.async = true; |
| 150 | + |
| 151 | + // Create the it function call |
| 152 | + const itFunctionCall = j.callExpression(j.identifier('it'), [ |
| 153 | + description, |
| 154 | + asyncArrowFunction, |
| 155 | + ]); |
| 156 | + |
| 157 | + // Replace the original call with the new it function call |
| 158 | + j(path).replaceWith(itFunctionCall); |
| 159 | + }); |
| 160 | +} |
0 commit comments