-
Notifications
You must be signed in to change notification settings - Fork 465
/
Copy pathtest.js
267 lines (260 loc) · 7.38 KB
/
test.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*jslint node*/
import fs from "fs";
import jslint from "./jslint.js";
function assertOrThrow(passed, msg) {
/*
* this function will throw <msg> if <passed> is falsy
*/
if (!passed) {
throw new Error(msg);
}
}
function noop() {
/*
* this function will do nothing
*/
return;
}
(function testCaseJslintCli() {
/*
* this function will test jslint's cli handling-behavior
*/
process.exit = function (exitCode) {
assertOrThrow(!exitCode, exitCode);
};
jslint("", {
cli_mode: true,
file: "jslint.js"
});
jslint("", {
cli_mode: true,
// suppress error
console_error: noop,
file: "syntax_error.js",
option: {
debug: true
},
source: "syntax error"
});
jslint("", {
cli_mode: true,
file: "aa.html",
source: "<script>\nlet aa = 0;\n</script>\n"
});
}());
(function testCaseJslintMisc() {
/*
* this function will test jslint's misc handling-behavior
*/
// test assertOrThrow's throw handling-behavior
try {
assertOrThrow(undefined, new Error());
} catch (ignore) {}
}());
(function testCaseJslintOption() {
/*
* this function will test jslint's option handling-behavior
*/
assertOrThrow(jslint([""], {
bitwise: true,
browser: true,
convert: true,
couch: true,
debug: true,
devel: true,
eval: true,
for: true,
fudge: true,
getset: true,
long: true,
node: true,
single: true,
this: true,
white: true
}).warnings.length === 0);
assertOrThrow(jslint("", {
test_internal_error: true
}).warnings.length === 1);
}());
(function testCaseJslintCodeValidate() {
/*
* this function will validate each code is valid in jslint
*/
Object.values({
array: [
"new Array(0);"
],
async_await: [
"async function aa() {\n await aa();\n}"
],
date: [
"Date.getTime();",
"let aa = aa().getTime();",
"let aa = aa.aa().getTime();"
],
directives: [
"#!\n/*jslint browser:false, node*/\n\"use strict\";",
"/*jslint bitwise*/\nlet aa = aa | 0;",
"/*jslint browser*/\n;",
"/*jslint debug*/\n",
"/*jslint devel*/\ndebugger;",
"/*jslint eval*/\nnew Function();\neval();",
"/*jslint getset*/\nlet aa = {get aa() {\n return;\n}};",
"/*jslint getset*/\nlet aa = {set aa(aa) {\n return aa;\n}};",
"/*jslint this*/\nlet aa = this;",
"/*jslint unordered*/\nlet {bb, aa} = 0;",
"/*jslint white*/\n\t",
"/*property aa bb*/"
],
fart: [
"function aa() {\n return () => 0;\n}"
],
json: [
"{\"aa\":[[],-0,null]}"
],
label: [
"function aa() {\nbb:\n while (true) {\n if (true) {\n"
+ " break bb;\n }\n }\n}"
],
loop: [
"function aa() {\n do {\n aa();\n } while (aa());\n}"
],
module: [
"export default Object.freeze();",
"import {aa, bb} from \"aa\";\naa(bb);",
"import {} from \"aa\";",
"import(\"aa\").then(function () {\n return;\n});"
],
number: [
"let aa = 0.0e0;",
"let aa = 0b0;",
"let aa = 0o0;",
"let aa = 0x0;"
],
optional_chaining: [
"let aa = aa?.bb?.cc;"
],
property: [
"let aa = aa[`!`];"
],
regexp: [
"function aa() {\n return /./;\n}",
"let aa = /(?!.)(?:.)(?=.)/;"
],
ternary: [
"let aa = (\n aa()\n ? 0\n : 1\n) "
+ "&& (\n aa()\n ? 0\n : 1\n);"
],
var: [
"let [...aa] = [...aa];",
"let [\n aa, bb = 0\n] = 0;",
"let {aa, bb} = 0;",
"let {\n aa: bb\n} = 0;"
]
}).forEach(function (codeList) {
codeList.forEach(function (code) {
let warnings;
warnings = jslint(code).warnings;
assertOrThrow(
warnings.length === 0,
JSON.stringify([code, warnings])
);
});
});
}());
(async function testCaseJslintWarningsValidate() {
/*
* this function will validate each jslint <warning> is raised with given
* malformed <code>
*/
Array.from(String(
await fs.promises.readFile("jslint.js", "utf8")
).matchAll(new RegExp((
"\\s*?"
+ "(\\/\\/\\s*?cause:.*?\\n(?:\\/\\/.*?\\n)*?)"
+ "(\\s*?^[^\\/].*?(?:\\n\\s*?\".*?)?$)"
), "gm"))).forEach(function ([
match0, causeList, warning
]) {
let cause0;
let expectedWarningCode;
let fnc;
// debug match0
console.error(match0.trim().replace((/\n\n/g), "\n"));
assertOrThrow(
match0.indexOf("\n\n" + causeList + "\n ") === 0,
JSON.stringify([
match0, causeList
], undefined, 4)
);
warning = warning.match(
"("
+ "at_margin"
+ "|expected_at"
+ "|left_check"
+ "|no_space_only"
+ "|one_space"
+ "|one_space_only"
+ "|semicolon"
+ "|stop"
+ "|stop_at"
+ "|warn"
+ "|warn_at"
+ ")"
+ "\\\u0028\\s*?\"?"
+ "(\\S[^\n\"]+)"
);
if (warning) {
expectedWarningCode = warning[2];
fnc = warning[1];
switch (fnc) {
case "at_margin":
case "expected_at":
expectedWarningCode = "expected_a_at_b_c";
break;
case "left_check":
expectedWarningCode = "unexpected_a";
break;
case "no_space_only":
expectedWarningCode = "unexpected_space_a_b";
break;
case "one_space":
case "one_space_only":
expectedWarningCode = "expected_space_a_b";
break;
case "semicolon":
expectedWarningCode = "expected_a_b";
break;
}
}
cause0 = "";
causeList.split(
/\/\/\u0020cause:[\n|\u0020]/
).slice(1).forEach(function (cause) {
assertOrThrow(cause === cause.trim() + "\n", JSON.stringify(cause));
cause = (
expectedWarningCode === "too_long"
? "//".repeat(100)
: cause[0] === "\""
? JSON.parse(cause)
: cause.replace((
/^\/\/\u0020/gm
), "")
);
// Assert causes are sorted.
assertOrThrow(cause0 < cause, JSON.stringify([
cause0, cause
], undefined, 4));
cause0 = cause;
// Assert expectedWarningCode from cause.
assertOrThrow(
jslint(cause).warnings.some(function ({
code
}) {
return code === expectedWarningCode;
}) || !expectedWarningCode,
"\n" + cause.trim()
);
});
});
}());