estools/estraverse

How to replace every AssignmentExpression in node?

xeroxstar opened this issue · 1 comments

I have this code where i need to get all required modules:

let html = "this.stripe = require('stripe');this.v = 1;this.test = require('./test.js')"

estraverse.replace(html, {
                        enter : function (node) {
                            if(node.type === 'AssignmentExpression'){
                                if(node.right && node.right.callee) {
                                    if (node.right.callee.callee ? node.right.callee.callee.name : node.right.callee.value === 'require') {
                                        modules.push(escodegen.generate(node));
                                        // works here: this.remove();
                                     
                                    }
                                }
                                if(node.arguments && node.arguments[0] && node.arguments[0].value && node.arguments[0].value.toString().startsWith('.')){
                                    node.arguments[0].value =  path.resolve(root,node.arguments[0].value).replace(/\\/gi,'/');
                                    node.arguments[0].raw   =  JSON.stringify(path.resolve(root,node.arguments[0].value).replace(/\\/gi,'/')) ;
                                    modules.push(escodegen.generate(node));
                                    // do not works here: this.remove();
                                }
                            }
                        },
                        leave : function (node) {
                                return this.break();
                        }
});

how do i loop through all AssignmentExpressions, for now this code only gets me the first AssignmentExpression [this.stripe] but it won't loop to the next AssignmentExpression [this.v] etc... Thanks

Nevermind, i had to remove the "leave" method and the replace loop through all nodes...