-
Notifications
You must be signed in to change notification settings - Fork 218
/
Copy pathwrap.js
67 lines (52 loc) · 1.86 KB
/
wrap.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
'use strict';
function wrap(ctx) {
/* eslint-disable new-cap, no-extend-native, no-param-reassign, no-native-reassign, no-undef */
var p;
var savedPrototype;
function isInUserCode(stack) {
return stack[0].getFileName().substring(0, ctx.mainProgram.length)
=== ctx.mainProgram;
}
ctx.usedPromise = false;
ctx.usedReject = false;
ctx.usedRejectWithError = false;
require('es6-promise');
p = Promise;
Promise = function Promise(func) {
var stack = ctx.$captureStack(Promise);
var inUserCode = isInUserCode(stack);
var transformedFunc = function (fulfill, reject) {
func(fulfill, function (err) {
ctx.usedReject = ctx.usedReject || inUserCode;
ctx.usedRejectWithError = ctx.usedRejectWithError || inUserCode && err instanceof Error;
reject(err);
});
};
ctx.usedPromise = ctx.usedPromise || inUserCode;
if (this instanceof Promise) {
return new p(transformedFunc);
}
return p(transformedFunc);
};
savedPrototype = {
then: p.prototype.then,
};
Promise.prototype = p.prototype;
ctx.usedPrototypeThen = false;
ctx.usedPrototypeThenFirstCb = false;
ctx.usedPrototypeThenSecondCb = false;
Promise.prototype.then = function (onFulfilled, onRejected) {
var stack = ctx.$captureStack(Promise.prototype.then);
var inUserCode = isInUserCode(stack);
ctx.usedPrototypeThen = ctx.usedPrototypeThen || inUserCode;
ctx.usedPrototypeThenFirstCb =
ctx.usedPrototypeThenFirstCb || inUserCode && typeof onFulfilled === 'function';
ctx.usedPrototypeThenSecondCb =
ctx.usedPrototypeThenSecondCb || inUserCode && typeof onRejected === 'function';
return savedPrototype.then.apply(this, arguments);
};
/* eslint-enable new-cap, no-extend-native, no-param-reassign, no-undef */
}
wrap.wrapSubmission = true;
wrap.wrapSolution = true;
module.exports = wrap;