7

To create an IDE that would autocomplete all variables the user declares but would be oblivious to other variables such as Math.PI or even the module Math, the IDE would need to be able to identify all identifiers relating to variables declared by the user. What mechanism could be used to capture all such variables, assuming you already have access to the AST (Abstract Symbol Table) for the program?

I am using reflect.js (https://github.com/zaach/reflect.js) to generate the AST.

4

2 回答 2

7

我认为这几乎是不可能的

这就是为什么我认为不执行它几乎是不可能的:

让我们从易到难,经历未探索的部分。

容易捕捉:

此处缺少函数范围:

(function(x){
    //x is now an object with an a property equal to 3
    // for the scope of that IIFE.
    x;
})({a: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;
}

(阅读的人:我求你不要在代码中使用最后两个来确定实际的范围:))

不容易:

括号符号:

var n = "lo";
a["h"+"e"+"l"+n] = "world"; // need to understand that a.hello is a property.
                        // not a part of the ast!

真正困难的部分:

让我们不要忘记调用编译器这些不会出现在 AST 中:

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 或 script 标签注入。

还有什么?当然,他们可以混淆他们想要的一切:

new Function(["w","i","n","dow.x"," = ","5"].join(""))(); // Good luck finding this!
new Function('new Function(["w","i","n","dow.x"," = ","5"].join(""))()')();// Getting dizzy already?

那么可以做些什么呢?

  • 更新符号表(仅相关部分)时,在封闭的定时环境中执行一次代码
  • 查看执行时生成的符号表是什么
  • 繁荣,你给自己一个符号表。

这不可靠,但可能与您所获得的一样接近。

我能想到的唯一另一种选择,即大多数IDE 正在做的事情是简单地忽略任何不是的东西:

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 能够处理任何事情。由于它们是迄今为止最常见的,我认为计算它们是完全合理的。

于 2013-06-27T20:24:00.617 回答
-2

通过将它们添加到已经存在的对象上......即

窗口.mynewvar = 5;

function mynewfunc() {

}
于 2013-06-27T19:35:11.967 回答