| |
|
|
| function addWithRouterImport(j, root) { |
| |
| |
| |
| const withRouterSpecifier = j.importSpecifier(j.identifier('withRouter')) |
|
|
| |
| |
| const originalRouterImport = root.find(j.ImportDeclaration, { |
| source: { |
| value: 'next/router', |
| }, |
| }) |
| if (originalRouterImport.length > 0) { |
| |
| if ( |
| originalRouterImport.find(j.ImportSpecifier, { |
| imported: { name: 'withRouter' }, |
| }).length > 0 |
| ) { |
| return |
| } |
|
|
| |
| originalRouterImport.forEach((node) => { |
| node.value.specifiers.push(withRouterSpecifier) |
| }) |
| return |
| } |
|
|
| |
| |
| const withRouterImport = j.importDeclaration( |
| [withRouterSpecifier], |
| j.stringLiteral('next/router') |
| ) |
|
|
| |
| const Program = root.find(j.Program) |
| |
| Program.forEach((node) => { |
| node.value.body.unshift(withRouterImport) |
| }) |
| } |
|
|
| function getThisPropsUrlNodes(j, tree) { |
| return tree.find(j.MemberExpression, { |
| object: { |
| type: 'MemberExpression', |
| object: { type: 'ThisExpression' }, |
| property: { name: 'props' }, |
| }, |
| property: { name: 'url' }, |
| }) |
| } |
|
|
| function getPropsUrlNodes(j, tree, name) { |
| return tree.find(j.MemberExpression, { |
| object: { name }, |
| property: { name: 'url' }, |
| }) |
| } |
|
|
| |
| |
| function wrapNodeInFunction(j, functionName, args) { |
| const mappedArgs = args.map((node) => { |
| |
| |
| if (node.type === 'ClassDeclaration') { |
| node.type = 'ClassExpression' |
| } |
|
|
| return node |
| }) |
| return j.callExpression(j.identifier(functionName), mappedArgs) |
| } |
|
|
| function turnUrlIntoRouter(j, tree) { |
| tree.find(j.Identifier, { name: 'url' }).replaceWith(j.identifier('router')) |
| } |
|
|
| export default function transformer(file, api) { |
| |
| const j = api.jscodeshift |
| |
| const root = j(file.source) |
|
|
| |
| const defaultExports = root.find(j.ExportDefaultDeclaration) |
|
|
| |
| |
| defaultExports.forEach((rule) => { |
| |
| const { value: node } = rule |
| |
| const { declaration } = node |
|
|
| function wrapDefaultExportInWithRouter() { |
| if ( |
| j(rule).find(j.CallExpression, { callee: { name: 'withRouter' } }) |
| .length > 0 |
| ) { |
| return |
| } |
| j(rule).replaceWith( |
| j.exportDefaultDeclaration( |
| wrapNodeInFunction(j, 'withRouter', [declaration]) |
| ) |
| ) |
| } |
|
|
| |
| |
| |
| if (declaration.type === 'Identifier') { |
| |
| const { name } = declaration |
|
|
| |
| let implementation = root.find(j.Declaration, { id: { name } }) |
| if (implementation.length === 0) { |
| implementation = root.find(j.VariableDeclarator, { id: { name } }) |
| } |
|
|
| implementation |
| .find(j.Property, { key: { name: 'url' } }) |
| .forEach((propertyRule) => { |
| const isThisPropsDestructure = j(propertyRule).closest( |
| j.VariableDeclarator, |
| { |
| init: { |
| object: { |
| type: 'ThisExpression', |
| }, |
| property: { name: 'props' }, |
| }, |
| } |
| ) |
| if (isThisPropsDestructure.length === 0) { |
| return |
| } |
| const originalKeyValue = propertyRule.value.value.name |
| propertyRule.value.key.name = 'router' |
| wrapDefaultExportInWithRouter() |
| addWithRouterImport(j, root) |
| |
| if (originalKeyValue !== 'url') { |
| return |
| } |
|
|
| propertyRule.value.value.name = 'router' |
| j(propertyRule) |
| .closest(j.BlockStatement) |
| .find(j.Identifier, (identifierNode) => { |
| if (identifierNode.type === 'JSXIdentifier') { |
| return false |
| } |
|
|
| if (identifierNode.name !== 'url') { |
| return false |
| } |
|
|
| return true |
| }) |
| .replaceWith(j.identifier('router')) |
| }) |
|
|
| |
| const thisPropsUrlUsage = getThisPropsUrlNodes(j, implementation) |
|
|
| if (thisPropsUrlUsage.length === 0) { |
| return |
| } |
|
|
| |
| turnUrlIntoRouter(j, thisPropsUrlUsage) |
| wrapDefaultExportInWithRouter() |
| addWithRouterImport(j, root) |
| return |
| } |
|
|
| const arrowFunctions = j(rule).find(j.ArrowFunctionExpression) |
| ;(() => { |
| if (arrowFunctions.length === 0) { |
| return |
| } |
|
|
| arrowFunctions.forEach((r) => { |
| |
| if (j(r).closest(j.Expression).length !== 0) { |
| return |
| } |
|
|
| if (!r.value.params || !r.value.params[0]) { |
| return |
| } |
|
|
| const name = r.value.params[0].name |
| const propsUrlUsage = getPropsUrlNodes(j, j(r), name) |
| if (propsUrlUsage.length === 0) { |
| return |
| } |
|
|
| turnUrlIntoRouter(j, propsUrlUsage) |
| wrapDefaultExportInWithRouter() |
| addWithRouterImport(j, root) |
| }) |
| return |
| })() |
|
|
| if (declaration.type === 'CallExpression') { |
| j(rule) |
| .find(j.CallExpression, (haystack) => { |
| const firstArgument = haystack.arguments[0] || {} |
| if (firstArgument.type === 'Identifier') { |
| return true |
| } |
|
|
| return false |
| }) |
| .forEach((callRule) => { |
| const { name } = callRule.value.arguments[0] |
|
|
| |
| let implementation = root.find(j.Declaration, { id: { name } }) |
| if (implementation.length === 0) { |
| implementation = root.find(j.VariableDeclarator, { id: { name } }) |
| } |
| |
| const thisPropsUrlUsage = getThisPropsUrlNodes(j, implementation) |
|
|
| implementation |
| .find(j.Property, { key: { name: 'url' } }) |
| .forEach((propertyRule) => { |
| const isThisPropsDestructure = j(propertyRule).closest( |
| j.VariableDeclarator, |
| { |
| init: { |
| object: { |
| type: 'ThisExpression', |
| }, |
| property: { name: 'props' }, |
| }, |
| } |
| ) |
| if (isThisPropsDestructure.length === 0) { |
| return |
| } |
| const originalKeyValue = propertyRule.value.value.name |
| propertyRule.value.key.name = 'router' |
| wrapDefaultExportInWithRouter() |
| addWithRouterImport(j, root) |
| |
| if (originalKeyValue !== 'url') { |
| return |
| } |
|
|
| propertyRule.value.value.name = 'router' |
| j(propertyRule) |
| .closest(j.BlockStatement) |
| .find(j.Identifier, (identifierNode) => { |
| if (identifierNode.type === 'JSXIdentifier') { |
| return false |
| } |
|
|
| if (identifierNode.name !== 'url') { |
| return false |
| } |
|
|
| return true |
| }) |
| .replaceWith(j.identifier('router')) |
| }) |
|
|
| if (thisPropsUrlUsage.length === 0) { |
| return |
| } |
|
|
| |
| turnUrlIntoRouter(j, thisPropsUrlUsage) |
| wrapDefaultExportInWithRouter() |
| addWithRouterImport(j, root) |
| return |
| }) |
| } |
|
|
| j(rule) |
| .find(j.Property, { key: { name: 'url' } }) |
| .forEach((propertyRule) => { |
| const isThisPropsDestructure = j(propertyRule).closest( |
| j.VariableDeclarator, |
| { |
| init: { |
| object: { |
| type: 'ThisExpression', |
| }, |
| property: { name: 'props' }, |
| }, |
| } |
| ) |
| if (isThisPropsDestructure.length === 0) { |
| return |
| } |
| const originalKeyValue = propertyRule.value.value.name |
| propertyRule.value.key.name = 'router' |
| wrapDefaultExportInWithRouter() |
| addWithRouterImport(j, root) |
| |
| if (originalKeyValue !== 'url') { |
| return |
| } |
|
|
| propertyRule.value.value.name = 'router' |
| j(propertyRule) |
| .closest(j.BlockStatement) |
| .find(j.Identifier, (identifierNode) => { |
| if (identifierNode.type === 'JSXIdentifier') { |
| return false |
| } |
|
|
| if (identifierNode.name !== 'url') { |
| return false |
| } |
|
|
| return true |
| }) |
| .replaceWith(j.identifier('router')) |
| }) |
|
|
| j(rule) |
| .find(j.MethodDefinition, { key: { name: 'componentWillReceiveProps' } }) |
| .forEach((methodRule) => { |
| const func = methodRule.value.value |
| if (!func.params[0]) { |
| return |
| } |
| const firstArgumentName = func.params[0].name |
| const propsUrlUsage = getPropsUrlNodes( |
| j, |
| j(methodRule), |
| firstArgumentName |
| ) |
| turnUrlIntoRouter(j, propsUrlUsage) |
| if (propsUrlUsage.length === 0) { |
| return |
| } |
| wrapDefaultExportInWithRouter() |
| addWithRouterImport(j, root) |
| }) |
|
|
| j(rule) |
| .find(j.MethodDefinition, { key: { name: 'componentDidUpdate' } }) |
| .forEach((methodRule) => { |
| const func = methodRule.value.value |
| if (!func.params[0]) { |
| return |
| } |
| const firstArgumentName = func.params[0].name |
| const propsUrlUsage = getPropsUrlNodes( |
| j, |
| j(methodRule), |
| firstArgumentName |
| ) |
| turnUrlIntoRouter(j, propsUrlUsage) |
| if (propsUrlUsage.length === 0) { |
| return |
| } |
| wrapDefaultExportInWithRouter() |
| addWithRouterImport(j, root) |
| }) |
|
|
| const thisPropsUrlUsage = getThisPropsUrlNodes(j, j(rule)) |
| const propsUrlUsage = getPropsUrlNodes(j, j(rule), 'props') |
|
|
| |
| turnUrlIntoRouter(j, thisPropsUrlUsage) |
| turnUrlIntoRouter(j, propsUrlUsage) |
|
|
| if (thisPropsUrlUsage.length === 0 && propsUrlUsage.length === 0) { |
| return |
| } |
|
|
| wrapDefaultExportInWithRouter() |
| addWithRouterImport(j, root) |
| return |
| }) |
|
|
| return root.toSource() |
| } |
|
|