Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion homework/1/rename.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,39 @@ const acorn = require('acorn');
const astring = require('astring');
const traverse = require('../../common/traverse');



function transform(root, originName, targetName) {

// 替换变量名
function replace(name){
if (name === originName) return targetName
}

// 遍历所有节点
return traverse((node, ctx, next) => {

// TODO: 作业代码写在这里
if (node.type === 'xxx') {
if (node.type === 'VariableDeclarator') {
node.id.name = replace(node.id.name)
}
if (node.type === 'MemberExpression') {
if (node.object.type === 'Identifier') {
node.object.name = replace(node.object.name)
}
}
if (node.type === 'BinaryExpression') {
if (node.left.type === 'Identifier') {
node.left.name = replace(node.left.name)
}
if (node.right.type === 'Identifier') {
node.right.name = replace(node.right.name)
}
}
if (node.type === 'FunctionDeclaration') {
if (node.id.type === 'Identifier') {
node.id.name = replace(node.id.name)
}
}

// 继续往下遍历
Expand Down
Loading