|
| 1 | +/* eslint-disable no-param-reassign */ |
| 2 | +/* eslint-disable no-console */ |
| 3 | +const chalk = require('chalk') |
| 4 | + |
| 5 | +const invokeWithMockedUpProp = (jscodeshift, file, prop) => { |
| 6 | + // We invoke the function previously passed as `loading` to react-loadable with this props |
| 7 | + // { |
| 8 | + // pastDelay: true, |
| 9 | + // error: false, |
| 10 | + // timedOut: false, |
| 11 | + // } |
| 12 | + const j = jscodeshift |
| 13 | + |
| 14 | + const defaultPropsObjProperties = [] |
| 15 | + |
| 16 | + defaultPropsObjProperties.push( |
| 17 | + j.objectProperty(j.identifier('pastDelay'), j.booleanLiteral(true)), |
| 18 | + ) |
| 19 | + defaultPropsObjProperties.push( |
| 20 | + j.objectProperty(j.identifier('error'), j.booleanLiteral(false)), |
| 21 | + ) |
| 22 | + defaultPropsObjProperties.push( |
| 23 | + j.objectProperty(j.identifier('timedOut'), j.booleanLiteral(false)), |
| 24 | + ) |
| 25 | + |
| 26 | + const defaultPropsObj = j.objectExpression(defaultPropsObjProperties) |
| 27 | + |
| 28 | + const callExpr = j.callExpression(prop.value, [defaultPropsObj]) |
| 29 | + |
| 30 | + prop.value = callExpr |
| 31 | + |
| 32 | + console.warn( |
| 33 | + chalk.yellow( |
| 34 | + `[WARN] '${file.path}' has some react-loadable specific logic in it. We could not codemod while keeping all the behaviors the same. Please check this file manually.`, |
| 35 | + ), |
| 36 | + ) |
| 37 | +} |
| 38 | + |
| 39 | +export default (file, api) => { |
| 40 | + const { source } = file |
| 41 | + const { jscodeshift: j } = api |
| 42 | + |
| 43 | + const root = j(source) |
| 44 | + |
| 45 | + // Rename `import Loadable from 'react-loadable';` to `import loadable from '@loadable/component'; |
| 46 | + root.find(j.ImportDeclaration).forEach(({ node }) => { |
| 47 | + if ( |
| 48 | + node.specifiers[0] && |
| 49 | + node.specifiers[0].local.name === 'Loadable' && |
| 50 | + node.source.value === 'react-loadable' |
| 51 | + ) { |
| 52 | + node.specifiers[0].local.name = 'loadable' |
| 53 | + node.source.value = '@loadable/component' |
| 54 | + } |
| 55 | + }) |
| 56 | + |
| 57 | + // Change Loadable({ ... }) invocation to loadable(() => {}, { ... }) invocation |
| 58 | + root |
| 59 | + .find(j.CallExpression, { callee: { name: 'Loadable' } }) |
| 60 | + .forEach(path => { |
| 61 | + const { node } = path |
| 62 | + const initialArgsProps = node.arguments[0].properties |
| 63 | + let loader // this will be a function returning a dynamic import promise |
| 64 | + |
| 65 | + // loop through the first argument (object) passed to `Loadable({ ... })` |
| 66 | + const newProps = initialArgsProps |
| 67 | + .map(prop => { |
| 68 | + if (prop.key.name === 'loader') { |
| 69 | + /** |
| 70 | + * In react-loadable, this is the function that returns a dynamic import |
| 71 | + * We'll keep it to `loader` variable for now, and remove it from the arg object |
| 72 | + */ |
| 73 | + loader = prop.value |
| 74 | + |
| 75 | + return undefined |
| 76 | + } |
| 77 | + |
| 78 | + if (prop.key.name === 'loading') { |
| 79 | + prop.key.name = 'fallback' // rename to fallback |
| 80 | + |
| 81 | + /** |
| 82 | + * react-loadable accepts a Function that returns JSX as the `loading` arg. |
| 83 | + * @loadable/component accepts a React.Element (what returned from React.createElement() calls) |
| 84 | + * |
| 85 | + */ |
| 86 | + if (prop.value.type === 'ArrowFunctionExpression') { |
| 87 | + // if it's an ArrowFunctionExpression like `() => <div>loading...</div>`, |
| 88 | + |
| 89 | + if ( |
| 90 | + (prop.value.params && prop.value.params.length > 0) || |
| 91 | + prop.value.type === 'Identifier' |
| 92 | + ) { |
| 93 | + // If the function accept props, we can invoke it and pass it a mocked-up props to get the component to |
| 94 | + // a should-be-acceptable default state, while also logs out a warning. |
| 95 | + // { |
| 96 | + // pastDelay: true, |
| 97 | + // error: false, |
| 98 | + // timedOut: false, |
| 99 | + // } |
| 100 | + |
| 101 | + invokeWithMockedUpProp(j, file, prop) |
| 102 | + } else { |
| 103 | + // If the function doesn't accept any params, we can safely just invoke it directly |
| 104 | + // we can change it to `(() => <div>loading...</div>)()` |
| 105 | + const callExpr = j.callExpression(prop.value, []) |
| 106 | + |
| 107 | + prop.value = callExpr |
| 108 | + } |
| 109 | + } else if (prop.value.type === 'Identifier') { |
| 110 | + // if it's an identifier like `Loading`, let's just invoke it with a mocked-up props |
| 111 | + invokeWithMockedUpProp(j, file, prop) |
| 112 | + } |
| 113 | + |
| 114 | + return prop |
| 115 | + } |
| 116 | + |
| 117 | + // for all other props, just remove them |
| 118 | + return undefined |
| 119 | + }) |
| 120 | + .filter(Boolean) |
| 121 | + |
| 122 | + // add the function that return a dynamic import we stored earlier as the first argument to `loadable()` call |
| 123 | + node.arguments.unshift(loader) |
| 124 | + node.arguments[1].properties = newProps |
| 125 | + node.callee.name = 'loadable' |
| 126 | + }) |
| 127 | + |
| 128 | + return root.toSource({ quote: 'single', trailingComma: true }) |
| 129 | +} |
0 commit comments