要创建一个IDE,它将自动完成用户声明的所有变量,但不会忘记其他变量,如Math.PI甚至模块Math,IDE将需要能够识别与用户声明的变量相关的所有标识符.假设您已经可以访问程序的AST(抽象符号表),可以使用什么机制来捕获所有这些变量?
我使用reflect.js(https://github.com/zaach/reflect.js)来生成AST.@H_502_3@
解决方法
我认为这几乎是不可能的
这就是为什么我认为没有执行它几乎是不可能的:@H_502_3@
让我们来看看未开发的部分,从易到难.@H_502_3@
易于捕捉:@H_502_3@
(function(x){ //x is now an object with an a property equal to 3 // for the scope of that IIFE. x; })({a:3});
这里有一些有趣的脏技巧:@H_502_3@
介绍…鼓卷…块范围!@H_502_3@
with({x:3}){ x;//x is now declared in the scope of that with and is equal to 3. } try{ throw 5}catch(x){ x // x is now declared in the scope of the try block and is equal to 5; }
(读者:请求你不要使用最后两个代码来实际确定代码:))@H_502_3@
不容易:@H_502_3@
括号表示法:@H_502_3@
var n = "lo"; a["h"+"e"+"l"+n] = "world"; // need to understand that a.hello is a property. // not a part of the ast!
真正困难的部分:@H_502_3@
我们不要忘记调用编译器这些不会出现在AST中:@H_502_3@
eval("var x=5"); // declares x as 5,just a string literal and a function call new Function("window.x = 5")();// or global in node
在node.js中,这也可以使用vm模块完成.在浏览器中使用document.write或脚本标记注入.@H_502_3@
还有什么?当然,他们可以混淆他们想要的一切:@H_502_3@
new Function(["w","i","n","dow.x"," = ","5"].join(""))(); // Good luck finding this! new Function('new Function(["w","5"].join(""))()')();// Getting dizzy already?
那可以做些什么呢?@H_502_3@
>更新符号表(只是相关部分)时,在闭合的定时环境中执行一次代码
>查看生成的符号表来自执行的内容
>轰,你有自己的符号表.@H_502_3@
这不可靠,但它可能会尽可能接近.@H_502_3@
我能想到的唯一其他选择,大多数IDE正在做的是简单地忽略任何不是:@H_502_3@
object.property = ... //property definition var a = ... //scoped b = ... //global,or error in strict mode function fn(){ //function declaration object["property"] //property with a _fixed_ literal in bracket notation.
我见过没有IDE可以处理除了这些之外的任何东西.由于它们是迄今为止最常见的,我认为计算它们是完全合理的.@H_502_3@