package-json-scripts-portability 0.0.1 → 0.0.2
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 +1 -1
- package/dist/analyze-script.d.mts +34 -0
- package/dist/analyze-script.mjs +48 -0
- package/dist/command-checker.mjs +295 -0
- package/dist/command-usage.mjs +364 -0
- package/dist/commands.mjs +520 -0
- package/dist/context.mjs +18 -0
- package/dist/fixes.d.mts +9 -0
- package/dist/fixes.mjs +210 -0
- package/dist/index.d.mts +6 -118
- package/dist/index.mjs +4 -3787
- package/dist/line-break-checker.mjs +71 -0
- package/dist/messages.mjs +30 -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/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/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-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/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/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/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 +27 -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/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/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 +151 -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/remeda/dist/isArray.mjs +18 -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/platforms.d.mts +8 -0
- package/dist/platforms.mjs +26 -0
- package/dist/redirection-checker.mjs +117 -0
- package/dist/reporter.d.mts +32 -0
- package/dist/reporter.mjs +98 -0
- package/dist/runners.d.mts +24 -0
- package/dist/runners.mjs +118 -0
- package/dist/script-analyzer.mjs +551 -0
- package/dist/word-checker.mjs +444 -0
- package/dist/words.mjs +43 -0
- package/package.json +22 -20
|
@@ -0,0 +1,551 @@
|
|
|
1
|
+
import { CommandChecker } from "./command-checker.mjs";
|
|
2
|
+
import { SHELLS, SHELL_STATE_COMMANDS } from "./commands.mjs";
|
|
3
|
+
import { bashLabel, rangeInScript, withCovered } from "./context.mjs";
|
|
4
|
+
import { lineBreakFix, removal, semicolonFix } from "./fixes.mjs";
|
|
5
|
+
import { LineBreakChecker } from "./line-break-checker.mjs";
|
|
6
|
+
import { BASH_4_SUGGESTION, BASH_SUGGESTION, NODE_SCRIPT_SUGGESTION, describeReplacement, truncate } from "./messages.mjs";
|
|
7
|
+
import { PLATFORMS, POSIX_PLATFORMS, WINDOWS_PLATFORMS } from "./platforms.mjs";
|
|
8
|
+
import { RedirectionChecker } from "./redirection-checker.mjs";
|
|
9
|
+
import { Reporter } from "./reporter.mjs";
|
|
10
|
+
import { runnerTraits } from "./runners.mjs";
|
|
11
|
+
import { arrayIsSubset } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-is-subset.mjs";
|
|
12
|
+
import { mapGetOrInsertComputed } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/map/map-get-or-insert-computed.mjs";
|
|
13
|
+
import { substringBefore } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/string/substring-before.mjs";
|
|
14
|
+
import { WordChecker } from "./word-checker.mjs";
|
|
15
|
+
import { DOUBLE_QUOTED_SPECIAL_CHARACTERS, WHITESPACE } from "./words.mjs";
|
|
16
|
+
import { parse } from "unbash";
|
|
17
|
+
// src/script-analyzer.ts
|
|
18
|
+
/** A `#` comment after a word boundary */
|
|
19
|
+
const COMMENT = /(?:^|[\s&;|])(#.*)/;
|
|
20
|
+
/** A glob of script names that `concurrently` expands, e.g. `npm run watch:*(!css)` */
|
|
21
|
+
const SCRIPT_GLOB = /((?:bun|npm|pnpm|yarn) run|node --run|deno task) (\S*\*\S*)/;
|
|
22
|
+
/** `shopt -s globstar`, which makes `**` recursive in the following commands */
|
|
23
|
+
const SHOPT_GLOBSTAR = /\bshopt\s+-s\s+(?:\w+\s+)*globstar\b/;
|
|
24
|
+
/** The rest of a script that only closes subshells */ const CLOSING_PARENTHESES = /^[\s)]*$/;
|
|
25
|
+
const WINDOWS_BATCH_SYNTAX = /\bif\s+(?:\/i\s+)?(?:not\s+)?(?:defined|errorlevel|exist)\b|\bfor\s+(?:\/[a-z]\s+)*%%?[a-z]\b/gi;
|
|
26
|
+
const SINGLE_QUOTE = /'/;
|
|
27
|
+
const ANY_CHARACTER = /[\s\S]/;
|
|
28
|
+
const doesChangeDirectory = (node) => {
|
|
29
|
+
switch (node.type) {
|
|
30
|
+
case "AndOr":
|
|
31
|
+
case "Pipeline": return node.commands.some(doesChangeDirectory);
|
|
32
|
+
case "Command": return node.name?.value === "cd" || node.name?.value === "pushd";
|
|
33
|
+
default: return false;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Checks whether commands change the shell's state, e.g. with `exit` or a variable assignment.
|
|
38
|
+
* @param node Commands of a command group
|
|
39
|
+
* @returns Whether they do, or might, e.g. in an `if` statement
|
|
40
|
+
*/
|
|
41
|
+
const doesChangeShellState = (node) => {
|
|
42
|
+
switch (node.type) {
|
|
43
|
+
case "AndOr":
|
|
44
|
+
case "Pipeline": return node.commands.some(doesChangeShellState);
|
|
45
|
+
case "BraceGroup": return node.body.commands.some((statement) => doesChangeShellState(statement.command));
|
|
46
|
+
case "Command": return node.name ? SHELL_STATE_COMMANDS.has(node.name.value) : node.prefix.some((item) => item.type === "Assignment");
|
|
47
|
+
case "Redirected": return doesChangeShellState(node.command);
|
|
48
|
+
case "Subshell": return false;
|
|
49
|
+
default: return true;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Gets the text that a command receives for a word, e.g. a script for `sh -c`.
|
|
54
|
+
* The outer shell's expansions are replaced by placeholders of the same length.
|
|
55
|
+
* Their results aren't known, and they're reported for the outer script already.
|
|
56
|
+
* @param parts The word's parts
|
|
57
|
+
* @returns The text, with placeholders
|
|
58
|
+
*/
|
|
59
|
+
const nestedSource = (parts) => parts.map((part) => {
|
|
60
|
+
switch (part.type) {
|
|
61
|
+
case "AnsiCQuoted":
|
|
62
|
+
case "Glob":
|
|
63
|
+
case "Literal":
|
|
64
|
+
case "SingleQuoted": return part.value;
|
|
65
|
+
case "DoubleQuoted":
|
|
66
|
+
case "LocaleString": return nestedSource(part.parts);
|
|
67
|
+
default: return "_".repeat(part.text.length);
|
|
68
|
+
}
|
|
69
|
+
}).join("");
|
|
70
|
+
/**
|
|
71
|
+
* Finds the end of a run of `||` alternatives that each break on some platform, but together work
|
|
72
|
+
* on all platforms, e.g. `open x || xdg-open x || start x`.
|
|
73
|
+
* @param node The list that the alternatives are in
|
|
74
|
+
* @param platformsOf Gets the platforms on which a command of the list doesn't work
|
|
75
|
+
* @param first Index of the run's first command
|
|
76
|
+
* @returns Index of the run's last command, if there's such a run
|
|
77
|
+
*/
|
|
78
|
+
const findFallbackEnd = (node, platformsOf, first) => {
|
|
79
|
+
let unsupported = platformsOf(first);
|
|
80
|
+
for (let last = first + 1; unsupported.size > 0 && last < node.commands.length && node.operators[last - 1] === "||"; last++) {
|
|
81
|
+
const platforms = platformsOf(last);
|
|
82
|
+
if (platforms.size === 0) return;
|
|
83
|
+
unsupported = unsupported.intersection(platforms);
|
|
84
|
+
if (unsupported.size === 0) return last;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
var ScriptAnalyzer = class {
|
|
88
|
+
#commands;
|
|
89
|
+
#lineBreaks;
|
|
90
|
+
/** Parsed sources of the script and of the scripts nested in it, e.g. in `sh -c "..."` */
|
|
91
|
+
#parsedScripts;
|
|
92
|
+
/** Commands whose output goes into a pipe */
|
|
93
|
+
#pipedCommands = /* @__PURE__ */ new Set();
|
|
94
|
+
#redirections;
|
|
95
|
+
#reporter;
|
|
96
|
+
#words;
|
|
97
|
+
constructor(length, options, traits, parsedScripts) {
|
|
98
|
+
this.#parsedScripts = parsedScripts;
|
|
99
|
+
this.#reporter = new Reporter(length);
|
|
100
|
+
this.#words = new WordChecker(this.#reporter, (script, part, context) => {
|
|
101
|
+
this.#walkNestedScript(script, part, context);
|
|
102
|
+
}, traits);
|
|
103
|
+
this.#lineBreaks = new LineBreakChecker(this.#reporter);
|
|
104
|
+
this.#redirections = new RedirectionChecker(this.#reporter, this.#words);
|
|
105
|
+
this.#commands = new CommandChecker(this.#reporter, options, (words, context, nested) => {
|
|
106
|
+
this.#analyzeNestedWords(words, context, nested);
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
get issues() {
|
|
110
|
+
return this.#reporter.issues;
|
|
111
|
+
}
|
|
112
|
+
analyzeSource(context) {
|
|
113
|
+
const script = mapGetOrInsertComputed(this.#parsedScripts, context.source, parse);
|
|
114
|
+
if (script.errors?.length) {
|
|
115
|
+
this.#checkWindowsBatchSyntax(context);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
this.#commands.addPositionalFixes(script.commands);
|
|
119
|
+
const scriptContext = context.shell === "bash" && SHOPT_GLOBSTAR.test(context.source) ? {
|
|
120
|
+
...context,
|
|
121
|
+
bashOptions: /* @__PURE__ */ new Set([...context.bashOptions || [], "globstar"])
|
|
122
|
+
} : context;
|
|
123
|
+
this.#walkStatements(script.commands, script.end, scriptContext);
|
|
124
|
+
this.#checkComments(script.commands, scriptContext);
|
|
125
|
+
}
|
|
126
|
+
#analyzeNestedWords(words, context, { bashOptions, crossEnv, maskScriptGlobs = false, shell }) {
|
|
127
|
+
const sources = words.map((word) => {
|
|
128
|
+
const source = word.parts ? nestedSource(word.parts) : word.value;
|
|
129
|
+
return maskScriptGlobs ? source.replace(SCRIPT_GLOB, (_, command, glob) => `${command} ${"_".repeat(glob.length)}`) : source;
|
|
130
|
+
});
|
|
131
|
+
const [word] = words;
|
|
132
|
+
if (!word) return;
|
|
133
|
+
const parts = word.parts || [];
|
|
134
|
+
const [part] = parts;
|
|
135
|
+
let { fixedRange } = context;
|
|
136
|
+
let offset = context.offset + word.pos;
|
|
137
|
+
let exclusion = ANY_CHARACTER;
|
|
138
|
+
if (words.length === 1 && parts.length === 1 && (part?.type === "SingleQuoted" || part?.type === "DoubleQuoted" && part.parts.every((child) => child.type !== "Literal" || child.value === child.text))) {
|
|
139
|
+
offset = context.offset + part.pos + 1;
|
|
140
|
+
exclusion = part.type === "SingleQuoted" ? SINGLE_QUOTE : DOUBLE_QUOTED_SPECIAL_CHARACTERS;
|
|
141
|
+
} else if (words.length > 1 || parts.length > 0 || word.text !== sources[0]) fixedRange ||= rangeInScript(context, {
|
|
142
|
+
end: words.at(-1)?.end || word.end,
|
|
143
|
+
pos: word.pos
|
|
144
|
+
});
|
|
145
|
+
this.analyzeSource({
|
|
146
|
+
bashOptions,
|
|
147
|
+
convertsSlashes: false,
|
|
148
|
+
covered: context.covered,
|
|
149
|
+
crossEnv,
|
|
150
|
+
fixExclusions: [...context.fixExclusions, exclusion],
|
|
151
|
+
fixedRange,
|
|
152
|
+
offset,
|
|
153
|
+
portable: false,
|
|
154
|
+
shell,
|
|
155
|
+
source: sources.join(" ")
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
#checkComments(statements, context) {
|
|
159
|
+
let position = 0;
|
|
160
|
+
[...statements, void 0].forEach((statement, index) => {
|
|
161
|
+
const end = statement?.pos ?? context.source.length;
|
|
162
|
+
const match = COMMENT.exec(context.source.slice(position, end));
|
|
163
|
+
const comment = match?.[1];
|
|
164
|
+
if (match && comment != null) {
|
|
165
|
+
const start = position + match.index + match[0].length - comment.length;
|
|
166
|
+
const commentEnd = start + comment.length;
|
|
167
|
+
this.#reporter.reportSyntax(context, {
|
|
168
|
+
end: commentEnd,
|
|
169
|
+
pos: start
|
|
170
|
+
}, "posix", false, "unsupportedSyntax", {
|
|
171
|
+
suggestion: "Remove it instead.",
|
|
172
|
+
syntax: "The `#` comment"
|
|
173
|
+
}, {
|
|
174
|
+
description: "Remove the comment.",
|
|
175
|
+
edits: [removal(context.source, start, commentEnd)]
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
position = statement ? this.#statementEnd(statement, statements.at(index + 1)?.pos || context.source.length, context.source) : end;
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Gets where the text after a statement starts, after the bodies of its here-documents.
|
|
183
|
+
* @param statement A statement of the source
|
|
184
|
+
* @param nextStart Where the next statement starts, or where the source ends
|
|
185
|
+
* @param source The source that the statement is in
|
|
186
|
+
* @returns The offset in the source
|
|
187
|
+
*/
|
|
188
|
+
#statementEnd(statement, nextStart, source) {
|
|
189
|
+
const hereDocumentEnds = (this.#redirections.hereDocumentEnds.get(source) || []).filter((end) => end <= nextStart);
|
|
190
|
+
return Math.max(statement.end, ...hereDocumentEnds);
|
|
191
|
+
}
|
|
192
|
+
#checkWindowsBatchSyntax(context) {
|
|
193
|
+
context.source.matchAll(WINDOWS_BATCH_SYNTAX).forEach((match) => {
|
|
194
|
+
this.#reporter.report(context, {
|
|
195
|
+
end: match.index + match[0].length,
|
|
196
|
+
pos: match.index
|
|
197
|
+
}, "unsupportedSyntax", {
|
|
198
|
+
suggestion: NODE_SCRIPT_SUGGESTION,
|
|
199
|
+
syntax: `The Windows batch syntax \`${truncate(match[0])}\``
|
|
200
|
+
}, context.portable ? PLATFORMS : POSIX_PLATFORMS);
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Drops the issues of alternatives for different platforms, e.g. `rm -rf a || rmdir /s /q a`.
|
|
205
|
+
* @param node The list of commands joined by `||` (and other operators)
|
|
206
|
+
* @param starts Index of the first issue of each command in the list, and of the end of its
|
|
207
|
+
* issues
|
|
208
|
+
*/
|
|
209
|
+
#dropPlatformFallbacks(node, starts) {
|
|
210
|
+
const platformsOf = (index) => new Set(this.#reporter.issues.slice(starts[index], starts[index + 1]).flatMap((issue) => issue.platforms));
|
|
211
|
+
const dropped = [];
|
|
212
|
+
let first = 0;
|
|
213
|
+
while (first < node.commands.length) {
|
|
214
|
+
const last = findFallbackEnd(node, platformsOf, first);
|
|
215
|
+
if (last == null) first++;
|
|
216
|
+
else {
|
|
217
|
+
dropped.push([first, last]);
|
|
218
|
+
first = last + 1;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
dropped.toReversed().forEach(([runStart, runEnd]) => {
|
|
222
|
+
const start = starts[runStart];
|
|
223
|
+
const end = starts[runEnd + 1];
|
|
224
|
+
if (start != null && end != null) this.#reporter.issues.splice(start, end - start);
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
#walkCommand(node, context) {
|
|
228
|
+
switch (node.type) {
|
|
229
|
+
case "AndOr": {
|
|
230
|
+
const starts = [];
|
|
231
|
+
node.commands.forEach((command) => {
|
|
232
|
+
starts.push(this.#reporter.issues.length);
|
|
233
|
+
this.#walkCommand(command, context);
|
|
234
|
+
});
|
|
235
|
+
starts.push(this.#reporter.issues.length);
|
|
236
|
+
this.#dropPlatformFallbacks(node, starts);
|
|
237
|
+
this.#lineBreaks.checkOperatorLineBreaks(node.commands, context);
|
|
238
|
+
break;
|
|
239
|
+
}
|
|
240
|
+
case "ArithmeticCommand":
|
|
241
|
+
this.#reporter.reportSyntax(context, node, "bash", false, "unsupportedSyntax", {
|
|
242
|
+
suggestion: BASH_SUGGESTION,
|
|
243
|
+
syntax: "The Bash-specific `(( ... ))` arithmetic command"
|
|
244
|
+
});
|
|
245
|
+
break;
|
|
246
|
+
case "ArithmeticFor":
|
|
247
|
+
this.#walkList(node.body, this.#reportStatement(context, node, "for (( ... ))", "bash"));
|
|
248
|
+
break;
|
|
249
|
+
case "BraceGroup": {
|
|
250
|
+
const isSubshellSafe = context.source[node.end - 1] === "}" && node.body.commands.every((statement) => !doesChangeShellState(statement.command));
|
|
251
|
+
const covered = this.#reporter.reportSyntax(context, {
|
|
252
|
+
end: node.pos + 1,
|
|
253
|
+
pos: node.pos
|
|
254
|
+
}, "posix", true, "unsupportedSyntax", {
|
|
255
|
+
suggestion: "Use `( ... )` instead.",
|
|
256
|
+
syntax: "The command group `{ ... }`"
|
|
257
|
+
}, isSubshellSafe ? {
|
|
258
|
+
description: "Replace `{ ... }` with `( ... )`.",
|
|
259
|
+
edits: [{
|
|
260
|
+
end: node.pos + 1,
|
|
261
|
+
pos: node.pos,
|
|
262
|
+
text: "("
|
|
263
|
+
}, {
|
|
264
|
+
end: node.end,
|
|
265
|
+
pos: node.end - 1,
|
|
266
|
+
text: ")"
|
|
267
|
+
}]
|
|
268
|
+
} : void 0);
|
|
269
|
+
this.#walkList(node.body, withCovered(context, covered));
|
|
270
|
+
break;
|
|
271
|
+
}
|
|
272
|
+
case "Case": {
|
|
273
|
+
const nestedContext = this.#reportStatement(context, node, "case", "posix");
|
|
274
|
+
this.#words.checkWord(node.word, nestedContext);
|
|
275
|
+
node.items.forEach(({ body, end, terminator }) => {
|
|
276
|
+
this.#walkList(body, nestedContext);
|
|
277
|
+
if (terminator === ";&" || terminator === ";;&") this.#reporter.reportSyntax(nestedContext, {
|
|
278
|
+
end,
|
|
279
|
+
pos: end - terminator.length
|
|
280
|
+
}, "bash4", false, "unsupportedSyntax", {
|
|
281
|
+
suggestion: BASH_4_SUGGESTION,
|
|
282
|
+
syntax: `The Bash 4 \`${terminator}\` case terminator`
|
|
283
|
+
});
|
|
284
|
+
});
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
287
|
+
case "Command":
|
|
288
|
+
this.#walkSimpleCommand(node, context);
|
|
289
|
+
break;
|
|
290
|
+
case "Coproc": {
|
|
291
|
+
const nestedContext = this.#reportStatement(context, node, "coproc", "bash4");
|
|
292
|
+
if (node.body.type === "CompoundList") this.#walkList(node.body, nestedContext);
|
|
293
|
+
else this.#walkCommand(node.body, nestedContext);
|
|
294
|
+
break;
|
|
295
|
+
}
|
|
296
|
+
case "For": {
|
|
297
|
+
const nestedContext = this.#reportStatement(context, node, "for", "posix");
|
|
298
|
+
node.wordlist?.forEach((word) => {
|
|
299
|
+
this.#words.checkWord(word, nestedContext);
|
|
300
|
+
});
|
|
301
|
+
this.#walkList(node.body, nestedContext);
|
|
302
|
+
break;
|
|
303
|
+
}
|
|
304
|
+
case "Function": {
|
|
305
|
+
const hasFunctionKeyword = context.source.startsWith("function", node.pos);
|
|
306
|
+
const covered = this.#reporter.reportSyntax(context, {
|
|
307
|
+
end: node.name.end,
|
|
308
|
+
pos: node.pos
|
|
309
|
+
}, hasFunctionKeyword ? "bash" : "posix", false, "unsupportedSyntax", hasFunctionKeyword ? {
|
|
310
|
+
suggestion: "Use `name() { ... }` or a Node.js script instead.",
|
|
311
|
+
syntax: "The Bash-specific `function` keyword"
|
|
312
|
+
} : {
|
|
313
|
+
suggestion: NODE_SCRIPT_SUGGESTION,
|
|
314
|
+
syntax: `The shell function definition \`${node.name.text}()\``
|
|
315
|
+
});
|
|
316
|
+
const nestedContext = withCovered(context, covered);
|
|
317
|
+
if (node.body.type === "CompoundList") this.#walkList(node.body, nestedContext);
|
|
318
|
+
else this.#walkCommand(node.body, nestedContext);
|
|
319
|
+
break;
|
|
320
|
+
}
|
|
321
|
+
case "If": {
|
|
322
|
+
const nestedContext = this.#reportStatement(context, node, "if", "posix");
|
|
323
|
+
this.#walkList(node.clause, nestedContext);
|
|
324
|
+
this.#walkList(node.then, nestedContext);
|
|
325
|
+
node.elif.forEach((elif) => {
|
|
326
|
+
this.#walkList(elif.clause, nestedContext);
|
|
327
|
+
this.#walkList(elif.then, nestedContext);
|
|
328
|
+
});
|
|
329
|
+
if (node.else) this.#walkList(node.else, nestedContext);
|
|
330
|
+
break;
|
|
331
|
+
}
|
|
332
|
+
case "Pipeline":
|
|
333
|
+
this.#walkPipeline(node, context);
|
|
334
|
+
break;
|
|
335
|
+
case "Redirected":
|
|
336
|
+
this.#walkCommand(node.command, context);
|
|
337
|
+
node.redirects.forEach((redirection) => {
|
|
338
|
+
this.#redirections.checkRedirection(redirection, context);
|
|
339
|
+
});
|
|
340
|
+
break;
|
|
341
|
+
case "Select": {
|
|
342
|
+
const nestedContext = this.#reportStatement(context, node, "select", "bash");
|
|
343
|
+
node.wordlist?.forEach((word) => {
|
|
344
|
+
this.#words.checkWord(word, nestedContext);
|
|
345
|
+
});
|
|
346
|
+
this.#walkList(node.body, nestedContext);
|
|
347
|
+
break;
|
|
348
|
+
}
|
|
349
|
+
case "Subshell":
|
|
350
|
+
if (!context.portable && node.body.commands.some((statement) => doesChangeDirectory(statement.command)) && !CLOSING_PARENTHESES.test(context.source.slice(node.end))) this.#reporter.reportSyntax(context, node, "posix", true, "inconsistentBehavior", {
|
|
351
|
+
reason: "cmd.exe only groups the commands, so `cd` also changes the directory of the commands that follow",
|
|
352
|
+
suggestion: "Use `npm --prefix <directory>` or workspaces instead.",
|
|
353
|
+
syntax: "The subshell `( ... )`"
|
|
354
|
+
});
|
|
355
|
+
this.#walkList(node.body, context);
|
|
356
|
+
break;
|
|
357
|
+
case "TestCommand": {
|
|
358
|
+
const covered = this.#reporter.reportSyntax(context, node, "bash", false, "unsupportedSyntax", {
|
|
359
|
+
suggestion: "Use `test` or a Node.js script instead.",
|
|
360
|
+
syntax: "The Bash-specific `[[ ... ]]` test"
|
|
361
|
+
});
|
|
362
|
+
this.#walkTestExpression(node.expression, withCovered(context, covered));
|
|
363
|
+
break;
|
|
364
|
+
}
|
|
365
|
+
case "While": {
|
|
366
|
+
const nestedContext = this.#reportStatement(context, node, node.kind, "posix");
|
|
367
|
+
this.#walkList(node.clause, nestedContext);
|
|
368
|
+
this.#walkList(node.body, nestedContext);
|
|
369
|
+
break;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
#walkList(list, context) {
|
|
374
|
+
this.#walkStatements(list.commands, list.end, context);
|
|
375
|
+
}
|
|
376
|
+
#walkNestedScript(script, part, context) {
|
|
377
|
+
const nestedContext = script.source == null ? context : {
|
|
378
|
+
...context,
|
|
379
|
+
fixedRange: rangeInScript(context, part),
|
|
380
|
+
source: script.source
|
|
381
|
+
};
|
|
382
|
+
this.#walkStatements(script.commands, script.end, nestedContext);
|
|
383
|
+
}
|
|
384
|
+
#walkPipeline(node, context) {
|
|
385
|
+
node.prefixes.forEach((prefix) => {
|
|
386
|
+
this.#reporter.reportSyntax(context, prefix, "posix", false, "unsupportedSyntax", prefix.type === "Negation" ? {
|
|
387
|
+
suggestion: NODE_SCRIPT_SUGGESTION,
|
|
388
|
+
syntax: "The pipeline negation `!`"
|
|
389
|
+
} : {
|
|
390
|
+
suggestion: "cmd.exe's `time` sets the system time; use a Node.js script instead.",
|
|
391
|
+
syntax: "The `time` keyword"
|
|
392
|
+
});
|
|
393
|
+
});
|
|
394
|
+
node.operators.forEach((operator, index) => {
|
|
395
|
+
if (operator !== "|&") return;
|
|
396
|
+
const position = context.source.indexOf("|&", node.commands[index]?.end);
|
|
397
|
+
const range = {
|
|
398
|
+
end: position + 2,
|
|
399
|
+
pos: position
|
|
400
|
+
};
|
|
401
|
+
const separator = WHITESPACE.test(context.source[position - 1] || "") ? "" : " ";
|
|
402
|
+
this.#reporter.reportSyntax(context, range, "bash4", true, "unsupportedSyntax", {
|
|
403
|
+
suggestion: "Use `2>&1 |` instead.",
|
|
404
|
+
syntax: "The Bash 4 `|&` operator"
|
|
405
|
+
}, {
|
|
406
|
+
description: describeReplacement("|&", "2>&1 |"),
|
|
407
|
+
edits: [{
|
|
408
|
+
...range,
|
|
409
|
+
text: `${separator}2>&1 |`
|
|
410
|
+
}]
|
|
411
|
+
});
|
|
412
|
+
});
|
|
413
|
+
node.commands.slice(0, -1).forEach((command) => {
|
|
414
|
+
this.#pipedCommands.add(command);
|
|
415
|
+
});
|
|
416
|
+
node.commands.forEach((command) => {
|
|
417
|
+
this.#walkCommand(command, context);
|
|
418
|
+
});
|
|
419
|
+
this.#lineBreaks.checkOperatorLineBreaks(node.commands, context);
|
|
420
|
+
}
|
|
421
|
+
#walkSimpleCommand(node, context) {
|
|
422
|
+
const isRedirection = node.name == null && node.prefix.length + node.suffix.length === 1;
|
|
423
|
+
node.prefix.forEach((item) => {
|
|
424
|
+
if (item.type === "Assignment") this.#words.checkAssignment(item, context, node.name ? node : void 0);
|
|
425
|
+
else this.#redirections.checkRedirection(item, context, isRedirection);
|
|
426
|
+
});
|
|
427
|
+
const invocation = node.name && this.#commands.checkInvocation(node.name, node.suffix.filter((item) => item.type === "Word"), context, 0);
|
|
428
|
+
let argumentContext = invocation?.crossEnv ? {
|
|
429
|
+
...context,
|
|
430
|
+
crossEnv: true
|
|
431
|
+
} : context;
|
|
432
|
+
if (invocation?.name != null && (SHELLS.includes(invocation.name) || invocation.name === "find")) argumentContext = withCovered(argumentContext, WINDOWS_PLATFORMS);
|
|
433
|
+
if (node.name) {
|
|
434
|
+
this.#words.checkWord(node.name, context, { commandName: true });
|
|
435
|
+
if (invocation?.name === "echo" && !context.portable) this.#redirections.checkEchoToFile(node, context);
|
|
436
|
+
}
|
|
437
|
+
this.#lineBreaks.checkCommandLineBreak(node, argumentContext);
|
|
438
|
+
const isPrintedToTerminal = invocation?.target === "echo" && node.redirects.length === 0 && !this.#pipedCommands.has(node);
|
|
439
|
+
node.suffix.forEach((item) => {
|
|
440
|
+
if (item.type === "Assignment") (item.value.type === "Word" ? [item.value] : item.value.elements).forEach((word) => {
|
|
441
|
+
this.#words.checkWord(word, argumentContext);
|
|
442
|
+
});
|
|
443
|
+
else if (item.type === "Word") this.#words.checkWord(item, argumentContext, {
|
|
444
|
+
command: invocation?.target,
|
|
445
|
+
printedToTerminal: isPrintedToTerminal
|
|
446
|
+
});
|
|
447
|
+
else this.#redirections.checkRedirection(item, context);
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
#reportStatement(context, node, keyword, kind) {
|
|
451
|
+
const firstWord = substringBefore(keyword, " ");
|
|
452
|
+
const covered = this.#reporter.reportSyntax(context, {
|
|
453
|
+
end: node.pos + firstWord.length,
|
|
454
|
+
pos: node.pos
|
|
455
|
+
}, kind, false, "unsupportedSyntax", kind === "posix" ? {
|
|
456
|
+
suggestion: NODE_SCRIPT_SUGGESTION,
|
|
457
|
+
syntax: `The shell \`${keyword}\` statement`
|
|
458
|
+
} : {
|
|
459
|
+
suggestion: kind === "bash4" ? BASH_4_SUGGESTION : BASH_SUGGESTION,
|
|
460
|
+
syntax: `The ${bashLabel(kind)} \`${keyword}\` statement`
|
|
461
|
+
});
|
|
462
|
+
return withCovered(context, covered);
|
|
463
|
+
}
|
|
464
|
+
#walkStatements(statements, end, context) {
|
|
465
|
+
const firstIssue = this.#reporter.issues.length;
|
|
466
|
+
const backgroundOperators = [];
|
|
467
|
+
statements.forEach((statement, index) => {
|
|
468
|
+
this.#walkCommand(statement.command, context);
|
|
469
|
+
if (statement.background) {
|
|
470
|
+
const position = context.source.lastIndexOf("&", statement.end - 1);
|
|
471
|
+
backgroundOperators.push({
|
|
472
|
+
end: position + 1,
|
|
473
|
+
pos: position
|
|
474
|
+
});
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
const next = statements.at(index + 1);
|
|
478
|
+
const gapEnd = next?.pos || end;
|
|
479
|
+
const gapStart = this.#statementEnd(statement, gapEnd, context.source);
|
|
480
|
+
const gap = context.source.slice(gapStart, gapEnd);
|
|
481
|
+
const comment = COMMENT.exec(gap);
|
|
482
|
+
const commentText = comment?.[1];
|
|
483
|
+
const commentStart = comment && commentText != null ? comment.index + comment[0].length - commentText.length : void 0;
|
|
484
|
+
const semicolon = gap.slice(0, commentStart).indexOf(";");
|
|
485
|
+
if (semicolon !== -1) {
|
|
486
|
+
const range = {
|
|
487
|
+
end: gapStart + semicolon + 1,
|
|
488
|
+
pos: gapStart + semicolon
|
|
489
|
+
};
|
|
490
|
+
this.#reporter.reportSyntax(context, range, "posix", true, "unsupportedSyntax", {
|
|
491
|
+
suggestion: "Use `&&` instead.",
|
|
492
|
+
syntax: "The `;` command separator"
|
|
493
|
+
}, semicolonFix(range, gap, next));
|
|
494
|
+
} else if (next && gap.includes("\n")) this.#lineBreaks.reportLineBreak(context, gapStart + gap.indexOf("\n"), "Join the commands with `&&` instead.", lineBreakFix(gapStart, gap, next));
|
|
495
|
+
});
|
|
496
|
+
if (!this.#reporter.issues.slice(firstIssue).some(({ platforms }) => platforms.length === POSIX_PLATFORMS.length && arrayIsSubset(platforms, POSIX_PLATFORMS))) backgroundOperators.forEach((range) => {
|
|
497
|
+
this.#reporter.reportSyntax(context, range, "posix", true, "inconsistentBehavior", {
|
|
498
|
+
reason: "cmd.exe runs the commands sequentially instead of in the background",
|
|
499
|
+
suggestion: "Use `concurrently` instead.",
|
|
500
|
+
syntax: "The `&` operator"
|
|
501
|
+
});
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
#walkTestExpression(expression, context) {
|
|
505
|
+
switch (expression.type) {
|
|
506
|
+
case "TestBinary":
|
|
507
|
+
this.#words.checkWord(expression.left, context, { skipGlobs: true });
|
|
508
|
+
this.#words.checkWord(expression.right, context, { skipGlobs: true });
|
|
509
|
+
break;
|
|
510
|
+
case "TestGroup":
|
|
511
|
+
this.#walkTestExpression(expression.expression, context);
|
|
512
|
+
break;
|
|
513
|
+
case "TestLogical":
|
|
514
|
+
expression.operands.forEach((operand) => {
|
|
515
|
+
this.#walkTestExpression(operand, context);
|
|
516
|
+
});
|
|
517
|
+
break;
|
|
518
|
+
case "TestNot":
|
|
519
|
+
this.#walkTestExpression(expression.operand, context);
|
|
520
|
+
break;
|
|
521
|
+
case "TestUnary": this.#words.checkWord(expression.operand, context, { skipGlobs: true });
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
};
|
|
525
|
+
/**
|
|
526
|
+
* Analyzes a script as each of the runners runs it.
|
|
527
|
+
* @param script The script's source
|
|
528
|
+
* @param options Analysis options
|
|
529
|
+
* @param runners What runs the script
|
|
530
|
+
* @returns The issues found with each runner
|
|
531
|
+
*/
|
|
532
|
+
const analyzeWithRunners = (script, options, runners) => {
|
|
533
|
+
const parsedScripts = /* @__PURE__ */ new Map();
|
|
534
|
+
return new Map(runners.map((runner) => {
|
|
535
|
+
const traits = runnerTraits(runner, options.shellEmulator || false);
|
|
536
|
+
const analyzer = new ScriptAnalyzer(script.length, options, traits, parsedScripts);
|
|
537
|
+
analyzer.analyzeSource({
|
|
538
|
+
convertsSlashes: traits.convertsSlashes || false,
|
|
539
|
+
covered: /* @__PURE__ */ new Set(),
|
|
540
|
+
crossEnv: false,
|
|
541
|
+
fixExclusions: [],
|
|
542
|
+
fixedRange: void 0,
|
|
543
|
+
offset: 0,
|
|
544
|
+
portable: traits.portableShell || false,
|
|
545
|
+
shell: "native",
|
|
546
|
+
source: script
|
|
547
|
+
});
|
|
548
|
+
return [runner, analyzer.issues];
|
|
549
|
+
}));
|
|
550
|
+
};
|
|
551
|
+
export { analyzeWithRunners };
|