JS_WALA icon indicating copy to clipboard operation
JS_WALA copied to clipboard

Handle node.js module scope

Open msridhar opened this issue 12 years ago • 3 comments

Consider normalizing the following node.js code:

var fs = require("fs");

The normalized code tries to read require from the global object, but in node, require is declared in the so-called "module" scope. Hence, the normalized code fails with a ReferenceError. I wonder if we can handle this?

msridhar avatar Jan 15 '14 00:01 msridhar

@xiemaisi any thoughts?

msridhar avatar Jan 15 '14 00:01 msridhar

One thought: it looks like module, which refers to the current module object, is a property of the global object in node. So maybe we could special-case accesses to certain "global" variables known to be properties of module, like require, and rewrite those as accessing properties of __global['module']?

msridhar avatar Jan 15 '14 04:01 msridhar

I haven't looked into normalising node.js code yet. As you suggest, the rewriting of global variable references would have to be changed to take module into account, but I'm unclear about the details.

xiemaisi avatar Jan 15 '14 08:01 xiemaisi