This repository was archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathannotations.js
More file actions
173 lines (141 loc) · 3.73 KB
/
annotations.js
File metadata and controls
173 lines (141 loc) · 3.73 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import {isFunction} from './util';
// This module contains:
// - built-in annotation classes
// - helpers to read/write annotations
// ANNOTATIONS
// A built-in token.
// Used to ask for pre-injected parent constructor.
// A class constructor can ask for this.
class SuperConstructor {}
// A built-in scope.
// Never cache.
class TransientScope {}
class Inject {
constructor(...tokens) {
this.tokens = tokens;
this.isPromise = false;
this.isLazy = false;
}
}
class InjectPromise extends Inject {
constructor(...tokens) {
this.tokens = tokens;
this.isPromise = true;
this.isLazy = false;
}
}
class InjectLazy extends Inject {
constructor(...tokens) {
this.tokens = tokens;
this.isPromise = false;
this.isLazy = true;
}
}
class Provide {
constructor(token) {
this.token = token;
this.isPromise = false;
}
}
class ProvidePromise extends Provide {
constructor(token) {
this.token = token;
this.isPromise = true;
}
}
// HELPERS
// Append annotation on a function or class.
// This can be helpful when not using ES6+.
function annotate(fn, annotation) {
fn.annotations = fn.annotations || [];
fn.annotations.push(annotation);
}
// Read annotations on a function or class and return whether given annotation is present.
function hasAnnotation(fn, annotationClass) {
if (!fn.annotations || fn.annotations.length === 0) {
return false;
}
for (var annotation of fn.annotations) {
if (annotation instanceof annotationClass) {
return true;
}
}
return false;
}
function hasParameterAnnotation(fn, AnnotationClass) {
if (!fn.parameters || fn.parameters.length === 0) {
return false;
}
for (var parameterAnnotations of fn.parameters) {
for (var annotation of parameterAnnotations) {
if (annotation instanceof AnnotationClass) {
return true;
}
}
}
return false;
}
// Read annotations on a function or class and collect "interesting" metadata:
function readAnnotations(fn) {
var collectedAnnotations = {
// Description of the provided value.
provide: {
token: null,
isPromise: false
},
// List of parameter descriptions.
// A parameter description is an object with properties:
// - token (anything)
// - isPromise (boolean)
// - isLazy (boolean)
params: []
};
if (fn.annotations && fn.annotations.length) {
for (var annotation of fn.annotations) {
if (annotation instanceof Inject) {
collectedAnnotations.params = annotation.tokens.map((token) => {
return {token: token, isPromise: annotation.isPromise, isLazy: annotation.isLazy};
});
}
if (annotation instanceof Provide) {
collectedAnnotations.provide.token = annotation.token;
collectedAnnotations.provide.isPromise = annotation.isPromise;
}
}
}
// Read annotations for individual parameters.
if (fn.parameters) {
fn.parameters.forEach((param, idx) => {
for (var paramAnnotation of param) {
// Type annotation.
if (isFunction(paramAnnotation) && !collectedAnnotations.params[idx]) {
collectedAnnotations.params[idx] = {
token: paramAnnotation,
isPromise: false,
isLazy: false
};
} else if (paramAnnotation instanceof Inject) {
collectedAnnotations.params[idx] = {
token: paramAnnotation.tokens[0],
isPromise: paramAnnotation.isPromise,
isLazy: paramAnnotation.isLazy
};
}
}
});
}
return collectedAnnotations;
}
export {
annotate,
hasAnnotation,
hasParameterAnnotation,
readAnnotations,
SuperConstructor,
TransientScope,
Inject,
InjectPromise,
InjectLazy,
Provide,
ProvidePromise
};