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,573 @@
|
|
|
1
|
+
import { CMD_BUILTINS, NON_GLOB_PROGRAMS, OS_COMMANDS, PATTERN_COMMANDS, SHELL_STATE_COMMANDS } from "./commands.mjs";
|
|
2
|
+
import { bashLabel, originalText, withCovered } from "./context.mjs";
|
|
3
|
+
import { backslashPathFix, braceExpansionFix, crossEnvEdits, quoting } from "./fixes.mjs";
|
|
4
|
+
import { BASH_SUGGESTION, CROSS_ENV_SUGGESTION, CROSS_ENV_VARIABLE_SUGGESTION, NODE_SCRIPT_SUGGESTION, VARIABLE_SUGGESTION, snippet, truncate } from "./messages.mjs";
|
|
5
|
+
import { MACOS_PLATFORMS, PLATFORMS, POSIX_PLATFORMS, WINDOWS_PLATFORMS } from "./platforms.mjs";
|
|
6
|
+
import { setHas } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/set/set-has.mjs";
|
|
7
|
+
import { substringAfterLast } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/string/substring-after-last.mjs";
|
|
8
|
+
import { toForwardSlashes } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/path/to-forward-slashes.mjs";
|
|
9
|
+
import { DOUBLE_QUOTED_SPECIAL_CHARACTERS, GLOB_PART_TYPES, POSIX_PATH, TILDE_EXPANSION, WHITESPACE, doesMentionVariables, isPlainAssignment, isPlainWord, staticValue } from "./words.mjs";
|
|
10
|
+
// src/word-checker.ts
|
|
11
|
+
/** Variables that Bash sets, but dash doesn't, by the kind of syntax they are */
|
|
12
|
+
const BASH_VARIABLES = /* @__PURE__ */ new Map([
|
|
13
|
+
["BASH_SOURCE", "bash"],
|
|
14
|
+
["BASH_VERSION", "bash"],
|
|
15
|
+
["EPOCHREALTIME", "bash4"],
|
|
16
|
+
["EPOCHSECONDS", "bash4"],
|
|
17
|
+
["EUID", "bash"],
|
|
18
|
+
["HOSTNAME", "bash"],
|
|
19
|
+
["OSTYPE", "bash"],
|
|
20
|
+
["PIPESTATUS", "bash"],
|
|
21
|
+
["RANDOM", "bash"],
|
|
22
|
+
["SECONDS", "bash"],
|
|
23
|
+
["SRANDOM", "bash4"],
|
|
24
|
+
["UID", "bash"]
|
|
25
|
+
]);
|
|
26
|
+
/** Variables that POSIX shells and systems set, but Windows doesn't, e.g. `PWD` */
|
|
27
|
+
const POSIX_ONLY_VARIABLES = /* @__PURE__ */ new Set([
|
|
28
|
+
...BASH_VARIABLES.keys(),
|
|
29
|
+
"LOGNAME",
|
|
30
|
+
"OLDPWD",
|
|
31
|
+
"PPID",
|
|
32
|
+
"PWD",
|
|
33
|
+
"SHELL",
|
|
34
|
+
"TMPDIR",
|
|
35
|
+
"USER"
|
|
36
|
+
]);
|
|
37
|
+
/** Variables that package managers set, e.g. `npm_package_version` */
|
|
38
|
+
const PACKAGE_MANAGER_VARIABLE = /^(?:npm_\w+|INIT_CWD)$/;
|
|
39
|
+
/** cmd.exe statements, which POSIX shells parse as commands, e.g. `IF` in `IF EXIST dist ...` */
|
|
40
|
+
const CMD_STATEMENTS = /* @__PURE__ */ new Set(["for", "if"]);
|
|
41
|
+
/** Characters that cmd.exe splits words at or interprets */
|
|
42
|
+
const CMD_SPECIAL_CHARACTERS = /[\s"%&<>^|]/;
|
|
43
|
+
/**
|
|
44
|
+
* Characters that shells interpret outside quotes, apart from whitespace, e.g. `*` and `;` in POSIX
|
|
45
|
+
* shells, or `%` and `^` in cmd.exe
|
|
46
|
+
*/
|
|
47
|
+
const SHELL_SPECIAL_CHARACTERS = /["#$%&'()*;<>?[\\\]^`{|}~]/;
|
|
48
|
+
/** Operators that cmd.exe runs even in a string that `echo` prints, e.g. `>` in `echo '-> done'` */
|
|
49
|
+
const CMD_OPERATORS = /[&<>|]/;
|
|
50
|
+
/** Backslash escapes that POSIX shells remove, but cmd.exe passes on, e.g. `\$` or `\ ` */
|
|
51
|
+
const UNQUOTED_ESCAPE = /\\[\t !#$&'()*;<>?[\]`{|}~]/g;
|
|
52
|
+
/** The same, in double quotes */
|
|
53
|
+
const QUOTED_ESCAPE = /\\[$`]/g;
|
|
54
|
+
/**
|
|
55
|
+
* Several backslashes, which POSIX shells replace with half as many, while programs on Windows
|
|
56
|
+
* only do so before a `"`, e.g. `\\` in `"\\.e2e"`
|
|
57
|
+
*/
|
|
58
|
+
const BACKSLASH_RUN = /(?<!\\)\\{2,}(?!["\\])/g;
|
|
59
|
+
/** `cross-env` removes the backslashes of these escapes itself */
|
|
60
|
+
const CROSS_ENV_ESCAPE = /^\\[$']$/;
|
|
61
|
+
/** A named variable, e.g. `$HOME` rather than `$?` */
|
|
62
|
+
const NAMED_VARIABLE = /^\$\w+$/;
|
|
63
|
+
/** A `**` path segment, which `sh` doesn't expand recursively */
|
|
64
|
+
const RECURSIVE_GLOB = /(?:^|[/=])\*\*\//;
|
|
65
|
+
/** A cmd.exe variable with a name of two or more characters, e.g. `%PORT%` */
|
|
66
|
+
const WINDOWS_VARIABLE = /%[a-z_]\w+%/i;
|
|
67
|
+
const WINDOWS_VARIABLES = new RegExp(WINDOWS_VARIABLE, "gi");
|
|
68
|
+
/** Several percent-encoded bytes, e.g. `%C3%BC` for `ü` in a URL */
|
|
69
|
+
const PERCENT_ENCODED_BYTES = /(?:%[\da-f]{2}){2,}/gi;
|
|
70
|
+
/**
|
|
71
|
+
* A Windows path: a drive, relative prefix or first segment, then `\`-separated segments.
|
|
72
|
+
* The segments may be globs, e.g. `lib\**\*.js`
|
|
73
|
+
*/
|
|
74
|
+
const BACKSLASH_PATH = /^(?:[a-z]:|[\w\-.]+|\*\*?(?:\.[\w-]+)*)(?:\\(?:[\w\-.]+|\*\*?(?:\.[\w-]+)*))+\\?$/i;
|
|
75
|
+
/** A Windows path's drive or relative prefix, e.g. `C:\` or `.\` */
|
|
76
|
+
const WINDOWS_PATH_PREFIX = /^(?:[a-z]:|\.{1,2})\\/i;
|
|
77
|
+
const FILE_EXTENSION = /\.\w+$/;
|
|
78
|
+
const MULTIPLE_BACKSLASHES = /\\.*\\/;
|
|
79
|
+
/** Backslashes that only escape dots are likely a regex, e.g. `logging\.spec\.ts` */
|
|
80
|
+
const NON_DOT_ESCAPE = /\\[^.]/;
|
|
81
|
+
/** A backslash that is neither a regex escape of a dot nor a character class, e.g. `\s` */
|
|
82
|
+
const NON_REGEX_ESCAPE = /\\(?![.bdsw])/i;
|
|
83
|
+
const hasBashArithmetic = (expression) => {
|
|
84
|
+
switch (expression?.type) {
|
|
85
|
+
case "ArithmeticBinary": return expression.operator.startsWith("**") || hasBashArithmetic(expression.left) || hasBashArithmetic(expression.right);
|
|
86
|
+
case "ArithmeticGroup": return hasBashArithmetic(expression.expression);
|
|
87
|
+
case "ArithmeticTernary": return [
|
|
88
|
+
expression.test,
|
|
89
|
+
expression.consequent,
|
|
90
|
+
expression.alternate
|
|
91
|
+
].some(hasBashArithmetic);
|
|
92
|
+
case "ArithmeticUnary": return expression.operator === "++" || expression.operator === "--" || hasBashArithmetic(expression.operand);
|
|
93
|
+
default: return false;
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
const parameterOperands = (operation) => {
|
|
97
|
+
switch (operation.type) {
|
|
98
|
+
case "CaseModification":
|
|
99
|
+
case "Default":
|
|
100
|
+
case "Remove":
|
|
101
|
+
case "Transform": return [operation.operand];
|
|
102
|
+
case "Replace": return [operation.pattern, operation.replacement];
|
|
103
|
+
case "Slice": return [operation.offset, operation.length];
|
|
104
|
+
default: return [];
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* Checks whether a command is likely a tool, e.g. `eslint`, and not an OS utility or a cmd.exe
|
|
109
|
+
* command.
|
|
110
|
+
* Tools usually expand globs themselves and accept `/` as a path separator.
|
|
111
|
+
* @param command Name of the command that receives the arguments
|
|
112
|
+
* @returns Whether the command is likely a tool
|
|
113
|
+
*/
|
|
114
|
+
const isTool = (command) => {
|
|
115
|
+
const name = command?.toLowerCase();
|
|
116
|
+
return name != null && !OS_COMMANDS.has(name) && !CMD_BUILTINS.has(name) && !CMD_STATEMENTS.has(name);
|
|
117
|
+
};
|
|
118
|
+
/** The fix that prefixes each command with `cross-env`, which its assignments share */
|
|
119
|
+
const crossEnvPrefixFixes = /* @__PURE__ */ new WeakMap();
|
|
120
|
+
/**
|
|
121
|
+
* Gets a fix that prefixes a command with `cross-env`, which takes over its assignments.
|
|
122
|
+
* @param command The command
|
|
123
|
+
* @returns The fix, if `cross-env` runs the same command with the same variables
|
|
124
|
+
*/
|
|
125
|
+
const crossEnvPrefixFix = (command) => {
|
|
126
|
+
if (crossEnvPrefixFixes.has(command)) return crossEnvPrefixFixes.get(command);
|
|
127
|
+
const names = /* @__PURE__ */ new Set();
|
|
128
|
+
const name = command.name && staticValue(command.name) || "";
|
|
129
|
+
const edits = name !== ":" && command.name != null && !SHELL_STATE_COMMANDS.has(name) && command.prefix.every((item) => {
|
|
130
|
+
if (!isPlainAssignment(item) || doesMentionVariables(item.value.text, names)) return false;
|
|
131
|
+
names.add(item.name);
|
|
132
|
+
return true;
|
|
133
|
+
}) ? crossEnvEdits(command.prefix, [command.name, ...command.suffix.filter((item) => item.type === "Word")]) : void 0;
|
|
134
|
+
const fix = edits && {
|
|
135
|
+
description: "Prefix the command with `cross-env`.",
|
|
136
|
+
edits: [{
|
|
137
|
+
end: command.pos,
|
|
138
|
+
pos: command.pos,
|
|
139
|
+
text: "cross-env "
|
|
140
|
+
}, ...edits]
|
|
141
|
+
};
|
|
142
|
+
crossEnvPrefixFixes.set(command, fix);
|
|
143
|
+
return fix;
|
|
144
|
+
};
|
|
145
|
+
/** Checks words, e.g. for quotes, expansions and paths */
|
|
146
|
+
var WordChecker = class {
|
|
147
|
+
#reporter;
|
|
148
|
+
#runnerTraits;
|
|
149
|
+
#walkNestedScript;
|
|
150
|
+
constructor(reporter, walkNestedScript, runnerTraits) {
|
|
151
|
+
this.#reporter = reporter;
|
|
152
|
+
this.#walkNestedScript = walkNestedScript;
|
|
153
|
+
this.#runnerTraits = runnerTraits;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Checks a variable assignment.
|
|
157
|
+
* @param assignment Assignment to check, e.g. `CI=1`
|
|
158
|
+
* @param context Where the assignment is
|
|
159
|
+
* @param command The command that the assignment prefixes, e.g. `jest` in `CI=1 jest`
|
|
160
|
+
*/
|
|
161
|
+
checkAssignment(assignment, context, command) {
|
|
162
|
+
const { value } = assignment;
|
|
163
|
+
const isBashSpecific = this.#checkBashAssignment(assignment, context);
|
|
164
|
+
const doesPrefixCommand = command != null;
|
|
165
|
+
if (!isBashSpecific) this.#reporter.reportSyntax(context, assignment, "posix", true, "unsupportedSyntax", {
|
|
166
|
+
suggestion: CROSS_ENV_SUGGESTION,
|
|
167
|
+
syntax: doesPrefixCommand ? `The inline environment variable assignment \`${snippet(context, assignment)}\`` : `The shell variable assignment \`${snippet(context, assignment)}\``
|
|
168
|
+
}, command && crossEnvPrefixFix(command));
|
|
169
|
+
const valueContext = doesPrefixCommand && !isBashSpecific ? {
|
|
170
|
+
...context,
|
|
171
|
+
crossEnv: "arguments"
|
|
172
|
+
} : context;
|
|
173
|
+
if (value.type === "ArrayValue") value.elements.forEach((word) => {
|
|
174
|
+
this.checkWord(word, valueContext);
|
|
175
|
+
});
|
|
176
|
+
else this.checkWord(value, valueContext, { skipGlobs: true });
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Checks an assignment that a declaration command receives, e.g. `A=1` in `export A=1`.
|
|
180
|
+
* @param assignment Assignment to check
|
|
181
|
+
* @param context Where the assignment is
|
|
182
|
+
*/
|
|
183
|
+
checkDeclaredAssignment(assignment, context) {
|
|
184
|
+
this.#checkBashAssignment(assignment, context);
|
|
185
|
+
const { value } = assignment;
|
|
186
|
+
(value.type === "ArrayValue" ? value.elements : [value]).forEach((word) => {
|
|
187
|
+
this.checkWord(word, context, { declaredValue: value.type === "Word" });
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Reports an assignment that only Bash supports, e.g. `A+=1` or `A=(1 2)`.
|
|
192
|
+
* @param assignment Assignment to check
|
|
193
|
+
* @param context Where the assignment is
|
|
194
|
+
* @returns Whether it's Bash-specific
|
|
195
|
+
*/
|
|
196
|
+
#checkBashAssignment(assignment, context) {
|
|
197
|
+
const { append, index, value } = assignment;
|
|
198
|
+
const isBashSpecific = value.type === "ArrayValue" || index != null || append;
|
|
199
|
+
if (isBashSpecific) this.#reporter.reportSyntax(context, assignment, "bash", false, "unsupportedSyntax", {
|
|
200
|
+
suggestion: BASH_SUGGESTION,
|
|
201
|
+
syntax: `The Bash-specific ${index || value.type === "ArrayValue" ? "array" : "appending"} assignment \`${snippet(context, assignment)}\``
|
|
202
|
+
});
|
|
203
|
+
return isBashSpecific;
|
|
204
|
+
}
|
|
205
|
+
#checkBashVariable(name, node, context) {
|
|
206
|
+
const kind = BASH_VARIABLES.get(name);
|
|
207
|
+
if (!kind) return;
|
|
208
|
+
let reason = "only Bash 5 sets it, and macOS ships Bash 3.2";
|
|
209
|
+
if (context.portable) reason = "Yarn's shell doesn't set it, so the script fails";
|
|
210
|
+
else if (kind === "bash") reason = "dash, the `sh` of Debian/Ubuntu, doesn't set it";
|
|
211
|
+
this.#reporter.reportSyntax(context, node, kind, name === "RANDOM", "inconsistentBehavior", {
|
|
212
|
+
reason,
|
|
213
|
+
suggestion: NODE_SCRIPT_SUGGESTION,
|
|
214
|
+
syntax: `The Bash variable \`$${name}\``
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Reports a variable that package managers set, but the runner doesn't, e.g. `node --run`.
|
|
219
|
+
* @param name Name of the variable, e.g. `npm_package_version`
|
|
220
|
+
* @param node The node to report
|
|
221
|
+
* @param context Where the variable is
|
|
222
|
+
* @param text How the script refers to the variable, e.g. `%npm_package_version%`
|
|
223
|
+
*/
|
|
224
|
+
#checkPackageManagerVariable(name, node, context, text = `$${name}`) {
|
|
225
|
+
if (this.#isMissingPackageManagerVariable(name)) this.#reporter.report(context, node, "inconsistentBehavior", {
|
|
226
|
+
reason: `${this.#runnerTraits.get("label")} doesn't set it`,
|
|
227
|
+
suggestion: NODE_SCRIPT_SUGGESTION,
|
|
228
|
+
syntax: `The variable \`${text}\``
|
|
229
|
+
}, PLATFORMS);
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Reports a variable that `cross-env` replaces on Windows with a different value than POSIX
|
|
233
|
+
* shells use, e.g. `$PWD`, or `$A` in `cross-env A=1 node $A`.
|
|
234
|
+
* @param name Name of the variable
|
|
235
|
+
* @param node The variable expansion
|
|
236
|
+
* @param context Where the expansion is
|
|
237
|
+
* @returns The platforms that the reports cover
|
|
238
|
+
*/
|
|
239
|
+
#checkCrossEnvVariable(name, node, context) {
|
|
240
|
+
if (context.portable) return [];
|
|
241
|
+
const syntax = `The variable \`${node.text}\``;
|
|
242
|
+
const isSetByCrossEnv = context.crossEnvVariables?.has(name);
|
|
243
|
+
const covered = isSetByCrossEnv ? this.#reporter.report(context, node, "inconsistentBehavior", {
|
|
244
|
+
reason: "POSIX shells expand it before `cross-env` sets it, so the command doesn't get the new value",
|
|
245
|
+
suggestion: "Use the value itself or a Node.js script instead.",
|
|
246
|
+
syntax
|
|
247
|
+
}, POSIX_PLATFORMS) : [];
|
|
248
|
+
if (!isSetByCrossEnv && this.#isUnsetOnWindows(name)) return this.#reporter.report(context, node, "inconsistentBehavior", {
|
|
249
|
+
reason: "Windows doesn't set it, so `cross-env` replaces it with an empty string",
|
|
250
|
+
suggestion: this.#variableSuggestion(name),
|
|
251
|
+
syntax
|
|
252
|
+
}, WINDOWS_PLATFORMS);
|
|
253
|
+
return context.crossEnv === "command" ? [...covered, ...this.#reporter.report(context, node, "inconsistentBehavior", {
|
|
254
|
+
reason: `\`cross-env\` replaces it with \`%${name}%\`, which isn't expanded without a shell`,
|
|
255
|
+
suggestion: "Use `cross-env-shell` or a Node.js script instead.",
|
|
256
|
+
syntax
|
|
257
|
+
}, WINDOWS_PLATFORMS)] : covered;
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Reports an unquoted carriage return, e.g. of a CRLF line break, which POSIX shells keep.
|
|
261
|
+
* @param word Word to check, e.g. `tsc` and a carriage return before a line break
|
|
262
|
+
* @param context Where the word is
|
|
263
|
+
*/
|
|
264
|
+
#checkCarriageReturn(word, context) {
|
|
265
|
+
if (context.portable || !word.text.includes("\r")) return;
|
|
266
|
+
const literal = word.parts ? word.parts.find((part) => part.type === "Literal" && part.text.includes("\r")) : word;
|
|
267
|
+
if (!literal) return;
|
|
268
|
+
const position = literal.pos + literal.text.indexOf("\r");
|
|
269
|
+
this.#reporter.report(context, {
|
|
270
|
+
end: position + 1,
|
|
271
|
+
pos: position
|
|
272
|
+
}, "inconsistentBehavior", {
|
|
273
|
+
reason: "POSIX shells treat it as part of the word, e.g. of a command's name",
|
|
274
|
+
suggestion: "Use line feeds (`\\n`) for line breaks instead.",
|
|
275
|
+
syntax: "The carriage return (`\\r`)"
|
|
276
|
+
}, POSIX_PLATFORMS, {
|
|
277
|
+
description: "Remove the carriage return.",
|
|
278
|
+
edits: [{
|
|
279
|
+
end: position + 1,
|
|
280
|
+
pos: position,
|
|
281
|
+
text: ""
|
|
282
|
+
}]
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Reports escapes that cmd.exe doesn't remove, e.g. `\$`, and its own escape character `^`.
|
|
287
|
+
* @param word Word to check
|
|
288
|
+
* @param context Where the word is
|
|
289
|
+
* @param shouldCheckCaret Whether to check for `^`, which a nested script checks itself
|
|
290
|
+
*/
|
|
291
|
+
#checkEscapes(word, context, shouldCheckCaret) {
|
|
292
|
+
const literals = word.parts ? word.parts.flatMap((part, index) => {
|
|
293
|
+
if (part.type === "Literal") {
|
|
294
|
+
const isBeforeQuote = word.parts?.[index + 1]?.type === "DoubleQuoted";
|
|
295
|
+
return {
|
|
296
|
+
pattern: UNQUOTED_ESCAPE,
|
|
297
|
+
text: isBeforeQuote ? `${part.text}"` : part.text
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
return part.type === "DoubleQuoted" ? part.parts.flatMap((child, childIndex) => child.type === "Literal" ? {
|
|
301
|
+
pattern: QUOTED_ESCAPE,
|
|
302
|
+
text: childIndex === part.parts.length - 1 ? `${child.text}"` : child.text
|
|
303
|
+
} : []) : [];
|
|
304
|
+
}) : [{
|
|
305
|
+
pattern: UNQUOTED_ESCAPE,
|
|
306
|
+
text: word.text
|
|
307
|
+
}];
|
|
308
|
+
const escape = literals.flatMap(({ pattern, text }) => [...text.match(pattern) || [], ...!context.crossEnv && text.match(BACKSLASH_RUN) || []]).find((match) => !(context.crossEnv && CROSS_ENV_ESCAPE.test(match)));
|
|
309
|
+
if (escape != null) this.#reporter.reportSyntax(context, word, "posix", true, "inconsistentBehavior", {
|
|
310
|
+
reason: "cmd.exe doesn't treat backslashes as escape characters, so it passes them on",
|
|
311
|
+
suggestion: escape.startsWith("\\\\") ? "Use a single backslash in double quotes (e.g. `\"\\.\"`) instead." : "Use escaped double quotes (`\\\"`) instead.",
|
|
312
|
+
syntax: `The backslash escape \`${escape}\``
|
|
313
|
+
});
|
|
314
|
+
if (shouldCheckCaret && literals.some(({ pattern, text }) => pattern === UNQUOTED_ESCAPE && text.includes("^"))) this.#reporter.reportSyntax(context, word, "posix", true, "inconsistentBehavior", {
|
|
315
|
+
reason: "cmd.exe treats `^` as an escape character and removes it",
|
|
316
|
+
suggestion: "Quote it with double quotes instead.",
|
|
317
|
+
syntax: `The caret in \`${snippet(context, word)}\``
|
|
318
|
+
}, isPlainWord(word) ? quoting(word, "Quote it with escaped double quotes.") : void 0);
|
|
319
|
+
}
|
|
320
|
+
#checkParameterExpansion(part, context) {
|
|
321
|
+
const { index, operation, prefix } = part;
|
|
322
|
+
const isBashSpecific = index != null || prefix === "!" || operation != null && operation.type !== "Default" && operation.type !== "Remove";
|
|
323
|
+
const hasPortableDefault = operation?.type === "Default" && (operation.operator === ":-" || operation.operator === ":+");
|
|
324
|
+
const bashKind = operation?.type === "CaseModification" || operation?.type === "Transform" ? "bash4" : "bash";
|
|
325
|
+
let covered;
|
|
326
|
+
if (isBashSpecific) covered = this.#reporter.reportSyntax(context, part, bashKind, false, "unsupportedSyntax", {
|
|
327
|
+
suggestion: NODE_SCRIPT_SUGGESTION,
|
|
328
|
+
syntax: `The ${bashLabel(bashKind)} parameter expansion \`${snippet(context, part)}\``
|
|
329
|
+
});
|
|
330
|
+
else if (operation == null && prefix == null && context.crossEnv) covered = this.#checkCrossEnvVariable(part.parameter, part, context);
|
|
331
|
+
else {
|
|
332
|
+
const isPortableSupported = prefix == null && (operation == null || hasPortableDefault);
|
|
333
|
+
covered = this.#reporter.reportSyntax(context, part, "posix", isPortableSupported, "unsupportedSyntax", {
|
|
334
|
+
suggestion: context.crossEnv ? CROSS_ENV_VARIABLE_SUGGESTION : this.#variableSuggestion(part.parameter),
|
|
335
|
+
syntax: `The variable expansion \`${snippet(context, part)}\``
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
const nestedContext = withCovered(context, covered);
|
|
339
|
+
if (!(hasPortableDefault && context.portable)) this.#checkBashVariable(part.parameter, part, nestedContext);
|
|
340
|
+
this.#checkPackageManagerVariable(part.parameter, part, nestedContext);
|
|
341
|
+
[index, ...operation ? parameterOperands(operation) : []].forEach((operand) => {
|
|
342
|
+
if (operand) this.checkWord(operand, nestedContext, { skipGlobs: true });
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
#checkParts(parts, context, state) {
|
|
346
|
+
parts.forEach((part) => {
|
|
347
|
+
this.#checkPart(part, context, state);
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Checks a part of a word, e.g. a quoted string or an expansion.
|
|
352
|
+
* @param part Part to check, e.g. `$HOME` in `$HOME/.npmrc`
|
|
353
|
+
* @param context Where the word is
|
|
354
|
+
* @param state What the word contains, collected while checking its parts
|
|
355
|
+
*/
|
|
356
|
+
#checkPart(part, context, state) {
|
|
357
|
+
switch (part.type) {
|
|
358
|
+
case "AnsiCQuoted":
|
|
359
|
+
this.#reporter.reportSyntax(context, part, "bash", true, "unsupportedSyntax", {
|
|
360
|
+
suggestion: BASH_SUGGESTION,
|
|
361
|
+
syntax: `The Bash-specific ANSI-C quoted string \`${snippet(context, part)}\``
|
|
362
|
+
});
|
|
363
|
+
break;
|
|
364
|
+
case "ArithmeticExpansion": {
|
|
365
|
+
const covered = this.#reporter.reportSyntax(context, part, "posix", true, "unsupportedSyntax", {
|
|
366
|
+
suggestion: NODE_SCRIPT_SUGGESTION,
|
|
367
|
+
syntax: `The arithmetic expansion \`${snippet(context, part)}\``
|
|
368
|
+
});
|
|
369
|
+
if (hasBashArithmetic(part.expression)) this.#reporter.reportSyntax(withCovered(context, covered), part, "bash", true, "unsupportedSyntax", {
|
|
370
|
+
suggestion: NODE_SCRIPT_SUGGESTION,
|
|
371
|
+
syntax: `The Bash-specific arithmetic in \`${snippet(context, part)}\``
|
|
372
|
+
});
|
|
373
|
+
break;
|
|
374
|
+
}
|
|
375
|
+
case "BraceExpansion":
|
|
376
|
+
state.braceExpansions.push(part);
|
|
377
|
+
break;
|
|
378
|
+
case "CommandExpansion": {
|
|
379
|
+
const covered = this.#reporter.reportSyntax(context, part, "posix", !part.text.startsWith("`"), "unsupportedSyntax", {
|
|
380
|
+
suggestion: NODE_SCRIPT_SUGGESTION,
|
|
381
|
+
syntax: `The command substitution \`${snippet(context, part)}\``
|
|
382
|
+
});
|
|
383
|
+
if (part.script && !part.script.errors?.length) this.#walkNestedScript(part.script, part, withCovered(context, covered));
|
|
384
|
+
break;
|
|
385
|
+
}
|
|
386
|
+
case "DoubleQuoted":
|
|
387
|
+
this.#checkParts(part.parts, context, state);
|
|
388
|
+
break;
|
|
389
|
+
case "ExtendedGlob":
|
|
390
|
+
if (context.bashOptions?.has("extglob")) break;
|
|
391
|
+
this.#reporter.report(context, part, "unsupportedSyntax", {
|
|
392
|
+
suggestion: state.skipGlobs ? "It requires Bash's `extglob` option; use a Node.js script instead." : "It requires Bash's `extglob` option; quote the pattern and let the tool expand it instead.",
|
|
393
|
+
syntax: `The extended glob \`${snippet(context, part)}\``
|
|
394
|
+
}, PLATFORMS);
|
|
395
|
+
break;
|
|
396
|
+
case "Glob":
|
|
397
|
+
if (part.kind === "star") state.globPosition ??= part.pos;
|
|
398
|
+
break;
|
|
399
|
+
case "LocaleString":
|
|
400
|
+
this.#reporter.reportSyntax(context, part, "bash", false, "unsupportedSyntax", {
|
|
401
|
+
suggestion: BASH_SUGGESTION,
|
|
402
|
+
syntax: `The Bash-specific locale string \`${snippet(context, part)}\``
|
|
403
|
+
});
|
|
404
|
+
this.#checkParts(part.parts, context, state);
|
|
405
|
+
break;
|
|
406
|
+
case "ParameterExpansion":
|
|
407
|
+
this.#checkParameterExpansion(part, context);
|
|
408
|
+
break;
|
|
409
|
+
case "ProcessSubstitution": {
|
|
410
|
+
const covered = this.#reporter.reportSyntax(context, part, "bashNonPosix", false, "unsupportedSyntax", {
|
|
411
|
+
suggestion: "Use a temporary file or a Node.js script instead.",
|
|
412
|
+
syntax: `The Bash-specific process substitution \`${snippet(context, part)}\``
|
|
413
|
+
});
|
|
414
|
+
if (part.script && !part.script.errors?.length) this.#walkNestedScript(part.script, part, withCovered(context, covered));
|
|
415
|
+
break;
|
|
416
|
+
}
|
|
417
|
+
case "SimpleExpansion": {
|
|
418
|
+
const name = part.text.slice(1);
|
|
419
|
+
const covered = context.crossEnv && NAMED_VARIABLE.test(part.text) ? this.#checkCrossEnvVariable(name, part, context) : this.#reporter.reportSyntax(context, part, "posix", true, "unsupportedSyntax", {
|
|
420
|
+
suggestion: context.crossEnv ? CROSS_ENV_VARIABLE_SUGGESTION : this.#variableSuggestion(name),
|
|
421
|
+
syntax: `The variable expansion \`${part.text}\``
|
|
422
|
+
});
|
|
423
|
+
const nestedContext = withCovered(context, covered);
|
|
424
|
+
this.#checkBashVariable(name, part, nestedContext);
|
|
425
|
+
this.#checkPackageManagerVariable(name, part, nestedContext);
|
|
426
|
+
break;
|
|
427
|
+
}
|
|
428
|
+
case "SingleQuoted": {
|
|
429
|
+
state.hasSingleQuotes = true;
|
|
430
|
+
const suggestion = "Use escaped double quotes (`\\\"`) instead.";
|
|
431
|
+
const syntax = `The single-quoted string \`${snippet(context, part)}\``;
|
|
432
|
+
const fix = state.command === "echo" || DOUBLE_QUOTED_SPECIAL_CHARACTERS.test(part.value) || WINDOWS_VARIABLE.test(part.value) ? void 0 : {
|
|
433
|
+
description: "Use escaped double quotes instead.",
|
|
434
|
+
edits: [{
|
|
435
|
+
end: part.end,
|
|
436
|
+
pos: part.pos,
|
|
437
|
+
text: `"${part.value}"`
|
|
438
|
+
}]
|
|
439
|
+
};
|
|
440
|
+
if (context.crossEnv === "script") {
|
|
441
|
+
if (SHELL_SPECIAL_CHARACTERS.test(part.value) || !state.printedToTerminal && WHITESPACE.test(part.value)) this.#reporter.report(context, part, "inconsistentBehavior", {
|
|
442
|
+
reason: "`cross-env-shell` removes single quotes before the shell runs the script, so the string isn't quoted",
|
|
443
|
+
suggestion,
|
|
444
|
+
syntax
|
|
445
|
+
}, PLATFORMS, fix);
|
|
446
|
+
break;
|
|
447
|
+
}
|
|
448
|
+
if (state.printedToTerminal && !CMD_OPERATORS.test(part.value) || context.crossEnv && !CMD_SPECIAL_CHARACTERS.test(part.value)) break;
|
|
449
|
+
this.#reporter.reportSyntax(context, part, "posix", true, "inconsistentBehavior", {
|
|
450
|
+
reason: state.printedToTerminal ? "cmd.exe doesn't treat single quotes as quotes, so it runs the operators in the string" : context.crossEnv ? "cmd.exe doesn't treat single quotes as quotes, so it splits the string before `cross-env` removes them" : "cmd.exe doesn't treat single quotes as quotes, so they're passed on literally",
|
|
451
|
+
suggestion,
|
|
452
|
+
syntax
|
|
453
|
+
}, fix);
|
|
454
|
+
break;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* Reports a brace expansion in the target of a redirection, e.g. in `> out.{log,txt}`.
|
|
460
|
+
* @param part The brace expansion
|
|
461
|
+
* @param context Where the redirection is
|
|
462
|
+
*/
|
|
463
|
+
#checkRedirectionBraces(part, context) {
|
|
464
|
+
if (!context.portable) this.#reporter.report(context, part, "inconsistentBehavior", {
|
|
465
|
+
reason: "Bash (the `sh` of macOS) expands it into several file names, which is an error",
|
|
466
|
+
suggestion: "Use a single file name instead.",
|
|
467
|
+
syntax: `The brace expansion \`${snippet(context, part)}\` in a redirection`
|
|
468
|
+
}, context.shell === "bash" ? PLATFORMS : MACOS_PLATFORMS);
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* Reports device paths like `/dev/null`, which don't exist on Windows.
|
|
472
|
+
* @param word Word that contains the path
|
|
473
|
+
* @param value Static value of the word
|
|
474
|
+
* @param context Where the word is
|
|
475
|
+
* @param fix A fix for the report, e.g. one that removes the redirection to the path
|
|
476
|
+
*/
|
|
477
|
+
checkPosixPath(word, value, context, fix) {
|
|
478
|
+
if (POSIX_PATH.test(value)) this.#reporter.report(context, word, "unsupportedSyntax", {
|
|
479
|
+
suggestion: "Remove it or use a Node.js script instead.",
|
|
480
|
+
syntax: `The POSIX path \`${truncate(value)}\``
|
|
481
|
+
}, WINDOWS_PLATFORMS, fix);
|
|
482
|
+
}
|
|
483
|
+
checkWord(word, context, options = {}) {
|
|
484
|
+
const { text } = word;
|
|
485
|
+
const shouldCheckText = !options.nestedScript;
|
|
486
|
+
if (shouldCheckText) this.#checkCarriageReturn(word, context);
|
|
487
|
+
if (shouldCheckText && TILDE_EXPANSION.test(text)) this.#reporter.reportSyntax(context, word, "posix", false, "unsupportedSyntax", {
|
|
488
|
+
suggestion: "Use a path relative to the package or a Node.js script instead.",
|
|
489
|
+
syntax: `The tilde expansion \`${snippet(context, word)}\``
|
|
490
|
+
});
|
|
491
|
+
const state = {
|
|
492
|
+
braceExpansions: [],
|
|
493
|
+
command: options.command,
|
|
494
|
+
globPosition: void 0,
|
|
495
|
+
hasSingleQuotes: false,
|
|
496
|
+
printedToTerminal: options.printedToTerminal || false,
|
|
497
|
+
skipGlobs: options.skipGlobs || false
|
|
498
|
+
};
|
|
499
|
+
if (word.parts) this.#checkParts(word.parts, context, state);
|
|
500
|
+
const { globPosition } = state;
|
|
501
|
+
const hasGlob = globPosition != null;
|
|
502
|
+
const globPrefix = hasGlob ? text.slice(0, globPosition - word.pos) : "";
|
|
503
|
+
const isLiteralPattern = globPrefix.startsWith("-") || globPrefix.includes("=");
|
|
504
|
+
if (shouldCheckText && hasGlob && !isLiteralPattern && RECURSIVE_GLOB.test(text) && !context.portable && !context.bashOptions?.has("globstar") && !options.commandName && !options.redirectTarget && !options.skipGlobs) this.#reporter.report(context, word, "inconsistentBehavior", {
|
|
505
|
+
reason: "`sh` doesn't support recursive `**` globs, and cmd.exe doesn't expand globs at all",
|
|
506
|
+
suggestion: "Quote it with escaped double quotes (`\\\"`) and let the tool expand it instead.",
|
|
507
|
+
syntax: `The unquoted glob \`${snippet(context, word)}\``
|
|
508
|
+
}, PLATFORMS, isTool(options.command) && !setHas(NON_GLOB_PROGRAMS, options.command) && isPlainWord(word, GLOB_PART_TYPES) ? quoting(word, "Quote it with escaped double quotes.") : void 0);
|
|
509
|
+
if (!options.skipGlobs && (options.command == null || OS_COMMANDS.has(options.command)) && (!hasGlob || options.command != null && !context.portable)) state.braceExpansions.forEach((part) => {
|
|
510
|
+
if (options.redirectTarget) {
|
|
511
|
+
this.#checkRedirectionBraces(part, context);
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
const suggestion = "List the alternatives explicitly instead.";
|
|
515
|
+
const fix = options.declaredValue ? void 0 : braceExpansionFix(word, part);
|
|
516
|
+
if (context.portable) this.#reporter.report(context, part, "inconsistentBehavior", {
|
|
517
|
+
reason: "Yarn's shell only expands it to the names of files that exist",
|
|
518
|
+
suggestion,
|
|
519
|
+
syntax: `The brace expansion \`${snippet(context, part)}\``
|
|
520
|
+
}, PLATFORMS, fix);
|
|
521
|
+
else this.#reporter.reportSyntax(context, part, "bash", true, "unsupportedSyntax", {
|
|
522
|
+
suggestion,
|
|
523
|
+
syntax: `The Bash-specific brace expansion \`${snippet(context, part)}\``
|
|
524
|
+
}, fix);
|
|
525
|
+
});
|
|
526
|
+
const hasBackslash = text.includes("\\");
|
|
527
|
+
const path = hasBackslash ? substringAfterLast(text.replaceAll("\"", ""), "=") : "";
|
|
528
|
+
const isBackslashPath = hasBackslash && BACKSLASH_PATH.test(path);
|
|
529
|
+
const hasPrefix = isBackslashPath && WINDOWS_PATH_PREFIX.test(path);
|
|
530
|
+
const hasExtension = isBackslashPath && FILE_EXTENSION.test(path);
|
|
531
|
+
if (isBackslashPath && shouldCheckText && !options.text && !setHas(PATTERN_COMMANDS, options.command) && !state.hasSingleQuotes && (hasPrefix || hasExtension || MULTIPLE_BACKSLASHES.test(path)) && NON_DOT_ESCAPE.test(path)) {
|
|
532
|
+
const isLikelyRegex = !hasPrefix && !hasExtension && !NON_REGEX_ESCAPE.test(path);
|
|
533
|
+
this.#reporter.report(context, word, "inconsistentBehavior", {
|
|
534
|
+
reason: "POSIX shells treat backslashes as escape characters",
|
|
535
|
+
suggestion: "Use forward slashes instead.",
|
|
536
|
+
syntax: `The backslash path separator in \`${truncate(path)}\``
|
|
537
|
+
}, context.portable && !text.startsWith("\"") ? PLATFORMS : POSIX_PLATFORMS, isLikelyRegex || options.commandName || options.redirectTarget || !isTool(options.command) ? void 0 : backslashPathFix(word, originalText(context, word), path === text && RECURSIVE_GLOB.test(toForwardSlashes(path)) && !context.portable && !context.bashOptions?.has("globstar")));
|
|
538
|
+
}
|
|
539
|
+
if (!(isBackslashPath || hasBackslash && WINDOWS_PATH_PREFIX.test(path)) && (hasBackslash || text.includes("^"))) this.#checkEscapes(word, context, shouldCheckText);
|
|
540
|
+
if (shouldCheckText && !state.hasSingleQuotes && options.command !== "date" && options.command !== "printf") {
|
|
541
|
+
const unencodedText = text.replaceAll(PERCENT_ENCODED_BYTES, "");
|
|
542
|
+
const match = WINDOWS_VARIABLE.exec(unencodedText);
|
|
543
|
+
if (match && !unencodedText.replaceAll(WINDOWS_VARIABLES, "").includes("%")) {
|
|
544
|
+
const covered = this.#reporter.report(context, word, "unsupportedSyntax", {
|
|
545
|
+
suggestion: VARIABLE_SUGGESTION,
|
|
546
|
+
syntax: `The Windows variable reference \`${match[0]}\``
|
|
547
|
+
}, context.portable ? PLATFORMS : POSIX_PLATFORMS);
|
|
548
|
+
const nestedContext = withCovered(context, covered);
|
|
549
|
+
unencodedText.matchAll(WINDOWS_VARIABLES).forEach(([reference]) => {
|
|
550
|
+
this.#checkPackageManagerVariable(reference.slice(1, -1), word, nestedContext, reference);
|
|
551
|
+
});
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
const value = staticValue(word);
|
|
555
|
+
if (shouldCheckText && value != null && !options.commandName && !options.redirectTarget) this.checkPosixPath(word, value, context);
|
|
556
|
+
}
|
|
557
|
+
/** Checks whether Windows doesn't set a variable, so `cross-env` replaces it with nothing */
|
|
558
|
+
#isUnsetOnWindows(name) {
|
|
559
|
+
return POSIX_ONLY_VARIABLES.has(name) || name === "HOME" && !this.#runnerTraits.get("setsHome");
|
|
560
|
+
}
|
|
561
|
+
/** Checks whether a variable is one that package managers set, but the runner doesn't */
|
|
562
|
+
#isMissingPackageManagerVariable(name) {
|
|
563
|
+
if (!PACKAGE_MANAGER_VARIABLE.test(name)) return false;
|
|
564
|
+
const variables = this.#runnerTraits.get("packageManagerVariables");
|
|
565
|
+
return variables != null && !variables.has(name);
|
|
566
|
+
}
|
|
567
|
+
/** Gets what to use instead of a variable on Windows, where `cross-env` can't help for some */
|
|
568
|
+
#variableSuggestion(name) {
|
|
569
|
+
if (name === "PWD") return "Use a relative path or a Node.js script instead.";
|
|
570
|
+
return this.#isUnsetOnWindows(name) || this.#isMissingPackageManagerVariable(name) ? NODE_SCRIPT_SUGGESTION : VARIABLE_SUGGESTION;
|
|
571
|
+
}
|
|
572
|
+
};
|
|
573
|
+
export { CMD_STATEMENTS, WordChecker };
|
package/dist/words.mjs
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// src/words.ts
|
|
2
|
+
/** A `NAME=value` word, e.g. an argument of `cross-env` */
|
|
3
|
+
const ASSIGNMENT = /^[a-z_]\w*=/i;
|
|
4
|
+
const SHORT_OPTION_CLUSTER = /^-[a-z]+$/i;
|
|
5
|
+
/**
|
|
6
|
+
* Checks whether a word is a cluster of short options that includes a letter, e.g. `-rf` for `f`.
|
|
7
|
+
* @param value Value of a word from the script
|
|
8
|
+
* @param letter Letter of the short option to look for
|
|
9
|
+
* @returns Whether the word includes the option
|
|
10
|
+
*/
|
|
11
|
+
const isShortOptionCluster = (value, letter) => value != null && SHORT_OPTION_CLUSTER.test(value) && value.includes(letter);
|
|
12
|
+
/**
|
|
13
|
+
* A tilde expansion at the start of a word, e.g. `~/.npmrc`.
|
|
14
|
+
* With a digit after `~`, it's a version range, e.g. `~1.2.3`, which shells keep as it is
|
|
15
|
+
*/
|
|
16
|
+
const TILDE_EXPANSION = /^~(?!\d)[\w\-.]*(?:\/|$)/;
|
|
17
|
+
/** Characters that POSIX shells interpret in double quotes, e.g. `$` */
|
|
18
|
+
const DOUBLE_QUOTED_SPECIAL_CHARACTERS = /["$\\`]/;
|
|
19
|
+
const WHITESPACE = /\s/;
|
|
20
|
+
const GLOB_PART_TYPES = [
|
|
21
|
+
"BraceExpansion",
|
|
22
|
+
"ExtendedGlob",
|
|
23
|
+
"Glob"
|
|
24
|
+
];
|
|
25
|
+
const isStatic = (parts) => parts.every((part) => part.type === "Literal" || part.type === "SingleQuoted" || part.type === "AnsiCQuoted" || part.type === "Glob" || part.type === "DoubleQuoted" && isStatic(part.parts));
|
|
26
|
+
/**
|
|
27
|
+
* Gets the text that a command receives for a word, e.g. a script for `sh -c`.
|
|
28
|
+
* The outer shell's expansions are replaced by placeholders of the same length.
|
|
29
|
+
* @param parts The word's parts
|
|
30
|
+
* @param shouldKeepExpansions Whether to keep the expansions' text instead, e.g. for reports
|
|
31
|
+
* @returns The text
|
|
32
|
+
*/
|
|
33
|
+
const nestedSource = (parts, shouldKeepExpansions = false) => parts.map((part) => {
|
|
34
|
+
switch (part.type) {
|
|
35
|
+
case "AnsiCQuoted":
|
|
36
|
+
case "Glob":
|
|
37
|
+
case "Literal":
|
|
38
|
+
case "SingleQuoted": return part.value;
|
|
39
|
+
case "DoubleQuoted":
|
|
40
|
+
case "LocaleString": return nestedSource(part.parts, shouldKeepExpansions);
|
|
41
|
+
default: return shouldKeepExpansions ? part.text : "".repeat(part.text.length);
|
|
42
|
+
}
|
|
43
|
+
}).join("");
|
|
44
|
+
/**
|
|
45
|
+
* Gets the value of a word, if it doesn't depend on expansions.
|
|
46
|
+
* @param word A word from the parsed script
|
|
47
|
+
* @returns The value, or `undefined` if it's dynamic
|
|
48
|
+
*/
|
|
49
|
+
const staticValue = (word) => (word.parts == null || isStatic(word.parts)) && !word.value.includes("") ? word.value : void 0;
|
|
50
|
+
/**
|
|
51
|
+
* Checks whether a word is unquoted text without escapes or expansions, e.g. `dist/*.js`.
|
|
52
|
+
* Double quotes don't change the value of such a word.
|
|
53
|
+
* @param word A word from the parsed script
|
|
54
|
+
* @param partTypes Types of parts that are allowed besides literal text, e.g. globs
|
|
55
|
+
* @returns Whether the word is plain
|
|
56
|
+
*/
|
|
57
|
+
const isPlainWord = (word, partTypes = []) => !DOUBLE_QUOTED_SPECIAL_CHARACTERS.test(word.text) && !word.text.includes("") && !TILDE_EXPANSION.test(word.text) && (word.parts || []).every((part) => part.type === "Literal" || partTypes.includes(part.type));
|
|
58
|
+
const isPlainAssignment = (item) => item.type === "Assignment" && item.value.type === "Word" && item.index == null && !item.append;
|
|
59
|
+
/** A word of letters, digits and underscores, e.g. a variable name */
|
|
60
|
+
const IDENTIFIER = /\w+/g;
|
|
61
|
+
/**
|
|
62
|
+
* Checks whether a piece of a script mentions any of some variables, e.g. `$PORT` for `PORT`.
|
|
63
|
+
* @param text The piece of the script
|
|
64
|
+
* @param names Names of the variables
|
|
65
|
+
* @returns Whether it does, even if only as a word that the shell doesn't expand
|
|
66
|
+
*/
|
|
67
|
+
const doesMentionVariables = (text, names) => (text.match(IDENTIFIER) || []).some((word) => names.has(word));
|
|
68
|
+
/** Device paths, e.g. `/dev/null` */
|
|
69
|
+
const POSIX_PATH = /(?:^|=)\/dev\//;
|
|
70
|
+
export { ASSIGNMENT, DOUBLE_QUOTED_SPECIAL_CHARACTERS, GLOB_PART_TYPES, POSIX_PATH, TILDE_EXPANSION, WHITESPACE, doesMentionVariables, isPlainAssignment, isPlainWord, isShortOptionCluster, nestedSource, staticValue };
|