-
Notifications
You must be signed in to change notification settings - Fork 37
/
preconditions.ts
66 lines (56 loc) · 2.14 KB
/
preconditions.ts
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
import { Precondition } from '../core/join_point';
import { MethodSelector, PropertySelector } from './selectors';
import { weave } from '../core/wove';
export class MethodPrecondition implements Precondition {
constructor(private selector: MethodSelector) {
// Automatically weave classes
const classes = selector.classes || [];
for (const c of classes) {
weave(c);
}
}
assert({ classDefinition, methodName }: { classDefinition: any; methodName: string }): boolean {
const s = this.selector;
const className = classDefinition.name;
const matchClass =
(!s.classNamePattern && !s.classes) ||
(s.classNamePattern && s.classNamePattern.test(className)) ||
(s.classes && s.classes.some(c => c === classDefinition));
if (!matchClass) {
return false;
}
return !!(
(!s.methodNamePattern && !s.methods) ||
(s.methodNamePattern && s.methodNamePattern.test(methodName)) ||
(s.methods && s.methods.some(m => classDefinition.prototype[methodName] === m))
);
}
}
export class MemberPrecondition implements Precondition {
constructor(private selector: PropertySelector) {}
assert({ classDefinition, fieldName }: { classDefinition: any; fieldName: string }): boolean {
const s = this.selector;
const className = classDefinition.name;
const matchClass =
(!s.classNamePattern && !s.classes) ||
(s.classNamePattern && s.classNamePattern.test(className)) ||
(s.classes && s.classes.some(c => c === classDefinition));
if (!matchClass) {
return false;
}
const d = Object.getOwnPropertyDescriptor(classDefinition.prototype, fieldName);
return !!(
(!s.propertyNamePattern && !s.properties) ||
(s.propertyNamePattern && s.propertyNamePattern.test(fieldName)) ||
(s.properties &&
s.properties.some(f => {
if (!f) {
throw new Error(
'Got invalid property descriptor for a member selector. Use Object.getOwnPropertyDescriptor(fn.prototype, name) if you are using field selectors.'
);
}
return d && (d.get === f.get && d.set === f.set);
}))
);
}
}