aboutsummaryrefslogtreecommitdiff
path: root/test.js
blob: 13955885748d4514aa3e95282906411de966ed00 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
var acorn = require('acorn/dist/acorn_loose');
var walk = require("acorn/dist/walk");

window.onload = function () {

	document.getElementById("parse").addEventListener("click",function(){
	
		var ast = acorn.parse_dammit(document.getElementById("input").value).body[0];
		document.getElementById("output").innerHTML = JSON.stringify(ast, null, "\t"); // Indented with tab
		console.log(ast);

		var flag = false;
		var amtloops = 0;

		walk.recursive(ast, null, {
			Literal(node, state, c) {
				console.log("literal");
			},
			Identifier(node, state, c){
				if(state.called === true){
					console.log("calls '"+node.name+"'");
				}
			},
			// The beggining of an "object chain" (obj1.obj2().property.value......)
			ExpressionStatement(node, state, c) {
				c(node["expression"],{});
			},
			CallExpression(node, state, c) {
				console.log("CallExpression");
				c(node["callee"],{"called":true});
				for(var i = 0; i < node.arguments.length; i++){
					console.log(node.arguments[i]);
					c(node.arguments[i],{});
				}
			},
			MemberExpression(node, state, c){
				if(state.called === true){
					console.log("calls '"+node.property.name+"'");
				}
				c(node["object"],{});
			},
			ArrayExpression(node, state, c){
				var len = 0;
				try{
					var temp = script.substring(node["start"],node["end"]);
					len = JSON.parse(temp).length;
				} catch(e){
					console.warn("Invalid array?");
					len = 99;
				}
				if(len > 50){
					console.log("%c NONTRIVIAL: Array longer than 50 elements. ("+len+")","color:red");
					flag = false;
				}

			},
			ForInStatement(node, state, c){
				console.log("ForInStatement");
				amtloops++;
			},
			ForStatement(node, state, c){
				console.log("ForStatement");
				amtloops++;
			},
			DoWhileStatement(node, state, c){
				console.log("DoWhileStatement");
				amtloops++;
			},
			WhileStatement(node, state, c){
				console.log("WhileStatement");
				amtloops++;
			},
			IfStatement(node, state, c){
				console.log("IfStatement");
				c(node.test,{});
				c(node.consequent,{});
				amtloops++;
			},
			SwitchStatement(node, state, c){
				console.log("SwitchStatement");
				amtloops++;
			}

		});
		if(flag == false){
			return false;
		}
		if(amtloops > 3){
			console.log("%c NONTRIVIAL: Too many loops/conditionals.","color:red");
			return false;
		}
	});

}