Skip to content

Commit

Permalink
Fix #7
Browse files Browse the repository at this point in the history
  • Loading branch information
angelozerr committed Dec 3, 2015
1 parent d198a49 commit c0a671a
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 8 deletions.
36 changes: 36 additions & 0 deletions demos/outline_objLitFn.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,42 @@ <h1>Demo with Outline Tern plugin </h1>
test_func: function() {
}
};


var test_class1 = function() {
this.public_func = private_func;

function private_func() {

}
};

var test_class2;
(function() {
test_class2 = function() {
function private_function() {
}
};

test_class2.prototype = {
proto_func: function() {
}
};
})();

var test_obj3;
(function() {
var test_class3 = function() {
function private_function() {
}
}

test_class3.prototype = {
proto_func: function() {
}
};
test_obj3 = new test_class3();
})();
</textarea>
</td>
<td valign="top">
Expand Down
2 changes: 1 addition & 1 deletion demos/outline_proto.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ <h1>Demo with Outline Tern plugin </h1>
<td valign="top" style="width: 50%">
<textarea id="code" name="code">function Juan() {
}
Juan.prototype = Object.create(window.prototype);
Juan.prototype = Object.create(window);
Juan.prototype.constructor = Juan;
Juan.prototype.hello = function () {
};
Expand Down
49 changes: 44 additions & 5 deletions outline.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,39 @@
});*/
});

function getChild(parent, name) {
var children = parent.children;
if (!children) return null;
for (var i = 0; i < children.length; i++) {
var child = children[i];
if (child.name == name) return child;
}
}

function fillTypes(type, parent, excludeTypes) {
if (!type) return;
excludeTypes.push(type);
infer.forAllPropertiesOf(type, function(prop, obj, depth) {
if (depth > 0) return;
var t = obj && obj.props[prop];
if (t && t.originNode && t.originNode.type != "ClassDeclaration" && excludeTypes.indexOf(t) == -1) {
excludeTypes.push(t);
var child = getChild(parent, prop);
if (!child) {
child = addChildNode(t.originNode, t, parent);
}
fillTypes(t, child, excludeTypes);
}
})
}

function addAnonymousFn(node, type, parent) {
var child = addChildNode(node.body, type, parent);
child.name = ANONYMOUS_FN;
if (child.type == "?") child.type = "fn()";
return child;
}

// Adapted from infer.searchVisitor.
// Record the scope and pass it through in the state.
// VariableDeclaration in infer.searchVisitor breaks things for us.
Expand All @@ -91,29 +124,35 @@
c(decl, scope);
}
},
AssignmentExpression: function (node, st) {
AssignmentExpression: function (node, st, c) {
var parent = st.parent, scope = st.scope;
if(node.left && node.left.object && node.left.object.type == "ThisExpression") {
var parent = st.parent, scope = st.scope;
var type = infer.expressionType({node: node.left, state: scope});
addChildNode(node.left, type, parent);
var child = addChildNode(node.left, type, parent);
st = {parent: child, scope: scope};
}
c(node.left, st, "Pattern");
c(node.right, st, "Expression");
},
Function: function(node, st, c) {
var parent = st.parent, scope = st.scope, type = infer.expressionType({node: node.id && node.type != "FunctionExpression" ? node.id : node, state: scope});
if (node.id) {
if (node.id.name == "✖") {
parent = addChildNode(node.body, type, parent);
parent.name = ANONYMOUS_FN;
if (parent.type == "?") parent.type = "fn()";
parent = addAnonymousFn(node, type, parent);
} else {
parent = addChildNode(node.id, type, parent);
}
} else {
var parentNode = infer.parentNode(node, node.sourceFile.ast);
if (!parentNode || (parentNode.type != "Property" && parentNode.type != "VariableDeclarator" && parentNode.type != "MethodDefinition" && parentNode.type != "AssignmentExpression")) parent = addAnonymousFn(node, type, parent);
}
var scope = {parent: parent, scope: node.body.scope ? node.body.scope: node.scope};
if (node.id) c(node.id, scope);
for (var i = 0; i < node.params.length; ++i)
c(node.params[i], scope);
c(node.body, scope, "ScopeBody");
fillTypes(type, parent, []);
},
Property: function (node, st, c) {
var parent = st.parent, scope = st.scope;
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"test": "node ./test/all.js"
},
"dependencies": {
"tern": "^0.15.0",
"acorn": "^2.4.0"
"tern": "^0.16.0",
"acorn": "^2.6.4"
},
"devDependencies": {
"test": ">=0.0.5",
Expand Down
65 changes: 65 additions & 0 deletions test/function_declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,69 @@ exports['test anonymous function'] = function() {
});
}

exports['test all function case'] = function() {

util.assertOutline("function f() {var i=0;}", {"outline":
[
{"name":"f","type":"fn()","start":9,"end":10,"children":
[
{"name":"i","type":"number","start":18,"end":19}
]
}
]
});

util.assertOutline("var f = function () {var i=0;}", {"outline":
[
{"name":"f","type":"fn()","start":4,"end":5,"children":
[
{"name":"i","type":"number","start":25,"end":26}
]
}
]
});

util.assertOutline("function g() {this.f = function () {var i=0;}}", {"outline":
[
{"name":"g","type":"fn()","start":9,"end":10,"children":
[
{"name":"f","type":"?","start":14,"end":20,"children":
[
{"name":"i","type":"number","start":40,"end":41}
]
}
]
}
]
});

util.assertOutline("var o = {'f': function () {var i=0;}}", {"outline":
[
{"name":"o","type":"o","start":4,"end":5,"children":
[
{"name":"f","type":"fn()","start":9,"end":12,"children":
[
{"name":"i","type":"number","start":31,"end":32}
]
}
]
}
]
});

util.assertOutline("class c {f() {var i=0;}}", {"outline":
[
{"name":"c","type":"fn()","start":0,"end":24,"kind": "class", "children":
[
{"name":"f","type":"fn()","start":9,"end":10,"kind": "method", "children":
[
{"name":"i","type":"number","start":18,"end":19}
]
}
]
}
]
});
}

if (module == require.main) require("test").run(exports);

0 comments on commit c0a671a

Please sign in to comment.