-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckr-hook.js
More file actions
605 lines (562 loc) · 21.9 KB
/
checkr-hook.js
File metadata and controls
605 lines (562 loc) · 21.9 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
'use strict';
/********* `code` support *********/
/** Copied from the typescript output folder. */
Object.defineProperty(exports, "__esModule", { value: true });
exports.code = exports.escapeRegExp = void 0;
// NOTE: 2 + 2 is still a literal. Base literals refers to unchained literals.
const baseLiterals = [
`"[\\s\\S]*?"`,
"'[\\s\\S]*?'",
'`[\\s\\S]*?`',
'-?\\d+([\\w\\.]*\\d*)*',
`\/[\\s\\S]*?\/`,
'true',
'false',
'NaN',
'undefined',
'null'
];
const baseLiteralRegex = `(${baseLiterals.join("|")})`;
const unitaryOperators = ['++', '--', '~'];
const binaryOperators = [
`+`,
'-',
`*`,
`**`,
`/`,
'%',
'=',
'==',
'===',
'!=',
'!==',
'>',
'<',
'>=',
'<=',
'&',
`|`,
'^',
'<<',
'>>',
'>>>'
];
// Excludes ternaries.
const operatorRegex = `(${unitaryOperators.map(escapeRegExp).concat(binaryOperators.map(escapeRegExp)).join("|")})`;
// Includes "future" reserved keywords.
const keywords = [
'break',
'case',
'catch',
'class',
'const',
'continue',
'debugger',
'default',
'delete',
'do',
'else',
'export',
'extends',
'finally',
'for',
'function',
'if',
'import',
'in',
'instanceof',
'new',
'return',
'super',
'switch',
'this',
'throw',
'try',
'typeof',
'var',
'void',
'while',
'with',
'yield',
'enum',
'implements',
'interface',
'let',
'package',
'private',
'protected',
'public',
'static',
'yield',
'await'
];
const keywordRegex = `(${keywords.join('|')})`;
const capturedVariableRegex = `(?!(?:do|if|in|for|let|new|try|var|case|else|enum|eval|false|null|this|true|void|with|break|catch|class|const|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|NaN|undefined|instanceof)$)([$A-Z_a-z]+[$A-Z_a-z0-9]*)`;
const variablePrefix = "_";
const literalPrefix = "__";
const operatorPrefix = "___";
const keywordPrefix = "____";
const blockPrefix = "_____";
function createNamedVariableRegex(name) {
// Greatly trimmed down and slightly modified from https://stackoverflow.com/questions/1661197/what-characters-are-valid-for-javascript-variable-names
return `(?!(?:do|if|in|for|let|new|try|var|case|else|enum|eval|false|null|this|true|void|with|break|catch|class|const|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|NaN|undefined|instanceof)$)(?<${name}>[$A-Z_a-z]+[$A-Z_a-z0-9]*)`;
}
function createNamedLiteralRegex(name) {
return `(?<${name}>${baseLiteralRegex})`;
}
function createNamedOperatorRegex(name) {
return `(?<${name}>${operatorRegex})`;
}
function createNamedKeywordRegex(name) {
return `(?<${name}>${keywordRegex})`;
}
// Copied from MDN docs.
function escapeRegExp(theString) {
return theString.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
exports.escapeRegExp = escapeRegExp;
function uniqueCaptureGroupName() {
// https://stackoverflow.com/a/57593036/16617265
return (new Date()).getTime().toString(36) + Math.random().toString(36).slice(2);
}
// Example usages:
// code`if ($a == $b) { return $a; }`;
// code`$#operator($1);`;
function code(strings, ...expressions) {
// replaceAll polyfill.
String.prototype.replaceAll = function (stringOrRegex, replacement) {
return stringOrRegex instanceof RegExp ?
this.replace(new RegExp(stringOrRegex, stringOrRegex.flags.includes("g") ?
stringOrRegex.flags :
stringOrRegex.flags + "g"), // Warning: typical `replaceAll` throws in this scenario.
replacement) :
this.replace(new RegExp(escapeRegExp(stringOrRegex), 'g'), replacement);
};
let regexTranslation = strings[0];
for (let i = 0; i < expressions.length; i++)
regexTranslation += expressions[i] + strings[i + 1];
// Tokenize special syntax before whitespace is inserted.
regexTranslation = regexTranslation.replaceAll("$$$", ":🍇:");
regexTranslation = regexTranslation.replaceAll("$$", ":🍉:");
const variableTokens = [];
const literalTokens = [];
const operatorTokens = [];
const keywordTokens = [];
const regexTokens = [];
let match;
while ((match = regexTranslation.match(/\$[a-zA-Z]+[0-9_]*/))) {
variableTokens.push(match);
regexTranslation = regexTranslation.replace(match, " :🍊: ");
}
while ((match = regexTranslation.match(/\$[0-9]+/))) {
literalTokens.push(match);
regexTranslation = regexTranslation.replace(match, " :🍍: ");
}
while ((match = regexTranslation.match(/\$@([a-zA-Z]+[0-9_]*)?/))) {
operatorTokens.push(match[0]);
regexTranslation = regexTranslation.replace(match[0], " :🍎: ");
}
while ((match = regexTranslation.match(/\$#([a-zA-Z]+[0-9_]*)?/))) {
keywordTokens.push(match[0]);
regexTranslation = regexTranslation.replace(match[0], " :🍓: ");
}
while ((match = regexTranslation.match(/REGEX\(([\s\S]*?)\)/))) {
regexTokens.push(match[1]);
regexTranslation = regexTranslation.replace(match[0], " :🍈: ");
}
// Insert whitespace between literals, variables, and keywords.
// Makes it easier to deal with scenarios such as `a+10` or `++a`.
regexTranslation = regexTranslation.replaceAll(new RegExp(capturedVariableRegex, "g"), " $1 ");
regexTranslation = regexTranslation.replaceAll(new RegExp(`(${baseLiteralRegex})`, "g"), " $1 ");
regexTranslation = regexTranslation.replaceAll(new RegExp(`(${keywordRegex})`, "g"), " $1 ");
// Insert whitespace between `{}`, `()`, and `[]`.
// Makes it easier to deal with scenarios such as `if()` vs `if ()`.
regexTranslation = regexTranslation.replaceAll('(', ' ( ');
regexTranslation = regexTranslation.replaceAll(')', ' ) ');
regexTranslation = regexTranslation.replaceAll('{', ' { ');
regexTranslation = regexTranslation.replaceAll('}', ' } ');
regexTranslation = regexTranslation.replaceAll('[', ' [ ');
regexTranslation = regexTranslation.replaceAll(']', ' ] ');
// Insert whitespace between `;`
regexTranslation = regexTranslation.replaceAll(";", " ; ");
// Handles overlap between JavaScript and RegExp. For example, `+` needs to be escaped because it has a different meaning in RegExp.
regexTranslation = escapeRegExp(regexTranslation);
// Re-add tokens
let vi = 0;
while (regexTranslation.includes(":🍊:"))
regexTranslation = regexTranslation.replace(":🍊:", variableTokens[vi++]);
let li = 0;
while (regexTranslation.includes(":🍍:"))
regexTranslation = regexTranslation.replace(":🍍:", literalTokens[li++]);
let oi = 0;
while (regexTranslation.includes(":🍎:"))
regexTranslation = regexTranslation.replace(":🍎:", operatorTokens[oi++]);
let ki = 0;
while (regexTranslation.includes(":🍓:"))
regexTranslation = regexTranslation.replace(":🍓:", keywordTokens[ki++]);
// Replace special characters, eg `$a`, `$1`, `$#a`, `$@a` etc.
regexTranslation = replaceVariablesWithRegex(regexTranslation);
regexTranslation = replaceLiteralsWithRegex(regexTranslation);
regexTranslation = replaceOperatorsWithRegex(regexTranslation);
regexTranslation = replaceKeywordsWithRegex(regexTranslation);
while (regexTranslation.includes(":🍇:"))
regexTranslation = regexTranslation.replace(":🍇:", `(?<${blockPrefix}${uniqueCaptureGroupName()}>[\\s\\S]*)`);
while (regexTranslation.includes(":🍉:"))
regexTranslation = regexTranslation.replace(":🍉:", `(?<${blockPrefix}${uniqueCaptureGroupName()}>[\\s\\S]*?)`);
// Replace whitespace with lenient whitespace skips.
const lenientSkip = '[\\s]*';
regexTranslation = regexTranslation.replace(new RegExp('[\\s]+', 'g'), lenientSkip);
// Remove preceding and trailing whitespace matcher. Handles cases such as `if ($a == $b) { $$ }` doesn't match "if (foo == bar) { baz(); } \n\n "
if (regexTranslation.startsWith(lenientSkip))
regexTranslation = regexTranslation.replace('[\\s]*', "");
if (regexTranslation.endsWith(lenientSkip))
regexTranslation = regexTranslation.substring(0, regexTranslation.length - lenientSkip.length);
// Re-add "escape hatch" regex.
let ri = 0;
while (regexTranslation.includes(":🍈:"))
regexTranslation = regexTranslation.replace(":🍈:", regexTokens[ri++]);
const extendedRegex = new RegExp(regexTranslation, "g");
extendedRegex.matchAll = function (str) {
return [...str.matchAll(extendedRegex)].map(parseMatch);
};
extendedRegex.matchFirst = function (str) {
return [...str.matchAll(extendedRegex)].map(parseMatch)[0] || {
variables: [],
literals: [],
keywords: [],
operators: [],
blocks: [],
others: []
};
};
return extendedRegex;
}
exports.code = code;
function parseMatch(match) {
const results = {
variables: [],
literals: [],
keywords: [],
operators: [],
blocks: [],
others: []
};
// This can occur in situations where no named capture groups are provided.
// Thus there still is a "match", it's just empty.
if (match == null || match.groups == null)
return results;
for (const [kind, value] of Object.entries(match.groups)) {
// Warning: `if` order matters.
if (kind.startsWith(blockPrefix))
results.blocks.push(value);
else if (kind.startsWith(keywordPrefix))
results.keywords.push(value);
else if (kind.startsWith(operatorPrefix))
results.operators.push(value);
else if (kind.startsWith(literalPrefix))
results.literals.push(value);
else if (kind.startsWith(variablePrefix))
results.variables.push(value);
else
results.others.push(value);
}
return results;
}
/*
* Note to future maintainer/self:
* Please do not "DRY" up the below code with a function generator (unless it can be done well).
* Consider a code generator if it's too tedious to add more functions. (Or refactor the whole algorithm).
*/
// Converts code variable matchers such as $a, $b, $foo, etc with regex.
// Handles complex replacements such as repeated variable captures, eg `$a == $a`.
function replaceVariablesWithRegex(codeString) {
const captureRegex = /\$([a-zA-Z]+[0-9_]*)/g;
const matches = codeString.match(captureRegex);
if (matches == null)
return codeString;
const encounteredVariables = new Set();
let result = codeString;
for (const match of matches) {
const normalizedName = match.replace('$', '');
if (encounteredVariables.has(normalizedName)) {
// Replace match with back reference, eg `$foobar` becomes `\k<PREFIX_foobar>`.
result = result.replace(match, `\\k<${variablePrefix}${normalizedName}>`);
}
else {
// Replace match with variable regex, eg `$foobar` becomes `(?<PREFIX_foobar>VAR_REGEX_STRING)`.
result = result.replace(match, createNamedVariableRegex(`${variablePrefix}${normalizedName}`));
encounteredVariables.add(normalizedName);
}
}
return result;
}
// Converts code literal matchers such as $1, $2, $99, etc with regex.
// Handles complex replacements such as repeated literal captures, eg `$1 == $1`.
function replaceLiteralsWithRegex(codeString) {
const captureRegex = /\$([0-9]+)/g;
const matches = codeString.match(captureRegex);
if (matches == null)
return codeString;
const encounteredLiterals = new Set();
let result = codeString;
for (const match of matches) {
const normalizedName = match.replace('$', '');
if (encounteredLiterals.has(normalizedName)) {
// Replace match with back reference, eg `$1` becomes `\k<PREFIX_1>`.
result = result.replace(match, `\\k<${literalPrefix}${normalizedName}>`);
}
else {
// Replace match with literal regex, eg `$1` becomes `(?<PREFIX_1>LITERAL_REGEX_STRING)`.
result = result.replace(match, createNamedLiteralRegex(`${literalPrefix}${normalizedName}`));
encounteredLiterals.add(normalizedName);
}
}
return result;
}
// Converts code operator matchers such as $@, $@op, etc with regex.
// Handles complex replacements such as repeated operator captures, eg `$@op $a $@op`.
function replaceOperatorsWithRegex(codeString) {
const captureRegex = /\$@([a-zA-Z]+[0-9_]*)?/g;
const matches = codeString.match(captureRegex);
if (matches == null)
return codeString;
const encounteredOperators = new Set();
let result = codeString;
for (const match of matches) {
const normalizedName = match.replace('$@', '') || uniqueCaptureGroupName();
if (encounteredOperators.has(normalizedName)) {
// Replace match with back reference, eg `$@op` becomes `\k<PREFIX_op>`.
result = result.replace(match, `\\k<${operatorPrefix}${normalizedName}>`);
}
else {
// Replace match with literal regex, eg `$@op` becomes `(?<PREFIX_op>OPERATOR_REGEX_STRING)`.
result = result.replace(match, createNamedOperatorRegex(`${operatorPrefix}${normalizedName}`));
encounteredOperators.add(normalizedName);
}
}
return result;
}
// Converts code keyword matchers such as $#, $#a, $#keyword1, etc with regex.
// Handles complex replacements such as repeated keyword captures, eg `$#keyword1 { $$ } $#keyword1`.
function replaceKeywordsWithRegex(codeString) {
const captureRegex = /\$#([a-zA-Z]+[0-9_]*)?/g;
const matches = codeString.match(captureRegex);
if (matches == null)
return codeString;
const encounteredKeywords = new Set();
let result = codeString;
for (const match of matches) {
const normalizedName = match.replace('$#', '') || uniqueCaptureGroupName();
if (encounteredKeywords.has(normalizedName)) {
// Replace match with back reference, eg `$#keyword` becomes `\k<PREFIX_keyword>`.
result = result.replace(match, `\\k<${keywordPrefix}${normalizedName}>`);
}
else {
// Replace match with literal regex, eg `$#keyword` becomes `(?<PREFIX_keyword>KEYWORD_REGEX_STRING)`.
result = result.replace(match, createNamedKeywordRegex(`${keywordPrefix}${normalizedName}`));
encounteredKeywords.add(normalizedName);
}
}
return result;
}
// Main script.
const path = require('path');
const fs = require('fs');
const child_process = require('child_process');
const exec = child_process.execSync;
let checkStatus = 0; // Set to 1 if any step goes wrong.
const projectRoot = process.cwd(); // Should be set to the git project root by hooks.
const stagedFiles = exec('git diff --staged --name-only', { encoding: 'utf8' })
.toString()
.split('\n')
.filter((x) => x);
for (const stagedFile of stagedFiles) {
const stagedFilePath = path.resolve(projectRoot, stagedFile);
let fileContents;
try {
fileContents = fs.readFileSync(stagedFilePath, { encoding: 'utf-8' });
} catch {
// File was locked, didn't exist, etc. Do nothing.
continue;
}
// Eg ['C:', 'foo', 'bar', 'example.js']
const filePathSegments = stagedFilePath.split('\\');
// Eg 'example'. Extension not included.
const fileName = filePathSegments.pop().replace(/\.[^/.]+$/, '');
// Eg 'C:\foo\bar'. Trailing slash not included. Filename not included.
const filePath = filePathSegments.join('\\');
// Eg 'js'. Leading period not included.
const fileExtension = (fileName && path.extname(stagedFilePath).substring(1)) || '';
const file = Object.freeze({
fileContents,
filePath,
fileName,
fileExtension,
});
const lineNumberRanges = getLineNumberRanges(fileContents);
const boundLogToConsole = (regexOrText, checkMessage, alert) =>
logToConsole(regexOrText, checkMessage, stagedFile, fileContents, lineNumberRanges, alert);
const checks = readCheckrFiles(filePathSegments);
for (const check of checks) {
const isCheckrFile = fileName === 'checkr' && fileExtension === 'js';
if (isCheckrFile)
continue; // Omit checkr.js files from checks.
// 'boundLogToConsole' is passed again as a second arg for backwards compatibility.
check({ ...file, fs, path, child_process, code, underline: boundLogToConsole }, boundLogToConsole);
}
}
// Eg c$\code\coolproject should be passed as [c$, code, coolproject].
function readCheckrFiles(filePathSegments) {
const checks = [];
const segments = filePathSegments;
// Navigate up the file tree looking for checkr.js files to parse.
while (segments.length !== 0) {
const path = `${segments.join('\\')}\\checkr.js`;
try {
const checkrFileContents = fs.readFileSync(path, 'utf8');
// Prevents newly created checkr.js files from throwing errors.
if (checkrFileContents === '')
continue;
// Warning: arrays of functions console.log as "[null, null, null]" when they are not actually null.
const evalChecks = new Function(`return ${checkrFileContents}`)();
const evalCheckIsArray = Array.isArray(evalChecks);
if (!evalCheckIsArray) {
checkStatus = 1;
console.log(
`[Bad checkr.js file] checkr.js must contain a single array of functions.\npath: ${path}\n`,
);
continue;
}
const evalChecksValid = evalChecks.every((evalCheck) => evalCheck instanceof Function);
if (!evalChecksValid) {
checkStatus = 1;
console.log(
`[Bad checkr.js file] checkr.js array elements must be functions.\npath: ${path}\n`,
);
continue;
}
checks.push(...evalChecks);
} catch (e) {
// If no checkr file, bad parse, etc then do nothing.
if (e instanceof SyntaxError) {
checkStatus = 1;
console.log(
`[Bad checkr.js file] checkr.js error ${path}\nError message: ${e.message}\nStack: ${e.stack}\n`,
);
}
} finally {
// Note this occurs even on continues.
segments.pop();
}
}
return checks;
}
function logToConsole(regexOrText, checkMessage, filePath, fileContents, lineNumberRanges, alert) {
// Validate args passed in by consumers.
if (typeof regexOrText !== 'string' && !(regexOrText instanceof RegExp)) {
checkStatus = 1;
console.log(
'[Bad checkr.js file] regexOrText must be a string or RegExp. Check your checkr.js files.\n',
);
return;
}
if (typeof checkMessage !== 'string') {
checkStatus = 1;
console.log(
'[Bad checkr.js file] hoverMessage must be a string. Check your checkr.js files.\n',
);
return;
}
const regex =
typeof regexOrText === 'string' ? new RegExp(escapeRegExp(regexOrText), 'g') : regexOrText;
const checkMatches = [];
const limit = 50;
let counter = 0;
let match;
const existingMatches = new Set();
while ((match = regex.exec(fileContents)) != null) {
// Mitigate excessive backtracking cases.
counter++;
if (counter > limit)
break;
// Prevent regex expressions that infinitely loop.
const matchIdentity = `${match.index}-${match[0].length}`;
const loopDetected = existingMatches.has(matchIdentity);
if (loopDetected)
break;
existingMatches.add(matchIdentity);
const startPosition = match.index;
const checkMatch = { startPosition, matchString: match[0] };
checkMatches.push(checkMatch);
}
if (checkMatches.length === 0)
return;
let alertLevel = alert;
if (alertLevel !== 'error' && alertLevel !== 'warn' && alertLevel !== 'warning' && alertLevel !== 'info')
alertLevel = 'error'; // Default to error.
// Console color codes.
const redTextColor = '\x1b[31m';
const yellowTextColor = '\x1b[33m';
const cyanTextColor = '\x1b[36m';
const resetColor = '\x1b[0m';
let alertTextColor;
if (alertLevel === 'error')
alertTextColor = redTextColor;
else if (alertLevel === 'warn' || alertLevel === 'warning')
alertTextColor = yellowTextColor;
else if (alertLevel === 'info')
alertTextColor = cyanTextColor;
console.log(`${alertTextColor}${alertLevel}${resetColor} ${checkMessage}`);
for (const checkInfo of checkMatches) {
if (alert === 'error')
checkStatus = 1;
const lineNumber = getLineNumber(checkInfo.startPosition, lineNumberRanges);
console.log(`\n${filePath}:${lineNumber}\n${checkInfo.matchString}`);
}
}
/*
* Input: "hello\n World how\n are you?"
* Output: [
0,7, // 'hello\n', line 1 [start index:end index]
8,19, // ' World how\n' line 2 [start index:end index]
20,28 // ' are you?' line 3 [start index:end index]
]
*/
function getLineNumberRanges(fileContents) {
let index = 0;
const lineNumberRanges = fileContents.split('\n').flatMap((line) => {
const lineStartIndex = index;
// Typically one should avoid mutation in maps, but who's watching?
index = index + line.length + '\n'.length;
const lineEndIndex = index;
return [lineStartIndex, lineEndIndex];
});
// Fix last lineEndIndex.
// If file contents don't end with "\n", lineEndIndex is too large.
if (!fileContents.endsWith('\n'))
lineNumberRanges[lineNumberRanges.length - 1] -= '\n'.length;
return lineNumberRanges;
}
function getLineNumber(position, lineNumberRanges) {
const lastIndex = lineNumberRanges.length - 1;
const rangeMax = lineNumberRanges[lastIndex];
if (position > rangeMax)
throw new Error(`index of ${position} must not be greater than rangeMax of ${rangeMax}`);
if (position < 0)
throw new Error('index must be non-negative.');
// Simple binary search for lowerbound.
let leftIndex = 0;
let rightIndex = lastIndex;
while (leftIndex <= rightIndex) {
const middleIndex = Math.floor((rightIndex + leftIndex) / 2);
if (lineNumberRanges[middleIndex] < position)
leftIndex = middleIndex + 1;
else
rightIndex = middleIndex - 1;
}
// Each line has a start and end position in the lineNumberRanges array,
// so divide the leftIndex by 2, add 1, and take the floor to get the associated line number.
const lineNumber = Math.floor(leftIndex / 2 + 1);
return lineNumber;
}
process.exit(checkStatus);