你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

【2021-12-20】AST BAbel 还原全局对象或函数

2021/12/20 14:21:29
 // ********************************************************************************************************************
        console.log("还原-常量折叠插件更新、将逗号表达式拆分......")
        const Statement_list = ['ForStatement'];
        traverse(ast, {
            VariableDeclaration(path, elements = []) { // 变量分离
                let { kind, declarations } = path.node;
                let _statement = path.getStatementParent();
                if (declarations.length < 2 || Statement_list.includes(_statement.type) || Statement_list.includes(path.parentPath.type)) return;
                elements = path.get("declarations").map(declaration => {
                    return TNT.variableDeclaration(kind, [declaration.node]);
                })
                path.replaceWithMultiple(elements);
            },
            ExpressionStatement(path, elements = []) {
                let { expression } = path.node;
                if (!TNT.isSequenceExpression(expression)) return;
                elements = expression.expressions.map(expression => {
                    return TNT.expressionStatement(expression);
                })
                path.replaceWithMultiple(elements);
            },
        });
        // ********************************************************************************************************************
        
        GLOBAL_PROPERTY = ['window', 'String', 'Error', 'Array', 'Math', 'parseInt', 'Date', 'Object', 'unescape', 'encodeURIComponent', 'Function'];
        traverse(ast, {
            Scope(path) {
                // console.log(path.type,path.toString())
            },
            VariableDeclarator(path) {
                let { id, init } = path.node;
                if (TNT.isIdentifier(id) && TNT.isIdentifier(init) && GLOBAL_PROPERTY.includes(init.name)) {
                    // path.scope.dump()
                    const _FunctionParent = path.getFunctionParent();
                    let binding = _FunctionParent.scope.getBinding(id.name);
                    // 查找没有修改过的
                    if (!binding || binding.constantViolations.length !== 0) return;

                    console.log(path.parentPath.type)
                    console.log("函数声明类型", binding.kind)
                    console.log("是否会被修改", binding.constant)
                    console.log("绑定是否被引用", binding.referenced)
                    console.log("重新定义的次数", binding.violations)
                    console.log("获取当前多有绑定修改", binding.constantViolations.length)
                    console.log("获取当前多有绑定路径", binding.referencePaths.length)
                    console.log(init.name, path.toString())
                    // let GLOBAL_NODE = template.statement(`${init.name}.NODE`);
                    // ggg = GLOBAL_NODE({
                    //     NODE: TNT.Identifier('Math')
                    // });
                    // console.log(GLOBAL_NODE.toString(), generator(ggg).code);
                    for (let refer_path of binding.referencePaths.reverse()) {
                    	refer_path.node.name = init.name
                    }
                    path.parentPath.remove();
                    _FunctionParent.scope.crawl()

                }
            }
        });