package-json-scripts-portability 0.0.2 → 0.0.4

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.
Files changed (71) hide show
  1. package/README.md +60 -19
  2. package/dist/analyze-script.d.mts +10 -3
  3. package/dist/analyze-script.mjs +24 -15
  4. package/dist/command-checker.mjs +331 -99
  5. package/dist/command-usage.mjs +85 -36
  6. package/dist/commands.mjs +124 -5
  7. package/dist/context.mjs +8 -1
  8. package/dist/fixes.d.mts +1 -1
  9. package/dist/fixes.mjs +193 -49
  10. package/dist/line-break-checker.mjs +40 -7
  11. package/dist/messages.mjs +9 -1
  12. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/sorted-array-insertion-index-with.mjs +33 -0
  13. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/sorted-array-insertion-index.mjs +10 -0
  14. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/semver/is-semver-in-range.mjs +5 -0
  15. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/semver/parse-semver.mjs +26 -0
  16. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/string/substring-after.mjs +24 -0
  17. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/_internal/isUnsafeProperty.mjs +18 -0
  18. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/_internal/setOwnValue.mjs +23 -0
  19. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/_internal/toPropertyKey.mjs +16 -0
  20. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/baseToString.mjs +26 -0
  21. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/hasOwn.mjs +13 -0
  22. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/isDeepKey.mjs +39 -0
  23. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/isIndex.mjs +10 -0
  24. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/tags.mjs +2 -1
  25. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/toKey.mjs +15 -0
  26. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/array/sortedIndex.mjs +36 -0
  27. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/array/sortedIndexBy.mjs +54 -0
  28. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/array/sortedLastIndex.mjs +34 -0
  29. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/array/sortedLastIndexBy.mjs +21 -0
  30. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/function/identity.mjs +5 -0
  31. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/object/cloneDeep.mjs +52 -0
  32. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/object/cloneDeepWith.mjs +179 -0
  33. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/object/get.mjs +67 -0
  34. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/object/has.mjs +53 -0
  35. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/object/property.mjs +26 -0
  36. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isArguments.mjs +26 -0
  37. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isBuffer.mjs +24 -0
  38. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isMatch.mjs +34 -0
  39. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isMatchWith.mjs +163 -0
  40. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isNaN.mjs +18 -0
  41. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isNil.mjs +24 -0
  42. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isNumber.mjs +24 -0
  43. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isObjectLike.mjs +28 -0
  44. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isSymbol.mjs +19 -0
  45. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isTypedArray.mjs +21 -0
  46. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/matches.mjs +36 -0
  47. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/matchesProperty.mjs +47 -0
  48. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/util/iteratee.mjs +46 -0
  49. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/util/toPath.mjs +75 -0
  50. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/util/toString.mjs +23 -0
  51. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/function/identity.mjs +24 -0
  52. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isEqualWith.mjs +1 -2
  53. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isNil.mjs +24 -0
  54. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isNull.mjs +25 -0
  55. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isPrimitive.mjs +26 -0
  56. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isSymbol.mjs +28 -0
  57. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isTypedArray.mjs +20 -0
  58. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isUndefined.mjs +25 -0
  59. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/remeda/dist/binarySearchCutoffIndex-Bu-OMJ0Z.mjs +26 -0
  60. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/remeda/dist/sortedIndexWith.mjs +7 -0
  61. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/verkit/dist/comparison-CmVirWIW.mjs +108 -0
  62. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/verkit/dist/range-C5wjdo9a.mjs +164 -0
  63. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/verkit/dist/set-BGFWKKE8.mjs +38 -0
  64. package/dist/redirection-checker.mjs +76 -24
  65. package/dist/reporter.mjs +63 -18
  66. package/dist/runners.d.mts +2 -5
  67. package/dist/runners.mjs +103 -22
  68. package/dist/script-analyzer.mjs +457 -155
  69. package/dist/word-checker.mjs +232 -103
  70. package/dist/words.mjs +34 -7
  71. package/package.json +27 -23
@@ -1,13 +1,16 @@
1
- import { bashLabel } from "./context.mjs";
1
+ import { bashLabel, originalText } from "./context.mjs";
2
2
  import { removal, replacement } from "./fixes.mjs";
3
- import { BASH_SUGGESTION, NODE_SCRIPT_SUGGESTION, describeReplacement, truncate } from "./messages.mjs";
3
+ import { BASH_4_SUGGESTION, NODE_SCRIPT_SUGGESTION, describeReplacement, truncate } from "./messages.mjs";
4
4
  import { POSIX_PLATFORMS } from "./platforms.mjs";
5
5
  import { mapGetOrInsert } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/map/map-get-or-insert.mjs";
6
6
  import { setHas } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/set/set-has.mjs";
7
+ import { sortedArrayInsertionIndex } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/sorted-array-insertion-index.mjs";
7
8
  import { POSIX_PATH, WHITESPACE, staticValue } from "./words.mjs";
8
9
  // src/redirection-checker.ts
9
10
  /** The target of a redirection that duplicates or closes a file descriptor, e.g. `1` or `-` */
10
11
  const DESCRIPTOR = /^(?:\d+|-)$/;
12
+ /** The target of a redirection that moves a file descriptor, e.g. `3-` */
13
+ const MOVED_DESCRIPTOR = /^\d+-$/;
11
14
  /** Device names, which cmd.exe treats as the device in any directory */
12
15
  const WINDOWS_DEVICES = /* @__PURE__ */ new Set([
13
16
  "aux",
@@ -15,9 +18,34 @@ const WINDOWS_DEVICES = /* @__PURE__ */ new Set([
15
18
  "nul",
16
19
  "prn"
17
20
  ]);
21
+ /** Redirection operators that write the standard output to a file, e.g. `>` */
22
+ const OUTPUT_OPERATORS = /* @__PURE__ */ new Set([
23
+ "&>",
24
+ "&>>",
25
+ ">",
26
+ ">&",
27
+ ">>",
28
+ ">|"
29
+ ]);
30
+ /** Checks whether a redirection is of the standard output, e.g. `>` or `1>`, but not `2>` */
31
+ const isOfStandardOutput = ({ descriptor }) => descriptor == null || descriptor.type === "FileDescriptor" && descriptor.value === 1;
32
+ /** Checks whether a redirection is of the standard output or error, e.g. `2>`, but not `3>` */
33
+ const isOfStandardStream = (redirection) => isOfStandardOutput(redirection) || redirection.descriptor?.type === "FileDescriptor" && redirection.descriptor.value === 2;
34
+ /**
35
+ * Checks whether a redirection writes the standard output to a file, e.g. `> out.log`, but not
36
+ * `>&2`, `2> err.log` or `> /dev/null`.
37
+ * @param redirection The redirection
38
+ * @returns Whether it does
39
+ */
40
+ const doesRedirectOutput = (redirection) => {
41
+ if (redirection.type !== "Redirect" || !OUTPUT_OPERATORS.has(redirection.operator)) return false;
42
+ const { operator, target } = redirection;
43
+ const value = target && staticValue(target);
44
+ return isOfStandardOutput(redirection) && !(operator === ">&" && value != null && DESCRIPTOR.test(value)) && !POSIX_PATH.test(value || "") && !setHas(WINDOWS_DEVICES, value?.toLowerCase());
45
+ };
18
46
  /** Checks redirections, e.g. for Bash-specific operators and device files */
19
47
  var RedirectionChecker = class {
20
- /** End offsets of here-documents, by the source that they're in */
48
+ /** End offsets of here-documents in ascending order, by the source that they're in */
21
49
  hereDocumentEnds = /* @__PURE__ */ new Map();
22
50
  #reporter;
23
51
  #words;
@@ -25,11 +53,15 @@ var RedirectionChecker = class {
25
53
  this.#reporter = reporter;
26
54
  this.#words = words;
27
55
  }
28
- checkEchoToFile(node, context) {
29
- const doesWriteFile = node.redirects.some((redirect) => redirect.type === "Redirect" && (redirect.operator === ">" || redirect.operator === ">>") && redirect.descriptor == null && !POSIX_PATH.test(redirect.target?.value || "") && !setHas(WINDOWS_DEVICES, redirect.target?.value.toLowerCase()));
56
+ /**
57
+ * Reports a double-quoted string that `echo` writes to a file or a pipe.
58
+ * @param node The `echo` command, whose output doesn't go to the terminal
59
+ * @param context Where the command is
60
+ */
61
+ checkQuotedEcho(node, context) {
30
62
  const quoted = node.args.find((argument) => argument.type === "Word" && argument.parts?.some((part) => part.type === "DoubleQuoted"));
31
- if (doesWriteFile && quoted) this.#reporter.reportSyntax(context, quoted, "posix", true, "inconsistentBehavior", {
32
- reason: "cmd.exe's `echo` writes the quotes into the file",
63
+ if (quoted) this.#reporter.reportSyntax(context, quoted, "posix", true, "inconsistentBehavior", {
64
+ reason: "cmd.exe's `echo` writes the quotes too",
33
65
  suggestion: NODE_SCRIPT_SUGGESTION,
34
66
  syntax: "Writing a double-quoted string with `echo`"
35
67
  });
@@ -41,15 +73,18 @@ var RedirectionChecker = class {
41
73
  * @param isWholeCommand Whether the redirection is the whole command, e.g. `>log` in `tsc | >log`
42
74
  */
43
75
  checkRedirection(redirection, context, isWholeCommand = false) {
44
- const text = context.source.slice(redirection.pos, redirection.end);
76
+ const text = originalText(context, redirection);
45
77
  switch (redirection.type) {
46
- case "HereDoc":
78
+ case "HereDoc": {
47
79
  this.#reporter.reportSyntax(context, redirection, "posix", false, "unsupportedSyntax", {
48
80
  suggestion: NODE_SCRIPT_SUGGESTION,
49
81
  syntax: `The here-document \`${redirection.operator}\``
50
82
  });
51
- if (redirection.closing) mapGetOrInsert(this.hereDocumentEnds, context.source, []).push(redirection.closing.end);
83
+ const end = redirection.closing?.end ?? redirection.body.end;
84
+ const ends = mapGetOrInsert(this.hereDocumentEnds, context.source, []);
85
+ ends.splice(sortedArrayInsertionIndex(ends, end, { rightmost: true }), 0, end);
52
86
  return;
87
+ }
53
88
  case "HereString":
54
89
  this.#reporter.reportSyntax(context, redirection, "bash", true, "unsupportedSyntax", {
55
90
  suggestion: "Pipe the value into the command instead.",
@@ -58,17 +93,21 @@ var RedirectionChecker = class {
58
93
  break;
59
94
  case "Redirect": {
60
95
  const { descriptor, operator } = redirection;
61
- if (descriptor?.type === "FileDescriptorVariable") this.#reporter.reportSyntax(context, descriptor, "bash", false, "unsupportedSyntax", {
62
- suggestion: BASH_SUGGESTION,
63
- syntax: `The Bash-specific file descriptor variable \`{${descriptor.name}}\``
96
+ if (descriptor?.type === "FileDescriptorVariable") this.#reporter.reportSyntax(context, descriptor, "bash4", false, "unsupportedSyntax", {
97
+ suggestion: BASH_4_SUGGESTION,
98
+ syntax: `The Bash 4 file descriptor variable \`{${descriptor.name}}\``
64
99
  });
65
100
  const separator = redirection.pos > 0 && !WHITESPACE.test(context.source.charAt(redirection.pos - 1)) ? " " : "";
66
- const redirectionFix = (fixed) => ({
67
- description: describeReplacement(text, fixed),
101
+ const bothOutputsFix = (target, fixedOperator) => ({
102
+ description: describeReplacement(text, `${fixedOperator} ${originalText(context, target)} 2>&1`),
68
103
  edits: [{
69
- end: redirection.end,
104
+ end: target.pos,
70
105
  pos: redirection.pos,
71
- text: `${separator}${fixed}`
106
+ text: `${separator}${fixedOperator} `
107
+ }, {
108
+ end: redirection.end,
109
+ pos: redirection.end,
110
+ text: " 2>&1"
72
111
  }]
73
112
  });
74
113
  if (operator === "&>" || operator === "&>>") {
@@ -76,7 +115,7 @@ var RedirectionChecker = class {
76
115
  this.#reporter.reportSyntax(context, redirection, kind, false, "unsupportedSyntax", {
77
116
  suggestion: `Use \`${operator.slice(1)} file 2>&1\` instead.`,
78
117
  syntax: `The ${bashLabel(kind)} \`${operator}\` redirection`
79
- }, redirection.target && redirectionFix(`${operator.slice(1)} ${redirection.target.text} 2>&1`));
118
+ }, redirection.target && bothOutputsFix(redirection.target, operator.slice(1)));
80
119
  } else if (operator === ">|" || operator === "<>") {
81
120
  const position = context.source.indexOf(operator, descriptor?.end || redirection.pos);
82
121
  this.#reporter.reportSyntax(context, redirection, "posix", false, "unsupportedSyntax", {
@@ -90,28 +129,41 @@ var RedirectionChecker = class {
90
129
  if (redirection.target && (operator === ">&" || operator === "<&")) {
91
130
  const target = staticValue(redirection.target);
92
131
  if (target != null && DESCRIPTOR.test(target)) return;
132
+ if (target != null && MOVED_DESCRIPTOR.test(target)) {
133
+ const descriptorNumber = target.slice(0, -1);
134
+ this.#reporter.reportSyntax(context, redirection, "bash", false, "unsupportedSyntax", {
135
+ suggestion: `Use \`${truncate(text.slice(0, -1))} ${descriptorNumber}>&-\` instead.`,
136
+ syntax: `The Bash-specific \`${truncate(text)}\` file descriptor move`
137
+ });
138
+ return;
139
+ }
93
140
  if (operator === ">&" && target != null) this.#reporter.reportSyntax(context, redirection, "bash", false, "unsupportedSyntax", {
94
141
  suggestion: `Use \`> ${truncate(target)} 2>&1\` instead.`,
95
142
  syntax: "The Bash-specific `>&` redirection to a file"
96
- }, descriptor ? void 0 : redirectionFix(`> ${redirection.target.text} 2>&1`));
143
+ }, descriptor ? void 0 : bothOutputsFix(redirection.target, ">"));
97
144
  }
98
145
  break;
99
146
  }
100
147
  }
101
148
  if (!redirection.target) return;
102
149
  const target = staticValue(redirection.target);
103
- if (target != null && WINDOWS_DEVICES.has(target.toLowerCase())) this.#reporter.report(context, redirection.target, "inconsistentBehavior", {
150
+ const isHereString = redirection.type === "HereString";
151
+ if (target != null && !isHereString && WINDOWS_DEVICES.has(target.toLowerCase())) this.#reporter.report(context, redirection.target, "inconsistentBehavior", {
104
152
  reason: `POSIX shells create a file named \`${target}\` instead`,
105
153
  suggestion: "Remove the redirection or use a Node.js script instead.",
106
154
  syntax: `The redirection to \`${target}\``
107
155
  }, POSIX_PLATFORMS);
108
- this.#words.checkWord(redirection.target, context, { redirectTarget: true });
109
- if (target == null || target === "/dev/null" && context.portable) return;
110
- const doesDiscardOutput = !isWholeCommand && redirection.type === "Redirect" && (redirection.operator === ">" || redirection.operator === ">>") && target === "/dev/null";
156
+ this.#words.checkWord(redirection.target, context, {
157
+ redirectTarget: true,
158
+ skipGlobs: isHereString,
159
+ text: isHereString
160
+ });
161
+ if (target == null || isHereString || target === "/dev/null" && context.portable) return;
162
+ const doesDiscardOutput = !isWholeCommand && (redirection.operator === ">" || redirection.operator === ">>") && isOfStandardStream(redirection) && target === "/dev/null" && !(context.outputRedirected && isOfStandardOutput(redirection));
111
163
  this.#words.checkPosixPath(redirection.target, target, context, doesDiscardOutput ? {
112
164
  description: `Remove \`${truncate(text)}\`.`,
113
165
  edits: [removal(context.source, redirection.pos, redirection.end)]
114
166
  } : void 0);
115
167
  }
116
168
  };
117
- export { RedirectionChecker };
169
+ export { RedirectionChecker, doesRedirectOutput };
package/dist/reporter.mjs CHANGED
@@ -1,5 +1,6 @@
1
1
  import { rangeInScript } from "./context.mjs";
2
2
  import { MACOS_PLATFORMS, NOT_LINUX_OR_WINDOWS, PLATFORMS, WINDOWS_PLATFORMS } from "./platforms.mjs";
3
+ import { arrayIntersection } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-intersection.mjs";
3
4
  import { clamp } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/math/clamp.mjs";
4
5
  // src/reporter.ts
5
6
  /**
@@ -37,15 +38,69 @@ const SYNTAX_PLATFORMS = {
37
38
  }
38
39
  };
39
40
  var Reporter = class {
41
+ /** Reported issues, including duplicates */
40
42
  issues = [];
41
43
  /**
42
- * Length of the analyzed script, which unbash's positions can exceed, e.g. after a trailing `\`
44
+ * Issues that make a command fail, e.g. `rm` on Windows, unlike ones in its arguments, with the
45
+ * platforms where it fails, if only on some of the issue's platforms
43
46
  */
47
+ #failures = /* @__PURE__ */ new WeakMap();
48
+ /** Length of the analyzed script */
44
49
  #length;
45
- #seen = /* @__PURE__ */ new Set();
46
50
  constructor(length) {
47
51
  this.#length = length;
48
52
  }
53
+ /** The issues without duplicates by their keys, each on the platforms of all of its duplicates */
54
+ get uniqueIssues() {
55
+ const unique = /* @__PURE__ */ new Map();
56
+ this.issues.forEach((issue) => {
57
+ const key = issueKey(issue);
58
+ const existing = unique.get(key);
59
+ unique.set(key, existing ? {
60
+ ...existing,
61
+ platforms: arrayIntersection(PLATFORMS, [...existing.platforms, ...issue.platforms])
62
+ } : issue);
63
+ });
64
+ return unique;
65
+ }
66
+ isFailure(issue) {
67
+ return this.#failures.has(issue);
68
+ }
69
+ /**
70
+ * Gets where an issue makes its command fail.
71
+ * @param issue The issue
72
+ * @returns The platforms, which are none if it doesn't make the command fail
73
+ */
74
+ failingPlatforms(issue) {
75
+ if (!this.#failures.has(issue)) return [];
76
+ const platforms = this.#failures.get(issue);
77
+ return platforms ? issue.platforms.filter((platform) => platforms.includes(platform)) : issue.platforms;
78
+ }
79
+ /**
80
+ * Marks the issues reported since an index as ones that make a command fail, unless they are
81
+ * marked already.
82
+ * @param start Index of the first issue to mark
83
+ * @param platforms Where they make the command fail, if only on some of their platforms, e.g.
84
+ * where the issue is that it's not known what the command is meant for
85
+ */
86
+ markFailures(start, platforms) {
87
+ this.issues.slice(start).forEach((issue) => {
88
+ if (!this.#failures.has(issue)) this.#failures.set(issue, platforms);
89
+ });
90
+ }
91
+ /**
92
+ * Removes platforms from the issues reported since an index, and drops the issues that are left
93
+ * without platforms.
94
+ * @param start Index of the first issue to update
95
+ * @param platformsOf Gets the platforms to remove from an issue
96
+ */
97
+ removePlatforms(start, platformsOf) {
98
+ this.issues.splice(start).forEach((issue) => {
99
+ const removed = platformsOf(issue);
100
+ if (removed) issue.platforms = issue.platforms.filter((platform) => !removed.has(platform));
101
+ if (issue.platforms.length > 0) this.issues.push(issue);
102
+ });
103
+ }
49
104
  /**
50
105
  * Maps a fix's positions to the analyzed script.
51
106
  * @param context Where the fix is
@@ -59,7 +114,7 @@ var Reporter = class {
59
114
  pos: context.offset + pos,
60
115
  text
61
116
  }));
62
- return edits.every(({ end, pos, text }) => pos >= 0 && pos <= end && end <= this.#length && context.fixExclusions.every((exclusion) => !exclusion.test(text))) ? {
117
+ return !fix.description.includes("￿") && edits.every(({ end, pos, text }) => pos >= 0 && pos <= end && end <= this.#length && !text.includes("￿") && context.fixExclusions.every((exclusion) => !exclusion.test(text))) ? {
63
118
  description: fix.description,
64
119
  edits
65
120
  } : void 0;
@@ -69,24 +124,14 @@ var Reporter = class {
69
124
  if (remaining.length > 0) {
70
125
  const range = rangeInScript(context, node);
71
126
  const pos = clamp(range.pos, 0, this.#length);
72
- const end = clamp(range.end, pos, this.#length);
73
- const key = issueKey({
127
+ this.issues.push({
74
128
  data,
75
- end,
129
+ end: clamp(range.end, pos, this.#length),
130
+ fix: this.#mapFix(context, fix),
76
131
  messageId,
132
+ platforms: remaining,
77
133
  pos
78
134
  });
79
- if (!this.#seen.has(key)) {
80
- this.#seen.add(key);
81
- this.issues.push({
82
- data,
83
- end,
84
- fix: this.#mapFix(context, fix),
85
- messageId,
86
- platforms: remaining,
87
- pos
88
- });
89
- }
90
135
  }
91
136
  return remaining;
92
137
  }
@@ -95,4 +140,4 @@ var Reporter = class {
95
140
  return this.report(context, node, messageId, data, platforms, fix);
96
141
  }
97
142
  };
98
- export { Reporter, issueKey };
143
+ export { Reporter };
@@ -1,14 +1,11 @@
1
1
  /** What can run the scripts: a package manager, or `node` for `node --run` */
2
2
  export type Runner = 'aube' | 'node' | 'npm' | 'pnpm' | 'pnpm-10' | 'upm' | 'yarn' | 'yarn-classic';
3
3
  export declare const RUNNERS: readonly Runner[];
4
- /**
5
- * Scripts that run with any package manager by default, as they run on the machine of whoever
6
- * installs the package: install scripts, and `prepare`, which runs when it's installed from git
7
- */
4
+ /** Scripts that any package manager may run by default */
8
5
  export declare const RUNNER_AGNOSTIC_SCRIPTS: ReadonlySet<string>;
9
6
  /**
10
7
  * Gets the runner for a package manager, e.g. the one in the `packageManager` or
11
- * `devEngines.packageManager` field of package.json.
8
+ * `devEngines.packageManager` field of `package.json`.
12
9
  * @param name Name of the package manager, e.g. `yarn`
13
10
  * @param version Its version or version range, which tells Yarn v1 from Yarn v2+, and pnpm v10
14
11
  * from later versions, e.g. `1.22.22`
package/dist/runners.mjs CHANGED
@@ -2,6 +2,8 @@ import { listFormat } from "./messages.mjs";
2
2
  import { allUnionMembers } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/ts/all-union-members.mjs";
3
3
  import { arrayDifference } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-difference.mjs";
4
4
  import { arrayIntersection } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-intersection.mjs";
5
+ import { isSemverInRange } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/semver/is-semver-in-range.mjs";
6
+ import { parseSemver } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/semver/parse-semver.mjs";
5
7
  import { setHas } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/set/set-has.mjs";
6
8
  // src/runners.ts
7
9
  const RUNNERS = allUnionMembers()([
@@ -14,12 +16,9 @@ const RUNNERS = allUnionMembers()([
14
16
  "yarn",
15
17
  "yarn-classic"
16
18
  ]);
17
- /** Runners that are package managers: all of them but `node`, which doesn't install packages */
19
+ /** Runners that are package managers, i.e. all of them but `node` */
18
20
  const PACKAGE_MANAGER_RUNNERS = arrayDifference(RUNNERS, ["node"]);
19
- /**
20
- * Scripts that run with any package manager by default, as they run on the machine of whoever
21
- * installs the package: install scripts, and `prepare`, which runs when it's installed from git
22
- */
21
+ /** Scripts that any package manager may run by default */
23
22
  const RUNNER_AGNOSTIC_SCRIPTS = /* @__PURE__ */ new Set([
24
23
  "install",
25
24
  "postinstall",
@@ -28,15 +27,15 @@ const RUNNER_AGNOSTIC_SCRIPTS = /* @__PURE__ */ new Set([
28
27
  ]);
29
28
  /**
30
29
  * Checks whether any package manager may run a script.
31
- * @param scriptName The script's name in package.json
30
+ * @param scriptName The script's name in `package.json`
32
31
  * @param scripts `true` for all scripts, `false` for none, or whether each script is
33
32
  * runner-agnostic, on top of the defaults
34
33
  * @returns Whether the script is runner-agnostic
35
34
  */
36
- const isRunnerAgnosticScript = (scriptName, scripts = {}) => {
35
+ const isRunnerAgnosticScript = (scriptName, scripts) => {
37
36
  if (typeof scripts === "boolean") return scripts;
38
37
  if (scriptName == null) return false;
39
- return Object.hasOwn(scripts, scriptName) ? scripts[scriptName] === true : RUNNER_AGNOSTIC_SCRIPTS.has(scriptName);
38
+ return scripts != null && Object.hasOwn(scripts, scriptName) ? scripts[scriptName] === true : RUNNER_AGNOSTIC_SCRIPTS.has(scriptName);
40
39
  };
41
40
  const RUNNER_TRAITS = {
42
41
  aube: { label: "aube" },
@@ -73,19 +72,79 @@ const RUNNER_TRAITS = {
73
72
  label: "Yarn v1"
74
73
  }
75
74
  };
75
+ /** How pnpm runs the scripts with `shell-emulator` on, i.e. with Yarn's portable shell */
76
+ const SHELL_EMULATOR_TRAITS = {
77
+ pnpm: {
78
+ ...RUNNER_TRAITS.pnpm,
79
+ portableShell: true
80
+ },
81
+ "pnpm-10": {
82
+ ...RUNNER_TRAITS["pnpm-10"],
83
+ portableShell: true
84
+ }
85
+ };
76
86
  /** Package managers whose runner always has the same name, i.e. all of them but pnpm and Yarn */
77
87
  const SAME_NAME_RUNNERS = /* @__PURE__ */ new Set([
78
88
  "aube",
79
89
  "npm",
80
90
  "upm"
81
91
  ]);
82
- /** The first number of a version or a version range, e.g. `1` in `^1.22.0` */
83
- const MAJOR_VERSION = /\d+/;
84
- /** A version range without an upper bound, e.g. `>=10`, which includes later major versions */
85
- const UNBOUNDED_RANGE = /^\s*>/;
92
+ /** A version in a version range, e.g. `1.22` in `^1.22 || ^4` */
93
+ const RANGE_VERSIONS = /\d+(?:\.\d+){0,2}/g;
94
+ /** Maximum length of a version or a version range that is checked */
95
+ const MAX_VERSION_CHARACTERS = 256;
96
+ /**
97
+ * Gets major versions that a version or a version range matches, enough to tell whether it matches
98
+ * versions before a major version, and versions from it on.
99
+ * @param version A version, e.g. `1.22.22`, or a version range, e.g. `^1.22 || ^4`
100
+ * @param major The major version, e.g. `2`
101
+ * @returns The major versions, or none for an unknown version, e.g. `latest`
102
+ */
103
+ const matchedMajors = (version, major) => {
104
+ if (version.length > MAX_VERSION_CHARACTERS) return [];
105
+ const exact = parseSemver(version);
106
+ if (exact) return [exact.major];
107
+ return [
108
+ [
109
+ 0,
110
+ 0,
111
+ 0
112
+ ],
113
+ [
114
+ major,
115
+ 0,
116
+ 0
117
+ ],
118
+ ...[...version.matchAll(RANGE_VERSIONS)].flatMap(([text]) => {
119
+ const [first = 0, second = 0, third = 0] = text.split(".").map(Number);
120
+ return [
121
+ [
122
+ first,
123
+ second,
124
+ third
125
+ ],
126
+ [
127
+ first,
128
+ second,
129
+ third + 1
130
+ ],
131
+ [
132
+ first,
133
+ second + 1,
134
+ 0
135
+ ],
136
+ [
137
+ first + 1,
138
+ 0,
139
+ 0
140
+ ]
141
+ ];
142
+ })
143
+ ].filter((candidate) => isSemverInRange(candidate.join("."), version)).map(([candidateMajor = 0]) => candidateMajor);
144
+ };
86
145
  /**
87
146
  * Gets the runner for a package manager, e.g. the one in the `packageManager` or
88
- * `devEngines.packageManager` field of package.json.
147
+ * `devEngines.packageManager` field of `package.json`.
89
148
  * @param name Name of the package manager, e.g. `yarn`
90
149
  * @param version Its version or version range, which tells Yarn v1 from Yarn v2+, and pnpm v10
91
150
  * from later versions, e.g. `1.22.22`
@@ -93,11 +152,15 @@ const UNBOUNDED_RANGE = /^\s*>/;
93
152
  * version
94
153
  */
95
154
  const getRunner = (name, version = "") => {
96
- const major = Number(MAJOR_VERSION.exec(version)?.[0]);
97
- if (name === "pnpm") return major <= 10 && !UNBOUNDED_RANGE.test(version) ? "pnpm-10" : "pnpm";
155
+ if (name === "pnpm") {
156
+ const majors = matchedMajors(version, 11);
157
+ return majors.length > 0 && majors.every((major) => major <= 10) ? "pnpm-10" : "pnpm";
158
+ }
98
159
  if (name !== "yarn") return setHas(SAME_NAME_RUNNERS, name) ? name : void 0;
99
- if (major === 1) return "yarn-classic";
100
- return major >= 2 ? "yarn" : void 0;
160
+ const majors = matchedMajors(version, 2);
161
+ const isClassic = majors.some((major) => major <= 1);
162
+ if (isClassic === majors.some((major) => major >= 2)) return;
163
+ return isClassic ? "yarn-classic" : "yarn";
101
164
  };
102
165
  /**
103
166
  * Gets how a runner runs the scripts.
@@ -105,14 +168,32 @@ const getRunner = (name, version = "") => {
105
168
  * @param isShellEmulated Whether pnpm's `shell-emulator` setting is on
106
169
  * @returns The runner's traits
107
170
  */
108
- const runnerTraits = (runner, isShellEmulated) => isShellEmulated && (runner === "pnpm" || runner === "pnpm-10") ? {
109
- ...RUNNER_TRAITS[runner],
110
- portableShell: true
111
- } : RUNNER_TRAITS[runner];
171
+ const runnerTraits = (runner, isShellEmulated) => isShellEmulated && SHELL_EMULATOR_TRAITS[runner] || RUNNER_TRAITS[runner];
172
+ /**
173
+ * Reads a runner's traits and remembers the values that were read.
174
+ * An analysis gives the same result for all runners with the same values of the traits it read, so
175
+ * it must read them only through this
176
+ */
177
+ var TraitsReader = class {
178
+ #readValues = /* @__PURE__ */ new Map();
179
+ #traits;
180
+ constructor(traits) {
181
+ this.#traits = traits;
182
+ }
183
+ get(key) {
184
+ const value = this.#traits[key];
185
+ this.#readValues.set(key, value);
186
+ return value;
187
+ }
188
+ /** Checks whether other traits have the same values as the ones that were read */
189
+ matches(traits) {
190
+ return this.#readValues.entries().every(([key, value]) => traits[key] === value);
191
+ }
192
+ };
112
193
  /**
113
194
  * Formats runners as a list for people, e.g. "pnpm and Yarn v1".
114
195
  * @param runners The runners to format
115
196
  * @returns The formatted list
116
197
  */
117
198
  const formatRunners = (runners) => listFormat.format(arrayIntersection(RUNNERS, runners).map((runner) => RUNNER_TRAITS[runner].label));
118
- export { PACKAGE_MANAGER_RUNNERS, RUNNERS, RUNNER_AGNOSTIC_SCRIPTS, formatRunners, getRunner, isRunnerAgnosticScript, runnerTraits };
199
+ export { PACKAGE_MANAGER_RUNNERS, RUNNERS, RUNNER_AGNOSTIC_SCRIPTS, TraitsReader, formatRunners, getRunner, isRunnerAgnosticScript, runnerTraits };