-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcontains-this.js
68 lines (56 loc) · 1.41 KB
/
contains-this.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
'use strict';
const { parseScript } = require('shift-parser');
const { thunkedReduce, ThunkedMonoidalReducer } = require('..');
class ContainsThisReducer extends ThunkedMonoidalReducer {
static containsThis(fn) {
if (fn.type !== 'FunctionExpression' && fn.type !== 'FunctionDeclaration') {
throw new TypeError('ContainsThisReducer must be passed a function node');
}
return thunkedReduce(new this, fn.params) || thunkedReduce(new this, fn.body);
}
constructor() {
super({
empty: () => false,
concatThunk: (a, b) => a || b(),
});
/* Equivalently:
super({
empty: () => false,
isAbsorbing: a => a,
concat: (a, b) => a || b,
});
*/
}
reduceThisExpression(node) {
return true;
}
reduceFunctionDeclaration() {
return false;
}
reduceFunctionExpression() {
return false;
}
reduceGetter(node, { name, body }) {
return name();
}
reduceSetter({ name, param, body }) {
return name();
}
reduceMethod(node, { name, params, body }) {
return name();
}
}
let functionWithoutThis = parseScript(`
function f() {
return function inner() {
return this;
}
}
`).statements[0];
console.log(ContainsThisReducer.containsThis(functionWithoutThis));
let functionWithThis = parseScript(`
function f() {
return () => this;
}
`).statements[0];
console.log(ContainsThisReducer.containsThis(functionWithThis));