-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathyaap.js
149 lines (112 loc) · 5.15 KB
/
yaap.js
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/** @license MIT License (c) copyright 2013 original author or authors */
/**
* A javascript annotation processor
*
* Licensed under the MIT License at:
* http://www.opensource.org/licenses/mit-license.php
*
* @author Peter Mucha
*
* @version 0.1.4
*/
"use strict";
(function (define) {
define(["underscore", "./registry", "./plugins/NotNullProcessor", "./plugins/DefaultProcessor", './parser/PanPG_util', './parser/ECMA5Parser_min', "./parser/walker"],
function (_, registry, NotNullProcessor, DefaultProcessor, PanPG_util, es5, walker) {
//default annotations are registered here
registry.register([
NotNullProcessor,
DefaultProcessor
]);
var options = {
shallow: false,
debug: false
};
function callProcessors(obj, fnDescription, config) {
//process function-annotations
_(fnDescription.annotations).each(function (annotation) {
_(registry.getProcessors(annotation.name)).each(function (processor) {
if (_(processor).has("processFunction"))
processor.processFunction(obj, fnDescription, annotation.parameters, config);
else
console.log("Function-annotations are not supported for: " + annotation.name);
});
});
//process parameter annotations
var parameterArray = {};
_(fnDescription.parameters).each(function (parameter) {
_(parameter.annotations).each(function (annotation) {
if (parameterArray[annotation.name] === undefined)
parameterArray[annotation.name] = [];
var entry = {
name: parameter.name,
index: parameter.index,
annotation: annotation
};
parameterArray[annotation.name].push(entry);
});
});
//call processors on annotated parameters
_(parameterArray).chain().keys().each(function (annotationName) {
_(registry.getProcessors(annotationName)).each(function (processor) {
if (_(processor).has("processParameter"))
processor.processParameter(obj, fnDescription, parameterArray[annotationName], config);
else
console.log("Parameter-annotations are not supported for: " + annotationName);
});
}).value();
}
function processFunction(obj, f, config) {
var source = obj[f].toString();
source = source.substring(0, source.indexOf("{")); //strip body //TODO: this is not secure, if comments contain '{'
//early bail-out: no annotation sign => skip
if (source.indexOf('@') < 0)
return;
if (options.debug)
console.log("yaap: processing function " + f);
var ast = es5.Program(source);
//console.log(PanPG_util.showTree(ast));
var fnDescriptions = null;
try {
fnDescriptions = PanPG_util.treeWalker(walker, ast);
} catch (e) {
console.error(e);
throw e;
}
_(fnDescriptions).each(function (fnDescription) {
fnDescription.name = f;
callProcessors(obj, fnDescription, config);
});
}
function processClassAnnotation(obj, f, config) {
//process function-annotations
_(registry.getProcessors(f)).each(function (processor) {
if (_(processor).has("processClass"))
processor.processClass(obj, obj[f], config);
else
console.log("Class-annotations are not supported for: " + f);
});
}
return {
register: function (processor) {
registry.register([processor]);
},
setOptions: function (opts) {
options = _.defaults(opts, options);
},
process: function (obj, config) {
for (var f in obj) {
if (_(obj[f]).isFunction() && (!options.shallow || _(obj.__proto__).has(f)))
processFunction(obj, f, config);
else if (typeof f === 'string' && f[0] === '@') //a string that starts with @
processClassAnnotation(obj, f, config);
}
return obj;
}
};
});
})(typeof define == 'function' ? define : function (deps, factory) {
module.exports = factory.apply(this, deps.map(function (x) {
return require(x);
}));
});