-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
173 lines (144 loc) · 5.28 KB
/
index.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'use strict';
var StackParser = require('error-stack-parser');
var env = typeof process !== 'undefined' && process.env.NODE_ENV;
var isDevelopment = !env || env === 'dev' || env === 'development';
var showModuleComplains = typeof process !== 'undefined' && Boolean(process.env.SHOW_MODULE_COMPLAINS);
var showNestedComplains = typeof process !== 'undefined' && Boolean(process.env.SHOW_NESTED_COMPLAINS);
var logger = typeof console !== 'undefined' && console.warn && console;
var cwd = typeof process !== 'undefined' && process.cwd && process.cwd() + '/' || '';
var linebreak = typeof process !== 'undefined' && 'win32' === process.platform ? '\r\n' : '\n';
var newline = /(\r\n|\r|\n)/g;
var slice = [].slice;
var ignoredLocation = "[ignore]";
var hits = {};
complain = isDevelopment ? complain : noop;
complain.method = isDevelopment ? method : noop;
complain.fn = isDevelopment ? fn : noopReturn;
complain.log = log;
complain.stream = typeof process !== 'undefined' && process.stderr;
complain.silence = false;
complain.color = complain.stream && complain.stream.isTTY;
complain.colors = { warning:'\x1b[31;1m', notice:'\x1b[33;1m', message:false, location:'\u001b[90m' };
complain.getModuleName = getModuleName;
/* istanbul ignore next */
if (typeof module !== 'undefined' && module.exports) {
module.exports = complain;
} else if(typeof window !== 'undefined') {
window.complain = complain;
}
function complain() {
var options;
var location;
var locationIndex;
var headingColor;
var heading;
var level;
var args = arguments;
if(complain.silence) return;
if(typeof args[args.length-1] === 'object') {
options = args[args.length-1];
args = slice.call(args, 0, -1);
} else {
options = {};
}
level = options.level || 2;
heading = options.heading || (level == 2 ? "WARNING!!" : "NOTICE");
headingColor = options.headingColor || (level == 2 ? complain.colors.warning : complain.colors.notice);
// Default to the location of the call to the deprecated function
locationIndex = options.locationIndex == null ? 1 : options.locationIndex;
// When the user sets location to false,
// We will use the location of the call to complain()
// To limit the log to only occurring once
if(options.location === false) {
locationIndex = 0;
}
location = options.location || getLocation(locationIndex);
var moduleName = complain.getModuleName(location);
if (moduleName && !showModuleComplains) {
if (!hits[moduleName]) {
var output = format("NOTICE", complain.colors.notice);
output += linebreak + format('The module ['+moduleName+'] is using deprecated features.', complain.colors.message);
output += linebreak + format('Run with process.env.SHOW_MODULE_COMPLAINS=1 to see all warnings.', complain.colors.message);
complain.log(linebreak + output + linebreak);
hits[moduleName] = true;
}
return;
}
/* istanbul ignore next */
// Location is only missing in older browsers.
if(location) {
if(hits[location] || location === ignoredLocation) return;
else hits[location] = true;
}
var output = format(heading, headingColor);
for(var i = 0; i < args.length; i++) {
output += linebreak + format(args[i], complain.colors.message);
}
if(options.location !== false && location) {
output += linebreak + format(' at '+location.replace(cwd, ''), complain.colors.location);
}
complain.log(linebreak + output + linebreak);
};
function method(object, methodName) {
var originalMethod = object[methodName];
var args = slice.call(arguments, 2);
object[methodName] = function() {
complain.apply(null, args);
return originalMethod.apply(this, arguments);
};
}
function fn(original) {
var args = slice.call(arguments, 1);
return function() {
complain.apply(null, args);
return original.apply(this, arguments);
}
}
function log(message, color) {
var formatted = format(message, color);
if(complain.stream) {
complain.stream.write(formatted+linebreak);
} else if(logger) {
logger.warn(formatted);
}
}
function format(message, color) {
return color && complain.color ? color + message + '\x1b[0m' : message;
}
function getLocation(locationIndex) {
var location = '';
var targetIndex = locationIndex + 2;
/**
* Stack index descriptions.
*
* 0: In getLocation(), the call to new Error()
* 1: In complain(), the call to getLocation()
* 2: In the deprecated function, the call to complain()
* 3: The call to the deprecated function (THIS IS THE DEFAULT)
*/
try {
var locations = StackParser.parse(new Error()).map(function(frame) {
return frame.fileName+':'+frame.lineNumber+':'+frame.columnNumber;
});
if (!showNestedComplains) {
for (var i = locations.length-1; i > targetIndex; i--) {
if (hits[locations[i]]) {
return ignoredLocation;
}
}
}
location = locations[targetIndex];
} catch(e) {}
return location;
}
function getModuleName(location) {
var locationParts = location.replace(cwd, '').split(/\/|\\/g);
for(var i = locationParts.length-1; i >= 0; i--) {
if (locationParts[i] === 'node_modules') {
var moduleName = locationParts[i+1];
return (moduleName[0] === '@') ? moduleName+'/'+locationParts[i+2] : moduleName;
}
}
}
function noop(){};
function noopReturn(r) { return r; };