Skip to content

Commit

Permalink
Fix #7 + add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
angelozerr committed Dec 4, 2015
1 parent c0a671a commit 9d56877
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 32 deletions.
10 changes: 5 additions & 5 deletions demos/outline.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ <h1>Demo with Outline Tern plugin </h1>
<table style="width: 100%;height: 500px">
<tr>
<td valign="top" style="width: 50%">
<textarea id="code" name="code">var a, b, ;
function f() {
};
<textarea id="code" name="code">function A() {

var g = function() {
var g1;
}

A.hello = function () {
var j = 0;
};
</textarea>
</td>
<td valign="top">
Expand Down
42 changes: 19 additions & 23 deletions outline.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,30 +74,23 @@
});*/
});

function getChild(parent, name) {
var children = parent.children;
function getChild(name, 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 getChildProperty(node, parent) {
if (node.property) {
var p = getChild(node.object.name, parent)
if (p) {
return getChildProperty(node.property, p.children)
}
})
} else if (node.name) {
return getChild(node.name, parent);
}
}

function addAnonymousFn(node, type, parent) {
Expand Down Expand Up @@ -126,10 +119,12 @@
},
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;
if(node.left && node.left.object/* && node.left.object.type == "ThisExpression"*/) {
var left = node.left.property ? node.left.property : node.left.object;
var p = getChildProperty(node.left.property ? node.left.object : node.left, parent);
var parent = p ? p: parent, scope = st.scope;
var type = infer.expressionType({node: node.left, state: scope});
var child = addChildNode(node.left, type, parent);
var child = addChildNode(left, type, parent);
st = {parent: child, scope: scope};
}
c(node.left, st, "Pattern");
Expand All @@ -145,20 +140,21 @@
}
} 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);
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;
var type = node.value && node.value.name != "✖" ? infer.expressionType({node: node.value, state: scope}) : null;
parent = addChildNode(node.key, type, parent);
var scope = {parent: parent, scope: node.scope};
var scope = {parent: parent, scope: scope};
if (node.computed) c(node.key, scope, "Expression");
c(node.value, scope, "Expression");
},
Expand All @@ -168,7 +164,7 @@
},
ClassDeclaration: function (node, st, c) {
var parent = st.parent, scope = st.scope, type = infer.expressionType({node: node.id ? node.id : node, state: scope});
var obj = addChildNode(node, type, parent);
var obj = addChildNode(node.id ? node.id : node, type, parent);
obj.kind = "class";
var scope = {parent: obj, scope: st.scope};
if (node.superClass) c(node.superClass, scope, "Expression");
Expand Down
4 changes: 2 additions & 2 deletions test/class_declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ exports['test simple class declaration'] = function() {
"}"+
"}",
{"outline":
[{"name":"Cat","type":"fn(name: ?)","start":0,"end":103,"kind":"class",
[{"name":"Cat","type":"fn(name: ?)","start":6,"end":9,"kind":"class",
"children":[{"name":"constructor","type":"fn(name: ?)","start":12,"end":23,"kind":"constructor",
"children":[{"name":"name","type":"?","start":31,"end":40}]},
"children":[{"name":"name","type":"?","start":36,"end":40}]},
{"name":"speak","type":"fn()","start":49,"end":54,"kind":"method"}
]}
]});
Expand Down
19 changes: 17 additions & 2 deletions test/function_declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ exports['test all function case'] = function() {
[
{"name":"g","type":"fn()","start":9,"end":10,"children":
[
{"name":"f","type":"?","start":14,"end":20,"children":
{"name":"f","type":"?","start":19,"end":20,"children":
[
{"name":"i","type":"number","start":40,"end":41}
]
Expand All @@ -113,7 +113,7 @@ exports['test all function case'] = function() {

util.assertOutline("class c {f() {var i=0;}}", {"outline":
[
{"name":"c","type":"fn()","start":0,"end":24,"kind": "class", "children":
{"name":"c","type":"fn()","start":6,"end":7,"kind": "class", "children":
[
{"name":"f","type":"fn()","start":9,"end":10,"kind": "method", "children":
[
Expand All @@ -126,4 +126,19 @@ exports['test all function case'] = function() {
});
}

exports['test prototype properties function'] = function() {
util.assertOutline("function Juan() {};Juan.prototype = Object.create(window);Juan.prototype.constructor = Juan;Juan.prototype.hello = function () {var j = 0;};",
{"outline":
[{"name":"Juan","type":"fn()","start":9,"end":13,
"children":[{"name":"prototype","type":"?","start":24,"end":33,
"children":[{"name":"constructor","type":"?","start":73,"end":84},
{"name":"hello","type":"?","start":107,"end":112,
"children":[{"name":"j","type":"number","start":132,"end":133}]
}
]
}]
}]}
);
}

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

0 comments on commit 9d56877

Please sign in to comment.