package-json-scripts-portability 0.0.1 → 0.0.3
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 +59 -20
- package/dist/analyze-script.d.mts +34 -0
- package/dist/analyze-script.mjs +53 -0
- package/dist/command-checker.mjs +527 -0
- package/dist/command-usage.mjs +413 -0
- package/dist/commands.mjs +639 -0
- package/dist/context.mjs +25 -0
- package/dist/fixes.d.mts +9 -0
- package/dist/fixes.mjs +354 -0
- package/dist/index.d.mts +6 -118
- package/dist/index.mjs +4 -3787
- package/dist/line-break-checker.mjs +104 -0
- package/dist/messages.mjs +38 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-difference.mjs +11 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-includes.mjs +8 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-intersection.mjs +13 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-is-subset.mjs +16 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/arrayify.mjs +7 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/sorted-array-insertion-index-with.mjs +33 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/sorted-array-insertion-index.mjs +10 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/iterable/sliding-window.mjs +30 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/map/map-get-or-insert-computed.mjs +42 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/map/map-get-or-insert.mjs +40 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/math/clamp.mjs +7 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/path/to-forward-slashes.mjs +8 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/predicate/is-array.mjs +39 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/semver/is-semver-in-range.mjs +5 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/semver/parse-semver.mjs +26 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/set/set-has.mjs +8 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/string/substring-after-last.mjs +24 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/string/substring-after.mjs +24 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/string/substring-before.mjs +24 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/string/truncate.mjs +46 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/ts/all-union-members.mjs +45 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/types/maybe-readonly-array.d.mts +17 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/value/is-equal.mjs +37 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/@antfu/utils/dist/index.mjs +5 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/_internal/globalThis.mjs +5 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/_internal/isUnsafeProperty.mjs +18 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/_internal/setOwnValue.mjs +23 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/_internal/toPropertyKey.mjs +16 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/array/differenceBy.mjs +40 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/array/differenceWith.mjs +37 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/array/intersectionWith.mjs +43 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/array/isSubsetWith.mjs +36 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/bigint/clamp.mjs +15 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/baseToString.mjs +26 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/getSymbols.mjs +5 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/getTag.mjs +13 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/hasOwn.mjs +13 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/isDeepKey.mjs +39 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/isIndex.mjs +10 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/regexMultiByte.mjs +10 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/tags.mjs +28 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/toKey.mjs +15 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/array/sortedIndex.mjs +36 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/array/sortedIndexBy.mjs +54 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/array/sortedLastIndex.mjs +34 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/array/sortedLastIndexBy.mjs +21 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/function/identity.mjs +5 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/object/cloneDeep.mjs +52 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/object/cloneDeepWith.mjs +179 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/object/get.mjs +67 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/object/has.mjs +53 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/object/property.mjs +26 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isArguments.mjs +26 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isBuffer.mjs +24 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isMatch.mjs +34 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isMatchWith.mjs +163 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isNaN.mjs +18 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isNil.mjs +24 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isNumber.mjs +24 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isObject.mjs +28 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isObjectLike.mjs +28 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isSymbol.mjs +19 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isTypedArray.mjs +21 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/matches.mjs +36 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/matchesProperty.mjs +47 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/string/truncate.mjs +78 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/util/eq.mjs +18 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/util/iteratee.mjs +46 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/util/toPath.mjs +75 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/util/toString.mjs +23 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/function/identity.mjs +24 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/function/noop.mjs +12 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/math/clamp.mjs +23 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isBuffer.mjs +24 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isEqual.mjs +21 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isEqualWith.mjs +150 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isNil.mjs +24 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isNull.mjs +25 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isPlainObject.mjs +50 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isPrimitive.mjs +26 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isSymbol.mjs +28 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isTypedArray.mjs +20 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isUndefined.mjs +25 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/remeda/dist/binarySearchCutoffIndex-Bu-OMJ0Z.mjs +26 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/remeda/dist/isArray.mjs +18 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/remeda/dist/sortedIndexWith.mjs +7 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/ts-extras/distribution/array-includes.mjs +5 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/ts-extras/distribution/set-has.mjs +5 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/verkit/dist/comparison-CmVirWIW.mjs +108 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/verkit/dist/range-C5wjdo9a.mjs +164 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/verkit/dist/set-BGFWKKE8.mjs +38 -0
- package/dist/platforms.d.mts +8 -0
- package/dist/platforms.mjs +26 -0
- package/dist/redirection-checker.mjs +169 -0
- package/dist/reporter.d.mts +32 -0
- package/dist/reporter.mjs +143 -0
- package/dist/runners.d.mts +21 -0
- package/dist/runners.mjs +199 -0
- package/dist/script-analyzer.mjs +853 -0
- package/dist/word-checker.mjs +573 -0
- package/dist/words.mjs +70 -0
- package/package.json +36 -30
|
@@ -0,0 +1,853 @@
|
|
|
1
|
+
import { forEachChild, parse } from "unbash";
|
|
2
|
+
import { CommandChecker } from "./command-checker.mjs";
|
|
3
|
+
import { SHELLS, SHELL_STATE_COMMANDS } from "./commands.mjs";
|
|
4
|
+
import { bashLabel, originalText, rangeInScript, withCovered } from "./context.mjs";
|
|
5
|
+
import { joinLinesFix, lineBreakFix, removal, semicolonFix } from "./fixes.mjs";
|
|
6
|
+
import { LineBreakChecker } from "./line-break-checker.mjs";
|
|
7
|
+
import { BASH_4_SUGGESTION, BASH_SUGGESTION, NODE_SCRIPT_SUGGESTION, describeReplacement, truncate } from "./messages.mjs";
|
|
8
|
+
import { PLATFORMS, POSIX_PLATFORMS, WINDOWS_PLATFORMS } from "./platforms.mjs";
|
|
9
|
+
import { RedirectionChecker, doesRedirectOutput } from "./redirection-checker.mjs";
|
|
10
|
+
import { Reporter } from "./reporter.mjs";
|
|
11
|
+
import { TraitsReader, runnerTraits } from "./runners.mjs";
|
|
12
|
+
import { arrayIsSubset } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-is-subset.mjs";
|
|
13
|
+
import { mapGetOrInsert } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/map/map-get-or-insert.mjs";
|
|
14
|
+
import { mapGetOrInsertComputed } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/map/map-get-or-insert-computed.mjs";
|
|
15
|
+
import { sortedArrayInsertionIndex } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/sorted-array-insertion-index.mjs";
|
|
16
|
+
import { substringBefore } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/string/substring-before.mjs";
|
|
17
|
+
import { CMD_STATEMENTS, WordChecker } from "./word-checker.mjs";
|
|
18
|
+
import { DOUBLE_QUOTED_SPECIAL_CHARACTERS, WHITESPACE, nestedSource, staticValue } from "./words.mjs";
|
|
19
|
+
// src/script-analyzer.ts
|
|
20
|
+
/** `#` comments after a word boundary, which end at the line feed, not at a carriage return */
|
|
21
|
+
const COMMENTS = /(?:^|[\s&;|])(#[^\n]*)/g;
|
|
22
|
+
/** Globs of script names that `concurrently` expands, e.g. `npm run watch:*(!css)` */
|
|
23
|
+
const SCRIPT_GLOBS = /((?:bun|npm|pnpm|yarn) run|node --run|deno task) (\S*\*\S*)/g;
|
|
24
|
+
/** `shopt -s globstar`, which makes `**` recursive in the following commands */
|
|
25
|
+
const SHOPT_GLOBSTAR = /\bshopt\s+-s\s+(?:\w+\s+)*globstar\b/;
|
|
26
|
+
/**
|
|
27
|
+
* The rest of a script that doesn't depend on the directory: closing parentheses, `;`, `&` and
|
|
28
|
+
* comments, and `exit`, `true`, `false` or `:` after `&&` or `||`, e.g. in `(cd a && b) || exit 1`
|
|
29
|
+
*/
|
|
30
|
+
const DIRECTORY_INDEPENDENT_REST = /^(?:[\s&);]|#[^\n]*(?:\n|$)|(?:&&|\|\|)\s*(?:exit(?:\s+\d+)?|true|false|:))*$/;
|
|
31
|
+
const ONE_LINE_SUGGESTION = "Put the commands on one line instead.";
|
|
32
|
+
/** cmd.exe's `if` and `for` statements, e.g. `if exist dist` or `if "%CI%"=="true"` */
|
|
33
|
+
const WINDOWS_BATCH_SYNTAX = [
|
|
34
|
+
/\bif\s+(?:\/i\s+)?(?:not\s+)?(?:defined|errorlevel|exist)\b/gi,
|
|
35
|
+
/\bif\s+(?:\/i\s+)?(?:not\s+)?(?:"[^\n"]*"|[^\s"]\S*?)(?:\s*==\s*|\s+(?:equ|geq|gtr|leq|lss|neq)\s+)(?:"[^\n"]*"|[^\s"]\S*)/gi,
|
|
36
|
+
/\bfor\s+(?:\/[a-z]\s+)*%%?[a-z]\b/gi
|
|
37
|
+
];
|
|
38
|
+
const SINGLE_QUOTE = /'/;
|
|
39
|
+
const DOUBLE_QUOTE = /"/;
|
|
40
|
+
const ANY_CHARACTER = /[\s\S]/;
|
|
41
|
+
const NO_PLATFORMS = /* @__PURE__ */ new Set();
|
|
42
|
+
const doesChangeDirectory = (node) => {
|
|
43
|
+
switch (node.type) {
|
|
44
|
+
case "AndOr":
|
|
45
|
+
case "Pipeline": return node.commands.some(doesChangeDirectory);
|
|
46
|
+
case "Command": return node.name?.value === "cd" || node.name?.value === "pushd";
|
|
47
|
+
default: return false;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
/** Arithmetic operators that assign, e.g. `+=` */
|
|
51
|
+
const ARITHMETIC_ASSIGNMENT = /^(?:[%&*+\-/^|]|<<|>>)?=$/;
|
|
52
|
+
/**
|
|
53
|
+
* Checks whether a piece of a command has an expansion that assigns a variable, e.g. `${A:=1}` or
|
|
54
|
+
* `$((i++))`.
|
|
55
|
+
* @param node The piece, e.g. a command or a word
|
|
56
|
+
* @returns Whether it does, apart from in a command substitution, which runs in a subshell
|
|
57
|
+
*/
|
|
58
|
+
const hasAssigningExpansion = (node) => {
|
|
59
|
+
switch (node.type) {
|
|
60
|
+
case "ArithmeticBinary":
|
|
61
|
+
if (ARITHMETIC_ASSIGNMENT.test(node.operator)) return true;
|
|
62
|
+
break;
|
|
63
|
+
case "ArithmeticCommandExpansion":
|
|
64
|
+
case "CommandExpansion":
|
|
65
|
+
case "ProcessSubstitution": return false;
|
|
66
|
+
case "ArithmeticUnary":
|
|
67
|
+
if (node.operator === "++" || node.operator === "--") return true;
|
|
68
|
+
break;
|
|
69
|
+
case "ParameterExpansion": if (node.operation?.type === "Default" && (node.operation.operator === "=" || node.operation.operator === ":=")) return true;
|
|
70
|
+
}
|
|
71
|
+
const children = [];
|
|
72
|
+
forEachChild(node, (child) => {
|
|
73
|
+
children.push(child);
|
|
74
|
+
});
|
|
75
|
+
return children.some(hasAssigningExpansion);
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Checks whether commands change the shell's state, e.g. with `exit` or a variable assignment.
|
|
79
|
+
* @param node A command group or its commands
|
|
80
|
+
* @returns Whether they do, or might, e.g. in an `if` statement
|
|
81
|
+
*/
|
|
82
|
+
const doesChangeShellState = (node) => {
|
|
83
|
+
switch (node.type) {
|
|
84
|
+
case "AndOr":
|
|
85
|
+
case "Pipeline": return node.commands.some(doesChangeShellState);
|
|
86
|
+
case "BraceGroup": return node.body.commands.some((statement) => statement.background || doesChangeShellState(statement.command));
|
|
87
|
+
case "Command": return (node.name ? SHELL_STATE_COMMANDS.has(node.name.value) : node.prefix.some((item) => item.type === "Assignment")) || hasAssigningExpansion(node);
|
|
88
|
+
case "Redirected": return doesChangeShellState(node.command) || node.redirects.some(hasAssigningExpansion);
|
|
89
|
+
case "Subshell": return false;
|
|
90
|
+
default: return true;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* Gets the original text of a script nested in a piece with placeholders, when it has a source of
|
|
95
|
+
* its own, e.g. in backticks, which is the piece's text without some backslashes.
|
|
96
|
+
* @param source The nested script's source
|
|
97
|
+
* @param text The piece's text, with placeholders
|
|
98
|
+
* @param original The piece's original text
|
|
99
|
+
* @returns The nested script's source with the original text instead of placeholders
|
|
100
|
+
*/
|
|
101
|
+
const originalNestedSource = (source, text, original) => {
|
|
102
|
+
let result = "";
|
|
103
|
+
let index = 0;
|
|
104
|
+
for (let textIndex = 0; textIndex < text.length && index < source.length; textIndex++) {
|
|
105
|
+
if (text[textIndex] !== source[index]) continue;
|
|
106
|
+
result += original[textIndex];
|
|
107
|
+
index++;
|
|
108
|
+
}
|
|
109
|
+
return result + source.slice(index);
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* Gets the parts of a command whose failures make the command itself fail, e.g. the last
|
|
113
|
+
* statement of `( ... )`, but no command of an `if` statement, which succeeds when its condition
|
|
114
|
+
* fails.
|
|
115
|
+
* @param node The command
|
|
116
|
+
* @returns The ranges of the parts in the command's source
|
|
117
|
+
*/
|
|
118
|
+
const failureRanges = (node) => {
|
|
119
|
+
switch (node.type) {
|
|
120
|
+
case "AndOr": return node.commands.slice(node.operators.lastIndexOf("||") + 1).flatMap(failureRanges);
|
|
121
|
+
case "ArithmeticFor":
|
|
122
|
+
case "Case":
|
|
123
|
+
case "Coproc":
|
|
124
|
+
case "For":
|
|
125
|
+
case "Function":
|
|
126
|
+
case "If":
|
|
127
|
+
case "Select":
|
|
128
|
+
case "While": return [];
|
|
129
|
+
case "BraceGroup":
|
|
130
|
+
case "Subshell": {
|
|
131
|
+
const last = node.body.commands.at(-1);
|
|
132
|
+
return last && !last.background ? failureRanges(last.command) : [];
|
|
133
|
+
}
|
|
134
|
+
case "Redirected": return [...failureRanges(node.command), ...node.redirects];
|
|
135
|
+
default: return [node];
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
/** Gets what identifies the message of an issue, e.g. to find the same issue elsewhere */
|
|
139
|
+
const issueDataKey = (issue) => JSON.stringify([issue.messageId, issue.data]);
|
|
140
|
+
/**
|
|
141
|
+
* Looks for a run of `||` alternatives that each fail on some platform, e.g.
|
|
142
|
+
* `open x || xdg-open x || start x`.
|
|
143
|
+
* @param node The list that the alternatives are in
|
|
144
|
+
* @param platformsOf Gets the platforms on which a command of the list fails
|
|
145
|
+
* @param first Index of the run's first command
|
|
146
|
+
* @returns Index of the run's last command, which is `first` if there is no run, and the platforms
|
|
147
|
+
* on which all of its commands fail, which are none if they work together on all platforms
|
|
148
|
+
*/
|
|
149
|
+
const findFallbackRun = (node, platformsOf, first) => {
|
|
150
|
+
let common = platformsOf(first);
|
|
151
|
+
let last = first;
|
|
152
|
+
while (common.size > 0 && last + 1 < node.commands.length && node.operators[last] === "||") {
|
|
153
|
+
const platforms = platformsOf(last + 1);
|
|
154
|
+
if (platforms.size === 0) break;
|
|
155
|
+
last++;
|
|
156
|
+
common = common.intersection(platforms);
|
|
157
|
+
}
|
|
158
|
+
return {
|
|
159
|
+
common,
|
|
160
|
+
last
|
|
161
|
+
};
|
|
162
|
+
};
|
|
163
|
+
/**
|
|
164
|
+
* Checks whether an alternative is meant for a platform where another one fails: it works there,
|
|
165
|
+
* and fails where the other one works.
|
|
166
|
+
* @param own Where the other alternative fails
|
|
167
|
+
* @param platform One of these platforms
|
|
168
|
+
* @param other Where the alternative fails, if there is one
|
|
169
|
+
* @returns Whether it is
|
|
170
|
+
*/
|
|
171
|
+
const isAlternativeOn = (own, platform, other) => other != null && !other.has(platform) && !other.isSubsetOf(own);
|
|
172
|
+
/**
|
|
173
|
+
* Gets where the alternatives of a run fail without it being reported, when they all still fail on
|
|
174
|
+
* some platforms, e.g. macOS for `sed -i s/a/b/ f` in `sed -i s/a/b/ f || sed -i '' s/a/b/ f`.
|
|
175
|
+
* That's where an alternative for that platform takes over: the one that runs next there, or an
|
|
176
|
+
* earlier one, unlike `cp f.bak f` in `sed -i s/a/b/ f || cp f.bak f`, which only handles errors.
|
|
177
|
+
* @param failing Where each alternative of the run fails
|
|
178
|
+
* @returns Where each of them fails without it being reported
|
|
179
|
+
*/
|
|
180
|
+
const partialFallbackPlatforms = (failing) => {
|
|
181
|
+
const removed = failing.map(() => /* @__PURE__ */ new Set());
|
|
182
|
+
const next = /* @__PURE__ */ new Map();
|
|
183
|
+
failing.toReversed().forEach((own, reversedIndex) => {
|
|
184
|
+
const ownRemoved = removed[failing.length - 1 - reversedIndex];
|
|
185
|
+
own.forEach((platform) => {
|
|
186
|
+
if (isAlternativeOn(own, platform, next.get(platform))) ownRemoved?.add(platform);
|
|
187
|
+
});
|
|
188
|
+
PLATFORMS.forEach((platform) => {
|
|
189
|
+
if (!own.has(platform)) next.set(platform, own);
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
const earlier = /* @__PURE__ */ new Map();
|
|
193
|
+
failing.forEach((own, index) => {
|
|
194
|
+
own.forEach((platform) => {
|
|
195
|
+
if (earlier.values().some((other) => isAlternativeOn(own, platform, other))) removed[index]?.add(platform);
|
|
196
|
+
});
|
|
197
|
+
earlier.set([...own].sort().join(","), own);
|
|
198
|
+
});
|
|
199
|
+
return removed;
|
|
200
|
+
};
|
|
201
|
+
var ScriptAnalyzer = class {
|
|
202
|
+
#commands;
|
|
203
|
+
/** How many scripts the analyzed source is nested in, e.g. 1 in `sh -c "..."` */
|
|
204
|
+
#depth = 0;
|
|
205
|
+
#ignoredCommands;
|
|
206
|
+
#lineBreaks;
|
|
207
|
+
/**
|
|
208
|
+
* Words joined into a nested script, e.g. in `cross-env-shell rm $DIR`, which loses their quotes,
|
|
209
|
+
* so quoting them doesn't help
|
|
210
|
+
*/
|
|
211
|
+
#joinedScriptWords;
|
|
212
|
+
/** Words analyzed as nested scripts, e.g. in `sh -c "..."`, whose text that analysis checks */
|
|
213
|
+
#nestedScriptWords;
|
|
214
|
+
/**
|
|
215
|
+
* Issues of the scripts joined from several words of the command being walked, e.g. in
|
|
216
|
+
* `cross-env-shell ls ~/x`, which are reported on all of the words
|
|
217
|
+
*/
|
|
218
|
+
#joinedScriptIssues;
|
|
219
|
+
/** Parsed sources of the script and of the scripts nested in it, e.g. in `sh -c "..."` */
|
|
220
|
+
#parsedScripts;
|
|
221
|
+
#redirections;
|
|
222
|
+
#reporter;
|
|
223
|
+
#words;
|
|
224
|
+
constructor(length, commandOptions, traits, parsedScripts) {
|
|
225
|
+
this.#parsedScripts = parsedScripts;
|
|
226
|
+
this.#ignoredCommands = commandOptions.ignoredCommands;
|
|
227
|
+
this.#reporter = new Reporter(length);
|
|
228
|
+
this.#words = new WordChecker(this.#reporter, (script, part, context) => {
|
|
229
|
+
this.#walkNestedScript(script, part, context);
|
|
230
|
+
}, traits);
|
|
231
|
+
this.#lineBreaks = new LineBreakChecker(this.#reporter);
|
|
232
|
+
this.#redirections = new RedirectionChecker(this.#reporter, this.#words);
|
|
233
|
+
this.#commands = new CommandChecker(this.#reporter, commandOptions, (words, context, nested) => {
|
|
234
|
+
this.#analyzeNestedWords(words, context, nested);
|
|
235
|
+
}, traits);
|
|
236
|
+
}
|
|
237
|
+
get issues() {
|
|
238
|
+
return this.#reporter.uniqueIssues;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Analyzes a script or a piece of it that is a script of its own, e.g. in `sh -c "..."`.
|
|
242
|
+
* @param context The piece
|
|
243
|
+
* @returns Whether it was parsed and analyzed, unlike one that isn't a valid POSIX script
|
|
244
|
+
*/
|
|
245
|
+
analyzeSource(context) {
|
|
246
|
+
const script = mapGetOrInsertComputed(this.#parsedScripts, context.source, parse);
|
|
247
|
+
if (script.errors?.length) {
|
|
248
|
+
this.#checkWindowsBatchSyntax(context);
|
|
249
|
+
return false;
|
|
250
|
+
}
|
|
251
|
+
this.#commands.addPositionalFixes(script.commands, context.source);
|
|
252
|
+
const scriptContext = context.shell === "bash" && SHOPT_GLOBSTAR.test(context.source) ? {
|
|
253
|
+
...context,
|
|
254
|
+
bashOptions: /* @__PURE__ */ new Set([...context.bashOptions || [], "globstar"])
|
|
255
|
+
} : context;
|
|
256
|
+
this.#walkStatements(script.commands, {
|
|
257
|
+
end: context.source.length,
|
|
258
|
+
pos: 0
|
|
259
|
+
}, scriptContext, "script");
|
|
260
|
+
return true;
|
|
261
|
+
}
|
|
262
|
+
#analyzeNestedWords(words, context, { bashOptions, crossEnv, maskScriptGlobs = false, outerWord, shell }) {
|
|
263
|
+
if (this.#depth >= 10) return;
|
|
264
|
+
const sources = words.map((word) => {
|
|
265
|
+
const source = word.parts ? nestedSource(word.parts) : word.value;
|
|
266
|
+
return maskScriptGlobs ? source.replaceAll(SCRIPT_GLOBS, (_, command, glob) => `${command} ${"".repeat(glob.length)}`) : source;
|
|
267
|
+
});
|
|
268
|
+
const source = sources.join(" ");
|
|
269
|
+
const [word] = words;
|
|
270
|
+
if (!word) return;
|
|
271
|
+
const parts = word.parts || [];
|
|
272
|
+
const [part] = parts;
|
|
273
|
+
let { fixedRange } = context;
|
|
274
|
+
let offset = context.offset + word.pos;
|
|
275
|
+
let exclusion = ANY_CHARACTER;
|
|
276
|
+
if (words.length === 1 && parts.length === 1 && (part?.type === "SingleQuoted" || part?.type === "DoubleQuoted" && part.parts.every((child) => child.type !== "Literal" || child.value === child.text))) {
|
|
277
|
+
offset = context.offset + part.pos + 1;
|
|
278
|
+
exclusion = part.type === "SingleQuoted" ? SINGLE_QUOTE : DOUBLE_QUOTED_SPECIAL_CHARACTERS;
|
|
279
|
+
} else if (words.length > 1 || parts.length > 0 || word.text !== sources[0]) fixedRange ||= rangeInScript(context, {
|
|
280
|
+
end: words.at(-1)?.end || word.end,
|
|
281
|
+
pos: word.pos
|
|
282
|
+
});
|
|
283
|
+
const firstIssue = this.#reporter.issues.length;
|
|
284
|
+
this.#depth++;
|
|
285
|
+
const isAnalyzed = this.analyzeSource({
|
|
286
|
+
bashOptions,
|
|
287
|
+
covered: context.covered,
|
|
288
|
+
crossEnv,
|
|
289
|
+
fixExclusions: [...context.fixExclusions, exclusion],
|
|
290
|
+
fixedRange,
|
|
291
|
+
offset,
|
|
292
|
+
originalSource: source.includes("") ? words.map((nested) => nested.parts ? nestedSource(nested.parts, true) : nested.value).join(" ") : source,
|
|
293
|
+
portable: false,
|
|
294
|
+
runnerScript: false,
|
|
295
|
+
shell,
|
|
296
|
+
source
|
|
297
|
+
});
|
|
298
|
+
this.#depth--;
|
|
299
|
+
if (isAnalyzed && words.length === 1) (this.#nestedScriptWords ||= /* @__PURE__ */ new WeakSet()).add(outerWord || word);
|
|
300
|
+
else if (words.length > 1) {
|
|
301
|
+
const joinedWords = this.#joinedScriptWords || /* @__PURE__ */ new WeakSet();
|
|
302
|
+
this.#joinedScriptWords = joinedWords;
|
|
303
|
+
words.forEach((joined) => {
|
|
304
|
+
joinedWords.add(joined);
|
|
305
|
+
});
|
|
306
|
+
if (this.#reporter.issues.length > firstIssue) (this.#joinedScriptIssues ||= []).push(...this.#reporter.issues.slice(firstIssue));
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Sets the issues collected for the command being walked.
|
|
311
|
+
* @param issues The issues to collect into from now on
|
|
312
|
+
* @returns The issues collected so far
|
|
313
|
+
*/
|
|
314
|
+
#swapJoinedScriptIssues(issues) {
|
|
315
|
+
const collected = this.#joinedScriptIssues;
|
|
316
|
+
this.#joinedScriptIssues = issues;
|
|
317
|
+
return collected;
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Removes the platforms of the issues of a script joined from several words on which a word
|
|
321
|
+
* itself has the same issue, which is reported at its exact position, e.g. `~/x` in
|
|
322
|
+
* `cross-env-shell ls ~/x`.
|
|
323
|
+
* @param joinedIssues Issues of the joined script
|
|
324
|
+
* @param wordIssues Issues of the words, among others
|
|
325
|
+
* @param start Index of the first issue that may be updated
|
|
326
|
+
*/
|
|
327
|
+
#dropJoinedDuplicates(joinedIssues, wordIssues, start) {
|
|
328
|
+
const wordIssuesByKey = /* @__PURE__ */ new Map();
|
|
329
|
+
wordIssues.forEach((issue) => {
|
|
330
|
+
mapGetOrInsert(wordIssuesByKey, issueDataKey(issue), []).push(issue);
|
|
331
|
+
});
|
|
332
|
+
const platformsByKey = /* @__PURE__ */ new Map();
|
|
333
|
+
const removedPlatforms = /* @__PURE__ */ new Map();
|
|
334
|
+
joinedIssues.forEach((joined) => {
|
|
335
|
+
const key = issueDataKey(joined);
|
|
336
|
+
const platforms = mapGetOrInsertComputed(platformsByKey, `${key} ${joined.pos} ${joined.end}`, () => new Set((wordIssuesByKey.get(key) || []).filter((issue) => joined.pos <= issue.pos && issue.end <= joined.end).flatMap((issue) => issue.platforms)));
|
|
337
|
+
if (platforms.size > 0) removedPlatforms.set(joined, platforms);
|
|
338
|
+
});
|
|
339
|
+
if (removedPlatforms.size > 0) this.#reporter.removePlatforms(start, (issue) => removedPlatforms.get(issue));
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Reports the comments in text between statements, which `unbash` skips.
|
|
343
|
+
* @param gap The text
|
|
344
|
+
* @param position Where the text starts in the source
|
|
345
|
+
* @param context Where the text is
|
|
346
|
+
* @returns Where the first comment starts in the text, or -1 if there is none
|
|
347
|
+
*/
|
|
348
|
+
#checkComments(gap, position, context) {
|
|
349
|
+
if (!gap.includes("#")) return -1;
|
|
350
|
+
let first = -1;
|
|
351
|
+
gap.matchAll(COMMENTS).forEach((match) => {
|
|
352
|
+
const [text, comment = ""] = match;
|
|
353
|
+
const start = match.index + text.length - comment.length;
|
|
354
|
+
const commentEnd = position + start + comment.length;
|
|
355
|
+
if (first === -1) first = start;
|
|
356
|
+
this.#reporter.reportSyntax(context, {
|
|
357
|
+
end: commentEnd,
|
|
358
|
+
pos: position + start
|
|
359
|
+
}, "posix", false, "unsupportedSyntax", {
|
|
360
|
+
suggestion: "Remove it instead.",
|
|
361
|
+
syntax: "The `#` comment"
|
|
362
|
+
}, {
|
|
363
|
+
description: "Remove the comment.",
|
|
364
|
+
edits: [removal(context.source, position + start, commentEnd)]
|
|
365
|
+
});
|
|
366
|
+
});
|
|
367
|
+
return first;
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Gets where the text after a statement starts, after the bodies of its here-documents.
|
|
371
|
+
* @param statement A statement of the source
|
|
372
|
+
* @param nextStart Where the next statement starts, or where the source ends
|
|
373
|
+
* @param source The source that the statement is in
|
|
374
|
+
* @returns The offset in the source
|
|
375
|
+
*/
|
|
376
|
+
#statementEnd(statement, nextStart, source) {
|
|
377
|
+
const hereDocumentEnds = this.#redirections.hereDocumentEnds.get(source);
|
|
378
|
+
if (!hereDocumentEnds) return statement.end;
|
|
379
|
+
const count = sortedArrayInsertionIndex(hereDocumentEnds, nextStart, { rightmost: true });
|
|
380
|
+
return Math.max(statement.end, hereDocumentEnds[count - 1] || 0);
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Reports cmd.exe syntax, e.g. `if exist`.
|
|
384
|
+
* @param context Where to look for it
|
|
385
|
+
* @param pos Where to start looking in the source
|
|
386
|
+
* @param end Where to stop looking in the source
|
|
387
|
+
* @returns Whether it was found
|
|
388
|
+
*/
|
|
389
|
+
#checkWindowsBatchSyntax(context, pos = 0, end = context.source.length) {
|
|
390
|
+
const firstIssue = this.#reporter.issues.length;
|
|
391
|
+
const text = context.source.slice(pos, end);
|
|
392
|
+
const matches = WINDOWS_BATCH_SYNTAX.flatMap((syntax) => [...text.matchAll(syntax)]);
|
|
393
|
+
matches.forEach((match) => {
|
|
394
|
+
const start = pos + match.index;
|
|
395
|
+
this.#reporter.report(context, {
|
|
396
|
+
end: start + match[0].length,
|
|
397
|
+
pos: start
|
|
398
|
+
}, "unsupportedSyntax", {
|
|
399
|
+
suggestion: NODE_SCRIPT_SUGGESTION,
|
|
400
|
+
syntax: `The Windows batch syntax \`${truncate(match[0])}\``
|
|
401
|
+
}, context.portable ? PLATFORMS : POSIX_PLATFORMS);
|
|
402
|
+
});
|
|
403
|
+
this.#reporter.markFailures(firstIssue);
|
|
404
|
+
return matches.length > 0;
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Drops the issues of alternatives for different platforms, e.g. `rm -rf a || rmdir /s /q a`.
|
|
408
|
+
* @param node The list of commands joined by `||` (and other operators)
|
|
409
|
+
* @param starts Index of the first issue of each command in the list, and of the end of its
|
|
410
|
+
* issues
|
|
411
|
+
* @param context Where the list is
|
|
412
|
+
*/
|
|
413
|
+
#dropPlatformFallbacks(node, starts, context) {
|
|
414
|
+
if (!node.operators.includes("||")) return;
|
|
415
|
+
const issuesOf = (index) => this.#reporter.issues.slice(starts[index], starts[index + 1]);
|
|
416
|
+
const failingPlatforms = node.commands.map((command, index) => {
|
|
417
|
+
const ranges = failureRanges(command).map((range) => rangeInScript(context, range));
|
|
418
|
+
return new Set(issuesOf(index).filter((issue) => ranges.some((range) => range.pos <= issue.pos && issue.end <= range.end)).flatMap((issue) => this.#reporter.failingPlatforms(issue)));
|
|
419
|
+
});
|
|
420
|
+
const platformsOf = (index) => failingPlatforms[index] || NO_PLATFORMS;
|
|
421
|
+
const removedPlatforms = /* @__PURE__ */ new Map();
|
|
422
|
+
let first = 0;
|
|
423
|
+
while (first < node.commands.length) {
|
|
424
|
+
const { common, last } = findFallbackRun(node, platformsOf, first);
|
|
425
|
+
if (last > first) {
|
|
426
|
+
const runStart = first;
|
|
427
|
+
const failing = failingPlatforms.slice(runStart, last + 1);
|
|
428
|
+
(common.size === 0 ? failing : partialFallbackPlatforms(failing)).forEach((platforms, offset) => {
|
|
429
|
+
issuesOf(runStart + offset).forEach((issue) => {
|
|
430
|
+
const failures = this.#reporter.failingPlatforms(issue);
|
|
431
|
+
removedPlatforms.set(issue, failures.length > 0 && failures.every((platform) => platforms.has(platform)) ? new Set(issue.platforms) : platforms);
|
|
432
|
+
});
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
first = last + 1;
|
|
436
|
+
}
|
|
437
|
+
const [start] = starts;
|
|
438
|
+
if (start != null && removedPlatforms.size > 0) this.#reporter.removePlatforms(start, (issue) => removedPlatforms.get(issue));
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* Walks a command and the commands in it.
|
|
442
|
+
* @param node The command
|
|
443
|
+
* @param context Where the command is
|
|
444
|
+
* @param commandEnd Where the command ends along with its redirections, e.g. `> log` after
|
|
445
|
+
* `( ... )`
|
|
446
|
+
* @param redirects The redirections of the command, if it's a compound command, e.g. `> log`
|
|
447
|
+
* after `( ... )`
|
|
448
|
+
*/
|
|
449
|
+
#walkCommand(node, context, commandEnd = node.end, redirects = []) {
|
|
450
|
+
switch (node.type) {
|
|
451
|
+
case "AndOr": {
|
|
452
|
+
const starts = [];
|
|
453
|
+
node.commands.forEach((command) => {
|
|
454
|
+
starts.push(this.#reporter.issues.length);
|
|
455
|
+
this.#walkCommand(command, context);
|
|
456
|
+
});
|
|
457
|
+
starts.push(this.#reporter.issues.length);
|
|
458
|
+
this.#dropPlatformFallbacks(node, starts, context);
|
|
459
|
+
this.#lineBreaks.checkOperatorLineBreaks(node.commands, context);
|
|
460
|
+
break;
|
|
461
|
+
}
|
|
462
|
+
case "ArithmeticCommand":
|
|
463
|
+
this.#reporter.reportSyntax(context, node, "bash", false, "unsupportedSyntax", {
|
|
464
|
+
suggestion: BASH_SUGGESTION,
|
|
465
|
+
syntax: "The Bash-specific `(( ... ))` arithmetic command"
|
|
466
|
+
});
|
|
467
|
+
break;
|
|
468
|
+
case "ArithmeticFor":
|
|
469
|
+
this.#walkList(node.body, this.#reportStatement(context, node, "for (( ... ))", "bash"));
|
|
470
|
+
break;
|
|
471
|
+
case "BraceGroup": {
|
|
472
|
+
const isSubshellSafe = context.source[node.end - 1] === "}" && !doesChangeShellState(node) && !redirects.some(hasAssigningExpansion);
|
|
473
|
+
const covered = this.#reporter.reportSyntax(context, {
|
|
474
|
+
end: node.pos + 1,
|
|
475
|
+
pos: node.pos
|
|
476
|
+
}, "posix", true, "unsupportedSyntax", {
|
|
477
|
+
suggestion: "Use `( ... )` instead.",
|
|
478
|
+
syntax: "The command group `{ ... }`"
|
|
479
|
+
}, isSubshellSafe ? {
|
|
480
|
+
description: "Replace `{ ... }` with `( ... )`.",
|
|
481
|
+
edits: [{
|
|
482
|
+
end: node.pos + 1,
|
|
483
|
+
pos: node.pos,
|
|
484
|
+
text: context.source[node.pos - 1] === "(" ? " (" : "("
|
|
485
|
+
}, {
|
|
486
|
+
end: node.end,
|
|
487
|
+
pos: node.end - 1,
|
|
488
|
+
text: context.source[node.end] === ")" ? ") " : ")"
|
|
489
|
+
}]
|
|
490
|
+
} : void 0);
|
|
491
|
+
this.#walkGroup(node, withCovered(context, covered));
|
|
492
|
+
break;
|
|
493
|
+
}
|
|
494
|
+
case "Case": {
|
|
495
|
+
const nestedContext = this.#reportStatement(context, node, "case", "posix");
|
|
496
|
+
this.#words.checkWord(node.word, nestedContext, { skipGlobs: true });
|
|
497
|
+
node.items.forEach(({ body, end, pattern, terminator }) => {
|
|
498
|
+
pattern.forEach((word) => {
|
|
499
|
+
this.#words.checkWord(word, nestedContext, { skipGlobs: true });
|
|
500
|
+
});
|
|
501
|
+
this.#walkList(body, nestedContext);
|
|
502
|
+
if (terminator === ";&" || terminator === ";;&") this.#reporter.reportSyntax(nestedContext, {
|
|
503
|
+
end,
|
|
504
|
+
pos: end - terminator.length
|
|
505
|
+
}, "bash4", false, "unsupportedSyntax", {
|
|
506
|
+
suggestion: BASH_4_SUGGESTION,
|
|
507
|
+
syntax: `The Bash 4 \`${terminator}\` case terminator`
|
|
508
|
+
});
|
|
509
|
+
});
|
|
510
|
+
break;
|
|
511
|
+
}
|
|
512
|
+
case "Command":
|
|
513
|
+
this.#walkSimpleCommand(node, context);
|
|
514
|
+
break;
|
|
515
|
+
case "Coproc": {
|
|
516
|
+
const nestedContext = this.#reportStatement(context, node, "coproc", "bash4");
|
|
517
|
+
if (node.body.type === "CompoundList") this.#walkList(node.body, nestedContext);
|
|
518
|
+
else this.#walkCommand(node.body, nestedContext);
|
|
519
|
+
break;
|
|
520
|
+
}
|
|
521
|
+
case "For": {
|
|
522
|
+
const nestedContext = this.#reportStatement(context, node, "for", "posix");
|
|
523
|
+
node.wordlist?.forEach((word) => {
|
|
524
|
+
this.#words.checkWord(word, nestedContext);
|
|
525
|
+
});
|
|
526
|
+
this.#walkList(node.body, nestedContext);
|
|
527
|
+
break;
|
|
528
|
+
}
|
|
529
|
+
case "Function": {
|
|
530
|
+
const hasFunctionKeyword = node.name.pos > node.pos;
|
|
531
|
+
const covered = this.#reporter.reportSyntax(context, {
|
|
532
|
+
end: node.name.end,
|
|
533
|
+
pos: node.pos
|
|
534
|
+
}, hasFunctionKeyword ? "bash" : "posix", false, "unsupportedSyntax", hasFunctionKeyword ? {
|
|
535
|
+
suggestion: "Use `name() { ... }` or a Node.js script instead.",
|
|
536
|
+
syntax: "The Bash-specific `function` keyword"
|
|
537
|
+
} : {
|
|
538
|
+
suggestion: NODE_SCRIPT_SUGGESTION,
|
|
539
|
+
syntax: `The shell function definition \`${originalText(context, node.name)}()\``
|
|
540
|
+
});
|
|
541
|
+
const nestedContext = withCovered(context, covered);
|
|
542
|
+
if (node.body.type === "CompoundList") this.#walkList(node.body, nestedContext);
|
|
543
|
+
else this.#walkCommand(node.body, nestedContext);
|
|
544
|
+
break;
|
|
545
|
+
}
|
|
546
|
+
case "If": {
|
|
547
|
+
const nestedContext = this.#reportStatement(context, node, "if", "posix");
|
|
548
|
+
this.#walkList(node.clause, nestedContext);
|
|
549
|
+
this.#walkList(node.then, nestedContext);
|
|
550
|
+
node.elif.forEach((elif) => {
|
|
551
|
+
this.#walkList(elif.clause, nestedContext);
|
|
552
|
+
this.#walkList(elif.then, nestedContext);
|
|
553
|
+
});
|
|
554
|
+
if (node.else) this.#walkList(node.else, nestedContext);
|
|
555
|
+
break;
|
|
556
|
+
}
|
|
557
|
+
case "Pipeline":
|
|
558
|
+
this.#walkPipeline(node, context);
|
|
559
|
+
break;
|
|
560
|
+
case "Redirected": {
|
|
561
|
+
const redirectedContext = node.redirects.some(doesRedirectOutput) ? {
|
|
562
|
+
...context,
|
|
563
|
+
outputRedirected: true
|
|
564
|
+
} : context;
|
|
565
|
+
this.#walkCommand(node.command, redirectedContext, commandEnd, node.redirects);
|
|
566
|
+
node.redirects.forEach((redirection) => {
|
|
567
|
+
this.#redirections.checkRedirection(redirection, redirectedContext);
|
|
568
|
+
});
|
|
569
|
+
break;
|
|
570
|
+
}
|
|
571
|
+
case "Select": {
|
|
572
|
+
const nestedContext = this.#reportStatement(context, node, "select", "bash");
|
|
573
|
+
node.wordlist?.forEach((word) => {
|
|
574
|
+
this.#words.checkWord(word, nestedContext);
|
|
575
|
+
});
|
|
576
|
+
this.#walkList(node.body, nestedContext);
|
|
577
|
+
break;
|
|
578
|
+
}
|
|
579
|
+
case "Subshell":
|
|
580
|
+
if (!context.portable && node.body.commands.some((statement) => doesChangeDirectory(statement.command)) && !DIRECTORY_INDEPENDENT_REST.test(context.source.slice(commandEnd))) this.#reporter.reportSyntax(context, node, "posix", true, "inconsistentBehavior", {
|
|
581
|
+
reason: "cmd.exe only groups the commands, so `cd` also changes the directory of the commands that follow",
|
|
582
|
+
suggestion: "Use `npm --prefix <directory>` or workspaces instead.",
|
|
583
|
+
syntax: "The subshell `( ... )`"
|
|
584
|
+
});
|
|
585
|
+
this.#walkGroup(node, context);
|
|
586
|
+
break;
|
|
587
|
+
case "TestCommand": {
|
|
588
|
+
const covered = this.#reporter.reportSyntax(context, node, "bash", false, "unsupportedSyntax", {
|
|
589
|
+
suggestion: "Use `test` or a Node.js script instead.",
|
|
590
|
+
syntax: "The Bash-specific `[[ ... ]]` test"
|
|
591
|
+
});
|
|
592
|
+
this.#walkTestExpression(node.expression, withCovered(context, covered));
|
|
593
|
+
break;
|
|
594
|
+
}
|
|
595
|
+
case "While": {
|
|
596
|
+
const nestedContext = this.#reportStatement(context, node, node.kind, "posix");
|
|
597
|
+
this.#walkList(node.clause, nestedContext);
|
|
598
|
+
this.#walkList(node.body, nestedContext);
|
|
599
|
+
break;
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
#walkList(list, context) {
|
|
604
|
+
this.#walkStatements(list.commands, list, context);
|
|
605
|
+
}
|
|
606
|
+
/**
|
|
607
|
+
* Walks the commands of a group, i.e. `( ... )` or `{ ... }`.
|
|
608
|
+
* @param node The group
|
|
609
|
+
* @param context Where the group is
|
|
610
|
+
*/
|
|
611
|
+
#walkGroup(node, context) {
|
|
612
|
+
this.#walkStatements(node.body.commands, {
|
|
613
|
+
end: Math.max(node.body.end, node.end - 1),
|
|
614
|
+
pos: node.pos + 1
|
|
615
|
+
}, context, "group");
|
|
616
|
+
}
|
|
617
|
+
#walkNestedScript(script, part, context) {
|
|
618
|
+
const nestedContext = script.source == null ? context : {
|
|
619
|
+
...context,
|
|
620
|
+
fixedRange: rangeInScript(context, part),
|
|
621
|
+
originalSource: context.originalSource === context.source ? script.source : originalNestedSource(script.source, context.source.slice(part.pos, part.end), originalText(context, part)),
|
|
622
|
+
source: script.source
|
|
623
|
+
};
|
|
624
|
+
this.#walkStatements(script.commands, script, nestedContext);
|
|
625
|
+
}
|
|
626
|
+
#walkPipeline(node, context) {
|
|
627
|
+
node.prefixes.forEach((prefix) => {
|
|
628
|
+
this.#reporter.reportSyntax(context, prefix, "posix", false, "unsupportedSyntax", prefix.type === "Negation" ? {
|
|
629
|
+
suggestion: NODE_SCRIPT_SUGGESTION,
|
|
630
|
+
syntax: "The pipeline negation `!`"
|
|
631
|
+
} : {
|
|
632
|
+
suggestion: "cmd.exe's `time` sets the system time; use a Node.js script instead.",
|
|
633
|
+
syntax: "The `time` keyword"
|
|
634
|
+
});
|
|
635
|
+
});
|
|
636
|
+
node.operators.forEach((operator, index) => {
|
|
637
|
+
if (operator !== "|&") return;
|
|
638
|
+
const position = context.source.indexOf("|&", node.commands[index]?.end);
|
|
639
|
+
const range = {
|
|
640
|
+
end: position + 2,
|
|
641
|
+
pos: position
|
|
642
|
+
};
|
|
643
|
+
const separator = WHITESPACE.test(context.source[position - 1] || "") ? "" : " ";
|
|
644
|
+
this.#reporter.reportSyntax(context, range, "bash4", true, "unsupportedSyntax", {
|
|
645
|
+
suggestion: "Use `2>&1 |` instead.",
|
|
646
|
+
syntax: "The Bash 4 `|&` operator"
|
|
647
|
+
}, {
|
|
648
|
+
description: describeReplacement("|&", "2>&1 |"),
|
|
649
|
+
edits: [{
|
|
650
|
+
...range,
|
|
651
|
+
text: `${separator}2>&1 |`
|
|
652
|
+
}]
|
|
653
|
+
});
|
|
654
|
+
});
|
|
655
|
+
const pipedContext = {
|
|
656
|
+
...context,
|
|
657
|
+
outputRedirected: true
|
|
658
|
+
};
|
|
659
|
+
node.commands.forEach((command, index) => {
|
|
660
|
+
this.#walkCommand(command, index < node.commands.length - 1 ? pipedContext : context);
|
|
661
|
+
});
|
|
662
|
+
this.#lineBreaks.checkOperatorLineBreaks(node.commands, context);
|
|
663
|
+
}
|
|
664
|
+
#walkSimpleCommand(node, context) {
|
|
665
|
+
const name = node.name && staticValue(node.name);
|
|
666
|
+
if (name != null && CMD_STATEMENTS.has(name.toLowerCase()) && this.#checkWindowsBatchSyntax(context, node.pos, node.end)) return;
|
|
667
|
+
const isRedirection = node.name == null && node.prefix.length + node.suffix.length === 1;
|
|
668
|
+
const isOutputUsed = context.outputRedirected || node.redirects.some(doesRedirectOutput);
|
|
669
|
+
const redirectionContext = isOutputUsed && !context.outputRedirected ? {
|
|
670
|
+
...context,
|
|
671
|
+
outputRedirected: true
|
|
672
|
+
} : context;
|
|
673
|
+
node.prefix.forEach((item) => {
|
|
674
|
+
if (item.type === "Assignment") this.#words.checkAssignment(item, context, node.name ? node : void 0);
|
|
675
|
+
else this.#redirections.checkRedirection(item, redirectionContext, isRedirection);
|
|
676
|
+
});
|
|
677
|
+
const enclosingJoinedIssues = this.#swapJoinedScriptIssues(void 0);
|
|
678
|
+
const invocationStart = this.#reporter.issues.length;
|
|
679
|
+
const invocation = node.name && this.#commands.checkInvocation(node.name, node.suffix.filter((item) => item.type === "Word"), context, 0);
|
|
680
|
+
const joinedIssues = this.#swapJoinedScriptIssues(enclosingJoinedIssues);
|
|
681
|
+
const argumentContext = invocation?.name != null && (SHELLS.includes(invocation.name) || invocation.name === "find") && !this.#ignoredCommands.has(invocation.name) ? withCovered(context, WINDOWS_PLATFORMS) : context;
|
|
682
|
+
const crossEnv = invocation?.crossEnv && {
|
|
683
|
+
...invocation.crossEnv,
|
|
684
|
+
commandContext: {
|
|
685
|
+
...argumentContext,
|
|
686
|
+
crossEnv: invocation.crossEnv.shell ? "arguments" : "command",
|
|
687
|
+
crossEnvVariables: invocation.crossEnv.variables
|
|
688
|
+
},
|
|
689
|
+
context: {
|
|
690
|
+
...argumentContext,
|
|
691
|
+
crossEnv: "arguments"
|
|
692
|
+
}
|
|
693
|
+
};
|
|
694
|
+
const contextOf = (item) => {
|
|
695
|
+
if (!crossEnv || item.pos < crossEnv.start) return argumentContext;
|
|
696
|
+
return item.pos < crossEnv.commandStart ? crossEnv.context : crossEnv.commandContext;
|
|
697
|
+
};
|
|
698
|
+
if (node.name) {
|
|
699
|
+
this.#words.checkWord(node.name, context, { commandName: true });
|
|
700
|
+
if (isOutputUsed && !context.portable && invocation?.name === "echo") this.#redirections.checkQuotedEcho(node, context);
|
|
701
|
+
}
|
|
702
|
+
this.#lineBreaks.checkCommandLineBreak(node, argumentContext);
|
|
703
|
+
const isPrintedToTerminal = invocation?.target === "echo" && !isOutputUsed;
|
|
704
|
+
const wordsStart = this.#reporter.issues.length;
|
|
705
|
+
node.suffix.forEach((item) => {
|
|
706
|
+
if (item.type === "Assignment") this.#words.checkDeclaredAssignment(item, contextOf(item));
|
|
707
|
+
else if (item.type === "Word") {
|
|
708
|
+
const wordContext = contextOf(item);
|
|
709
|
+
this.#words.checkWord(item, this.#joinedScriptWords?.has(item) ? {
|
|
710
|
+
...wordContext,
|
|
711
|
+
fixExclusions: [...wordContext.fixExclusions, DOUBLE_QUOTE]
|
|
712
|
+
} : wordContext, {
|
|
713
|
+
command: invocation?.target,
|
|
714
|
+
nestedScript: this.#nestedScriptWords?.has(item),
|
|
715
|
+
printedToTerminal: isPrintedToTerminal
|
|
716
|
+
});
|
|
717
|
+
} else this.#redirections.checkRedirection(item, redirectionContext);
|
|
718
|
+
});
|
|
719
|
+
if (joinedIssues) this.#dropJoinedDuplicates(joinedIssues, this.#reporter.issues.slice(wordsStart), invocationStart);
|
|
720
|
+
}
|
|
721
|
+
#reportStatement(context, node, keyword, kind) {
|
|
722
|
+
const firstWord = substringBefore(keyword, " ");
|
|
723
|
+
const covered = this.#reporter.reportSyntax(context, {
|
|
724
|
+
end: node.pos + firstWord.length,
|
|
725
|
+
pos: node.pos
|
|
726
|
+
}, kind, false, "unsupportedSyntax", kind === "posix" ? {
|
|
727
|
+
suggestion: NODE_SCRIPT_SUGGESTION,
|
|
728
|
+
syntax: `The shell \`${keyword}\` statement`
|
|
729
|
+
} : {
|
|
730
|
+
suggestion: kind === "bash4" ? BASH_4_SUGGESTION : BASH_SUGGESTION,
|
|
731
|
+
syntax: `The ${bashLabel(kind)} \`${keyword}\` statement`
|
|
732
|
+
});
|
|
733
|
+
return withCovered(context, covered);
|
|
734
|
+
}
|
|
735
|
+
/**
|
|
736
|
+
* Walks a list of statements and checks the text around them, e.g. `;` and comments.
|
|
737
|
+
* @param statements The statements
|
|
738
|
+
* @param listRange Where the list is, including the text before the first statement and after
|
|
739
|
+
* the last one, e.g. up to the closing parenthesis of `( ... )`
|
|
740
|
+
* @param context Where the list is
|
|
741
|
+
* @param kind What the list is: a whole script, the body of `( ... )` or `{ ... }` (a group), or
|
|
742
|
+
* another list, e.g. the body of `if`
|
|
743
|
+
*/
|
|
744
|
+
#walkStatements(statements, listRange, context, kind = "list") {
|
|
745
|
+
const firstIssue = this.#reporter.issues.length;
|
|
746
|
+
const backgroundOperators = [];
|
|
747
|
+
const { source } = context;
|
|
748
|
+
const [first] = statements;
|
|
749
|
+
const leadingGap = source.slice(listRange.pos, first?.pos ?? listRange.end);
|
|
750
|
+
const leadingComment = this.#checkComments(leadingGap, listRange.pos, context);
|
|
751
|
+
const leadingLineBreak = leadingGap.indexOf("\n");
|
|
752
|
+
if (first && kind !== "list" && leadingLineBreak !== -1 && (leadingComment === -1 || leadingComment > leadingLineBreak)) {
|
|
753
|
+
const position = listRange.pos + leadingLineBreak;
|
|
754
|
+
this.#lineBreaks.reportLineBreak(context, position, ONE_LINE_SUGGESTION, joinLinesFix(source, position, first.pos));
|
|
755
|
+
}
|
|
756
|
+
statements.forEach((statement, index) => {
|
|
757
|
+
this.#walkCommand(statement.command, context);
|
|
758
|
+
if (statement.background) {
|
|
759
|
+
const position = source.lastIndexOf("&", statement.end - 1);
|
|
760
|
+
backgroundOperators.push({
|
|
761
|
+
end: position + 1,
|
|
762
|
+
pos: position
|
|
763
|
+
});
|
|
764
|
+
}
|
|
765
|
+
const next = statements.at(index + 1);
|
|
766
|
+
const gapEnd = next?.pos || listRange.end;
|
|
767
|
+
const gapStart = this.#statementEnd(statement, gapEnd, source);
|
|
768
|
+
const gap = source.slice(gapStart, gapEnd);
|
|
769
|
+
const comment = this.#checkComments(gap, gapStart, context);
|
|
770
|
+
const semicolon = gap.slice(0, comment === -1 ? void 0 : comment).indexOf(";");
|
|
771
|
+
if (semicolon !== -1) {
|
|
772
|
+
const range = {
|
|
773
|
+
end: gapStart + semicolon + 1,
|
|
774
|
+
pos: gapStart + semicolon
|
|
775
|
+
};
|
|
776
|
+
if (this.#reporter.reportSyntax(context, range, "posix", true, "unsupportedSyntax", {
|
|
777
|
+
suggestion: "Use `&&` instead.",
|
|
778
|
+
syntax: "The `;` command separator"
|
|
779
|
+
}, semicolonFix(range, gap, statement, next)).length > 0) return;
|
|
780
|
+
}
|
|
781
|
+
if ((next || kind === "group") && gap.includes("\n")) this.#lineBreaks.reportLineBreak(context, gapStart + gap.indexOf("\n"), next && !statement.background ? "Join the commands with `&&` instead." : ONE_LINE_SUGGESTION, semicolon === -1 && next ? lineBreakFix(source, gapStart, statement, next) : void 0);
|
|
782
|
+
});
|
|
783
|
+
if (backgroundOperators.length === 0) return;
|
|
784
|
+
if (!this.#reporter.issues.slice(firstIssue).some((issue) => this.#reporter.isFailure(issue) && issue.platforms.length === POSIX_PLATFORMS.length && arrayIsSubset(issue.platforms, POSIX_PLATFORMS))) backgroundOperators.forEach((range) => {
|
|
785
|
+
this.#reporter.reportSyntax(context, range, "posix", true, "inconsistentBehavior", {
|
|
786
|
+
reason: "cmd.exe runs the commands sequentially instead of in the background",
|
|
787
|
+
suggestion: "Use `concurrently` instead.",
|
|
788
|
+
syntax: "The `&` operator"
|
|
789
|
+
});
|
|
790
|
+
});
|
|
791
|
+
}
|
|
792
|
+
#walkTestExpression(expression, context) {
|
|
793
|
+
switch (expression.type) {
|
|
794
|
+
case "TestBinary":
|
|
795
|
+
this.#words.checkWord(expression.left, context, { skipGlobs: true });
|
|
796
|
+
this.#words.checkWord(expression.right, context, { skipGlobs: true });
|
|
797
|
+
break;
|
|
798
|
+
case "TestGroup":
|
|
799
|
+
this.#walkTestExpression(expression.expression, context);
|
|
800
|
+
break;
|
|
801
|
+
case "TestLogical":
|
|
802
|
+
expression.operands.forEach((operand) => {
|
|
803
|
+
this.#walkTestExpression(operand, context);
|
|
804
|
+
});
|
|
805
|
+
break;
|
|
806
|
+
case "TestNot":
|
|
807
|
+
this.#walkTestExpression(expression.operand, context);
|
|
808
|
+
break;
|
|
809
|
+
case "TestUnary": this.#words.checkWord(expression.operand, context, { skipGlobs: true });
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
};
|
|
813
|
+
/**
|
|
814
|
+
* Analyzes a script as each of the runners runs it.
|
|
815
|
+
* @param script The script's source
|
|
816
|
+
* @param options Analysis options
|
|
817
|
+
* @param runners What runs the script
|
|
818
|
+
* @returns The issues found with each runner, by their keys
|
|
819
|
+
*/
|
|
820
|
+
const analyzeWithRunners = (script, options, runners) => {
|
|
821
|
+
const parsedScripts = /* @__PURE__ */ new Map();
|
|
822
|
+
const commandOptions = {
|
|
823
|
+
dependencies: options.dependencies || [],
|
|
824
|
+
ignoredCommands: new Set(options.ignoreCommands)
|
|
825
|
+
};
|
|
826
|
+
const analyses = [];
|
|
827
|
+
return new Map(runners.map((runner) => {
|
|
828
|
+
const traits = runnerTraits(runner, options.shellEmulator || false);
|
|
829
|
+
const reused = analyses.find((analysis) => analysis.traits.matches(traits));
|
|
830
|
+
if (reused) return [runner, reused.issues];
|
|
831
|
+
const reader = new TraitsReader(traits);
|
|
832
|
+
const analyzer = new ScriptAnalyzer(script.length, commandOptions, reader, parsedScripts);
|
|
833
|
+
analyzer.analyzeSource({
|
|
834
|
+
covered: /* @__PURE__ */ new Set(),
|
|
835
|
+
crossEnv: false,
|
|
836
|
+
fixExclusions: [],
|
|
837
|
+
fixedRange: void 0,
|
|
838
|
+
offset: 0,
|
|
839
|
+
originalSource: script,
|
|
840
|
+
portable: reader.get("portableShell") || false,
|
|
841
|
+
runnerScript: true,
|
|
842
|
+
shell: "native",
|
|
843
|
+
source: script
|
|
844
|
+
});
|
|
845
|
+
const { issues } = analyzer;
|
|
846
|
+
analyses.push({
|
|
847
|
+
issues,
|
|
848
|
+
traits: reader
|
|
849
|
+
});
|
|
850
|
+
return [runner, issues];
|
|
851
|
+
}));
|
|
852
|
+
};
|
|
853
|
+
export { analyzeWithRunners };
|