@hughescr/stryker-bun-runner 1.7.1 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +339 -25
- package/dist/index.d.ts +77 -9
- package/dist/index.js +1766 -1146
- package/dist/templates/coverage-preload.js +6 -2
- package/dist/templates/import-scan.js +1644 -21
- package/package.json +25 -19
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
// @bun
|
|
2
2
|
// src/coverage/import-scan.ts
|
|
3
|
-
import { readFileSync, realpathSync, statSync } from "fs";
|
|
3
|
+
import { readFileSync, realpathSync, statSync, writeSync } from "fs";
|
|
4
4
|
import { createRequire, isBuiltin } from "module";
|
|
5
5
|
import path from "path";
|
|
6
|
-
import { pathToFileURL } from "url";
|
|
6
|
+
import { fileURLToPath, pathToFileURL } from "url";
|
|
7
7
|
|
|
8
8
|
// src/utils/code-files.ts
|
|
9
9
|
var CODE_EXTENSION_PATTERN = String.raw`(?:[jt]sx?|[mc][jt]s)`;
|
|
@@ -295,6 +295,7 @@ class CallCollector {
|
|
|
295
295
|
requireNames = 0;
|
|
296
296
|
importCalls = 0;
|
|
297
297
|
requireUnknown = false;
|
|
298
|
+
requireAliased = false;
|
|
298
299
|
unknown;
|
|
299
300
|
constructor(tokens) {
|
|
300
301
|
this.tokens = tokens;
|
|
@@ -409,13 +410,18 @@ class CallCollector {
|
|
|
409
410
|
if (called !== undefined) {
|
|
410
411
|
this.requires.push(called);
|
|
411
412
|
} else if (resolved === undefined) {
|
|
412
|
-
this.
|
|
413
|
+
const resolveCall = this.is(index + 1, ".") && this.is(index + 2, "resolve") && this.is(index + 3, "(");
|
|
414
|
+
this.flagRequire("a computed, aliased or passed require", !resolveCall && !this.readsMain(index));
|
|
413
415
|
} else {
|
|
414
416
|
this.requireResolves.push(resolved);
|
|
415
417
|
}
|
|
416
418
|
}
|
|
417
|
-
|
|
419
|
+
readsMain(index) {
|
|
420
|
+
return this.is(index + 1, ".") && this.is(index + 2, "main");
|
|
421
|
+
}
|
|
422
|
+
flagRequire(reason, aliased = true) {
|
|
418
423
|
this.requireUnknown = true;
|
|
424
|
+
this.requireAliased ||= aliased;
|
|
419
425
|
this.flag(reason);
|
|
420
426
|
}
|
|
421
427
|
import(index) {
|
|
@@ -439,6 +445,1157 @@ class CallCollector {
|
|
|
439
445
|
}
|
|
440
446
|
}
|
|
441
447
|
}
|
|
448
|
+
var WORKER_MODULES = new Set(["worker_threads", "node:worker_threads"]);
|
|
449
|
+
var CHILD_MODULES = new Set(["child_process", "node:child_process"]);
|
|
450
|
+
var CHILD_COMMANDS = new Set(["spawn", "spawnSync", "execFile", "execFileSync"]);
|
|
451
|
+
var SHELL_COMMANDS = new Set(["exec", "execSync"]);
|
|
452
|
+
var BUN_SPAWNS = new Set(["spawn", "spawnSync"]);
|
|
453
|
+
var BUN_SHELL = "$";
|
|
454
|
+
var SHELL_HELPERS = new Set(["escape", "braces"]);
|
|
455
|
+
var LAUNCHER_MODULES = new Set([...WORKER_MODULES, ...CHILD_MODULES, "bun"]);
|
|
456
|
+
var SITE_GLOBALS = new Set(["globalThis"]);
|
|
457
|
+
var WRITTEN_GLOBALS = new Set(["globalThis", "global", "self", "window"]);
|
|
458
|
+
var GLOBAL_MODULE = "globalThis";
|
|
459
|
+
function launcherKey(named) {
|
|
460
|
+
const module = named.module.startsWith("node:") ? named.module.slice("node:".length) : named.module;
|
|
461
|
+
return `${module} ${named.name}`;
|
|
462
|
+
}
|
|
463
|
+
var BUN_LAUNCHER_KEYS = [...BUN_SPAWNS].map((name) => launcherKey({ module: "bun", name }));
|
|
464
|
+
var GLOBAL_LAUNCHER_KEYS = new Map([
|
|
465
|
+
["Worker", [launcherKey({ module: GLOBAL_MODULE, name: "Worker" })]],
|
|
466
|
+
["Bun", BUN_LAUNCHER_KEYS]
|
|
467
|
+
]);
|
|
468
|
+
var PROCESS_MODULES = new Set(["process", "node:process"]);
|
|
469
|
+
var WHOLE_MODULE = new Set(["*", "default"]);
|
|
470
|
+
var RUNTIME_PATHS = new Set(["execPath", "argv0"]);
|
|
471
|
+
var PATH_MODULES = new Set(["path", "node:path"]);
|
|
472
|
+
var SWITCH_HELPER = "stryMutAct_";
|
|
473
|
+
var COVER_HELPER = "stryCov_";
|
|
474
|
+
var NAMESPACE_HELPER = "stryNS_";
|
|
475
|
+
var SUFFIX_MARK = "$SUFFIX$";
|
|
476
|
+
var MAX_MUTANT_ID_LENGTH = 32;
|
|
477
|
+
var ACCESSOR_WORDS = new Set(["get", "set", "async"]);
|
|
478
|
+
var COMPOUND_OPERATORS = new Set(["+", "-", "*", "/", "%", "**", "<<", ">>", ">>>", "&", "|", "^", "&&", "||", "??"]);
|
|
479
|
+
var HAND_OFF_WORDS = /(?<![\w$])(?:Worker|spawn|spawnSync|fork|execFile|execFileSync|child_process|worker_threads)(?![\w$])/;
|
|
480
|
+
var BUN_SHELL_MEMBER = /(?<![\w$])Bun\s*\??\.\s*\$(?![\w$])/;
|
|
481
|
+
var BUN_MODULE = /["']bun["']/;
|
|
482
|
+
var LONE_DOLLAR = /(?<![\w$])\$(?![\w${])/;
|
|
483
|
+
function mayHandOff(output) {
|
|
484
|
+
return HAND_OFF_WORDS.test(output) || BUN_SHELL_MEMBER.test(output) || BUN_MODULE.test(output) && LONE_DOLLAR.test(output);
|
|
485
|
+
}
|
|
486
|
+
var DECLARING = new Set(["const", "let", "var", "class", "function"]);
|
|
487
|
+
var OPENERS = { "(": ")", "[": "]", "{": "}", "${": "}" };
|
|
488
|
+
var CLOSERS = new Set([")", "]", "}"]);
|
|
489
|
+
var BLOCK_HEADS = new Set(["if", "while", "for", "switch", "with"]);
|
|
490
|
+
var PLAIN_FLAGS = new Set(["--smol", "--bun", "--hot", "--watch", "--no-install", "--no-warnings", "--no-deprecation", "--expose-gc", "--trace-warnings"]);
|
|
491
|
+
var BUN_SUBCOMMANDS = new Set([
|
|
492
|
+
"run",
|
|
493
|
+
"test",
|
|
494
|
+
"t",
|
|
495
|
+
"check",
|
|
496
|
+
"fmt",
|
|
497
|
+
"x",
|
|
498
|
+
"repl",
|
|
499
|
+
"exec",
|
|
500
|
+
"install",
|
|
501
|
+
"i",
|
|
502
|
+
"add",
|
|
503
|
+
"a",
|
|
504
|
+
"remove",
|
|
505
|
+
"rm",
|
|
506
|
+
"r",
|
|
507
|
+
"uninstall",
|
|
508
|
+
"update",
|
|
509
|
+
"audit",
|
|
510
|
+
"dedupe",
|
|
511
|
+
"prune",
|
|
512
|
+
"outdated",
|
|
513
|
+
"link",
|
|
514
|
+
"unlink",
|
|
515
|
+
"publish",
|
|
516
|
+
"patch",
|
|
517
|
+
"patch-commit",
|
|
518
|
+
"pm",
|
|
519
|
+
"info",
|
|
520
|
+
"why",
|
|
521
|
+
"build",
|
|
522
|
+
"bun",
|
|
523
|
+
"init",
|
|
524
|
+
"create",
|
|
525
|
+
"c",
|
|
526
|
+
"upgrade",
|
|
527
|
+
"help",
|
|
528
|
+
"discord",
|
|
529
|
+
"completions",
|
|
530
|
+
"getcompletes",
|
|
531
|
+
"feedback",
|
|
532
|
+
"pack",
|
|
533
|
+
"list",
|
|
534
|
+
"ls",
|
|
535
|
+
"ci",
|
|
536
|
+
"login",
|
|
537
|
+
"logout",
|
|
538
|
+
"whoami",
|
|
539
|
+
"deploy",
|
|
540
|
+
"version",
|
|
541
|
+
"lockfile",
|
|
542
|
+
"prime"
|
|
543
|
+
]);
|
|
544
|
+
var PROJECT_DIRECTORY = ".";
|
|
545
|
+
function instrumentationHeaderSource(suffix) {
|
|
546
|
+
return `function stryNS_${suffix}(){
|
|
547
|
+
var g = typeof globalThis === 'object' && globalThis && globalThis.Math === Math && globalThis || new Function("return this")();
|
|
548
|
+
var ns = g.__stryker__ || (g.__stryker__ = {});
|
|
549
|
+
if (ns.activeMutant === undefined && g.process && g.process.env && g.process.env.__STRYKER_ACTIVE_MUTANT__) {
|
|
550
|
+
ns.activeMutant = g.process.env.__STRYKER_ACTIVE_MUTANT__;
|
|
551
|
+
}
|
|
552
|
+
function retrieveNS(){
|
|
553
|
+
return ns;
|
|
554
|
+
}
|
|
555
|
+
stryNS_${suffix} = retrieveNS;
|
|
556
|
+
return retrieveNS();
|
|
557
|
+
}
|
|
558
|
+
stryNS_${suffix}();
|
|
559
|
+
|
|
560
|
+
function stryCov_${suffix}() {
|
|
561
|
+
var ns = stryNS_${suffix}();
|
|
562
|
+
var cov = ns.mutantCoverage || (ns.mutantCoverage = { static: {}, perTest: {} });
|
|
563
|
+
function cover() {
|
|
564
|
+
var c = cov.static;
|
|
565
|
+
if (ns.currentTestId) {
|
|
566
|
+
c = cov.perTest[ns.currentTestId] = cov.perTest[ns.currentTestId] || {};
|
|
567
|
+
}
|
|
568
|
+
var a = arguments;
|
|
569
|
+
for(var i=0; i < a.length; i++){
|
|
570
|
+
c[a[i]] = (c[a[i]] || 0) + 1;
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
stryCov_${suffix} = cover;
|
|
574
|
+
cover.apply(null, arguments);
|
|
575
|
+
}
|
|
576
|
+
function stryMutAct_${suffix}(id) {
|
|
577
|
+
var ns = stryNS_${suffix}();
|
|
578
|
+
function isActive(id) {
|
|
579
|
+
if (ns.activeMutant === id) {
|
|
580
|
+
if (ns.hitCount !== void 0 && ++ns.hitCount > ns.hitLimit) {
|
|
581
|
+
throw new Error('Stryker: Hit count limit reached (' + ns.hitCount + ')');
|
|
582
|
+
}
|
|
583
|
+
return true;
|
|
584
|
+
}
|
|
585
|
+
return false;
|
|
586
|
+
}
|
|
587
|
+
stryMutAct_${suffix} = isActive;
|
|
588
|
+
return isActive(id);
|
|
589
|
+
}`;
|
|
590
|
+
}
|
|
591
|
+
function sameToken(token, template, suffix) {
|
|
592
|
+
if (token?.kind !== template.kind) {
|
|
593
|
+
return false;
|
|
594
|
+
}
|
|
595
|
+
if (template.kind === "string") {
|
|
596
|
+
return token.value !== undefined && token.value === template.value;
|
|
597
|
+
}
|
|
598
|
+
return token.text === template.text.replace(SUFFIX_MARK, suffix);
|
|
599
|
+
}
|
|
600
|
+
function depthAt(tokens, index) {
|
|
601
|
+
let depth = 0;
|
|
602
|
+
for (let at = 0;at < index; at++) {
|
|
603
|
+
const { kind, text } = tokens[at];
|
|
604
|
+
if (kind === "punct" && text in OPENERS) {
|
|
605
|
+
depth++;
|
|
606
|
+
} else if (kind === "punct" && CLOSERS.has(text)) {
|
|
607
|
+
depth--;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
return depth;
|
|
611
|
+
}
|
|
612
|
+
function instrumentationHeader(tokens) {
|
|
613
|
+
const start = tokens.findIndex((token, index) => token.kind === "name" && token.text === "function" && tokens[index + 1]?.kind === "name" && tokens[index + 1].text.startsWith(NAMESPACE_HELPER));
|
|
614
|
+
if (start === -1) {
|
|
615
|
+
return;
|
|
616
|
+
}
|
|
617
|
+
const suffix = tokens[start + 1].text.slice(NAMESPACE_HELPER.length);
|
|
618
|
+
const template = headerTokens();
|
|
619
|
+
if (!/^[\w$]+$/.test(suffix) || depthAt(tokens, start) !== 0 || !template.every((expected, offset) => sameToken(tokens[start + offset], expected, suffix))) {
|
|
620
|
+
return;
|
|
621
|
+
}
|
|
622
|
+
const end = start + template.length;
|
|
623
|
+
const helpers = new Set([NAMESPACE_HELPER, SWITCH_HELPER, COVER_HELPER].map((helper) => `${helper}${suffix}`));
|
|
624
|
+
for (const [index, token] of tokens.entries()) {
|
|
625
|
+
if ((index < start || index >= end) && token.kind === "name" && helpers.has(token.text) && !onlyCalled(tokens, index)) {
|
|
626
|
+
return;
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
return { suffix, start, end };
|
|
630
|
+
}
|
|
631
|
+
var isPunct = (token, text) => token?.kind === "punct" && token.text === text;
|
|
632
|
+
var isFunctionWord = (token) => token?.kind === "name" && token.text === "function";
|
|
633
|
+
var isProperty = (tokens, index) => isPunct(tokens[index - 1], ".") || isPunct(tokens[index - 1], "?.");
|
|
634
|
+
function onlyCalled(tokens, index) {
|
|
635
|
+
if (isProperty(tokens, index)) {
|
|
636
|
+
return true;
|
|
637
|
+
}
|
|
638
|
+
const before = tokens[index - 1];
|
|
639
|
+
const declared = isFunctionWord(before) || isPunct(before, "*") && isFunctionWord(tokens[index - 2]);
|
|
640
|
+
return !declared && isPunct(tokens[index + 1], "(");
|
|
641
|
+
}
|
|
642
|
+
function idValue(token) {
|
|
643
|
+
const value = token?.kind === "string" ? token.value : undefined;
|
|
644
|
+
return value !== undefined && value !== "" && value.length <= MAX_MUTANT_ID_LENGTH ? value : undefined;
|
|
645
|
+
}
|
|
646
|
+
function literalArguments(tokens, index) {
|
|
647
|
+
const values = [];
|
|
648
|
+
for (let at = index + 2;; at += 2) {
|
|
649
|
+
const value = idValue(tokens[at]);
|
|
650
|
+
if (value === undefined) {
|
|
651
|
+
return;
|
|
652
|
+
}
|
|
653
|
+
values.push(value);
|
|
654
|
+
if (isPunct(tokens[at + 1], ")")) {
|
|
655
|
+
return isPunct(tokens[at + 2], "{") ? undefined : values;
|
|
656
|
+
}
|
|
657
|
+
if (!isPunct(tokens[at + 1], ",")) {
|
|
658
|
+
return;
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
function instrumentedMutantIds(tokens) {
|
|
663
|
+
const header = instrumentationHeader(tokens);
|
|
664
|
+
if (header === undefined) {
|
|
665
|
+
return [];
|
|
666
|
+
}
|
|
667
|
+
const switchName = `${SWITCH_HELPER}${header.suffix}`;
|
|
668
|
+
const coverName = `${COVER_HELPER}${header.suffix}`;
|
|
669
|
+
const ids = new Set;
|
|
670
|
+
for (const [index, token] of tokens.entries()) {
|
|
671
|
+
const outside = index < header.start || index >= header.end;
|
|
672
|
+
const helper = outside && token.kind === "name" && (token.text === switchName || token.text === coverName) && !isProperty(tokens, index) ? token.text : undefined;
|
|
673
|
+
const values = helper === undefined ? undefined : literalArguments(tokens, index);
|
|
674
|
+
for (const value of values !== undefined && (helper === coverName || values.length === 1) ? values : []) {
|
|
675
|
+
ids.add(value);
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
return [...ids];
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
class HandOffReader {
|
|
682
|
+
tokens;
|
|
683
|
+
requireKnown;
|
|
684
|
+
closes = new Map;
|
|
685
|
+
bindings = new Map;
|
|
686
|
+
declarations = [];
|
|
687
|
+
declared = new Map;
|
|
688
|
+
lists = [];
|
|
689
|
+
sites = [];
|
|
690
|
+
uses = [];
|
|
691
|
+
listed = new Set;
|
|
692
|
+
exportLists = [];
|
|
693
|
+
writes = [];
|
|
694
|
+
globalWrites = [];
|
|
695
|
+
replaced = new Set;
|
|
696
|
+
changesDirectory = false;
|
|
697
|
+
starts = [];
|
|
698
|
+
constants = new Map;
|
|
699
|
+
stack = [];
|
|
700
|
+
closed = { at: -1, open: -1 };
|
|
701
|
+
closedGroup = { at: -1, open: -1 };
|
|
702
|
+
assigned = [];
|
|
703
|
+
suffix;
|
|
704
|
+
handOffs = [];
|
|
705
|
+
unreadable = false;
|
|
706
|
+
constructor(tokens, requireKnown) {
|
|
707
|
+
this.tokens = tokens;
|
|
708
|
+
this.requireKnown = requireKnown;
|
|
709
|
+
this.brackets();
|
|
710
|
+
for (const { index, word, scope } of this.starts) {
|
|
711
|
+
if (word === "import") {
|
|
712
|
+
this.importStatement(index);
|
|
713
|
+
} else {
|
|
714
|
+
this.declaration(index, word, scope);
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
this.suffix = instrumentationHeader(tokens)?.suffix;
|
|
718
|
+
for (const open of this.lists) {
|
|
719
|
+
if (!BLOCK_HEADS.has(this.nameAt(open - 1) ?? "")) {
|
|
720
|
+
this.declarePattern(open);
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
for (const open of this.assigned) {
|
|
724
|
+
this.declarePattern(open);
|
|
725
|
+
}
|
|
726
|
+
const relevant = new Set(["Worker", "Bun", "require", "process", "URL", ...WRITTEN_GLOBALS, ...this.bindings.keys()]);
|
|
727
|
+
this.callSites(relevant);
|
|
728
|
+
this.countDeclarations();
|
|
729
|
+
this.replaceWritten();
|
|
730
|
+
this.changesDirectory = this.sites.some((site) => this.isChdir(site));
|
|
731
|
+
for (const site of this.sites) {
|
|
732
|
+
const kind = this.launches(site);
|
|
733
|
+
if (kind !== undefined) {
|
|
734
|
+
this.readLaunch(kind, site.open);
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
this.unreadable ||= this.escapes();
|
|
738
|
+
}
|
|
739
|
+
escapes() {
|
|
740
|
+
if (this.uses.length === 0) {
|
|
741
|
+
return false;
|
|
742
|
+
}
|
|
743
|
+
for (const open of this.exportLists) {
|
|
744
|
+
const close = this.closes.get(open) ?? open;
|
|
745
|
+
const reexport = this.isCode(close + 1, "from");
|
|
746
|
+
for (let index = open + 1;index < close; index++) {
|
|
747
|
+
if (reexport || this.isCode(index - 1, "as")) {
|
|
748
|
+
this.listed.add(index);
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
const declared = new Set(this.declarations);
|
|
753
|
+
return this.uses.some((index) => this.escapesAt(index, declared));
|
|
754
|
+
}
|
|
755
|
+
escapesAt(index, declared) {
|
|
756
|
+
if (this.noUse(index, declared)) {
|
|
757
|
+
return false;
|
|
758
|
+
}
|
|
759
|
+
const local = this.tokens[index].text;
|
|
760
|
+
if (local === "require") {
|
|
761
|
+
return this.requireEscapes(index, declared);
|
|
762
|
+
}
|
|
763
|
+
const whole = this.exportOf({ local }, SITE_GLOBALS);
|
|
764
|
+
if (whole !== undefined && this.isLauncher(whole)) {
|
|
765
|
+
return !this.calledAt(index + 1, whole);
|
|
766
|
+
}
|
|
767
|
+
const member = this.isCode(index + 1, ".") || this.isCode(index + 1, "?.") ? this.nameAt(index + 2) : undefined;
|
|
768
|
+
if (member === undefined) {
|
|
769
|
+
return whole !== undefined && WHOLE_MODULE.has(whole.name ?? "") && LAUNCHER_MODULES.has(whole.module);
|
|
770
|
+
}
|
|
771
|
+
const named = this.exportOf({ local, member }, SITE_GLOBALS);
|
|
772
|
+
return named !== undefined && this.isLauncher(named) && !this.calledAt(index + 3, named);
|
|
773
|
+
}
|
|
774
|
+
noUse(index, declared) {
|
|
775
|
+
const key = this.isCode(index + 1, ":") && (this.isCode(index - 1, "{") || this.isCode(index - 1, ","));
|
|
776
|
+
return key || declared.has(index) || this.listed.has(index) || this.isCode(index - 1, "typeof") || this.isCode(index - 1, "instanceof");
|
|
777
|
+
}
|
|
778
|
+
requireEscapes(index, declared) {
|
|
779
|
+
const module = this.isCode(index + 1, "(") && this.isCode(index + 3, ")") ? this.literalAt(index + 2) : undefined;
|
|
780
|
+
if (module === undefined || !LAUNCHER_MODULES.has(module) || !this.requireTrusted()) {
|
|
781
|
+
return false;
|
|
782
|
+
}
|
|
783
|
+
const member = this.isCode(index + 4, ".") ? this.nameAt(index + 5) : undefined;
|
|
784
|
+
const after = member === undefined ? index + 4 : index + 6;
|
|
785
|
+
const bound = this.isCode(index - 1, "=") && (declared.has(index - 2) || this.isCode(index - 2, "}")) && this.endsInitialiser(after);
|
|
786
|
+
if (bound) {
|
|
787
|
+
return false;
|
|
788
|
+
}
|
|
789
|
+
if (member === undefined) {
|
|
790
|
+
return true;
|
|
791
|
+
}
|
|
792
|
+
const named = { module, name: member };
|
|
793
|
+
return this.isLauncher(named) && !this.calledAt(after, named);
|
|
794
|
+
}
|
|
795
|
+
isLauncher(named) {
|
|
796
|
+
const kind = HandOffReader.launcher(named.module, named.name, true) ?? HandOffReader.launcher(named.module, named.name, false);
|
|
797
|
+
return kind !== undefined && !this.replaced.has(launcherKey(named));
|
|
798
|
+
}
|
|
799
|
+
calledAt(at, named) {
|
|
800
|
+
if (this.isCode(at, "(") || this.isCode(at, "?.") && this.isCode(at + 1, "(") || this.tokens[at]?.kind === "template") {
|
|
801
|
+
return true;
|
|
802
|
+
}
|
|
803
|
+
return named.name === BUN_SHELL && this.isCode(at, ".") && SHELL_HELPERS.has(this.nameAt(at + 1) ?? "");
|
|
804
|
+
}
|
|
805
|
+
isChdir(site) {
|
|
806
|
+
if (site.member === "chdir" && (site.local === "process" || PROCESS_MODULES.has(site.module ?? ""))) {
|
|
807
|
+
return true;
|
|
808
|
+
}
|
|
809
|
+
const binding = site.local === undefined ? undefined : this.bindings.get(site.local);
|
|
810
|
+
if (binding === undefined || !PROCESS_MODULES.has(binding.module)) {
|
|
811
|
+
return false;
|
|
812
|
+
}
|
|
813
|
+
return site.member === undefined ? binding.imported === "chdir" : site.member === "chdir" && WHOLE_MODULE.has(binding.imported);
|
|
814
|
+
}
|
|
815
|
+
replaceWritten() {
|
|
816
|
+
for (const write of this.writes) {
|
|
817
|
+
const written = this.exportOf(write, WRITTEN_GLOBALS);
|
|
818
|
+
if (written !== undefined) {
|
|
819
|
+
this.replaced.add(launcherKey(written));
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
for (const { keys, via } of this.globalWrites) {
|
|
823
|
+
if (via === undefined || this.declaredCount(via) === 0) {
|
|
824
|
+
for (const key of keys) {
|
|
825
|
+
this.replaced.add(key);
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
brackets() {
|
|
831
|
+
let index = -1;
|
|
832
|
+
for (const token of this.tokens) {
|
|
833
|
+
index++;
|
|
834
|
+
if (token.kind === "name") {
|
|
835
|
+
this.statementWord(index, token.text);
|
|
836
|
+
} else if (token.kind === "punct") {
|
|
837
|
+
this.punctuator(index, token.text);
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
statementWord(index, text) {
|
|
842
|
+
if ((DECLARING.has(text) || text === "import") && !this.isCode(index - 1, ".") && !this.isCode(index - 1, "?.")) {
|
|
843
|
+
this.starts.push({ index, word: text, scope: this.stack.at(-1) ?? -1 });
|
|
844
|
+
} else if (text === "export" && this.isCode(index + 1, "{")) {
|
|
845
|
+
this.exportLists.push(index + 1);
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
punctuator(index, text) {
|
|
849
|
+
if ((text === "{" || text === "=>") && this.closed.at === index - 1) {
|
|
850
|
+
this.lists.push(this.closed.open);
|
|
851
|
+
} else if (text === "=>" && this.tokens[index - 1]?.kind === "name") {
|
|
852
|
+
this.declarations.push(index - 1);
|
|
853
|
+
} else if (text === "=") {
|
|
854
|
+
this.assignment(index);
|
|
855
|
+
}
|
|
856
|
+
if (text in OPENERS) {
|
|
857
|
+
this.stack.push(index);
|
|
858
|
+
return;
|
|
859
|
+
}
|
|
860
|
+
const open = CLOSERS.has(text) ? this.stack.at(-1) : undefined;
|
|
861
|
+
if (open !== undefined && OPENERS[this.tokens[open].text] === text) {
|
|
862
|
+
this.stack.pop();
|
|
863
|
+
this.closes.set(open, index);
|
|
864
|
+
if (text === ")") {
|
|
865
|
+
this.closed = { at: index, open };
|
|
866
|
+
} else {
|
|
867
|
+
this.closedGroup = { at: index, open };
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
assignment(index) {
|
|
872
|
+
if (this.closedGroup.at === index - 1 && !this.isCode(index + 1, "=") && !this.isMemberAccess(this.closedGroup.open)) {
|
|
873
|
+
this.assigned.push(this.closedGroup.open);
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
isMemberAccess(open) {
|
|
877
|
+
const before = this.tokens[open - 1];
|
|
878
|
+
return this.isCode(open, "[") && before !== undefined && (before.kind === "name" || before.kind === "string" || this.isCode(open - 1, ")") || this.isCode(open - 1, "]") || this.isCode(open - 1, "?."));
|
|
879
|
+
}
|
|
880
|
+
callSites(relevant) {
|
|
881
|
+
const declaredNames = new Set(this.declarations);
|
|
882
|
+
let index = -1;
|
|
883
|
+
for (const token of this.tokens) {
|
|
884
|
+
index++;
|
|
885
|
+
if (token.kind === "name" && relevant.has(token.text) && !this.isCode(index - 1, ".") && !this.isCode(index - 1, "?.")) {
|
|
886
|
+
if (this.assignedAt(index + 1) || this.isCode(index - 1, "++") || this.isCode(index - 1, "--") || this.isCode(index + 1, "++") || this.isCode(index + 1, "--")) {
|
|
887
|
+
this.nameWrite(index, token.text, declaredNames);
|
|
888
|
+
}
|
|
889
|
+
this.memberWrite(index, token.text);
|
|
890
|
+
this.site(index, token.text, this.isCode(index - 1, "new"));
|
|
891
|
+
if (token.text !== "process" && token.text !== "URL") {
|
|
892
|
+
this.uses.push(index);
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
nameWrite(index, name, declaredNames) {
|
|
898
|
+
this.declarations.push(index);
|
|
899
|
+
const keys = GLOBAL_LAUNCHER_KEYS.get(name);
|
|
900
|
+
if (keys !== undefined && !declaredNames.has(index)) {
|
|
901
|
+
this.globalWrites.push({ keys });
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
assignedAt(index) {
|
|
905
|
+
let operator = "";
|
|
906
|
+
for (let at = index;at < index + 4; at++) {
|
|
907
|
+
if (this.isCode(at, "=")) {
|
|
908
|
+
return operator === "" ? !this.isCode(at + 1, "=") : COMPOUND_OPERATORS.has(operator);
|
|
909
|
+
}
|
|
910
|
+
const token = this.tokens[at];
|
|
911
|
+
if (token?.kind !== "punct") {
|
|
912
|
+
return false;
|
|
913
|
+
}
|
|
914
|
+
operator += token.text;
|
|
915
|
+
}
|
|
916
|
+
return false;
|
|
917
|
+
}
|
|
918
|
+
countDeclarations() {
|
|
919
|
+
for (const index of this.declarations) {
|
|
920
|
+
const name = this.tokens[index].text;
|
|
921
|
+
let at = this.declared.get(name);
|
|
922
|
+
if (at === undefined) {
|
|
923
|
+
at = new Set;
|
|
924
|
+
this.declared.set(name, at);
|
|
925
|
+
}
|
|
926
|
+
at.add(index);
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
declaredCount(name) {
|
|
930
|
+
return this.declared.get(name)?.size ?? 0;
|
|
931
|
+
}
|
|
932
|
+
requireTrusted() {
|
|
933
|
+
return this.requireKnown && this.declaredCount("require") === 0;
|
|
934
|
+
}
|
|
935
|
+
isCode(index, text) {
|
|
936
|
+
const token = this.tokens[index];
|
|
937
|
+
return token !== undefined && token.kind !== "string" && token.kind !== "template" && token.text === text;
|
|
938
|
+
}
|
|
939
|
+
nameAt(index) {
|
|
940
|
+
const token = this.tokens[index];
|
|
941
|
+
return token?.kind === "name" ? token.text : undefined;
|
|
942
|
+
}
|
|
943
|
+
literalAt(index) {
|
|
944
|
+
const token = this.tokens[index];
|
|
945
|
+
if (token?.kind === "string") {
|
|
946
|
+
return token.value;
|
|
947
|
+
}
|
|
948
|
+
const plainTemplate = token?.kind === "template" && token.text.length > 1 && token.text.startsWith("`") && token.text.endsWith("`") && !token.text.includes("\\");
|
|
949
|
+
return plainTemplate ? token.text.slice(1, -1) : undefined;
|
|
950
|
+
}
|
|
951
|
+
memberWrite(index, local) {
|
|
952
|
+
const module = local === "require" && this.isCode(index + 1, "(") && this.isCode(index + 3, ")") ? this.literalAt(index + 2) : undefined;
|
|
953
|
+
const dot = module === undefined ? index + 1 : index + 4;
|
|
954
|
+
const member = this.isCode(dot, ".") ? this.nameAt(dot + 1) : undefined;
|
|
955
|
+
if (member !== undefined && this.assignedAt(dot + 2)) {
|
|
956
|
+
this.writes.push(module === undefined ? { local, member } : { module, member });
|
|
957
|
+
}
|
|
958
|
+
if (WRITTEN_GLOBALS.has(local) && member === "Bun") {
|
|
959
|
+
this.globalBunWrite(local, dot + 2);
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
globalBunWrite(via, after) {
|
|
963
|
+
const launcher = this.isCode(after, ".") ? this.nameAt(after + 1) : undefined;
|
|
964
|
+
if (this.assignedAt(after)) {
|
|
965
|
+
this.globalWrites.push({ keys: BUN_LAUNCHER_KEYS, via });
|
|
966
|
+
} else if (launcher !== undefined && this.assignedAt(after + 2)) {
|
|
967
|
+
this.globalWrites.push({ keys: [launcherKey({ module: "bun", name: launcher })], via });
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
site(index, local, isNew) {
|
|
971
|
+
let next = index + 1;
|
|
972
|
+
let module;
|
|
973
|
+
let member;
|
|
974
|
+
const required = local === "require" && this.isCode(next, "(") && this.isCode(next + 2, ")") ? this.literalAt(next + 1) : undefined;
|
|
975
|
+
if (required !== undefined && this.isCode(next + 3, ".") && this.nameAt(next + 4) !== undefined) {
|
|
976
|
+
module = required;
|
|
977
|
+
member = this.nameAt(next + 4);
|
|
978
|
+
next += 5;
|
|
979
|
+
} else if ((this.isCode(next, ".") || this.isCode(next, "?.")) && this.nameAt(next + 1) !== undefined) {
|
|
980
|
+
member = this.nameAt(next + 1);
|
|
981
|
+
next += 2;
|
|
982
|
+
}
|
|
983
|
+
if (this.isCode(next, "?.")) {
|
|
984
|
+
next++;
|
|
985
|
+
}
|
|
986
|
+
if (this.isCode(next, "(") || this.tokens[next]?.kind === "template") {
|
|
987
|
+
this.sites.push(module === undefined ? { local, member, isNew, open: next } : { module, member, isNew, open: next });
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
declarePattern(open) {
|
|
991
|
+
const close = this.closes.get(open) ?? open;
|
|
992
|
+
let index = open + 1;
|
|
993
|
+
while (index < close) {
|
|
994
|
+
if (this.isCode(index, "=")) {
|
|
995
|
+
index = this.defaultEnd(index);
|
|
996
|
+
continue;
|
|
997
|
+
}
|
|
998
|
+
if (this.tokens[index].kind === "name" && !this.isCode(index + 1, ":") && !this.isCode(index - 1, ".")) {
|
|
999
|
+
this.declarations.push(index);
|
|
1000
|
+
}
|
|
1001
|
+
index++;
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
defaultEnd(at) {
|
|
1005
|
+
let index = at + 1;
|
|
1006
|
+
while (!this.isCode(index, ",") && !this.endsGroup(index)) {
|
|
1007
|
+
index = this.after(index);
|
|
1008
|
+
}
|
|
1009
|
+
return index;
|
|
1010
|
+
}
|
|
1011
|
+
declaration(index, word, scope) {
|
|
1012
|
+
if (word === "const" || word === "let" || word === "var") {
|
|
1013
|
+
this.declarators(index + 1, word === "const" ? scope : undefined);
|
|
1014
|
+
return;
|
|
1015
|
+
}
|
|
1016
|
+
let next = index + 1;
|
|
1017
|
+
if (word === "function" && this.isCode(next, "*")) {
|
|
1018
|
+
next++;
|
|
1019
|
+
}
|
|
1020
|
+
const name = this.nameAt(next);
|
|
1021
|
+
if (name !== undefined) {
|
|
1022
|
+
this.declarations.push(next);
|
|
1023
|
+
next++;
|
|
1024
|
+
}
|
|
1025
|
+
if (word === "function" && this.isCode(next, "(")) {
|
|
1026
|
+
this.declarePattern(next);
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
declarators(first, constScope) {
|
|
1030
|
+
for (let at = first;at !== undefined; at = this.nextDeclarator(at)) {
|
|
1031
|
+
const name = this.nameAt(at);
|
|
1032
|
+
if (name !== undefined) {
|
|
1033
|
+
this.declarations.push(at);
|
|
1034
|
+
if (constScope !== undefined) {
|
|
1035
|
+
this.constants.set(at, constScope);
|
|
1036
|
+
}
|
|
1037
|
+
this.requireBinding(name, at + 1);
|
|
1038
|
+
} else if (this.isCode(at, "{") || this.isCode(at, "[")) {
|
|
1039
|
+
this.declarePattern(at);
|
|
1040
|
+
this.requirePattern(at);
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
nextDeclarator(at) {
|
|
1045
|
+
for (let index = at;index < this.tokens.length; index = this.after(index)) {
|
|
1046
|
+
if (this.isCode(index, ",")) {
|
|
1047
|
+
return index + 1;
|
|
1048
|
+
}
|
|
1049
|
+
if (this.isCode(index, ";") || this.endsGroup(index)) {
|
|
1050
|
+
return;
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
return;
|
|
1054
|
+
}
|
|
1055
|
+
after(index) {
|
|
1056
|
+
return (this.tokens[index].kind === "punct" ? this.closes.get(index) ?? index : index) + 1;
|
|
1057
|
+
}
|
|
1058
|
+
endsGroup(index) {
|
|
1059
|
+
const token = this.tokens[index];
|
|
1060
|
+
return token.kind === "punct" && CLOSERS.has(token.text);
|
|
1061
|
+
}
|
|
1062
|
+
requireAt(index) {
|
|
1063
|
+
const module = this.isCode(index, "=") && this.isCode(index + 1, "require") && this.isCode(index + 2, "(") ? this.literalAt(index + 3) : undefined;
|
|
1064
|
+
return module !== undefined && this.isCode(index + 4, ")") ? { module, end: index + 5 } : undefined;
|
|
1065
|
+
}
|
|
1066
|
+
endsInitialiser(index) {
|
|
1067
|
+
return index >= this.tokens.length || this.isCode(index, ",") || this.isCode(index, ";") || this.endsGroup(index);
|
|
1068
|
+
}
|
|
1069
|
+
requireBinding(local, index) {
|
|
1070
|
+
const required = this.requireAt(index);
|
|
1071
|
+
if (required === undefined) {
|
|
1072
|
+
return;
|
|
1073
|
+
}
|
|
1074
|
+
const member = this.isCode(required.end, ".") ? this.nameAt(required.end + 1) : undefined;
|
|
1075
|
+
if (member === undefined && this.endsInitialiser(required.end)) {
|
|
1076
|
+
this.bind(local, { module: required.module, imported: "*", viaRequire: true });
|
|
1077
|
+
} else if (member !== undefined && this.endsInitialiser(required.end + 2)) {
|
|
1078
|
+
this.bind(local, { module: required.module, imported: member, viaRequire: true });
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
bind(local, binding) {
|
|
1082
|
+
if (LAUNCHER_MODULES.has(binding.module) || PATH_MODULES.has(binding.module) || PROCESS_MODULES.has(binding.module)) {
|
|
1083
|
+
this.bindings.set(local, binding);
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
requirePattern(open) {
|
|
1087
|
+
const close = this.closes.get(open);
|
|
1088
|
+
const required = close === undefined || !this.isCode(open, "{") ? undefined : this.requireAt(close + 1);
|
|
1089
|
+
if (required === undefined || !this.endsInitialiser(required.end)) {
|
|
1090
|
+
return;
|
|
1091
|
+
}
|
|
1092
|
+
for (let index = open + 1;index < close; index = (this.closes.get(index) ?? index) + 1) {
|
|
1093
|
+
const key = this.nameAt(index);
|
|
1094
|
+
if (key !== undefined && (this.isCode(index - 1, "{") || this.isCode(index - 1, ","))) {
|
|
1095
|
+
const local = this.isCode(index + 1, ":") ? this.nameAt(index + 2) : key;
|
|
1096
|
+
if (local !== undefined) {
|
|
1097
|
+
this.bind(local, { module: required.module, imported: key, viaRequire: true });
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
importStatement(index) {
|
|
1103
|
+
if (this.isCode(index + 1, "(") || this.isCode(index + 1, ".")) {
|
|
1104
|
+
return;
|
|
1105
|
+
}
|
|
1106
|
+
const locals = [];
|
|
1107
|
+
let next = index + 1;
|
|
1108
|
+
const defaultName = this.nameAt(next);
|
|
1109
|
+
if (defaultName !== undefined && defaultName !== "from") {
|
|
1110
|
+
locals.push({ local: defaultName, imported: "default", at: next });
|
|
1111
|
+
next += this.isCode(next + 1, ",") ? 2 : 1;
|
|
1112
|
+
}
|
|
1113
|
+
if (this.isCode(next, "*") && this.isCode(next + 1, "as") && this.nameAt(next + 2) !== undefined) {
|
|
1114
|
+
locals.push({ local: this.nameAt(next + 2), imported: "*", at: next + 2 });
|
|
1115
|
+
next += 3;
|
|
1116
|
+
} else if (this.isCode(next, "{")) {
|
|
1117
|
+
next = this.importSpecifiers(next, locals);
|
|
1118
|
+
}
|
|
1119
|
+
const module = this.isCode(next, "from") ? this.literalAt(next + 1) : undefined;
|
|
1120
|
+
for (const { local, imported, at } of locals) {
|
|
1121
|
+
this.declarations.push(at);
|
|
1122
|
+
if (module !== undefined) {
|
|
1123
|
+
this.bind(local, { module, imported, viaRequire: false });
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
importSpecifiers(open, locals) {
|
|
1128
|
+
const close = this.closes.get(open) ?? open;
|
|
1129
|
+
for (let entry = open + 1;entry < close; entry++) {
|
|
1130
|
+
const imported = this.nameAt(entry) ?? this.literalAt(entry);
|
|
1131
|
+
const at = this.isCode(entry + 1, "as") ? entry + 2 : entry;
|
|
1132
|
+
const local = this.nameAt(at);
|
|
1133
|
+
if (imported !== undefined && local !== undefined && (this.isCode(entry - 1, "{") || this.isCode(entry - 1, ","))) {
|
|
1134
|
+
locals.push({ local, imported, at });
|
|
1135
|
+
this.listed.add(entry);
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
return close + 1;
|
|
1139
|
+
}
|
|
1140
|
+
static launcher(module, name, isNew) {
|
|
1141
|
+
if (isNew) {
|
|
1142
|
+
return (WORKER_MODULES.has(module) || module === GLOBAL_MODULE) && name === "Worker" ? "worker" : undefined;
|
|
1143
|
+
}
|
|
1144
|
+
if (CHILD_MODULES.has(module)) {
|
|
1145
|
+
if (name === "fork") {
|
|
1146
|
+
return "fork";
|
|
1147
|
+
}
|
|
1148
|
+
if (SHELL_COMMANDS.has(name ?? "")) {
|
|
1149
|
+
return "shell";
|
|
1150
|
+
}
|
|
1151
|
+
return CHILD_COMMANDS.has(name ?? "") ? "command" : undefined;
|
|
1152
|
+
}
|
|
1153
|
+
if (module !== "bun") {
|
|
1154
|
+
return;
|
|
1155
|
+
}
|
|
1156
|
+
if (name === BUN_SHELL) {
|
|
1157
|
+
return "shell";
|
|
1158
|
+
}
|
|
1159
|
+
return BUN_SPAWNS.has(name ?? "") ? "bun-spawn" : undefined;
|
|
1160
|
+
}
|
|
1161
|
+
launches(site) {
|
|
1162
|
+
const named = this.exportOf(site, SITE_GLOBALS);
|
|
1163
|
+
const kind = named === undefined ? undefined : HandOffReader.launcher(named.module, named.name, site.isNew);
|
|
1164
|
+
return kind === undefined || this.replaced.has(launcherKey(named)) ? undefined : kind;
|
|
1165
|
+
}
|
|
1166
|
+
exportOf(callee, globals) {
|
|
1167
|
+
if (callee.module !== undefined) {
|
|
1168
|
+
return this.requireTrusted() ? { module: callee.module, name: callee.member } : undefined;
|
|
1169
|
+
}
|
|
1170
|
+
const local = callee.local;
|
|
1171
|
+
const declarations = this.declaredCount(local);
|
|
1172
|
+
if (declarations === 0) {
|
|
1173
|
+
return HandOffReader.globalExport(local, callee.member, globals);
|
|
1174
|
+
}
|
|
1175
|
+
const binding = declarations === 1 ? this.bindings.get(local) : undefined;
|
|
1176
|
+
if (binding === undefined || binding.viaRequire && !this.requireTrusted()) {
|
|
1177
|
+
return;
|
|
1178
|
+
}
|
|
1179
|
+
if (callee.member === undefined) {
|
|
1180
|
+
return { module: binding.module, name: binding.imported };
|
|
1181
|
+
}
|
|
1182
|
+
return binding.imported === "*" || binding.imported === "default" ? { module: binding.module, name: callee.member } : undefined;
|
|
1183
|
+
}
|
|
1184
|
+
static globalExport(local, member, globals) {
|
|
1185
|
+
if (local === "Worker" && member === undefined || globals.has(local) && member === "Worker") {
|
|
1186
|
+
return { module: GLOBAL_MODULE, name: "Worker" };
|
|
1187
|
+
}
|
|
1188
|
+
return local === "Bun" ? { module: "bun", name: member } : undefined;
|
|
1189
|
+
}
|
|
1190
|
+
readLaunch(kind, open) {
|
|
1191
|
+
const before = this.handOffs.length;
|
|
1192
|
+
if (this.launchOf(kind, open) && this.handOffs.length === before) {
|
|
1193
|
+
this.unreadable = true;
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
launchOf(kind, open) {
|
|
1197
|
+
const args = this.split(open)?.map((arg) => this.unwrap(arg));
|
|
1198
|
+
const first = args?.[0];
|
|
1199
|
+
if (first === undefined) {
|
|
1200
|
+
return true;
|
|
1201
|
+
}
|
|
1202
|
+
switch (kind) {
|
|
1203
|
+
case "worker": {
|
|
1204
|
+
const options = { options: args[1] };
|
|
1205
|
+
if (this.flagOff(options, "eval")) {
|
|
1206
|
+
this.entry(first, PROJECT_DIRECTORY);
|
|
1207
|
+
this.unreadable ||= !this.flagOff(options, "preload");
|
|
1208
|
+
}
|
|
1209
|
+
return true;
|
|
1210
|
+
}
|
|
1211
|
+
case "fork": {
|
|
1212
|
+
const options = this.childOptions(args);
|
|
1213
|
+
this.entry(first, this.optionsCwd(options));
|
|
1214
|
+
this.unreadable ||= !this.flagOff(options, "execArgv");
|
|
1215
|
+
return true;
|
|
1216
|
+
}
|
|
1217
|
+
case "command": {
|
|
1218
|
+
const options = this.childOptions(args);
|
|
1219
|
+
return !this.flagOff(options, "shell") || this.command([first, ...this.arrayItems(args[1]) ?? []], this.optionsCwd(options));
|
|
1220
|
+
}
|
|
1221
|
+
case "bun-spawn": {
|
|
1222
|
+
return this.bunSpawn(first, args[1]);
|
|
1223
|
+
}
|
|
1224
|
+
case "shell": {
|
|
1225
|
+
return true;
|
|
1226
|
+
}
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
bunSpawn(first, options) {
|
|
1230
|
+
const items = this.arrayItems(first);
|
|
1231
|
+
if (items !== undefined) {
|
|
1232
|
+
return this.command(items, this.optionsCwd({ options }));
|
|
1233
|
+
}
|
|
1234
|
+
const cmd = this.isGroup(first, "{") ? this.property(first.start, "cmd")?.value : undefined;
|
|
1235
|
+
const cmdItems = cmd === undefined ? undefined : this.arrayItems(cmd);
|
|
1236
|
+
return cmdItems === undefined || this.command(cmdItems, this.objectCwd(first.start));
|
|
1237
|
+
}
|
|
1238
|
+
split(open) {
|
|
1239
|
+
const close = this.closes.get(open);
|
|
1240
|
+
if (close === undefined) {
|
|
1241
|
+
return;
|
|
1242
|
+
}
|
|
1243
|
+
const entries = [];
|
|
1244
|
+
let start = open + 1;
|
|
1245
|
+
for (let index = start;index < close; index = this.after(index)) {
|
|
1246
|
+
if (this.isCode(index, ",")) {
|
|
1247
|
+
if (index === start) {
|
|
1248
|
+
return;
|
|
1249
|
+
}
|
|
1250
|
+
entries.push({ start, end: index });
|
|
1251
|
+
start = index + 1;
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
if (close > start) {
|
|
1255
|
+
entries.push({ start, end: close });
|
|
1256
|
+
}
|
|
1257
|
+
return entries;
|
|
1258
|
+
}
|
|
1259
|
+
unwrap(range) {
|
|
1260
|
+
let current = range;
|
|
1261
|
+
for (let original = this.originalOf(current);original !== undefined; original = this.originalOf(current)) {
|
|
1262
|
+
current = original;
|
|
1263
|
+
}
|
|
1264
|
+
return current;
|
|
1265
|
+
}
|
|
1266
|
+
originalOf(range) {
|
|
1267
|
+
return this.switchOriginal(range) ?? this.coveredOriginal(range);
|
|
1268
|
+
}
|
|
1269
|
+
switchOriginal(range) {
|
|
1270
|
+
const { start, end } = range;
|
|
1271
|
+
if (!this.isHelperCall(start, SWITCH_HELPER)) {
|
|
1272
|
+
return;
|
|
1273
|
+
}
|
|
1274
|
+
const test = this.closes.get(start + 1);
|
|
1275
|
+
if (!this.isCode(test + 1, "?")) {
|
|
1276
|
+
return;
|
|
1277
|
+
}
|
|
1278
|
+
let depth = 0;
|
|
1279
|
+
for (let index = test + 2;index < end; index = this.after(index)) {
|
|
1280
|
+
if (this.isCode(index, "?") && this.isCode(index + 1, "?")) {
|
|
1281
|
+
index++;
|
|
1282
|
+
} else if (this.isCode(index, "?")) {
|
|
1283
|
+
depth++;
|
|
1284
|
+
} else if (this.isCode(index, ":") && depth > 0) {
|
|
1285
|
+
depth--;
|
|
1286
|
+
} else if (this.isCode(index, ":")) {
|
|
1287
|
+
return index + 1 < end ? { start: index + 1, end } : undefined;
|
|
1288
|
+
}
|
|
1289
|
+
}
|
|
1290
|
+
return;
|
|
1291
|
+
}
|
|
1292
|
+
coveredOriginal(range) {
|
|
1293
|
+
const { start, end } = range;
|
|
1294
|
+
if (!this.isCode(start, "(") || this.closes.get(start) !== end - 1 || !this.isHelperCall(start + 1, COVER_HELPER)) {
|
|
1295
|
+
return;
|
|
1296
|
+
}
|
|
1297
|
+
const cover = this.closes.get(start + 2);
|
|
1298
|
+
return this.isCode(cover + 1, ",") && cover + 2 < end - 1 ? { start: cover + 2, end: end - 1 } : undefined;
|
|
1299
|
+
}
|
|
1300
|
+
isHelperCall(index, helper) {
|
|
1301
|
+
if (this.suffix === undefined || this.nameAt(index) !== `${helper}${this.suffix}` || !this.isCode(index + 1, "(")) {
|
|
1302
|
+
return false;
|
|
1303
|
+
}
|
|
1304
|
+
const args = this.split(index + 1) ?? [];
|
|
1305
|
+
const literals = args.length > 0 && args.every((arg) => arg.end - arg.start === 1 && this.tokens[arg.start].kind === "string");
|
|
1306
|
+
return literals && (helper !== SWITCH_HELPER || args.length === 1);
|
|
1307
|
+
}
|
|
1308
|
+
isGroup(range, opener) {
|
|
1309
|
+
return range !== undefined && this.isCode(range.start, opener) && this.closes.get(range.start) === range.end - 1;
|
|
1310
|
+
}
|
|
1311
|
+
arrayItems(range) {
|
|
1312
|
+
const array = range === undefined ? undefined : this.unwrap(range);
|
|
1313
|
+
return this.isGroup(array, "[") ? this.split(array.start)?.map((item) => this.unwrap(item)) : undefined;
|
|
1314
|
+
}
|
|
1315
|
+
isFunction(range) {
|
|
1316
|
+
const { start } = range;
|
|
1317
|
+
if (this.isCode(start, "function") || this.isCode(start, "async") || this.isCode(start + 1, "=>")) {
|
|
1318
|
+
return true;
|
|
1319
|
+
}
|
|
1320
|
+
return this.isCode(start, "(") && this.isCode((this.closes.get(start) ?? start) + 1, "=>");
|
|
1321
|
+
}
|
|
1322
|
+
property(open, key) {
|
|
1323
|
+
const properties = this.split(open);
|
|
1324
|
+
if (properties === undefined) {
|
|
1325
|
+
return;
|
|
1326
|
+
}
|
|
1327
|
+
let found;
|
|
1328
|
+
for (const property of properties) {
|
|
1329
|
+
const shape = this.propertyShape(property);
|
|
1330
|
+
if (shape.key === undefined || shape.key === key) {
|
|
1331
|
+
if (found !== undefined || shape.value === undefined) {
|
|
1332
|
+
return;
|
|
1333
|
+
}
|
|
1334
|
+
found = this.unwrap(shape.value);
|
|
1335
|
+
}
|
|
1336
|
+
}
|
|
1337
|
+
return { value: found };
|
|
1338
|
+
}
|
|
1339
|
+
propertyShape(property) {
|
|
1340
|
+
const { start, end } = property;
|
|
1341
|
+
const key = this.keyAt(start);
|
|
1342
|
+
if (key !== undefined && this.isCode(start + 1, ":")) {
|
|
1343
|
+
return { key, value: { start: start + 2, end } };
|
|
1344
|
+
}
|
|
1345
|
+
let at = start;
|
|
1346
|
+
if (ACCESSOR_WORDS.has(key ?? "") && start + 1 < end && !this.isCode(start + 1, "(")) {
|
|
1347
|
+
at++;
|
|
1348
|
+
}
|
|
1349
|
+
if (this.isCode(at, "*")) {
|
|
1350
|
+
at++;
|
|
1351
|
+
}
|
|
1352
|
+
return this.isCode(at, "...") || this.isCode(at, "[") ? {} : { key: this.keyAt(at) };
|
|
1353
|
+
}
|
|
1354
|
+
keyAt(index) {
|
|
1355
|
+
const token = this.tokens[index];
|
|
1356
|
+
return this.nameAt(index) ?? this.literalAt(index) ?? (token?.kind === "number" ? token.text : undefined);
|
|
1357
|
+
}
|
|
1358
|
+
childOptions(args) {
|
|
1359
|
+
const second = args[1];
|
|
1360
|
+
if (this.isGroup(second, "[")) {
|
|
1361
|
+
return { options: args[2] };
|
|
1362
|
+
}
|
|
1363
|
+
return second === undefined || this.isGroup(second, "{") || this.isFunction(second) ? { options: second } : undefined;
|
|
1364
|
+
}
|
|
1365
|
+
optionsCwd(slot) {
|
|
1366
|
+
const options = slot?.options;
|
|
1367
|
+
if (slot === undefined) {
|
|
1368
|
+
return;
|
|
1369
|
+
}
|
|
1370
|
+
if (options === undefined || this.isFunction(options)) {
|
|
1371
|
+
return PROJECT_DIRECTORY;
|
|
1372
|
+
}
|
|
1373
|
+
return this.isGroup(options, "{") ? this.objectCwd(options.start) : undefined;
|
|
1374
|
+
}
|
|
1375
|
+
flagOff(slot, key) {
|
|
1376
|
+
const options = slot?.options;
|
|
1377
|
+
if (slot === undefined) {
|
|
1378
|
+
return false;
|
|
1379
|
+
}
|
|
1380
|
+
if (options === undefined || this.isFunction(options)) {
|
|
1381
|
+
return true;
|
|
1382
|
+
}
|
|
1383
|
+
if (!this.isGroup(options, "{")) {
|
|
1384
|
+
return false;
|
|
1385
|
+
}
|
|
1386
|
+
const found = this.property(options.start, key);
|
|
1387
|
+
const value = found?.value;
|
|
1388
|
+
return found !== undefined && (value === undefined || value.end - value.start === 1 && this.isCode(value.start, "false"));
|
|
1389
|
+
}
|
|
1390
|
+
objectCwd(open) {
|
|
1391
|
+
const found = this.property(open, "cwd");
|
|
1392
|
+
if (found === undefined) {
|
|
1393
|
+
return;
|
|
1394
|
+
}
|
|
1395
|
+
return found.value === undefined ? PROJECT_DIRECTORY : this.literalIn(found.value);
|
|
1396
|
+
}
|
|
1397
|
+
command(items, cwd) {
|
|
1398
|
+
const [command, ...rest] = items;
|
|
1399
|
+
if (command === undefined) {
|
|
1400
|
+
return true;
|
|
1401
|
+
}
|
|
1402
|
+
const runtime = this.runtimeAt(command);
|
|
1403
|
+
if (runtime === undefined) {
|
|
1404
|
+
const entry = this.pathExpression(command);
|
|
1405
|
+
if (entry !== undefined && (entry.how !== "path" || entry.specifier.includes("/"))) {
|
|
1406
|
+
this.record(entry, cwd, "system");
|
|
1407
|
+
}
|
|
1408
|
+
return this.literalThrough(command)?.includes("/") !== false;
|
|
1409
|
+
}
|
|
1410
|
+
const found = this.scriptOf(rest, runtime);
|
|
1411
|
+
if (found !== undefined) {
|
|
1412
|
+
this.entry(found.script, cwd, runtime, found.packageScript === true);
|
|
1413
|
+
}
|
|
1414
|
+
return true;
|
|
1415
|
+
}
|
|
1416
|
+
scriptOf(args, runtime) {
|
|
1417
|
+
let mayRun = runtime === "bun";
|
|
1418
|
+
let afterRun = false;
|
|
1419
|
+
for (const arg of args) {
|
|
1420
|
+
const literal = this.literalThrough(arg);
|
|
1421
|
+
if (literal === "run" && mayRun) {
|
|
1422
|
+
mayRun = false;
|
|
1423
|
+
afterRun = true;
|
|
1424
|
+
} else if (literal?.startsWith("-") !== true) {
|
|
1425
|
+
return literal === undefined ? { script: arg } : HandOffReader.scriptWord(arg, literal, runtime, afterRun);
|
|
1426
|
+
} else if (!PLAIN_FLAGS.has(literal)) {
|
|
1427
|
+
return;
|
|
1428
|
+
}
|
|
1429
|
+
}
|
|
1430
|
+
return;
|
|
1431
|
+
}
|
|
1432
|
+
static scriptWord(arg, word, runtime, afterRun) {
|
|
1433
|
+
if (runtime === "node") {
|
|
1434
|
+
return word === "inspect" ? undefined : { script: arg };
|
|
1435
|
+
}
|
|
1436
|
+
if (afterRun ? /^\.\.?\//.test(word) || path.isAbsolute(word) : /[./]/.test(word)) {
|
|
1437
|
+
return { script: arg };
|
|
1438
|
+
}
|
|
1439
|
+
return !afterRun && BUN_SUBCOMMANDS.has(word) ? undefined : { script: arg, packageScript: true };
|
|
1440
|
+
}
|
|
1441
|
+
runtimeAt(range) {
|
|
1442
|
+
const expression = this.unwrap(range);
|
|
1443
|
+
const command = this.unwrap(this.constantAt(expression) ?? expression);
|
|
1444
|
+
const literal = this.literalIn(command);
|
|
1445
|
+
if (literal === "bun" || literal === "node") {
|
|
1446
|
+
return literal;
|
|
1447
|
+
}
|
|
1448
|
+
return this.runsBun(command) ? "bun" : undefined;
|
|
1449
|
+
}
|
|
1450
|
+
runsBun(range) {
|
|
1451
|
+
const { property, after } = this.processProperty(range.start);
|
|
1452
|
+
if (after === range.end && RUNTIME_PATHS.has(property ?? "")) {
|
|
1453
|
+
return true;
|
|
1454
|
+
}
|
|
1455
|
+
const index = this.tokens[after + 1];
|
|
1456
|
+
return property === "argv" && range.end === after + 3 && this.isCode(after, "[") && this.isCode(after + 2, "]") && index?.kind === "number" && index.text === "0";
|
|
1457
|
+
}
|
|
1458
|
+
processProperty(start) {
|
|
1459
|
+
const exported = this.processBinding(start);
|
|
1460
|
+
if (exported !== undefined && !WHOLE_MODULE.has(exported.imported)) {
|
|
1461
|
+
return { property: exported.imported, after: start + 1 };
|
|
1462
|
+
}
|
|
1463
|
+
const isProcess = exported !== undefined || this.isCode(start, "process") && this.declaredCount("process") === 0;
|
|
1464
|
+
return isProcess && this.isCode(start + 1, ".") ? { property: this.nameAt(start + 2), after: start + 3 } : { after: start + 1 };
|
|
1465
|
+
}
|
|
1466
|
+
processBinding(index) {
|
|
1467
|
+
const local = this.nameAt(index);
|
|
1468
|
+
const binding = local === undefined || this.declaredCount(local) !== 1 ? undefined : this.bindings.get(local);
|
|
1469
|
+
return binding !== undefined && PROCESS_MODULES.has(binding.module) && (!binding.viaRequire || this.requireTrusted()) ? binding : undefined;
|
|
1470
|
+
}
|
|
1471
|
+
entry(range, cwd, runs = "bun", packageScript = false) {
|
|
1472
|
+
const entry = this.pathExpression(range);
|
|
1473
|
+
if (entry !== undefined) {
|
|
1474
|
+
this.record(packageScript ? { ...entry, packageScript } : entry, cwd, runs);
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1477
|
+
pathExpression(range, throughName = true) {
|
|
1478
|
+
const expression = this.unwrap(range);
|
|
1479
|
+
const literal = this.literalIn(expression);
|
|
1480
|
+
if (literal !== undefined) {
|
|
1481
|
+
return { specifier: literal, how: "path" };
|
|
1482
|
+
}
|
|
1483
|
+
const initialiser = throughName ? this.constantAt(expression) : undefined;
|
|
1484
|
+
if (initialiser !== undefined) {
|
|
1485
|
+
return this.pathExpression(initialiser, false);
|
|
1486
|
+
}
|
|
1487
|
+
return this.urlIn(expression) ?? this.resolveIn(expression) ?? this.joinIn(expression);
|
|
1488
|
+
}
|
|
1489
|
+
constantAt(range) {
|
|
1490
|
+
const name = range.end - range.start === 1 ? this.nameAt(range.start) : undefined;
|
|
1491
|
+
const declared = name === undefined ? undefined : this.declared.get(name);
|
|
1492
|
+
const at = declared?.size === 1 ? [...declared][0] : undefined;
|
|
1493
|
+
const scope = at === undefined ? undefined : this.constants.get(at);
|
|
1494
|
+
const inScope = scope === -1 || scope !== undefined && this.isCode(scope, "{") && scope < range.start && range.start < (this.closes.get(scope) ?? -1);
|
|
1495
|
+
if (!inScope || !this.isCode(at + 1, "=")) {
|
|
1496
|
+
return;
|
|
1497
|
+
}
|
|
1498
|
+
let end = at + 2;
|
|
1499
|
+
while (!this.endsInitialiser(end)) {
|
|
1500
|
+
end = this.after(end);
|
|
1501
|
+
}
|
|
1502
|
+
return { start: at + 2, end };
|
|
1503
|
+
}
|
|
1504
|
+
literalIn(range) {
|
|
1505
|
+
const expression = this.unwrap(range);
|
|
1506
|
+
return expression.end - expression.start === 1 ? this.literalAt(expression.start) : undefined;
|
|
1507
|
+
}
|
|
1508
|
+
literalThrough(range) {
|
|
1509
|
+
const expression = this.unwrap(range);
|
|
1510
|
+
return this.literalIn(this.constantAt(expression) ?? expression);
|
|
1511
|
+
}
|
|
1512
|
+
importMeta(index, property) {
|
|
1513
|
+
return this.isCode(index, "import") && this.isCode(index + 1, ".") && this.isCode(index + 2, "meta") && this.isCode(index + 3, ".") && this.isCode(index + 4, property);
|
|
1514
|
+
}
|
|
1515
|
+
finalCall(open, range) {
|
|
1516
|
+
return this.isCode(open, "(") && this.closes.get(open) === range.end - 1 ? this.split(open) : undefined;
|
|
1517
|
+
}
|
|
1518
|
+
urlIn(range) {
|
|
1519
|
+
const { start } = range;
|
|
1520
|
+
const isUrl = this.isCode(start, "new") && this.isCode(start + 1, "URL") && this.isCode(start + 2, "(") && this.declaredCount("URL") === 0;
|
|
1521
|
+
const close = isUrl ? this.closes.get(start + 2) : undefined;
|
|
1522
|
+
const args = close !== undefined && this.urlEnds(close, range.end) ? this.split(start + 2) : undefined;
|
|
1523
|
+
const literal = args?.length === 2 && this.isMeta(args[1], "url") ? this.literalIn(args[0]) : undefined;
|
|
1524
|
+
return literal === undefined ? undefined : { specifier: literal, how: "url" };
|
|
1525
|
+
}
|
|
1526
|
+
urlEnds(close, end) {
|
|
1527
|
+
const property = this.nameAt(close + 2);
|
|
1528
|
+
return end === close + 1 || end === close + 3 && this.isCode(close + 1, ".") && (property === "href" || property === "pathname");
|
|
1529
|
+
}
|
|
1530
|
+
isMeta(range, property) {
|
|
1531
|
+
return range.end - range.start === 5 && this.importMeta(range.start, property);
|
|
1532
|
+
}
|
|
1533
|
+
resolveIn(range) {
|
|
1534
|
+
const { start } = range;
|
|
1535
|
+
let resolver;
|
|
1536
|
+
if (this.importMeta(start, "resolve")) {
|
|
1537
|
+
resolver = { how: "import", at: start + 5 };
|
|
1538
|
+
} else if (this.isCode(start, "require") && this.isCode(start + 1, ".") && this.isCode(start + 2, "resolve") && this.requireTrusted()) {
|
|
1539
|
+
resolver = { how: "require", at: start + 3 };
|
|
1540
|
+
}
|
|
1541
|
+
const args = resolver === undefined ? undefined : this.finalCall(resolver.at, range);
|
|
1542
|
+
const literal = args?.length === 1 ? this.literalIn(args[0]) : undefined;
|
|
1543
|
+
return literal === undefined ? undefined : { specifier: literal, how: resolver.how };
|
|
1544
|
+
}
|
|
1545
|
+
joinIn(range) {
|
|
1546
|
+
const { name, method } = this.pathMethod(range.start);
|
|
1547
|
+
const args = name === "join" || name === "resolve" ? this.finalCall(method + 1, range) : undefined;
|
|
1548
|
+
const base = args?.[0];
|
|
1549
|
+
if (base === undefined || !this.isFileDirectory(base)) {
|
|
1550
|
+
return;
|
|
1551
|
+
}
|
|
1552
|
+
const segments = args.slice(1).map((segment) => this.literalIn(segment));
|
|
1553
|
+
if (segments.length === 0 || segments.includes(undefined)) {
|
|
1554
|
+
return;
|
|
1555
|
+
}
|
|
1556
|
+
const parts = segments;
|
|
1557
|
+
const lastAbsolute = name === "resolve" ? parts.findLastIndex((segment) => path.isAbsolute(segment)) : -1;
|
|
1558
|
+
const joined = lastAbsolute === -1 ? path.join(".", ...parts) : path.join(...parts.slice(lastAbsolute));
|
|
1559
|
+
return { specifier: joined, how: "dir" };
|
|
1560
|
+
}
|
|
1561
|
+
pathMethod(start) {
|
|
1562
|
+
const binding = this.pathBinding(this.nameAt(start) ?? "");
|
|
1563
|
+
if (!this.isCode(start + 1, ".")) {
|
|
1564
|
+
return { name: binding?.imported, method: start };
|
|
1565
|
+
}
|
|
1566
|
+
const wholeModule = binding?.imported === "*" || binding?.imported === "default";
|
|
1567
|
+
return { name: wholeModule ? this.nameAt(start + 2) : undefined, method: start + 2 };
|
|
1568
|
+
}
|
|
1569
|
+
pathBinding(local) {
|
|
1570
|
+
const binding = this.declaredCount(local) === 1 ? this.bindings.get(local) : undefined;
|
|
1571
|
+
return binding !== undefined && PATH_MODULES.has(binding.module) && (!binding.viaRequire || this.requireTrusted()) ? binding : undefined;
|
|
1572
|
+
}
|
|
1573
|
+
isFileDirectory(range) {
|
|
1574
|
+
return this.isMeta(range, "dir") || this.isMeta(range, "dirname") || range.end - range.start === 1 && this.isCode(range.start, "__dirname");
|
|
1575
|
+
}
|
|
1576
|
+
record(found, cwd, runs) {
|
|
1577
|
+
let entry = found;
|
|
1578
|
+
if (runs === "node") {
|
|
1579
|
+
entry = { ...found, node: true };
|
|
1580
|
+
} else if (runs === "system") {
|
|
1581
|
+
entry = { ...found, asWritten: true };
|
|
1582
|
+
}
|
|
1583
|
+
if (entry.how !== "path") {
|
|
1584
|
+
this.handOffs.push(entry);
|
|
1585
|
+
} else if (path.isAbsolute(entry.specifier)) {
|
|
1586
|
+
this.handOffs.push({ ...entry, cwd: PROJECT_DIRECTORY });
|
|
1587
|
+
} else if (cwd !== undefined && (!this.changesDirectory || path.isAbsolute(cwd))) {
|
|
1588
|
+
this.handOffs.push({ ...entry, cwd });
|
|
1589
|
+
}
|
|
1590
|
+
}
|
|
1591
|
+
}
|
|
1592
|
+
function readLaunches(tokens, requireKnown = true) {
|
|
1593
|
+
const reader = new HandOffReader(tokens, requireKnown);
|
|
1594
|
+
return { handOffs: reader.handOffs, unreadable: reader.unreadable };
|
|
1595
|
+
}
|
|
1596
|
+
function readHandOffs(tokens, requireKnown = true) {
|
|
1597
|
+
return readLaunches(tokens, requireKnown).handOffs;
|
|
1598
|
+
}
|
|
442
1599
|
function wordReason(output, calls) {
|
|
443
1600
|
const forbidden = FORBIDDEN_WORDS.exec(output);
|
|
444
1601
|
if (forbidden !== null) {
|
|
@@ -461,14 +1618,22 @@ function scannerAgrees(scanned, calls) {
|
|
|
461
1618
|
const requiresAgree = scannerRequires.length === 0 || sameList(scannerRequires, calls.requires);
|
|
462
1619
|
return requiresAgree && sameList(scannerList("dynamic-import"), calls.imports) && sameList(scannerList("require-resolve"), calls.requireResolves);
|
|
463
1620
|
}
|
|
464
|
-
function analyseOutput(output, scanned) {
|
|
1621
|
+
function analyseOutput(output, scanned, launches = readLaunches) {
|
|
465
1622
|
const records = scanned.filter((entry) => entry.kind === "import-statement").map((entry) => ({ kind: "static", specifier: entry.path }));
|
|
466
1623
|
let calls;
|
|
1624
|
+
let tokens;
|
|
467
1625
|
try {
|
|
468
|
-
|
|
1626
|
+
tokens = tokenize(output);
|
|
1627
|
+
calls = new CallCollector(tokens);
|
|
469
1628
|
} catch (error) {
|
|
470
1629
|
return { records, unknown: `bun's output could not be read (${error.message})` };
|
|
471
1630
|
}
|
|
1631
|
+
let read = { handOffs: [], unreadable: false };
|
|
1632
|
+
try {
|
|
1633
|
+
read = mayHandOff(output) ? launches(tokens, !calls.requireAliased) : read;
|
|
1634
|
+
} catch {
|
|
1635
|
+
read = { handOffs: [], unreadable: true };
|
|
1636
|
+
}
|
|
472
1637
|
const wordProblem = wordReason(output, calls);
|
|
473
1638
|
if (wordProblem !== undefined) {
|
|
474
1639
|
calls.flag(wordProblem);
|
|
@@ -489,7 +1654,14 @@ function analyseOutput(output, scanned) {
|
|
|
489
1654
|
if (records.some((record) => record.specifier === "bun") && WORD_PLUGIN.test(output)) {
|
|
490
1655
|
calls.flag("a bun plugin may be installed (plugin from bun)");
|
|
491
1656
|
}
|
|
492
|
-
|
|
1657
|
+
for (const handOff of read.handOffs) {
|
|
1658
|
+
records.push({ kind: "handed", ...handOff });
|
|
1659
|
+
}
|
|
1660
|
+
const facts = { records, unknown: calls.unknown, ...unprovenOf(calls) };
|
|
1661
|
+
return read.unreadable ? { ...facts, unreadableLaunch: true } : facts;
|
|
1662
|
+
}
|
|
1663
|
+
function unprovenOf(calls) {
|
|
1664
|
+
return calls.requireUnknown && calls.requires.length > 0 ? { unprovenCalls: [...calls.requires] } : {};
|
|
493
1665
|
}
|
|
494
1666
|
var MAX_EXTENDS_DEPTH = 32;
|
|
495
1667
|
function resolveTsconfigBase(base, dir) {
|
|
@@ -573,6 +1745,19 @@ function generatedJsxResolution(resolutions, reason) {
|
|
|
573
1745
|
}
|
|
574
1746
|
var STATIC_EDGES = new Set(["static"]);
|
|
575
1747
|
var LOADING_EDGES = new Set(["static", "possible"]);
|
|
1748
|
+
var HANDED_EDGES = new Set(["static", "handed"]);
|
|
1749
|
+
var ATTRIBUTION_EDGES = new Set(["static", "named", "possible"]);
|
|
1750
|
+
var MAX_LAUNCHERS = 1e5;
|
|
1751
|
+
function withSite(credit, site) {
|
|
1752
|
+
return credit === undefined ? site : { every: credit.every || site.every, launchers: new Set([...credit.launchers, ...site.launchers]) };
|
|
1753
|
+
}
|
|
1754
|
+
function printedCredit(file, ids, credit) {
|
|
1755
|
+
if (credit === undefined || credit.launchers.size === 0) {
|
|
1756
|
+
return { file, ids };
|
|
1757
|
+
}
|
|
1758
|
+
const launchers = [...credit.launchers].toSorted((a, b) => a - b);
|
|
1759
|
+
return credit.every ? { file, ids, launchers, every: true } : { file, ids, launchers };
|
|
1760
|
+
}
|
|
576
1761
|
function attempt(how) {
|
|
577
1762
|
try {
|
|
578
1763
|
return how();
|
|
@@ -587,7 +1772,37 @@ function real(file) {
|
|
|
587
1772
|
return file;
|
|
588
1773
|
}
|
|
589
1774
|
}
|
|
1775
|
+
var isFile = (file) => statSync(file, { throwIfNoEntry: false })?.isFile() === true;
|
|
1776
|
+
var isPackageDirectory = (file) => statSync(file, { throwIfNoEntry: false })?.isDirectory() === true && statSync(path.join(file, "package.json"), { throwIfNoEntry: false }) !== undefined;
|
|
1777
|
+
var NODE_MAIN_EXTENSIONS = [".js", ".json", ".node"];
|
|
1778
|
+
var withNodeExtensions = (base) => NODE_MAIN_EXTENSIONS.map((extension) => `${base}${extension}`);
|
|
1779
|
+
var nodeFile = (spelling) => [spelling, ...withNodeExtensions(spelling)].find((each) => isFile(each));
|
|
1780
|
+
function namesPackageDirectory(spelling, handOff) {
|
|
1781
|
+
if (handOff.asWritten === true || !isPackageDirectory(spelling)) {
|
|
1782
|
+
return false;
|
|
1783
|
+
}
|
|
1784
|
+
return handOff.node !== true || nodeFile(spelling) === undefined;
|
|
1785
|
+
}
|
|
1786
|
+
function entryFile(spelling, handOff) {
|
|
1787
|
+
if (handOff.asWritten === true) {
|
|
1788
|
+
return spelling;
|
|
1789
|
+
}
|
|
1790
|
+
if (handOff.node === true) {
|
|
1791
|
+
return nodeFile(spelling) ?? withNodeExtensions(path.join(spelling, "index")).find((each) => isFile(each));
|
|
1792
|
+
}
|
|
1793
|
+
return attempt(() => Bun.resolveSync(spelling, path.dirname(spelling))) ?? spelling;
|
|
1794
|
+
}
|
|
590
1795
|
var SCAN_DEFINE = Object.fromEntries(["process.env.NODE_ENV", "process.env.BUN_ENV", "process.browser"].map((key) => [key, key]));
|
|
1796
|
+
var headerTemplate;
|
|
1797
|
+
function headerTokens() {
|
|
1798
|
+
headerTemplate ??= tokenize(new Bun.Transpiler({ loader: "js", define: SCAN_DEFINE }).transformSync(instrumentationHeaderSource(SUFFIX_MARK)));
|
|
1799
|
+
return headerTemplate;
|
|
1800
|
+
}
|
|
1801
|
+
var HAND_OFF_BUDGET = { wallMs: 250, files: 1000, ids: 1e5 };
|
|
1802
|
+
|
|
1803
|
+
class HandOffStop extends Error {
|
|
1804
|
+
name = "HandOffStop";
|
|
1805
|
+
}
|
|
591
1806
|
|
|
592
1807
|
class ImportScan {
|
|
593
1808
|
input;
|
|
@@ -605,9 +1820,17 @@ class ImportScan {
|
|
|
605
1820
|
requires = new Map;
|
|
606
1821
|
configChains = new Map;
|
|
607
1822
|
analysed = new Map;
|
|
1823
|
+
unprovenEdges = new Map;
|
|
608
1824
|
importers = new Map;
|
|
1825
|
+
budget;
|
|
1826
|
+
handOffMs = 0;
|
|
1827
|
+
handOffProblem;
|
|
1828
|
+
reached = new Set;
|
|
1829
|
+
handOffDeadline;
|
|
1830
|
+
handOffReader = (tokens, requireKnown) => this.timedHandOffs(() => readLaunches(tokens, requireKnown), { handOffs: [], unreadable: false });
|
|
609
1831
|
constructor(input) {
|
|
610
1832
|
this.input = input;
|
|
1833
|
+
this.budget = input.handOffBudget ?? HAND_OFF_BUDGET;
|
|
611
1834
|
this.projectReal = real(input.projectDir);
|
|
612
1835
|
for (const file of input.eager) {
|
|
613
1836
|
for (const key of new Set([file, real(file)])) {
|
|
@@ -626,7 +1849,7 @@ class ImportScan {
|
|
|
626
1849
|
}
|
|
627
1850
|
}
|
|
628
1851
|
}
|
|
629
|
-
|
|
1852
|
+
primary() {
|
|
630
1853
|
const mandatoryFiles = new Set;
|
|
631
1854
|
this.reach(this.roots, STATIC_EDGES, mandatoryFiles, (info) => this.noteFirst(info));
|
|
632
1855
|
const mandatory = this.candidatesIn(mandatoryFiles);
|
|
@@ -635,15 +1858,272 @@ class ImportScan {
|
|
|
635
1858
|
this.reach(namedTargets, STATIC_EDGES, namedFiles, (info) => this.noteFirst(info));
|
|
636
1859
|
const named = new Set([...this.candidatesIn(namedFiles)].filter((file) => !mandatory.has(file)));
|
|
637
1860
|
const covered = (file) => mandatory.has(file) || named.has(file) || this.always.has(file);
|
|
1861
|
+
const answer = this.answer(namedFiles, (file) => covered(file));
|
|
1862
|
+
this.reached = namedFiles;
|
|
638
1863
|
return {
|
|
639
|
-
answer
|
|
1864
|
+
answer,
|
|
640
1865
|
mandatory: this.input.eager.filter((file) => mandatory.has(file)),
|
|
641
1866
|
named: this.input.eager.filter((file) => named.has(file)),
|
|
642
|
-
reasons: this.reasons,
|
|
643
|
-
notes: this.notes,
|
|
1867
|
+
reasons: [...this.reasons],
|
|
1868
|
+
notes: [...this.notes],
|
|
644
1869
|
scannedFiles: this.analysed.size
|
|
645
1870
|
};
|
|
646
1871
|
}
|
|
1872
|
+
handOffs() {
|
|
1873
|
+
const started = performance.now();
|
|
1874
|
+
const deadline = started + this.budget.wallMs - this.handOffMs;
|
|
1875
|
+
let found;
|
|
1876
|
+
try {
|
|
1877
|
+
if (this.handOffProblem !== undefined) {
|
|
1878
|
+
throw new HandOffStop(this.handOffProblem);
|
|
1879
|
+
}
|
|
1880
|
+
this.handOffDeadline = deadline;
|
|
1881
|
+
const { files, visited } = this.handedClosure(this.edgesOf([...this.reached], "handed"), new Set, deadline);
|
|
1882
|
+
found = { files, handed: this.handedIds(files, [], deadline), visited };
|
|
1883
|
+
this.throwIfStopped();
|
|
1884
|
+
} catch (error) {
|
|
1885
|
+
return { handed: [], notes: [`hand-offs not read: ${error.message}`] };
|
|
1886
|
+
}
|
|
1887
|
+
const { handed, visited, note } = this.credited(found, deadline);
|
|
1888
|
+
const read = visited === 0 ? [] : [`hand-offs read in ${Math.round(performance.now() - started + this.handOffMs)} ms over ${visited} files`];
|
|
1889
|
+
return { handed, notes: note === undefined ? read : [...read, note] };
|
|
1890
|
+
}
|
|
1891
|
+
throwIfStopped() {
|
|
1892
|
+
const problem = this.stopped();
|
|
1893
|
+
if (problem !== undefined) {
|
|
1894
|
+
throw new HandOffStop(problem);
|
|
1895
|
+
}
|
|
1896
|
+
}
|
|
1897
|
+
credited(base, deadline) {
|
|
1898
|
+
let found = base;
|
|
1899
|
+
const everyTest = (why) => found.handed.length === 0 ? { handed: [], visited: found.visited } : { handed: found.handed.map(({ file, ids }) => ({ file, ids })), visited: found.visited, note: `hand-offs credited to every test: ${why}` };
|
|
1900
|
+
let printed;
|
|
1901
|
+
try {
|
|
1902
|
+
found = base.handed.length > 0 ? this.loadedHandOffs(base, deadline) : base;
|
|
1903
|
+
if (found.handed.length === 0) {
|
|
1904
|
+
return { handed: [], visited: found.visited };
|
|
1905
|
+
}
|
|
1906
|
+
const credits = this.attribute(deadline);
|
|
1907
|
+
printed = found.handed.map(({ file, ids }) => printedCredit(file, ids, credits.get(file)));
|
|
1908
|
+
} catch (error) {
|
|
1909
|
+
return everyTest(error.message);
|
|
1910
|
+
}
|
|
1911
|
+
const launchers = printed.reduce((sum, entry) => sum + (entry.launchers?.length ?? 0), 0);
|
|
1912
|
+
return launchers > MAX_LAUNCHERS ? everyTest(`their launchers number more than ${MAX_LAUNCHERS}`) : { handed: printed, visited: found.visited };
|
|
1913
|
+
}
|
|
1914
|
+
loadedHandOffs(found, deadline) {
|
|
1915
|
+
const seeds = this.edgesOf(this.testProcessFiles(deadline), "handed");
|
|
1916
|
+
const seen = new Set(found.files);
|
|
1917
|
+
const { files, visited } = this.handedClosure(seeds, seen, deadline);
|
|
1918
|
+
const handed = visited === found.visited ? found.handed : this.handedIds(files, found.handed, deadline);
|
|
1919
|
+
this.throwIfStopped();
|
|
1920
|
+
return { files, handed, visited };
|
|
1921
|
+
}
|
|
1922
|
+
attribute(deadline) {
|
|
1923
|
+
if (this.input.tsconfig !== null && this.tsconfig === undefined) {
|
|
1924
|
+
throw new HandOffStop(`the tsconfig ${this.input.tsconfig} is not usable`);
|
|
1925
|
+
}
|
|
1926
|
+
const { closure, wildcards, importersOf } = this.loadingClosure(deadline);
|
|
1927
|
+
const testIndex = this.testIndex();
|
|
1928
|
+
const anyFile = this.rootsReaching(wildcards, importersOf, deadline);
|
|
1929
|
+
const credits = new Map;
|
|
1930
|
+
const closures = new Map;
|
|
1931
|
+
const credit = (files, started) => {
|
|
1932
|
+
for (const file of files) {
|
|
1933
|
+
for (const candidate of this.candidatesAt.get(file) ?? []) {
|
|
1934
|
+
credits.set(candidate, withSite(credits.get(candidate), started));
|
|
1935
|
+
}
|
|
1936
|
+
}
|
|
1937
|
+
};
|
|
1938
|
+
let anyHanded;
|
|
1939
|
+
for (const site of closure) {
|
|
1940
|
+
const targets = this.analyse(site).edges.filter((edge) => edge.type === "handed").map((edge) => edge.target);
|
|
1941
|
+
if (targets.length > 0) {
|
|
1942
|
+
const roots = new Set([...this.rootsReaching([site], importersOf, deadline), ...anyFile]);
|
|
1943
|
+
const started = { every: [...roots].some((root) => !testIndex.has(root)), launchers: new Set([...roots].flatMap((root) => testIndex.get(root) ?? [])) };
|
|
1944
|
+
const files = new Set(targets.flatMap((target) => [...this.handedFrom(target, closures, deadline)]));
|
|
1945
|
+
if ([...files].some((file) => ImportScan.incomplete(this.analyse(file)))) {
|
|
1946
|
+
anyHanded = withSite(anyHanded, started);
|
|
1947
|
+
}
|
|
1948
|
+
credit(files, started);
|
|
1949
|
+
}
|
|
1950
|
+
}
|
|
1951
|
+
if (anyHanded !== undefined) {
|
|
1952
|
+
credit(new Set(credits.keys()), anyHanded);
|
|
1953
|
+
}
|
|
1954
|
+
const problem = this.stopped();
|
|
1955
|
+
if (problem !== undefined) {
|
|
1956
|
+
throw new HandOffStop(problem);
|
|
1957
|
+
}
|
|
1958
|
+
return credits;
|
|
1959
|
+
}
|
|
1960
|
+
loadingClosure(deadline) {
|
|
1961
|
+
const closure = new Set;
|
|
1962
|
+
const wildcards = [];
|
|
1963
|
+
const importersOf = new Map;
|
|
1964
|
+
const queue = [...this.roots];
|
|
1965
|
+
for (let file = queue.pop();file !== undefined; file = queue.pop()) {
|
|
1966
|
+
if (!closure.has(file)) {
|
|
1967
|
+
closure.add(file);
|
|
1968
|
+
this.checkDeadline(deadline);
|
|
1969
|
+
const info = this.analyse(file);
|
|
1970
|
+
if (info.unknown !== undefined || info.unreadableLaunch === true) {
|
|
1971
|
+
wildcards.push(file);
|
|
1972
|
+
}
|
|
1973
|
+
for (const edge of this.walkEdges(file).filter((each) => ATTRIBUTION_EDGES.has(each.type))) {
|
|
1974
|
+
const importers = importersOf.get(edge.target) ?? [];
|
|
1975
|
+
importers.push(file);
|
|
1976
|
+
importersOf.set(edge.target, importers);
|
|
1977
|
+
queue.push(edge.target);
|
|
1978
|
+
}
|
|
1979
|
+
}
|
|
1980
|
+
}
|
|
1981
|
+
return { closure, wildcards, importersOf };
|
|
1982
|
+
}
|
|
1983
|
+
testIndex() {
|
|
1984
|
+
const realOf = new Map(this.input.roots.map((root, index) => [root, this.roots[index]]));
|
|
1985
|
+
const tests = new Set(this.input.tests);
|
|
1986
|
+
const others = new Set(this.input.roots.filter((root) => !tests.has(root)).map((root) => realOf.get(root)));
|
|
1987
|
+
const index = new Map;
|
|
1988
|
+
for (const [position, file] of this.input.tests.entries()) {
|
|
1989
|
+
const key = realOf.get(file);
|
|
1990
|
+
if (!others.has(key) && !index.has(key)) {
|
|
1991
|
+
index.set(key, position);
|
|
1992
|
+
}
|
|
1993
|
+
}
|
|
1994
|
+
return index;
|
|
1995
|
+
}
|
|
1996
|
+
rootsReaching(starts, importersOf, deadline) {
|
|
1997
|
+
const seen = new Set;
|
|
1998
|
+
const roots = new Set;
|
|
1999
|
+
const queue = [...starts];
|
|
2000
|
+
for (let file = queue.pop();file !== undefined; file = queue.pop()) {
|
|
2001
|
+
if (!seen.has(file)) {
|
|
2002
|
+
seen.add(file);
|
|
2003
|
+
this.checkDeadline(deadline);
|
|
2004
|
+
if (this.rootSet.has(file)) {
|
|
2005
|
+
roots.add(file);
|
|
2006
|
+
}
|
|
2007
|
+
for (const importer of importersOf.get(file) ?? []) {
|
|
2008
|
+
queue.push(importer);
|
|
2009
|
+
}
|
|
2010
|
+
}
|
|
2011
|
+
}
|
|
2012
|
+
return roots;
|
|
2013
|
+
}
|
|
2014
|
+
handedFrom(target, closures, deadline) {
|
|
2015
|
+
const known = closures.get(target);
|
|
2016
|
+
if (known !== undefined) {
|
|
2017
|
+
return known;
|
|
2018
|
+
}
|
|
2019
|
+
const seen = new Set;
|
|
2020
|
+
const queue = [target];
|
|
2021
|
+
for (let file = queue.pop();file !== undefined; file = queue.pop()) {
|
|
2022
|
+
if (!seen.has(file)) {
|
|
2023
|
+
seen.add(file);
|
|
2024
|
+
this.checkDeadline(deadline);
|
|
2025
|
+
for (const edge of this.walkEdges(file)) {
|
|
2026
|
+
if (ImportScan.followedInH(edge) && !seen.has(edge.target)) {
|
|
2027
|
+
queue.push(edge.target);
|
|
2028
|
+
}
|
|
2029
|
+
}
|
|
2030
|
+
}
|
|
2031
|
+
}
|
|
2032
|
+
closures.set(target, seen);
|
|
2033
|
+
return seen;
|
|
2034
|
+
}
|
|
2035
|
+
static incomplete(info) {
|
|
2036
|
+
return info.unreadableLaunch === true || info.unknown !== undefined;
|
|
2037
|
+
}
|
|
2038
|
+
static followedInH(edge) {
|
|
2039
|
+
return HANDED_EDGES.has(edge.type) || edge.call === true;
|
|
2040
|
+
}
|
|
2041
|
+
timedHandOffs(work, none) {
|
|
2042
|
+
if (this.handOffProblem !== undefined) {
|
|
2043
|
+
return none;
|
|
2044
|
+
}
|
|
2045
|
+
const started = performance.now();
|
|
2046
|
+
try {
|
|
2047
|
+
return work();
|
|
2048
|
+
} finally {
|
|
2049
|
+
this.handOffMs += performance.now() - started;
|
|
2050
|
+
if (this.handOffMs > this.budget.wallMs) {
|
|
2051
|
+
this.handOffProblem = `reading them took more than ${this.budget.wallMs} ms`;
|
|
2052
|
+
}
|
|
2053
|
+
}
|
|
2054
|
+
}
|
|
2055
|
+
checkDeadline(deadline) {
|
|
2056
|
+
if (performance.now() > deadline) {
|
|
2057
|
+
throw new HandOffStop(`reading them took more than ${this.budget.wallMs} ms`);
|
|
2058
|
+
}
|
|
2059
|
+
}
|
|
2060
|
+
checkWalkDeadline() {
|
|
2061
|
+
if (this.handOffDeadline !== undefined) {
|
|
2062
|
+
this.checkDeadline(this.handOffDeadline);
|
|
2063
|
+
}
|
|
2064
|
+
}
|
|
2065
|
+
stopped() {
|
|
2066
|
+
return this.handOffProblem;
|
|
2067
|
+
}
|
|
2068
|
+
handedClosure(starts, seen, deadline) {
|
|
2069
|
+
const queue = [...starts];
|
|
2070
|
+
for (let file = queue.pop();file !== undefined; file = queue.pop()) {
|
|
2071
|
+
if (!seen.has(file)) {
|
|
2072
|
+
seen.add(file);
|
|
2073
|
+
if (seen.size > this.budget.files) {
|
|
2074
|
+
throw new HandOffStop(`the walk over their closure reached more than ${this.budget.files} files`);
|
|
2075
|
+
}
|
|
2076
|
+
this.checkDeadline(deadline);
|
|
2077
|
+
for (const edge of this.walkEdges(file)) {
|
|
2078
|
+
if (ImportScan.followedInH(edge) && !seen.has(edge.target)) {
|
|
2079
|
+
queue.push(edge.target);
|
|
2080
|
+
}
|
|
2081
|
+
}
|
|
2082
|
+
}
|
|
2083
|
+
}
|
|
2084
|
+
return { files: seen, visited: seen.size };
|
|
2085
|
+
}
|
|
2086
|
+
testProcessFiles(deadline) {
|
|
2087
|
+
const seen = new Set(this.reached);
|
|
2088
|
+
const queue = [...this.reached];
|
|
2089
|
+
for (let file = queue.pop();file !== undefined; file = queue.pop()) {
|
|
2090
|
+
for (const edge of this.walkEdges(file)) {
|
|
2091
|
+
if ((edge.type === "static" || edge.call === true) && !seen.has(edge.target)) {
|
|
2092
|
+
this.checkDeadline(deadline);
|
|
2093
|
+
seen.add(edge.target);
|
|
2094
|
+
queue.push(edge.target);
|
|
2095
|
+
}
|
|
2096
|
+
}
|
|
2097
|
+
}
|
|
2098
|
+
return [...seen];
|
|
2099
|
+
}
|
|
2100
|
+
handedIds(files, known, deadline) {
|
|
2101
|
+
const idsOf = new Map(known.map((entry) => [entry.file, entry.ids]));
|
|
2102
|
+
let total = known.reduce((sum, entry) => sum + entry.ids.length, 0);
|
|
2103
|
+
for (const file of files) {
|
|
2104
|
+
const candidates = this.candidatesAt.get(file) ?? [];
|
|
2105
|
+
if (candidates.length > 0 && !candidates.every((candidate) => idsOf.has(candidate))) {
|
|
2106
|
+
this.checkDeadline(deadline);
|
|
2107
|
+
const ids = this.mutantIdsOf(file);
|
|
2108
|
+
total += ids.length * candidates.length;
|
|
2109
|
+
if (total > this.budget.ids) {
|
|
2110
|
+
throw new HandOffStop(`their files hold more than ${this.budget.ids} mutant ids`);
|
|
2111
|
+
}
|
|
2112
|
+
for (const candidate of candidates) {
|
|
2113
|
+
idsOf.set(candidate, ids);
|
|
2114
|
+
}
|
|
2115
|
+
}
|
|
2116
|
+
}
|
|
2117
|
+
return this.input.eager.filter((file) => idsOf.has(file)).map((file) => ({ file, ids: idsOf.get(file) }));
|
|
2118
|
+
}
|
|
2119
|
+
mutantIdsOf(file) {
|
|
2120
|
+
try {
|
|
2121
|
+
const source = readFileSync(file, "utf8");
|
|
2122
|
+
return MACRO_ATTRIBUTE.test(source) ? [] : instrumentedMutantIds(tokenize(this.transpilerFor(loaderOf(file)).transformSync(source)));
|
|
2123
|
+
} catch (error) {
|
|
2124
|
+
throw new HandOffStop(`cannot read the mutant ids of ${this.display(file)} (${error.message})`);
|
|
2125
|
+
}
|
|
2126
|
+
}
|
|
647
2127
|
answer(namedFiles, covered) {
|
|
648
2128
|
if (this.input.eager.every((file) => covered(file))) {
|
|
649
2129
|
this.reasons.length = 0;
|
|
@@ -721,6 +2201,9 @@ class ImportScan {
|
|
|
721
2201
|
try {
|
|
722
2202
|
info = this.analyseFile(file);
|
|
723
2203
|
} catch (error) {
|
|
2204
|
+
if (error instanceof HandOffStop) {
|
|
2205
|
+
throw error;
|
|
2206
|
+
}
|
|
724
2207
|
info = { edges: [], unknown: `cannot scan ${this.display(file)} (${error.message})` };
|
|
725
2208
|
}
|
|
726
2209
|
this.analysed.set(file, info);
|
|
@@ -735,11 +2218,17 @@ class ImportScan {
|
|
|
735
2218
|
const loader = loaderOf(file);
|
|
736
2219
|
const output = this.transpilerFor(loader).transformSync(source);
|
|
737
2220
|
const scanned = this.outputScanner.scan(output).imports;
|
|
738
|
-
const facts = analyseOutput(output, scanned);
|
|
2221
|
+
const facts = analyseOutput(output, scanned, this.handOffReader);
|
|
739
2222
|
let unknown = this.configProblem(file) ?? (facts.unknown === undefined ? undefined : `${facts.unknown} in ${this.display(file)}`);
|
|
740
2223
|
const edges = [];
|
|
2224
|
+
let unreadableLaunch = facts.unreadableLaunch === true;
|
|
741
2225
|
const generated = generatedJsxRuntimes(scanned, () => this.outputScanner.scan(this.transpilerFor(loader, false).transformSync(source)).imports);
|
|
742
2226
|
for (const record of facts.records) {
|
|
2227
|
+
this.checkWalkDeadline();
|
|
2228
|
+
if (record.kind === "handed") {
|
|
2229
|
+
unreadableLaunch = this.addHandedEdges(record, file, edges) || unreadableLaunch;
|
|
2230
|
+
continue;
|
|
2231
|
+
}
|
|
743
2232
|
const index = generated.indexOf(record.specifier);
|
|
744
2233
|
const resolved = index === -1 ? this.resolve(record.specifier, file) : this.resolveGeneratedJsx(generated.splice(index, 1)[0], file);
|
|
745
2234
|
if ("reason" in resolved) {
|
|
@@ -749,11 +2238,91 @@ class ImportScan {
|
|
|
749
2238
|
}
|
|
750
2239
|
}
|
|
751
2240
|
for (const edge of edges) {
|
|
752
|
-
if (!this.importers.has(edge.target)) {
|
|
2241
|
+
if (edge.type !== "handed" && !this.importers.has(edge.target)) {
|
|
753
2242
|
this.importers.set(edge.target, file);
|
|
754
2243
|
}
|
|
755
2244
|
}
|
|
756
|
-
return { edges, unknown };
|
|
2245
|
+
return ImportScan.fileInfo({ edges, unknown }, unreadableLaunch, facts.unprovenCalls);
|
|
2246
|
+
}
|
|
2247
|
+
static fileInfo(info, unreadableLaunch, unprovenCalls) {
|
|
2248
|
+
const unproven = unprovenCalls === undefined ? info : { ...info, unprovenCalls };
|
|
2249
|
+
return unreadableLaunch ? { ...unproven, unreadableLaunch: true } : unproven;
|
|
2250
|
+
}
|
|
2251
|
+
walkEdges(file) {
|
|
2252
|
+
const info = this.analyse(file);
|
|
2253
|
+
if (info.unprovenCalls === undefined) {
|
|
2254
|
+
return info.edges;
|
|
2255
|
+
}
|
|
2256
|
+
const known = this.unprovenEdges.get(file);
|
|
2257
|
+
if (known !== undefined) {
|
|
2258
|
+
return known;
|
|
2259
|
+
}
|
|
2260
|
+
const edges = [...info.edges];
|
|
2261
|
+
for (const specifier of info.unprovenCalls) {
|
|
2262
|
+
this.checkWalkDeadline();
|
|
2263
|
+
const resolved = this.resolve(specifier, file);
|
|
2264
|
+
if (resolved.kind === "follow") {
|
|
2265
|
+
edges.push({ type: "possible", target: resolved.file, call: true });
|
|
2266
|
+
}
|
|
2267
|
+
}
|
|
2268
|
+
this.unprovenEdges.set(file, edges);
|
|
2269
|
+
return edges;
|
|
2270
|
+
}
|
|
2271
|
+
addHandedEdges(handOff, from, edges) {
|
|
2272
|
+
const targets = this.timedHandOffs(() => this.handedTargets(handOff, from), []);
|
|
2273
|
+
for (const target of targets ?? []) {
|
|
2274
|
+
edges.push({ type: "handed", target });
|
|
2275
|
+
}
|
|
2276
|
+
return targets === undefined;
|
|
2277
|
+
}
|
|
2278
|
+
handedTargets(handOff, from) {
|
|
2279
|
+
try {
|
|
2280
|
+
return this.handedTarget(handOff, from);
|
|
2281
|
+
} catch {
|
|
2282
|
+
return;
|
|
2283
|
+
}
|
|
2284
|
+
}
|
|
2285
|
+
handedTarget(handOff, from) {
|
|
2286
|
+
const dir = path.dirname(from);
|
|
2287
|
+
const { specifier } = handOff;
|
|
2288
|
+
const spelling = {
|
|
2289
|
+
path: () => path.resolve(this.input.projectDir, handOff.cwd ?? PROJECT_DIRECTORY, specifier),
|
|
2290
|
+
url: () => attempt(() => fileURLToPath(new URL(specifier, pathToFileURL(from)))),
|
|
2291
|
+
dir: () => path.resolve(dir, specifier),
|
|
2292
|
+
import: () => attempt(() => Bun.resolveSync(specifier, dir)),
|
|
2293
|
+
require: () => attempt(() => this.requireFrom(dir, from).resolve(specifier))
|
|
2294
|
+
}[handOff.how]();
|
|
2295
|
+
if (handOff.packageScript === true && this.mayRunPackageScript(specifier, path.resolve(this.input.projectDir, handOff.cwd ?? PROJECT_DIRECTORY))) {
|
|
2296
|
+
return;
|
|
2297
|
+
}
|
|
2298
|
+
if (spelling !== undefined && namesPackageDirectory(spelling, handOff)) {
|
|
2299
|
+
return [];
|
|
2300
|
+
}
|
|
2301
|
+
const file = spelling === undefined ? undefined : entryFile(spelling, handOff);
|
|
2302
|
+
const target = file === undefined ? undefined : this.identity(file);
|
|
2303
|
+
if (target === undefined || !isFile(target)) {
|
|
2304
|
+
return;
|
|
2305
|
+
}
|
|
2306
|
+
return this.candidatesAt.has(target) || this.isEntryPoint(target) ? [target] : [];
|
|
2307
|
+
}
|
|
2308
|
+
mayRunPackageScript(word, dir) {
|
|
2309
|
+
for (let at = dir;; at = path.dirname(at)) {
|
|
2310
|
+
const file = path.join(at, "package.json");
|
|
2311
|
+
if (isFile(file)) {
|
|
2312
|
+
try {
|
|
2313
|
+
const scripts = Bun.JSONC.parse(readFileSync(file, "utf8"))?.scripts;
|
|
2314
|
+
return scripts !== undefined && (!isPlainObject(scripts) || Object.hasOwn(scripts, word));
|
|
2315
|
+
} catch {
|
|
2316
|
+
return true;
|
|
2317
|
+
}
|
|
2318
|
+
}
|
|
2319
|
+
if (path.dirname(at) === at) {
|
|
2320
|
+
return false;
|
|
2321
|
+
}
|
|
2322
|
+
}
|
|
2323
|
+
}
|
|
2324
|
+
isEntryPoint(target) {
|
|
2325
|
+
return CODE_FILE_RE.test(target) && !this.isPackageCode(target) && statSync(target, { throwIfNoEntry: false })?.isFile() === true;
|
|
757
2326
|
}
|
|
758
2327
|
resolveGeneratedJsx(specifier, from) {
|
|
759
2328
|
const sibling = jsxRuntimeSibling(specifier);
|
|
@@ -765,7 +2334,7 @@ class ImportScan {
|
|
|
765
2334
|
return [{ type: "static", target }];
|
|
766
2335
|
}
|
|
767
2336
|
if (kind === "call") {
|
|
768
|
-
return [{ type: this.rootSet.has(from) ? "named" : "possible", target }];
|
|
2337
|
+
return [{ type: this.rootSet.has(from) ? "named" : "possible", target, call: true }];
|
|
769
2338
|
}
|
|
770
2339
|
return this.candidatesAt.has(target) ? [{ type: "possible", target }] : [];
|
|
771
2340
|
}
|
|
@@ -851,10 +2420,22 @@ class ImportScan {
|
|
|
851
2420
|
return real(query === -1 ? resolved : resolved.slice(0, query));
|
|
852
2421
|
}
|
|
853
2422
|
}
|
|
854
|
-
|
|
2423
|
+
var since = (started) => Number((performance.now() - started).toFixed(1));
|
|
2424
|
+
function scanInTwoParts(input, answered) {
|
|
855
2425
|
const started = performance.now();
|
|
856
|
-
const
|
|
857
|
-
|
|
2426
|
+
const scan = new ImportScan(input);
|
|
2427
|
+
answered({ ...scan.primary(), elapsedMs: since(started) });
|
|
2428
|
+
const handOffStarted = performance.now();
|
|
2429
|
+
return { ...scan.handOffs(), elapsedMs: since(handOffStarted) };
|
|
2430
|
+
}
|
|
2431
|
+
function scanImports(input) {
|
|
2432
|
+
let primary;
|
|
2433
|
+
const handOffs = scanInTwoParts(input, (answered) => {
|
|
2434
|
+
primary = answered;
|
|
2435
|
+
});
|
|
2436
|
+
const { notes, ...rest } = primary;
|
|
2437
|
+
const handed = handOffs.handed.map(({ launchers, ...entry }) => launchers === undefined ? entry : { ...entry, launchers: launchers.map((index) => input.tests[index]) });
|
|
2438
|
+
return { ...rest, handed, notes: [...notes, ...handOffs.notes] };
|
|
858
2439
|
}
|
|
859
2440
|
function parseScanInput(text) {
|
|
860
2441
|
const value = JSON.parse(text);
|
|
@@ -868,21 +2449,53 @@ function parseScanInput(text) {
|
|
|
868
2449
|
}
|
|
869
2450
|
return list;
|
|
870
2451
|
};
|
|
871
|
-
const { tsconfig, projectDir } = value;
|
|
2452
|
+
const { tsconfig, projectDir, handOffBudget } = value;
|
|
872
2453
|
if (tsconfig !== null && (typeof tsconfig !== "string" || !path.isAbsolute(tsconfig))) {
|
|
873
2454
|
throw new Error("tsconfig must be an absolute path or null");
|
|
874
2455
|
}
|
|
875
2456
|
if (typeof projectDir !== "string" || !path.isAbsolute(projectDir)) {
|
|
876
2457
|
throw new Error("projectDir must be an absolute path");
|
|
877
2458
|
}
|
|
878
|
-
|
|
2459
|
+
const roots = paths("roots");
|
|
2460
|
+
const tests = paths("tests");
|
|
2461
|
+
const rootSet = new Set(roots);
|
|
2462
|
+
if (!tests.every((file) => rootSet.has(file))) {
|
|
2463
|
+
throw new Error("tests must be roots");
|
|
2464
|
+
}
|
|
2465
|
+
const input = { eager: paths("eager"), roots, tests, always: paths("always"), tsconfig, projectDir };
|
|
2466
|
+
return handOffBudget === undefined ? input : { ...input, handOffBudget: parseHandOffBudget(handOffBudget) };
|
|
2467
|
+
}
|
|
2468
|
+
var isLimit = (limit, whole) => typeof limit === "number" && limit >= 0 && (whole ? Number.isSafeInteger(limit) : Number.isFinite(limit));
|
|
2469
|
+
function parseHandOffBudget(value) {
|
|
2470
|
+
const budget = isPlainObject(value) ? value : {};
|
|
2471
|
+
const { wallMs, files, ids } = budget;
|
|
2472
|
+
if (!isLimit(wallMs, false) || !isLimit(files, true) || !isLimit(ids, true)) {
|
|
2473
|
+
throw new Error("handOffBudget must be an object of non-negative limits wallMs, files and ids");
|
|
2474
|
+
}
|
|
2475
|
+
return { wallMs, files, ids };
|
|
2476
|
+
}
|
|
2477
|
+
function writeNow(fd, text) {
|
|
2478
|
+
const bytes = Buffer.from(text);
|
|
2479
|
+
for (let offset = 0;offset < bytes.length; ) {
|
|
2480
|
+
try {
|
|
2481
|
+
offset += writeSync(fd, bytes, offset);
|
|
2482
|
+
} catch (error) {
|
|
2483
|
+
if (error.code !== "EAGAIN") {
|
|
2484
|
+
throw error;
|
|
2485
|
+
}
|
|
2486
|
+
}
|
|
2487
|
+
}
|
|
879
2488
|
}
|
|
880
2489
|
function main(inputFile) {
|
|
881
2490
|
try {
|
|
882
2491
|
if (inputFile === undefined) {
|
|
883
2492
|
throw new Error("usage: bun --no-macros --no-install --config=<an empty file> import-scan.js <input.json>");
|
|
884
2493
|
}
|
|
885
|
-
|
|
2494
|
+
const handOffs = scanInTwoParts(parseScanInput(readFileSync(inputFile, "utf8")), (primary) => {
|
|
2495
|
+
writeNow(1, `${JSON.stringify(primary)}
|
|
2496
|
+
`);
|
|
2497
|
+
});
|
|
2498
|
+
writeNow(1, `${JSON.stringify(handOffs)}
|
|
886
2499
|
`);
|
|
887
2500
|
} catch (error) {
|
|
888
2501
|
process.stderr.write(`stryker-bun-runner import scan: ${error.message}
|
|
@@ -894,16 +2507,26 @@ if (import.meta.main) {
|
|
|
894
2507
|
main(process.argv[2]);
|
|
895
2508
|
}
|
|
896
2509
|
export {
|
|
2510
|
+
HAND_OFF_BUDGET,
|
|
897
2511
|
INSTRUMENTER_GLOBAL_IDIOM,
|
|
898
2512
|
MACRO_ATTRIBUTE,
|
|
2513
|
+
MAX_LAUNCHERS,
|
|
2514
|
+
MAX_MUTANT_ID_LENGTH,
|
|
899
2515
|
SCAN_DEFINE,
|
|
900
2516
|
ScanProblem,
|
|
901
2517
|
analyseOutput,
|
|
902
2518
|
generatedJsxResolution,
|
|
903
2519
|
generatedJsxRuntimes,
|
|
2520
|
+
instrumentationHeader,
|
|
2521
|
+
instrumentationHeaderSource,
|
|
2522
|
+
instrumentedMutantIds,
|
|
904
2523
|
jsxRuntimeSibling,
|
|
905
2524
|
parseScanInput,
|
|
2525
|
+
printedCredit,
|
|
906
2526
|
readCompilerOptions,
|
|
2527
|
+
readHandOffs,
|
|
2528
|
+
readLaunches,
|
|
907
2529
|
scanImports,
|
|
2530
|
+
scanInTwoParts,
|
|
908
2531
|
tokenize
|
|
909
2532
|
};
|