Created
October 13, 2021 11:18
-
-
Save liuwenzhuang/730b495aa1e4f9c4e3a122240e4a1e6c to your computer and use it in GitHub Desktop.
babel plugin to destructUserToken
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function ({ types: t }) { | |
let hasUserTokenImport = false; | |
let defaultImportIdentifier = ''; | |
return { | |
visitor: { | |
Program: { | |
enter(path) { | |
const children = path.node.body; | |
const userTokenImportIndex = children.findIndex(item => { | |
if (item.type !== 'ImportDeclaration') { | |
return false; | |
} | |
const { source, specifiers = [] } = item; | |
const { value } = source; | |
const isUserTokenImport = specifiers.some( | |
item => item.type === 'ImportSpecifier' && item.local.name === 'userToken' | |
); | |
const isImportFromConstant = value.toLowerCase().indexOf('global/constant') !== -1; | |
return isUserTokenImport && isImportFromConstant; | |
}); | |
hasUserTokenImport = userTokenImportIndex !== -1; | |
if (hasUserTokenImport) { | |
// 有 import userToken from 'Global/constant' 引用,替换掉 | |
const node = children[userTokenImportIndex]; | |
const { specifiers = [] } = node; | |
const hasOtherImport = specifiers.length > 1; // 仍有其他的引入,替换 | |
let defaultImport; | |
if (hasOtherImport) { | |
const defaultSpecifier = specifiers.find(item => item.type === 'ImportDefaultSpecifier'); | |
if (defaultSpecifier) { | |
defaultImportIdentifier = defaultSpecifier.local.name; | |
} | |
defaultImport = t.importDeclaration( | |
specifiers.filter( | |
item => !(item.type === 'ImportSpecifier' && item.local.name === 'userToken') | |
), | |
t.stringLiteral('Global/constant') | |
); | |
} else { | |
// 没有其他引入 | |
defaultImport = t.importDeclaration( | |
[t.importDefaultSpecifier(t.identifier('constant'))], | |
t.stringLiteral('Global/constant') | |
); | |
defaultImportIdentifier = 'constant'; | |
} | |
children.splice(userTokenImportIndex, 1, defaultImport); | |
} | |
} | |
}, | |
MemberExpression(path) { | |
if (!hasUserTokenImport || !defaultImportIdentifier) { | |
return; | |
} | |
const node = path.node; | |
// userToken.xx => constant.userToken.xx | |
if (node.object.name !== 'userToken' || node.__handled__) { | |
return; | |
} | |
const replaceExpression = t.memberExpression( | |
t.memberExpression(t.identifier(defaultImportIdentifier), t.identifier('userToken')), | |
node.property | |
); | |
node.__handled__ = true; | |
path.replaceWith(replaceExpression); | |
}, | |
VariableDeclarator(path) { | |
if (!hasUserTokenImport || !defaultImportIdentifier) { | |
return; | |
} | |
const node = path.node; | |
// const { email } = userToken => const { email } = constant.userToken | |
if (!node.init || node.init.name !== 'userToken' || node.__handled__) { | |
return; | |
} | |
const replaceExpression = t.memberExpression(t.identifier(defaultImportIdentifier), node.init); | |
node.init = replaceExpression; | |
node.__handled__ = true; | |
}, | |
ObjectProperty(path) { | |
if (!hasUserTokenImport || !defaultImportIdentifier) { | |
return; | |
} | |
const node = path.node; | |
// const { email } = userToken => const { email } = constant.userToken | |
if (node.value.name !== 'userToken' || node.__handled__) { | |
return; | |
} | |
node.value = t.memberExpression(t.identifier(defaultImportIdentifier), node.value); | |
node.__handled__ = true; | |
} | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment