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,4 +1,5 @@
1
- import { NO_LONG_OPTIONS_ON_MACOS } from "./commands.mjs";
1
+ import { NO_LONG_OPTIONS_ON_MACOS, TIMEOUT_VALUE_OPTIONS } from "./commands.mjs";
2
+ import { isDotfileName } from "./fixes.mjs";
2
3
  import { CROSS_ENV_SUGGESTION, NODE_SCRIPT_SUGGESTION, RIMRAF_SUGGESTION, describeReplacement, truncate } from "./messages.mjs";
3
4
  import { LINUX_PLATFORMS, MACOS_PLATFORMS, NOT_MACOS_OR_WINDOWS, PLATFORMS, POSIX_PLATFORMS, WINDOWS_PLATFORMS } from "./platforms.mjs";
4
5
  import { setHas } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/set/set-has.mjs";
@@ -7,6 +8,7 @@ import { ASSIGNMENT, isShortOptionCluster } from "./words.mjs";
7
8
  // src/command-usage.ts
8
9
  /** Commands that also exist in cmd.exe, whose names are case-insensitive there */
9
10
  const CMD_COMMANDS = /* @__PURE__ */ new Set([
11
+ "exit",
10
12
  "mkdir",
11
13
  "rmdir",
12
14
  "set",
@@ -32,27 +34,48 @@ const GNU_FIND_PRIMARIES = /* @__PURE__ */ new Set([
32
34
  ]);
33
35
  /** BSD `find` options, which come before the starting paths */
34
36
  const BSD_FIND_OPTIONS = /^-[EHLPXdsx]+$/;
35
- /** GNU regex escapes that BSD `sed` doesn't support, e.g. `\s` */
36
- const GNU_SED_ESCAPE = /\\[<>bsw]/i;
37
+ /**
38
+ * GNU regex escapes that BSD `sed` doesn't support, e.g. `\s`, but not after an escaped backslash,
39
+ * e.g. in `\\s`
40
+ */
41
+ const GNU_SED_ESCAPE = /(?<!\\)(?:\\\\)*(\\[<>bsw])/i;
37
42
  /** The same, in basic regexes only */
38
- const GNU_SED_BASIC_ESCAPE = /\\[+?]/;
43
+ const GNU_SED_BASIC_ESCAPE = /(?<!\\)(?:\\\\)*(\\[+?])/;
39
44
  /** GNU `xargs` options that BSD `xargs` doesn't support, e.g. `-d` */
40
45
  const GNU_XARGS_OPTION = /^(?:-[0prtx]*[adil]|--(?:arg-file|delimiter|max-lines|replace)\b)/;
41
46
  const LONG_OPTION = /^--[^-]/;
47
+ /** Long options that some BSD utilities on macOS do support, by utility */
48
+ const BSD_LONG_OPTIONS = /* @__PURE__ */ new Map([["df", ["--libxo", "--si"]], ["du", ["--libxo", "--si"]]]);
42
49
  /** Any option, e.g. `-p` */
43
50
  const POSIX_OPTION = /^-./;
44
- /** A cmd.exe option, e.g. `/s` */
45
- const CMD_OPTION = /^\/[a-z]/i;
51
+ /**
52
+ * Switches of cmd.exe's `dir`, also combined or turned off with `-`, e.g. `/b`, `/a:-d`, `/o-n` or
53
+ * `/b/s`, unlike paths like `/usr/lib`
54
+ */
55
+ const DIR_CMD_OPTION = /^(?:\/-?(?:[4bcdlnp-swx]|a:?(?:-?[adhilors])*|o:?(?:-?[degns])*|t:?[acw]?))+$/i;
46
56
  /** Including combined switches, e.g. `/s/q` */
47
57
  const RMDIR_CMD_OPTION = /^(?:\/[qs])+$/i;
48
- const TIMEOUT_CMD_OPTION = /^\/t$/i;
58
+ const EXIT_CMD_OPTION = /^\/b$/i;
59
+ /** Options of cmd.exe's `timeout`, which are case-insensitive, e.g. `/T` */
60
+ const TIMEOUT_CMD_OPTION = /^\/(?:nobreak|t)$/i;
49
61
  const ECHO_OPTIONS = /^-[Een]+$/;
50
62
  const HEAD_COUNT_OPTION = /^-[cn]$/;
51
63
  /** e.g. `-n-1` */
52
64
  const HEAD_NEGATIVE_COUNT_OPTION = /^-[cn]-\d/;
53
65
  const NEGATIVE_NUMBER = /^-\d/;
54
- const SED_EXTENDED_OPTION = /^-[a-z]*[er]/i;
55
- const SED_IN_PLACE_OPTION = /^-[a-z]*i$/i;
66
+ /**
67
+ * `-E` or `-r`, also in a cluster like `-nE`, but not after an option that takes the rest of the
68
+ * cluster as its value, like `-e` (a script) or `-i` (a backup suffix)
69
+ */
70
+ const SED_EXTENDED_OPTION = /^-(?:(?![Iefil])[A-Za-z])*[Er]/;
71
+ /** `-i` without a suffix, also in a cluster like `-ni`, but not as the value of `-e`, like in `-ei` */
72
+ const SED_IN_PLACE_OPTION = /^-(?:(?![Iefil])[A-Za-z])*[Ii]$/;
73
+ /** Options of `sed` that take the next argument as their value, e.g. `-e` or `-ne` */
74
+ const SED_VALUE_OPTION = /^(?:-(?:(?![Iefil])[A-Za-z])*[ef]|--(?:expression|file))$/;
75
+ /** `-P`, also in a cluster like `-oP`, but not as the value of an option, like in `-eP` */
76
+ const GREP_PERL_OPTION = /^(?:-(?:(?![A-DPd-fm])[A-Za-z])*P[A-Za-z]*|--perl-regexp)$/;
77
+ /** Options of `grep` that take the next argument as their value, e.g. `-e` or `-ie` */
78
+ const GREP_VALUE_OPTION = /^(?:-(?:(?![A-Dd-fm])[A-Za-z])*[A-Dd-fm]|--(?:after-context|before-context|binary-files|context|devices|directories|exclude|exclude-dir|exclude-from|file|include|label|max-count|regexp))$/;
56
79
  /** `set -o`, possibly after other options, e.g. `set -euo` */
57
80
  const SET_NAMED_OPTION = /^-[a-z]*o$/i;
58
81
  const SET_SHELL_OPTIONS = /^[+-][a-z]+$/i;
@@ -61,16 +84,27 @@ const STAT_FORMAT_OPTION = /^-[cf]/;
61
84
  /**
62
85
  * Checks a command's options for ones that don't work (the same) on all platforms, e.g. `sed -i`.
63
86
  * @param name Name that the command was invoked with, e.g. `sed`
64
- * @param commandArguments The words that follow the command name
87
+ * @param values Values of the words that follow the command name, or `undefined` for the ones that
88
+ * depend on expansions
89
+ * @param words The words themselves
65
90
  * @param options Where the command runs, and where its options end
66
91
  * @returns The issues found
67
92
  */
68
- const checkCommandUsage = (name, commandArguments, options) => {
69
- const { bash, optionsEnd, portable } = options;
70
- const values = commandArguments.map(({ value }) => value);
71
- const textOf = (index) => commandArguments[index]?.text || "";
93
+ const checkCommandUsage = (name, values, words, options) => {
94
+ const { bash, native, optionsEnd, portable } = options;
95
+ const textOf = (index) => words[index]?.text || "";
72
96
  const issues = [];
73
97
  const optionIndex = (isMatch) => values.findIndex((value, index) => index < optionsEnd && value != null && isMatch(value, index));
98
+ /**
99
+ * Gets the arguments that are values of options, which aren't options themselves, e.g. `-P` in
100
+ * `grep -e -P`.
101
+ * @param valueOption Options that take the next argument as their value
102
+ * @returns Indexes of the values
103
+ */
104
+ const optionValueIndexes = (valueOption) => values.reduce((indexes, value, index) => {
105
+ if (value != null && index < optionsEnd && !indexes.has(index) && valueOption.test(value)) indexes.add(index + 1);
106
+ return indexes;
107
+ }, /* @__PURE__ */ new Set());
74
108
  const report = (index, platforms, data, fix) => {
75
109
  if (data.syntax) issues.push({
76
110
  argumentIndex: index,
@@ -115,7 +149,7 @@ const checkCommandUsage = (name, commandArguments, options) => {
115
149
  });
116
150
  };
117
151
  if (NO_LONG_OPTIONS_ON_MACOS.has(name)) {
118
- const index = optionIndex((value) => LONG_OPTION.test(value));
152
+ const index = optionIndex((value) => LONG_OPTION.test(value) && !BSD_LONG_OPTIONS.get(name)?.includes(substringBefore(value, "=")));
119
153
  if (index !== -1) report(index, MACOS_PLATFORMS, {
120
154
  reason: `BSD \`${name}\` doesn't support GNU-style long options`,
121
155
  suggestion: "Use short options instead.",
@@ -130,12 +164,12 @@ const checkCommandUsage = (name, commandArguments, options) => {
130
164
  if (isDash && file != null && !file.includes("/")) report(0, LINUX_PLATFORMS, {
131
165
  reason: "dash only looks for the file in `PATH`, not in the current directory",
132
166
  suggestion: `Use \`. ./${truncate(file)}\` instead.`
133
- }, replaceArgument(0, `./${textOf(0)}`));
167
+ }, isDotfileName(file) ? replaceArgument(0, `./${textOf(0)}`) : void 0);
134
168
  break;
135
169
  }
136
170
  case "[":
137
171
  case "test": {
138
- const index = values.indexOf("==");
172
+ const index = words.findIndex(({ text }) => text === "==");
139
173
  if (isDash && index !== -1) report(index, LINUX_PLATFORMS, {
140
174
  reason: `${DASH} only supports \`=\` for comparing strings`,
141
175
  suggestion: "Use `=` instead.",
@@ -143,12 +177,6 @@ const checkCommandUsage = (name, commandArguments, options) => {
143
177
  }, replaceArgument(index, "="));
144
178
  break;
145
179
  }
146
- case "base64":
147
- reportOption((value) => isShortOptionCluster(value, "w"), MACOS_PLATFORMS, {
148
- reason: "BSD `base64` uses `-b` to wrap lines",
149
- suggestion: NODE_SCRIPT_SUGGESTION
150
- });
151
- break;
152
180
  case "bash":
153
181
  case "shopt": {
154
182
  const option = name === "bash" ? "-O" : "-s";
@@ -173,7 +201,7 @@ const checkCommandUsage = (name, commandArguments, options) => {
173
201
  });
174
202
  break;
175
203
  case "dir": {
176
- const index = optionIndex((value) => CMD_OPTION.test(value));
204
+ const index = optionIndex((value) => DIR_CMD_OPTION.test(value));
177
205
  if (index !== -1) report(index, LINUX_PLATFORMS, {
178
206
  reason: `\`${values[index] || ""}\` is a cmd.exe option`,
179
207
  suggestion: NODE_SCRIPT_SUGGESTION
@@ -198,6 +226,14 @@ const checkCommandUsage = (name, commandArguments, options) => {
198
226
  }
199
227
  break;
200
228
  }
229
+ case "exit": {
230
+ const [option = ""] = values;
231
+ if (EXIT_CMD_OPTION.test(option)) report(0, POSIX_PLATFORMS, {
232
+ reason: `\`${option}\` is a cmd.exe option`,
233
+ suggestion: `Remove \`${option}\` instead.`
234
+ });
235
+ break;
236
+ }
201
237
  case "find": {
202
238
  const start = values.findIndex((value) => value == null || !BSD_FIND_OPTIONS.test(value));
203
239
  const primary = values.findIndex((value) => setHas(GNU_FIND_PRIMARIES, value));
@@ -222,12 +258,14 @@ const checkCommandUsage = (name, commandArguments, options) => {
222
258
  suggestion: NODE_SCRIPT_SUGGESTION
223
259
  });
224
260
  break;
225
- case "grep":
226
- reportOption((value) => isShortOptionCluster(value, "P") || value === "--perl-regexp", MACOS_PLATFORMS, {
261
+ case "grep": {
262
+ const optionValues = optionValueIndexes(GREP_VALUE_OPTION);
263
+ reportOption((value, index) => !optionValues.has(index) && GREP_PERL_OPTION.test(value), MACOS_PLATFORMS, {
227
264
  reason: "BSD `grep` doesn't support Perl-compatible regular expressions",
228
265
  suggestion: "Use `grep -E` or a Node.js script instead."
229
266
  });
230
267
  break;
268
+ }
231
269
  case "head": {
232
270
  const index = optionIndex((value, valueIndex) => HEAD_NEGATIVE_COUNT_OPTION.test(value) || HEAD_COUNT_OPTION.test(value) && NEGATIVE_NUMBER.test(values[valueIndex + 1] || ""));
233
271
  if (index !== -1) {
@@ -278,14 +316,15 @@ const checkCommandUsage = (name, commandArguments, options) => {
278
316
  }
279
317
  case "sed": {
280
318
  const isExtended = optionIndex((value) => SED_EXTENDED_OPTION.test(value) || value === "--regexp-extended") !== -1;
281
- const findEscape = (value) => value == null || value.startsWith("-") ? void 0 : (GNU_SED_ESCAPE.exec(value) || (isExtended ? null : GNU_SED_BASIC_ESCAPE.exec(value)))?.[0];
319
+ const findEscape = (value) => value == null || value.startsWith("-") ? void 0 : (GNU_SED_ESCAPE.exec(value) || (isExtended ? null : GNU_SED_BASIC_ESCAPE.exec(value)))?.[1];
282
320
  const escapeIndex = values.findIndex((value) => findEscape(value) != null);
283
321
  if (escapeIndex !== -1) report(escapeIndex, MACOS_PLATFORMS, {
284
322
  reason: "BSD `sed` doesn't support it, so the pattern doesn't match",
285
323
  suggestion: "Use POSIX regex syntax (e.g. `[[:space:]]`, or `+` with `sed -E`) instead.",
286
324
  syntax: `The GNU regex escape \`${findEscape(values[escapeIndex]) || ""}\``
287
325
  });
288
- const index = optionIndex((value) => SED_IN_PLACE_OPTION.test(value));
326
+ const optionValues = optionValueIndexes(SED_VALUE_OPTION);
327
+ const index = optionIndex((value, valueIndex) => !optionValues.has(valueIndex) && SED_IN_PLACE_OPTION.test(value));
289
328
  if (index === -1) break;
290
329
  if (values[index + 1]?.length === 0) report(index + 1, LINUX_PLATFORMS, {
291
330
  reason: "GNU `sed` treats the empty backup suffix as the script",
@@ -300,6 +339,10 @@ const checkCommandUsage = (name, commandArguments, options) => {
300
339
  }
301
340
  case "set": {
302
341
  const [first] = values;
342
+ if (portable) {
343
+ reportCommand(PLATFORMS, first != null && ASSIGNMENT.test(first) ? CROSS_ENV_SUGGESTION : NODE_SCRIPT_SUGGESTION);
344
+ break;
345
+ }
303
346
  const pipefail = optionIndex((value, index) => value === "pipefail" && SET_NAMED_OPTION.test(values[index - 1] || ""));
304
347
  if (isDash && pipefail !== -1) report(pipefail, LINUX_PLATFORMS, {
305
348
  reason: `${DASH} doesn't support \`pipefail\``,
@@ -307,14 +350,14 @@ const checkCommandUsage = (name, commandArguments, options) => {
307
350
  usage: "set -o pipefail"
308
351
  });
309
352
  if (first == null) break;
310
- if (SET_SHELL_OPTIONS.test(first)) report(0, WINDOWS_PLATFORMS, {
353
+ if (native && SET_SHELL_OPTIONS.test(first)) report(0, WINDOWS_PLATFORMS, {
311
354
  reason: "cmd.exe's `set` manages environment variables instead of shell options",
312
355
  suggestion: "Remove it or use a Node.js script instead."
313
356
  });
314
- else if (ASSIGNMENT.test(first)) report(0, portable ? PLATFORMS : POSIX_PLATFORMS, {
357
+ else if (ASSIGNMENT.test(first)) report(0, POSIX_PLATFORMS, {
315
358
  reason: "POSIX shells don't set environment variables with `set`",
316
359
  suggestion: CROSS_ENV_SUGGESTION,
317
- syntax: `\`set ${truncate(first)}\``
360
+ syntax: `\`set ${truncate(textOf(0))}\``
318
361
  });
319
362
  break;
320
363
  }
@@ -338,19 +381,25 @@ const checkCommandUsage = (name, commandArguments, options) => {
338
381
  });
339
382
  break;
340
383
  }
341
- case "timeout":
384
+ case "timeout": {
385
+ const duration = values.findIndex((value, index) => value == null || !POSIX_OPTION.test(value) && !TIMEOUT_VALUE_OPTIONS.includes(values[index - 1] || ""));
342
386
  if (TIMEOUT_CMD_OPTION.test(values[0] || "")) report(0, POSIX_PLATFORMS, {
343
- reason: "`/t` is an option of Windows' `timeout` command",
387
+ reason: `\`${values[0] || ""}\` is an option of Windows' \`timeout\` command`,
388
+ suggestion: NODE_SCRIPT_SUGGESTION
389
+ });
390
+ else if (duration !== -1 && duration === values.length - 1) report(duration, POSIX_PLATFORMS, {
391
+ reason: "only Windows' `timeout` works without a command",
344
392
  suggestion: NODE_SCRIPT_SUGGESTION
345
393
  });
346
- else reportCommand(NOT_MACOS_OR_WINDOWS, NODE_SCRIPT_SUGGESTION);
394
+ else reportCommand(NOT_MACOS_OR_WINDOWS, "Older macOS versions don't have it, and Windows' `timeout` only waits; use a Node.js script instead.");
347
395
  break;
396
+ }
348
397
  case "trap": {
349
398
  const index = values.findIndex((value, valueIndex) => valueIndex > 0 && setHas(BASH_TRAP_CONDITIONS, value));
350
399
  if (isDash && index !== -1) report(index, LINUX_PLATFORMS, {
351
- reason: `${DASH} doesn't support the \`${values[index] || ""}\` condition, so it ignores the trap`,
400
+ reason: `${DASH} doesn't support the \`${values[index] || ""}\` condition`,
352
401
  suggestion: NODE_SCRIPT_SUGGESTION,
353
- syntax: `\`trap ... ${values[index] || ""}\``
402
+ usage: `trap ... ${values[index] || ""}`
354
403
  });
355
404
  break;
356
405
  }
package/dist/commands.mjs CHANGED
@@ -341,6 +341,62 @@ const COMMANDS = new Map([...COMMAND_GROUPS.flatMap(([names, platforms, suggesti
341
341
  platforms,
342
342
  suggestion
343
343
  }]))]);
344
+ /** Shell builtins that macOS' `sh` only finds by their exact name, unlike executables */
345
+ const EXACT_NAME_BUILTINS = /* @__PURE__ */ new Set([
346
+ ...BASH_BUILTINS.flatMap(([names]) => names),
347
+ "alias",
348
+ "bg",
349
+ "cd",
350
+ "eval",
351
+ "exec",
352
+ "exit",
353
+ "export",
354
+ "fg",
355
+ "getopts",
356
+ "jobs",
357
+ "local",
358
+ "read",
359
+ "readonly",
360
+ "return",
361
+ "shift",
362
+ "trap",
363
+ "ulimit",
364
+ "umask",
365
+ "unset",
366
+ "wait"
367
+ ]);
368
+ /** Builtins of both dash and Bash, i.e. of `sh` on Linux and macOS */
369
+ const POSIX_SHELL_BUILTINS = /* @__PURE__ */ new Set([
370
+ ".",
371
+ ":",
372
+ "[",
373
+ "alias",
374
+ "bg",
375
+ "command",
376
+ "eval",
377
+ "exec",
378
+ "export",
379
+ "false",
380
+ "fg",
381
+ "getopts",
382
+ "hash",
383
+ "jobs",
384
+ "kill",
385
+ "local",
386
+ "printf",
387
+ "pwd",
388
+ "read",
389
+ "readonly",
390
+ "return",
391
+ "shift",
392
+ "test",
393
+ "trap",
394
+ "true",
395
+ "ulimit",
396
+ "umask",
397
+ "unset",
398
+ "wait"
399
+ ]);
344
400
  /** cmd.exe builtins (rather than executables), which Yarn's portable shell can't run on Windows */
345
401
  const CMD_BUILTINS = /* @__PURE__ */ new Set([
346
402
  "assoc",
@@ -362,9 +418,11 @@ const CMD_BUILTINS = /* @__PURE__ */ new Set([
362
418
  "rem",
363
419
  "ren",
364
420
  "rename",
421
+ "set",
365
422
  "setlocal",
366
423
  "start",
367
424
  "title",
425
+ "type",
368
426
  "ver"
369
427
  ]);
370
428
  /** Builtins of Yarn's portable shell, which work on all platforms */
@@ -413,11 +471,39 @@ const OS_COMMANDS = /* @__PURE__ */ new Set([
413
471
  ...COMMANDS.keys(),
414
472
  "cd",
415
473
  "echo",
474
+ "exit",
416
475
  "mkdir",
476
+ "more",
417
477
  "rmdir",
418
478
  "sort",
419
479
  "timeout"
420
480
  ]);
481
+ /**
482
+ * Well-known programs that aren't npm packages or OS commands, and don't expand globs in their
483
+ * arguments themselves, e.g. `tar`
484
+ */
485
+ const NON_GLOB_PROGRAMS = /* @__PURE__ */ new Set([
486
+ "bun",
487
+ "cargo",
488
+ "curl",
489
+ "deno",
490
+ "docker",
491
+ "git",
492
+ "go",
493
+ "java",
494
+ "kubectl",
495
+ "make",
496
+ "node",
497
+ "perl",
498
+ "php",
499
+ "podman",
500
+ "python",
501
+ "python3",
502
+ "ruby",
503
+ "scp",
504
+ "ssh",
505
+ "tar"
506
+ ]);
421
507
  /** Package manager subcommands that run the command given as their arguments */
422
508
  const EXEC_SUBCOMMANDS = {
423
509
  aube: ["dlx", "exec"],
@@ -439,8 +525,23 @@ const PACKAGE_MANAGER_VALUE_OPTIONS = [
439
525
  "-C",
440
526
  "-F"
441
527
  ];
528
+ /** Options of npm (before its subcommand) that take a value, unlike pnpm's `-w` */
529
+ const NPM_VALUE_OPTIONS = [...PACKAGE_MANAGER_VALUE_OPTIONS, "-w"];
442
530
  /** Options of package managers' exec subcommands that take a value */
443
531
  const EXEC_VALUE_OPTIONS = ["--package", "-p"];
532
+ /** Options of `npm exec` and `npx` that take a value */
533
+ const NPM_EXEC_VALUE_OPTIONS = [
534
+ ...EXEC_VALUE_OPTIONS,
535
+ "--workspace",
536
+ "-w"
537
+ ];
538
+ /** Options of GNU `timeout` that take a value */
539
+ const TIMEOUT_VALUE_OPTIONS = [
540
+ "--kill-after",
541
+ "--signal",
542
+ "-k",
543
+ "-s"
544
+ ];
444
545
  /** Commands whose arguments are often patterns rather than paths */
445
546
  const PATTERN_COMMANDS = /* @__PURE__ */ new Set([
446
547
  "awk",
@@ -467,7 +568,7 @@ const WRAPPER_VALUE_OPTIONS = /* @__PURE__ */ new Map([
467
568
  ["exec", ["-a"]],
468
569
  ["nice", ["--adjustment", "-n"]],
469
570
  ["nohup", []],
470
- ["npx", EXEC_VALUE_OPTIONS],
571
+ ["npx", NPM_EXEC_VALUE_OPTIONS],
471
572
  ["pnpx", EXEC_VALUE_OPTIONS],
472
573
  ["sudo", [
473
574
  "-C",
@@ -494,27 +595,45 @@ const WRAPPER_VALUE_OPTIONS = /* @__PURE__ */ new Map([
494
595
  ]]
495
596
  ]);
496
597
  /**
497
- * Builtins that change the shell's state.
498
- * In a subshell, the change doesn't reach the commands that follow
598
+ * Builtins that change or use the state of the shell that runs them.
599
+ * They don't work the same in a subshell, or when another command runs them, e.g. `cross-env`
499
600
  */
500
601
  const SHELL_STATE_COMMANDS = /* @__PURE__ */ new Set([
501
602
  ".",
502
603
  "alias",
604
+ "builtin",
503
605
  "cd",
606
+ "command",
607
+ "declare",
608
+ "eval",
609
+ "exec",
504
610
  "exit",
505
611
  "export",
612
+ "getopts",
613
+ "let",
614
+ "local",
615
+ "mapfile",
506
616
  "popd",
507
617
  "pushd",
508
618
  "read",
619
+ "readarray",
509
620
  "readonly",
510
621
  "return",
511
622
  "set",
512
623
  "shift",
624
+ "shopt",
513
625
  "source",
514
626
  "trap",
627
+ "typeset",
515
628
  "ulimit",
516
629
  "umask",
517
630
  "unalias",
518
- "unset"
631
+ "unset",
632
+ "wait"
519
633
  ]);
520
- export { CMD_BUILTINS, COMMANDS, EXEC_SUBCOMMANDS, EXEC_VALUE_OPTIONS, NO_LONG_OPTIONS_ON_MACOS, OS_COMMANDS, PACKAGE_MANAGER_VALUE_OPTIONS, PATTERN_COMMANDS, PORTABLE_SHELL_BUILTINS, SHELLS, SHELL_STATE_COMMANDS, WRAPPER_VALUE_OPTIONS };
634
+ /**
635
+ * Shell builtins that Yarn's portable shell doesn't have, and that have no executables that work
636
+ * the same, e.g. `export`
637
+ */
638
+ const NON_PORTABLE_BUILTINS = new Set([...SHELL_STATE_COMMANDS, ...EXACT_NAME_BUILTINS].filter((name) => !PORTABLE_SHELL_BUILTINS.has(name) && name !== "command" && name !== "set"));
639
+ export { CMD_BUILTINS, COMMANDS, EXACT_NAME_BUILTINS, EXEC_SUBCOMMANDS, EXEC_VALUE_OPTIONS, NON_GLOB_PROGRAMS, NON_PORTABLE_BUILTINS, NO_LONG_OPTIONS_ON_MACOS, NPM_EXEC_VALUE_OPTIONS, NPM_VALUE_OPTIONS, OS_COMMANDS, PACKAGE_MANAGER_VALUE_OPTIONS, PATTERN_COMMANDS, PORTABLE_SHELL_BUILTINS, POSIX_SHELL_BUILTINS, SHELLS, SHELL_STATE_COMMANDS, TIMEOUT_VALUE_OPTIONS, WRAPPER_VALUE_OPTIONS };
package/dist/context.mjs CHANGED
@@ -9,10 +9,17 @@ const rangeInScript = (context, node) => context.fixedRange || {
9
9
  end: context.offset + node.end,
10
10
  pos: context.offset + node.pos
11
11
  };
12
+ /**
13
+ * Gets the text of a node as the script has it, e.g. with `$OUT` instead of its placeholder.
14
+ * @param context The piece of the script that the node is in
15
+ * @param node The node, with positions relative to the piece's source
16
+ * @returns The text
17
+ */
18
+ const originalText = (context, node) => context.originalSource.slice(node.pos, node.end);
12
19
  const withCovered = (context, platforms) => platforms.every((platform) => context.covered.has(platform)) ? context : {
13
20
  ...context,
14
21
  covered: /* @__PURE__ */ new Set([...context.covered, ...platforms])
15
22
  };
16
23
  /** Gets how reports name Bash syntax of a kind, e.g. "Bash 4" for Bash 4 syntax */
17
24
  const bashLabel = (kind) => kind === "bash4" ? "Bash 4" : "Bash-specific";
18
- export { bashLabel, rangeInScript, withCovered };
25
+ export { bashLabel, originalText, rangeInScript, withCovered };
package/dist/fixes.d.mts CHANGED
@@ -1,5 +1,5 @@
1
- import { ScriptFix } from "./reporter.mjs";
2
1
  import "unbash";
2
+ import { ScriptFix } from "./reporter.mjs";
3
3
  /**
4
4
  * Applies a fix to the script that it was found in.
5
5
  * @param script Text of the script to change