@eslint-sh/eslint-plugin 0.12.0 → 0.13.0
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 +5 -2
- package/dist/api.d.ts +1 -1
- package/dist/command-harvest/harvest-commands.d.ts +5 -3
- package/dist/command-harvest/harvest-commands.js +34 -12
- package/dist/eslint-plugin-api.d.ts +25 -11
- package/dist/language/command-resolution.d.ts +16 -6
- package/dist/language/command-resolution.js +44 -17
- package/dist/language/shell-source-code.d.ts +18 -10
- package/dist/language/shell-source-code.js +24 -25
- package/dist/language/word-resolution.d.ts +18 -12
- package/dist/language/word-resolution.js +133 -3
- package/dist/rules/compat/bash-builtin-destination.d.ts +2 -0
- package/dist/rules/compat/bash-builtin-destination.js +4 -0
- package/dist/rules/compat/declaration-options.d.ts +2 -2
- package/dist/rules/compat/declaration-options.js +23 -17
- package/dist/rules/compat/no-case-attributes.js +2 -4
- package/dist/rules/compat/no-mapfile.js +16 -19
- package/dist/rules/compat/no-negative-array-index.d.ts +3 -8
- package/dist/rules/compat/no-negative-array-index.js +7 -30
- package/dist/rules/compat/no-nounset-empty-array.js +10 -7
- package/dist/rules/compat/no-parameter-transformation.js +2 -1
- package/dist/rules/compat/no-printf-time-format.js +38 -16
- package/dist/rules/compat/no-source.d.ts +3 -8
- package/dist/rules/compat/no-source.js +3 -40
- package/dist/rules/compat/no-test-v.js +243 -61
- package/dist/rules/compat/no-wait-n.js +43 -12
- package/dist/rules/compat/nounset-array-proof.d.ts +3 -1
- package/dist/rules/compat/nounset-array-proof.js +84 -63
- package/dist/rules/compat/posix-absent-command.js +3 -2
- package/dist/rules/compat/shopt-options.js +3 -2
- package/dist/rules/create-wrapper-rule.js +16 -9
- package/dist/rules/no-dynamic-invocation.js +8 -9
- package/dist/rules/no-eval.js +19 -8
- package/dist/rules/no-masked-exit-status.js +29 -17
- package/dist/rules/no-secret-flags.js +4 -3
- package/dist/rules/no-unchecked-cd.d.ts +8 -3
- package/dist/rules/no-unchecked-cd.js +54 -25
- package/dist/rules/no-unused-declarations.js +51 -19
- package/dist/rules/prefer-local.js +136 -16
- package/dist/rules/require-strict-mode.d.ts +2 -2
- package/dist/rules/require-strict-mode.js +27 -18
- package/dist/rules/require-tty-guard.d.ts +2 -0
- package/dist/rules/require-tty-guard.js +197 -45
- package/dist/rules/set-options.d.ts +3 -1
- package/dist/rules/set-options.js +5 -1
- package/dist/rules/strict-mode-prologue.d.ts +9 -2
- package/dist/rules/strict-mode-prologue.js +45 -8
- package/docs/custom-rule-tutorial.md +10 -7
- package/docs/error-codes.md +4 -0
- package/docs/examples/no-curl-in-functions-rule.mjs +7 -5
- package/docs/rules/README.md +4 -4
- package/docs/rules/compat/no-mapfile.md +11 -20
- package/docs/rules/compat/no-negative-array-index.md +9 -22
- package/docs/rules/compat/no-nounset-empty-array.md +5 -5
- package/docs/rules/compat/no-printf-time-format.md +2 -1
- package/docs/rules/compat/no-source.md +8 -17
- package/docs/rules/compat/no-test-v.md +7 -1
- package/docs/rules/compat/no-wait-n.md +3 -1
- package/docs/rules/no-dynamic-invocation.md +3 -0
- package/docs/rules/no-eval.md +22 -1
- package/docs/rules/no-masked-exit-status.md +2 -1
- package/docs/rules/no-secret-flags.md +22 -2
- package/docs/rules/no-unchecked-cd.md +2 -1
- package/docs/rules/no-undeclared-commands.md +1 -1
- package/docs/rules/no-unused-declarations.md +10 -6
- package/docs/rules/prefer-local.md +4 -0
- package/docs/rules/require-network-wrapper.md +1 -1
- package/docs/rules/require-strict-mode.md +19 -11
- package/docs/rules/require-tty-guard.md +26 -4
- package/docs/wrapper-rules.md +1 -1
- package/package.json +4 -4
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createRule } from './create-rule.js';
|
|
2
2
|
import { SET_OPTION_NAMES, formatSetCommand, shebangOptionChanges } from './set-options.js';
|
|
3
|
-
import { enabledAfter, resolveStrictModePrologue,
|
|
3
|
+
import { enabledAfter, resolveStrictModePrologue, setStatementArguments, } from './strict-mode-prologue.js';
|
|
4
4
|
import { shellLineBreakOf } from '../language/line-breaks.js';
|
|
5
5
|
/**
|
|
6
6
|
* Dialect-aware policy defaults. Bats runs under Bash; POSIX omits pipefail
|
|
@@ -17,7 +17,7 @@ export const requireStrictMode = createRule({
|
|
|
17
17
|
docs: {
|
|
18
18
|
description: 'require the configured strict-mode options (`set -euo pipefail` by default, `set -eu` for POSIX) near the top of the file',
|
|
19
19
|
},
|
|
20
|
-
|
|
20
|
+
hasSuggestions: true,
|
|
21
21
|
schema: [
|
|
22
22
|
{
|
|
23
23
|
type: 'object',
|
|
@@ -35,6 +35,7 @@ export const requireStrictMode = createRule({
|
|
|
35
35
|
// Absence must survive defaulting so each file's dialect selects its policy.
|
|
36
36
|
defaultOptions: [{}],
|
|
37
37
|
messages: {
|
|
38
|
+
enableStrictMode: 'Insert `{{prologue}}` to change the shell options for this script.',
|
|
38
39
|
missingStrictMode: 'Enable strict mode with `{{prologue}}` within {{window}}; not enabled: {{missing}}.',
|
|
39
40
|
},
|
|
40
41
|
},
|
|
@@ -49,7 +50,8 @@ export const requireStrictMode = createRule({
|
|
|
49
50
|
const text = sourceCode.text;
|
|
50
51
|
const shebangChanges = allowShebangOnly ? shebangOptionChanges(text) : [];
|
|
51
52
|
const statements = node.stmts.slice(0, withinStatements);
|
|
52
|
-
const
|
|
53
|
+
const argumentsByStatement = statements.map((stmt) => setStatementArguments(stmt, sourceCode));
|
|
54
|
+
const statementChanges = argumentsByStatement.map((args) => args?.changes ?? []);
|
|
53
55
|
const enabled = enabledAfter(shebangChanges, statementChanges);
|
|
54
56
|
const missing = required.filter((option) => !enabled.has(option));
|
|
55
57
|
if (missing.length === 0)
|
|
@@ -58,7 +60,8 @@ export const requireStrictMode = createRule({
|
|
|
58
60
|
// holds one fewer original statement. Fix only if that would pass.
|
|
59
61
|
const prologueChanges = required.map((option) => ({ option, enable: true }));
|
|
60
62
|
const fixedEnabled = enabledAfter([...shebangChanges, ...prologueChanges], statementChanges.slice(0, withinStatements - 1));
|
|
61
|
-
const
|
|
63
|
+
const insertionSatisfies = argumentsByStatement.every((args) => args?.stoppedBy !== 'unknown') &&
|
|
64
|
+
required.every((option) => fixedEnabled.has(option));
|
|
62
65
|
context.report({
|
|
63
66
|
loc: { line: 1, column: 1 },
|
|
64
67
|
messageId: 'missingStrictMode',
|
|
@@ -69,20 +72,26 @@ export const requireStrictMode = createRule({
|
|
|
69
72
|
: `the first ${String(withinStatements)} statements`,
|
|
70
73
|
missing: missing.join(', '),
|
|
71
74
|
},
|
|
72
|
-
|
|
73
|
-
?
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
75
|
+
suggest: insertionSatisfies
|
|
76
|
+
? [
|
|
77
|
+
{
|
|
78
|
+
messageId: 'enableStrictMode',
|
|
79
|
+
data: { prologue },
|
|
80
|
+
fix: (fixer) => {
|
|
81
|
+
let bodyStart = 0;
|
|
82
|
+
if (text.startsWith('#!')) {
|
|
83
|
+
const headerEnd = text.indexOf('\n');
|
|
84
|
+
if (headerEnd === -1) {
|
|
85
|
+
return fixer.insertTextAfterRange([0, text.length], '\n' + prologue);
|
|
86
|
+
}
|
|
87
|
+
bodyStart = headerEnd + 1;
|
|
88
|
+
}
|
|
89
|
+
const lineBreak = shellLineBreakOf(text, bodyStart);
|
|
90
|
+
return fixer.insertTextBeforeRange([bodyStart, bodyStart], prologue + lineBreak);
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
]
|
|
94
|
+
: [],
|
|
86
95
|
});
|
|
87
96
|
},
|
|
88
97
|
};
|
|
@@ -11,5 +11,7 @@ export interface RequireTtyGuardOptions {
|
|
|
11
11
|
}
|
|
12
12
|
/** Message ids of this rule. @internal */
|
|
13
13
|
export type RequireTtyGuardMessageId = 'unguardedInteractive' | 'wrapInTtyGuard';
|
|
14
|
+
/** What the rule can establish about the command's input source. @internal */
|
|
15
|
+
export type InputSource = 'stdin' | 'explicit' | 'unknown' | 'terminal';
|
|
14
16
|
/** Built-in rule `sh/require-tty-guard`. @internal */
|
|
15
17
|
export declare const requireTtyGuard: import("./define-shell-rule.js").ShellRule<[RequireTtyGuardOptions?], RequireTtyGuardMessageId>;
|
|
@@ -30,7 +30,9 @@
|
|
|
30
30
|
* that here-document's terminator and would carry the other command into the
|
|
31
31
|
* guard.
|
|
32
32
|
*/
|
|
33
|
+
import { analysisDialect, describeWordAtPosition, isSingleField, resolveWordTextAtPosition, } from '../language/word-resolution.js';
|
|
33
34
|
import { DashHdoc, Hdoc } from 'sh-ast';
|
|
35
|
+
import { someShellNode } from '../language/syntax-view.js';
|
|
34
36
|
import { createRule } from './create-rule.js';
|
|
35
37
|
import { lineBreakOf, shellLineBreakOf } from '../language/line-breaks.js';
|
|
36
38
|
/** Registry name of this rule. */
|
|
@@ -56,14 +58,43 @@ const REDIRECT_INPUT_OPS = new Set([
|
|
|
56
58
|
const DUP_INPUT = 67;
|
|
57
59
|
/** The `-t` unary test operator (`sh-ast`'s `TsFdTerm`). */
|
|
58
60
|
const TS_FD_TERM = 123;
|
|
59
|
-
/**
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
*/
|
|
63
|
-
const
|
|
61
|
+
/** Decimal timeout accepted by Bash read, including signs and zero spellings. */
|
|
62
|
+
const BASH_READ_TIMEOUT = /^(?:\+?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]*)|-(?:0+(?:\.0*)?|\.0*))$/u;
|
|
63
|
+
const MKSH_READ_TIMEOUT = /^[0-9]+(?:\.[0-9]+)?$/u;
|
|
64
|
+
/** Flags whose argument is required, and switches that leave stdin selected. */
|
|
65
|
+
const READ_FLAGS = {
|
|
66
|
+
bash: { operands: 'adinNptu', switches: 'Eers' },
|
|
67
|
+
posix: { operands: 'p', switches: 'r' },
|
|
68
|
+
mksh: { operands: 'adnNt', switches: 'Ars' },
|
|
69
|
+
zsh: { operands: 'du', switches: 'Aers' },
|
|
70
|
+
};
|
|
71
|
+
/** Decimal spellings of fd 0 and supported alternate descriptors. */
|
|
72
|
+
function descriptorInput(target, dialect, redirect) {
|
|
73
|
+
if (target === null)
|
|
74
|
+
return 'unknown';
|
|
75
|
+
if (redirect && target === '-')
|
|
76
|
+
return 'explicit';
|
|
77
|
+
if (target === 'p' && (dialect === 'mksh' || dialect === 'zsh'))
|
|
78
|
+
return 'explicit';
|
|
79
|
+
const numeric = dialect === 'posix' || dialect === 'mksh'
|
|
80
|
+
? /^[0-9]$/u
|
|
81
|
+
: redirect
|
|
82
|
+
? dialect === 'bash'
|
|
83
|
+
? /^[0-9]+-?$/u
|
|
84
|
+
: /^[0-9]+$/u
|
|
85
|
+
: /^[ \t\n\r\f\v]*[+-]?[0-9]+$/u;
|
|
86
|
+
if (!numeric.test(target))
|
|
87
|
+
return 'unknown';
|
|
88
|
+
const fd = Number(redirect ? target.replace(/-$/u, '') : target);
|
|
89
|
+
if (!Number.isSafeInteger(fd) || fd < 0 || fd > 2_147_483_647)
|
|
90
|
+
return 'unknown';
|
|
91
|
+
if (fd > 0)
|
|
92
|
+
return 'explicit';
|
|
93
|
+
return redirect && target.endsWith('-') ? 'unknown' : 'stdin';
|
|
94
|
+
}
|
|
64
95
|
/** The static text of `node` if it is a `Word` with a known value. */
|
|
65
96
|
function textOf(sourceCode, node) {
|
|
66
|
-
return node?.type === 'Word' ? sourceCode
|
|
97
|
+
return node?.type === 'Word' ? resolveWordTextAtPosition(sourceCode, node) : null;
|
|
67
98
|
}
|
|
68
99
|
/**
|
|
69
100
|
* Whether `call` is a `[ … -t 0 … ]` or `test … -t 0 …` command. `-t 0` must
|
|
@@ -78,7 +109,9 @@ function isCommandTtyTest(call, sourceCode) {
|
|
|
78
109
|
}
|
|
79
110
|
/** Whether `test` is a `[[ -t 0 ]]` unary test. */
|
|
80
111
|
function isClauseTtyTest(test, sourceCode) {
|
|
81
|
-
return test.op === TS_FD_TERM &&
|
|
112
|
+
return (test.op === TS_FD_TERM &&
|
|
113
|
+
test.x?.type === 'Word' &&
|
|
114
|
+
resolveWordTextAtPosition(sourceCode, test.x) === '0');
|
|
82
115
|
}
|
|
83
116
|
/** Whether `inner` lies within `outer`, by source range. */
|
|
84
117
|
function within(inner, outer) {
|
|
@@ -91,46 +124,138 @@ function listRange(stmts) {
|
|
|
91
124
|
return [stmts[0].range[0], stmts[stmts.length - 1].range[1]];
|
|
92
125
|
}
|
|
93
126
|
/**
|
|
94
|
-
* Whether a redirect
|
|
95
|
-
* `< file`, `0< file`, `<<`, `<<<`, `<> file`,
|
|
127
|
+
* Whether a redirect feeds fd 0 from an explicit source:
|
|
128
|
+
* `< file`, `0< file`, `<<`, `<<<`, `<> file`, a known nonzero descriptor,
|
|
129
|
+
* a Zsh or mksh coprocess (`<&p`), or a closed descriptor (`<&-`).
|
|
96
130
|
*/
|
|
97
|
-
function
|
|
98
|
-
|
|
131
|
+
function redirectedInput(stmt, sourceCode) {
|
|
132
|
+
const dialect = analysisDialect(sourceCode.dialect);
|
|
133
|
+
let input = 'stdin';
|
|
134
|
+
for (const redir of stmt.redirs) {
|
|
99
135
|
if (!REDIRECT_INPUT_OPS.has(redir.op))
|
|
100
|
-
|
|
136
|
+
continue;
|
|
101
137
|
const fd = redir.n === undefined ? '0' : redir.n.value;
|
|
102
|
-
if (fd !== '0')
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
138
|
+
if (fd !== '0' && !((dialect === 'bash' || dialect === 'zsh') && /^0+$/u.test(fd)))
|
|
139
|
+
continue;
|
|
140
|
+
if (redir.op !== DUP_INPUT)
|
|
141
|
+
return 'explicit';
|
|
142
|
+
const duplicated = descriptorInput(textOf(sourceCode, redir.word), dialect, true);
|
|
143
|
+
if (duplicated === 'explicit')
|
|
144
|
+
return 'explicit';
|
|
145
|
+
if (duplicated === 'unknown')
|
|
146
|
+
input = 'unknown';
|
|
147
|
+
}
|
|
148
|
+
return input;
|
|
106
149
|
}
|
|
107
150
|
/**
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
* non-option word.
|
|
151
|
+
* The final input descriptor selected by `read`. An unresolved option or
|
|
152
|
+
* operand can change subsequent option parsing. Even when the final descriptor
|
|
153
|
+
* is zero, an earlier nonzero descriptor can fail Bash's checks before the read.
|
|
112
154
|
*/
|
|
113
|
-
function
|
|
114
|
-
const
|
|
155
|
+
function readInput(call, sourceCode) {
|
|
156
|
+
const dialect = analysisDialect(sourceCode.dialect);
|
|
157
|
+
const { operands, switches } = READ_FLAGS[dialect];
|
|
158
|
+
const words = call.args
|
|
159
|
+
.slice(1)
|
|
160
|
+
.map((word) => describeWordAtPosition(sourceCode, word))
|
|
161
|
+
.filter((facts) => facts.fields.max !== 0);
|
|
162
|
+
let input = 'stdin';
|
|
163
|
+
let checkedAlternateDescriptor = false;
|
|
164
|
+
let usesCoprocess = false;
|
|
165
|
+
let readsKeys = false;
|
|
166
|
+
let selectedDescriptor = false;
|
|
167
|
+
let uncertainArguments = false;
|
|
115
168
|
for (let i = 0; i < words.length; i += 1) {
|
|
116
|
-
const
|
|
117
|
-
if (
|
|
118
|
-
return
|
|
169
|
+
const word = words[i];
|
|
170
|
+
if (!isSingleField(word))
|
|
171
|
+
return 'unknown';
|
|
172
|
+
if (!word.resolution.static) {
|
|
173
|
+
if (word.knownPrefix === '' || word.knownPrefix.startsWith('-'))
|
|
174
|
+
return 'unknown';
|
|
175
|
+
// Option processing stops here. The variable operand can still be invalid.
|
|
176
|
+
uncertainArguments = true;
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
const text = word.resolution.text;
|
|
180
|
+
if (!text.startsWith('-') || text === '-' || text === '--')
|
|
181
|
+
break;
|
|
119
182
|
for (let j = 1; j < text.length; j += 1) {
|
|
120
183
|
const flag = text.charAt(j);
|
|
121
|
-
if (!READ_ARG_FLAGS.has(flag))
|
|
122
|
-
continue;
|
|
123
184
|
const rest = text.slice(j + 1);
|
|
185
|
+
if (switches.includes(flag))
|
|
186
|
+
continue;
|
|
187
|
+
if (dialect === 'zsh' && flag === 'k') {
|
|
188
|
+
readsKeys = true;
|
|
189
|
+
if (rest !== '') {
|
|
190
|
+
if (!/^[0-9]+$/u.test(rest))
|
|
191
|
+
return 'unknown';
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
const next = words.at(i + 1)?.resolution;
|
|
195
|
+
if (next?.static && /^[0-9]+$/u.test(next.text))
|
|
196
|
+
i++;
|
|
197
|
+
}
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
if (flag === 'p' && (dialect === 'mksh' || dialect === 'zsh')) {
|
|
201
|
+
if (dialect === 'zsh')
|
|
202
|
+
usesCoprocess = true;
|
|
203
|
+
input = 'explicit';
|
|
204
|
+
checkedAlternateDescriptor = true;
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
// mksh's -u takes an optional attached descriptor. -u with no digits
|
|
208
|
+
// selects stdin and leaves the next word to be read as a variable name.
|
|
209
|
+
if (flag === 'u' && dialect === 'mksh') {
|
|
210
|
+
input = descriptorInput(rest === '' ? '0' : rest, dialect, false);
|
|
211
|
+
if (input === 'unknown')
|
|
212
|
+
return input;
|
|
213
|
+
checkedAlternateDescriptor ||= input === 'explicit';
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
if (!operands.includes(flag))
|
|
217
|
+
return 'unknown';
|
|
218
|
+
let operand = rest;
|
|
219
|
+
if (rest === '') {
|
|
220
|
+
const next = words.at(++i);
|
|
221
|
+
if (next === undefined || !isSingleField(next))
|
|
222
|
+
return 'unknown';
|
|
223
|
+
operand = next.resolution.static ? next.resolution.text : null;
|
|
224
|
+
// These arguments can be any one string. Keep uncertain expansions
|
|
225
|
+
// out of the proposed edit when read still uses stdin.
|
|
226
|
+
uncertainArguments ||= operand === null;
|
|
227
|
+
}
|
|
124
228
|
if (flag === 'u') {
|
|
125
|
-
|
|
126
|
-
|
|
229
|
+
selectedDescriptor = true;
|
|
230
|
+
if (dialect === 'zsh' && operand === 'p')
|
|
231
|
+
usesCoprocess = true;
|
|
232
|
+
input = descriptorInput(operand, dialect, false);
|
|
233
|
+
if (input === 'unknown')
|
|
234
|
+
return input;
|
|
235
|
+
checkedAlternateDescriptor ||= input === 'explicit';
|
|
236
|
+
}
|
|
237
|
+
else if (flag === 't') {
|
|
238
|
+
const timeout = dialect === 'bash' ? BASH_READ_TIMEOUT : MKSH_READ_TIMEOUT;
|
|
239
|
+
if (operand === null || !timeout.test(operand))
|
|
240
|
+
return 'unknown';
|
|
241
|
+
}
|
|
242
|
+
else if ((flag === 'n' || flag === 'N') &&
|
|
243
|
+
(operand === null || !/^[0-9]+$/u.test(operand) || Number(operand) > 2_147_483_647)) {
|
|
244
|
+
return 'unknown';
|
|
245
|
+
}
|
|
246
|
+
else if (flag === 'a' && (operand === null || !/^[A-Za-z_][A-Za-z_0-9]*$/u.test(operand))) {
|
|
247
|
+
return 'unknown';
|
|
127
248
|
}
|
|
128
|
-
if (rest === '')
|
|
129
|
-
i += 1;
|
|
130
249
|
break;
|
|
131
250
|
}
|
|
132
251
|
}
|
|
133
|
-
|
|
252
|
+
if (usesCoprocess)
|
|
253
|
+
return 'explicit';
|
|
254
|
+
if (readsKeys && !selectedDescriptor)
|
|
255
|
+
return 'terminal';
|
|
256
|
+
return input === 'stdin' && (checkedAlternateDescriptor || uncertainArguments)
|
|
257
|
+
? 'unknown'
|
|
258
|
+
: input;
|
|
134
259
|
}
|
|
135
260
|
/**
|
|
136
261
|
* Whether the exit status of `stmt` steers control flow: it is a condition of
|
|
@@ -219,6 +344,21 @@ export const requireTtyGuard = createRule({
|
|
|
219
344
|
},
|
|
220
345
|
create(context) {
|
|
221
346
|
const { sourceCode } = context;
|
|
347
|
+
const dialect = analysisDialect(sourceCode.dialect);
|
|
348
|
+
const statusNames = new Set(['?']);
|
|
349
|
+
if (dialect === 'bash')
|
|
350
|
+
statusNames.add('PIPESTATUS');
|
|
351
|
+
if (dialect === 'zsh') {
|
|
352
|
+
statusNames.add('pipestatus');
|
|
353
|
+
statusNames.add('status');
|
|
354
|
+
}
|
|
355
|
+
const readsStatus = (stmt) => someShellNode(stmt, (part) => {
|
|
356
|
+
if (part.type === 'ParamExp') {
|
|
357
|
+
return statusNames.has(part.param?.value ?? '') || (dialect === 'bash' && part.excl);
|
|
358
|
+
}
|
|
359
|
+
return ((part.type === 'ArithmExp' || part.type === 'ArithmCmd') &&
|
|
360
|
+
someShellNode(part, (term) => term.type === 'Lit' && statusNames.has(term.value)));
|
|
361
|
+
});
|
|
222
362
|
const options = context.options[0];
|
|
223
363
|
// Empty replacements intentionally remove defaults; a set makes overlapping
|
|
224
364
|
// base/additional names one policy without mutating either configured list.
|
|
@@ -250,23 +390,29 @@ export const requireTtyGuard = createRule({
|
|
|
250
390
|
return false;
|
|
251
391
|
};
|
|
252
392
|
/** Whether stdin is supplied to `node` by a redirect or a pipe. */
|
|
253
|
-
const
|
|
393
|
+
const inheritedInput = (node) => {
|
|
254
394
|
let child = node;
|
|
395
|
+
let input = 'stdin';
|
|
255
396
|
for (let parent = sourceCode.getParent(child); parent; parent = sourceCode.getParent(child)) {
|
|
256
|
-
if (parent.type === 'Stmt'
|
|
257
|
-
|
|
397
|
+
if (parent.type === 'Stmt') {
|
|
398
|
+
const redirected = redirectedInput(parent, sourceCode);
|
|
399
|
+
if (redirected === 'explicit')
|
|
400
|
+
return 'explicit';
|
|
401
|
+
if (redirected === 'unknown')
|
|
402
|
+
input = 'unknown';
|
|
403
|
+
}
|
|
258
404
|
// A coprocess body reads the coprocess's own input pipe, never the
|
|
259
405
|
// terminal, so a terminal guard is meaningless there.
|
|
260
406
|
if (parent.type === 'CoprocClause')
|
|
261
|
-
return
|
|
407
|
+
return 'explicit';
|
|
262
408
|
if (parent.type === 'BinaryCmd' &&
|
|
263
409
|
(parent.op === PIPE || parent.op === PIPE_ALL) &&
|
|
264
410
|
parent.y === child) {
|
|
265
|
-
return
|
|
411
|
+
return 'explicit';
|
|
266
412
|
}
|
|
267
413
|
child = parent;
|
|
268
414
|
}
|
|
269
|
-
return
|
|
415
|
+
return input;
|
|
270
416
|
};
|
|
271
417
|
return {
|
|
272
418
|
CallExpr(node) {
|
|
@@ -275,9 +421,10 @@ export const requireTtyGuard = createRule({
|
|
|
275
421
|
const name = sourceCode.getCommandName(node);
|
|
276
422
|
if (name === null || !commands.has(name))
|
|
277
423
|
return;
|
|
278
|
-
|
|
424
|
+
const input = name === 'read' ? readInput(node, sourceCode) : 'stdin';
|
|
425
|
+
if (input === 'explicit')
|
|
279
426
|
return;
|
|
280
|
-
candidates.push({ node, name });
|
|
427
|
+
candidates.push({ node, name, input });
|
|
281
428
|
},
|
|
282
429
|
UnaryTest(node) {
|
|
283
430
|
if (isClauseTtyTest(node, sourceCode))
|
|
@@ -285,21 +432,26 @@ export const requireTtyGuard = createRule({
|
|
|
285
432
|
},
|
|
286
433
|
ForClause(node) {
|
|
287
434
|
if (node.select && commands.has('select')) {
|
|
288
|
-
candidates.push({ node, name: 'select' });
|
|
435
|
+
candidates.push({ node, name: 'select', input: 'stdin' });
|
|
289
436
|
}
|
|
290
437
|
},
|
|
291
438
|
'File:exit'() {
|
|
292
|
-
for (const { node, name } of candidates) {
|
|
293
|
-
|
|
439
|
+
for (const { node, name, input } of candidates) {
|
|
440
|
+
const inherited = inheritedInput(node);
|
|
441
|
+
if ((inherited === 'explicit' && input !== 'terminal') || isGuarded(node))
|
|
294
442
|
continue;
|
|
295
443
|
const stmt = sourceCode.getParent(node);
|
|
296
444
|
// A statement that runs in the background cannot be wrapped without
|
|
297
445
|
// changing what is backgrounded, so it gets no suggestion.
|
|
298
446
|
// A statement sharing its here-document's opener line with another
|
|
299
|
-
// command gets none either (see `sharesHeredocOpenerLine`).
|
|
300
|
-
|
|
447
|
+
// command gets none either (see `sharesHeredocOpenerLine`). The TTY
|
|
448
|
+
// test also changes the status visible to expansions in the statement.
|
|
449
|
+
const wrappable = input === 'stdin' &&
|
|
450
|
+
inherited === 'stdin' &&
|
|
451
|
+
stmt?.type === 'Stmt' &&
|
|
301
452
|
!stmt.background &&
|
|
302
453
|
!isControlFlowOperand(stmt, sourceCode) &&
|
|
454
|
+
!readsStatus(stmt) &&
|
|
303
455
|
!sharesHeredocOpenerLine(stmt, sourceCode.text);
|
|
304
456
|
context.report({
|
|
305
457
|
node,
|
|
@@ -55,6 +55,8 @@ export interface ParsedSetArguments {
|
|
|
55
55
|
*/
|
|
56
56
|
readonly stoppedBy: 'operand' | 'unknown' | 'malformed' | undefined;
|
|
57
57
|
}
|
|
58
|
+
/** One field known to start positional arguments when an option does not consume it. @internal */
|
|
59
|
+
export declare const SET_POSITIONAL_OPERAND: unique symbol;
|
|
58
60
|
/**
|
|
59
61
|
* Reads a `set`-style argument list (the arguments after the command word, so
|
|
60
62
|
* `["-euo", "pipefail"]` for `set -euo pipefail`) into the option changes it
|
|
@@ -69,7 +71,7 @@ export interface ParsedSetArguments {
|
|
|
69
71
|
* @param args - Each argument's static text, or `null` when it is not static.
|
|
70
72
|
* @internal
|
|
71
73
|
*/
|
|
72
|
-
export declare function parseSetArguments(args: readonly (string | null)[]): ParsedSetArguments;
|
|
74
|
+
export declare function parseSetArguments(args: readonly (string | null | typeof SET_POSITIONAL_OPERAND)[]): ParsedSetArguments;
|
|
73
75
|
/**
|
|
74
76
|
* Applies `changes` to `enabled` in order, so a later `+e` undoes an earlier
|
|
75
77
|
* `-e`. Mutates and returns `enabled`.
|
|
@@ -60,6 +60,8 @@ const SET_OPTION_FLAGS = Object.freeze({
|
|
|
60
60
|
export const SET_OPTION_NAMES = Object.freeze(Object.keys(SET_OPTION_FLAGS));
|
|
61
61
|
/** Single-letter flag → the long option name it toggles (the inverse of {@link SET_OPTION_FLAGS}). */
|
|
62
62
|
const OPTION_NAME_BY_FLAG = new Map(Object.entries(SET_OPTION_FLAGS).flatMap(([name, flag]) => flag === undefined ? [] : [[flag, name]]));
|
|
63
|
+
/** One field known to start positional arguments when an option does not consume it. @internal */
|
|
64
|
+
export const SET_POSITIONAL_OPERAND = Symbol('set positional operand');
|
|
63
65
|
/**
|
|
64
66
|
* Reads a `set`-style argument list (the arguments after the command word, so
|
|
65
67
|
* `["-euo", "pipefail"]` for `set -euo pipefail`) into the option changes it
|
|
@@ -80,6 +82,8 @@ export function parseSetArguments(args) {
|
|
|
80
82
|
while (index < args.length) {
|
|
81
83
|
const arg = args[index];
|
|
82
84
|
index += 1;
|
|
85
|
+
if (arg === SET_POSITIONAL_OPERAND)
|
|
86
|
+
return { changes, stoppedBy: 'operand' };
|
|
83
87
|
if (arg === null)
|
|
84
88
|
return { changes, stoppedBy: 'unknown' };
|
|
85
89
|
if (arg === '--')
|
|
@@ -98,7 +102,7 @@ export function parseSetArguments(args) {
|
|
|
98
102
|
if (flag === 'o') {
|
|
99
103
|
const name = index < args.length ? args[index] : undefined;
|
|
100
104
|
index += 1;
|
|
101
|
-
if (name === null)
|
|
105
|
+
if (name === null || name === SET_POSITIONAL_OPERAND)
|
|
102
106
|
return { changes, stoppedBy: 'unknown' };
|
|
103
107
|
if (name === undefined || !Object.hasOwn(SET_OPTION_FLAGS, name)) {
|
|
104
108
|
return { changes, stoppedBy: 'malformed' };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { SettingsConfig } from '@eslint/core';
|
|
2
2
|
import type { ShellFile, ShellNodeOf } from '../language/syntax-view.js';
|
|
3
3
|
import type { ShellSourceCode } from '../language/shell-source-code.js';
|
|
4
|
-
import type { SetOptionChange } from './set-options.js';
|
|
4
|
+
import type { ParsedSetArguments, SetOptionChange } from './set-options.js';
|
|
5
5
|
/** The normalized project definition, independent of any required option list. @internal */
|
|
6
6
|
export interface StrictModePrologue {
|
|
7
7
|
/** Whether interpreter flags count, despite being bypassed by `bash script.sh`. */
|
|
@@ -24,6 +24,10 @@ export declare function resolveStrictModePrologue(settings: SettingsConfig): Str
|
|
|
24
24
|
*
|
|
25
25
|
* @internal
|
|
26
26
|
*/
|
|
27
|
+
export declare function setStatementArguments(stmt: ShellNodeOf<'Stmt'>, sourceCode: ShellSourceCode): ParsedSetArguments | undefined;
|
|
28
|
+
/** Reads shell set options from the proven argument shape. @internal */
|
|
29
|
+
export declare function parseSetWords(source: ShellSourceCode, words: readonly ShellNodeOf<'Word'>[]): ParsedSetArguments;
|
|
30
|
+
/** The known option changes for a plain `set` statement. @internal */
|
|
27
31
|
export declare function setStatementChanges(stmt: ShellNodeOf<'Stmt'>, sourceCode: ShellSourceCode): readonly SetOptionChange[];
|
|
28
32
|
/**
|
|
29
33
|
* The options enabled after applying `seed`, then each statement's `set`
|
|
@@ -40,4 +44,7 @@ export declare function enabledAfter(seed: readonly SetOptionChange[], statement
|
|
|
40
44
|
*
|
|
41
45
|
* @internal
|
|
42
46
|
*/
|
|
43
|
-
export declare function
|
|
47
|
+
export declare function prologueOptionState(file: ShellFile, sourceCode: ShellSourceCode, config: StrictModePrologue): {
|
|
48
|
+
enabled: ReadonlySet<string>;
|
|
49
|
+
hasUnknownOptions: boolean;
|
|
50
|
+
};
|
|
@@ -5,8 +5,9 @@
|
|
|
5
5
|
* ESLINT_SH_STRICT_MODE_SETTING identifies a malformed project prologue fact;
|
|
6
6
|
* rule-option schema failures remain ESLint responsibilities.
|
|
7
7
|
*/
|
|
8
|
+
import { describeWordAtPosition, isSingleField } from '../language/word-resolution.js';
|
|
8
9
|
import { configurationMessage } from '../configuration-message.js';
|
|
9
|
-
import { applySetOptionChanges, parseSetArguments, shebangOptionChanges } from './set-options.js';
|
|
10
|
+
import { applySetOptionChanges, parseSetArguments, SET_POSITIONAL_OPERAND, shebangOptionChanges, } from './set-options.js';
|
|
10
11
|
/** The user-written path of the shared project fact, extended with its offending field. */
|
|
11
12
|
const SETTING = 'settings.sh.strictModePrologue';
|
|
12
13
|
/**
|
|
@@ -52,16 +53,48 @@ export function resolveStrictModePrologue(settings) {
|
|
|
52
53
|
*
|
|
53
54
|
* @internal
|
|
54
55
|
*/
|
|
55
|
-
export function
|
|
56
|
+
export function setStatementArguments(stmt, sourceCode) {
|
|
56
57
|
if (stmt.background) {
|
|
57
|
-
return
|
|
58
|
+
return undefined;
|
|
58
59
|
}
|
|
59
60
|
const { cmd } = stmt;
|
|
60
61
|
if (cmd?.type !== 'CallExpr' || sourceCode.getCommandName(cmd) !== 'set') {
|
|
61
|
-
return
|
|
62
|
+
return undefined;
|
|
62
63
|
}
|
|
63
|
-
const
|
|
64
|
-
|
|
64
|
+
const parsed = parseSetWords(sourceCode, cmd.args.slice(1));
|
|
65
|
+
if (stmt.redirs.length === 0)
|
|
66
|
+
return parsed;
|
|
67
|
+
// The redirection may fail before set runs. A successful disable can
|
|
68
|
+
// invalidate an earlier proof. Even a close may need a spare descriptor
|
|
69
|
+
// to restore the original one after set returns.
|
|
70
|
+
const final = new Map(parsed.changes.map((change) => [change.option, change]));
|
|
71
|
+
return {
|
|
72
|
+
changes: [...final.values()].filter((change) => !change.enable),
|
|
73
|
+
stoppedBy: 'unknown',
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/** Reads shell set options from the proven argument shape. @internal */
|
|
77
|
+
export function parseSetWords(source, words) {
|
|
78
|
+
const args = [];
|
|
79
|
+
for (const word of words) {
|
|
80
|
+
const facts = describeWordAtPosition(source, word);
|
|
81
|
+
if (facts.fields.max === 0)
|
|
82
|
+
continue;
|
|
83
|
+
if (facts.resolution.static && isSingleField(facts)) {
|
|
84
|
+
args.push(facts.resolution.text);
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
const first = facts.knownPrefix.charAt(0);
|
|
88
|
+
args.push(isSingleField(facts) && first !== '' && first !== '-' && first !== '+'
|
|
89
|
+
? SET_POSITIONAL_OPERAND
|
|
90
|
+
: null);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return parseSetArguments(args);
|
|
94
|
+
}
|
|
95
|
+
/** The known option changes for a plain `set` statement. @internal */
|
|
96
|
+
export function setStatementChanges(stmt, sourceCode) {
|
|
97
|
+
return setStatementArguments(stmt, sourceCode)?.changes ?? [];
|
|
65
98
|
}
|
|
66
99
|
/**
|
|
67
100
|
* The options enabled after applying `seed`, then each statement's `set`
|
|
@@ -84,8 +117,12 @@ export function enabledAfter(seed, statementChanges) {
|
|
|
84
117
|
*
|
|
85
118
|
* @internal
|
|
86
119
|
*/
|
|
87
|
-
export function
|
|
120
|
+
export function prologueOptionState(file, sourceCode, config) {
|
|
88
121
|
const shebangChanges = config.allowShebangOnly ? shebangOptionChanges(sourceCode.text) : [];
|
|
89
122
|
const statements = file.stmts.slice(0, config.withinStatements);
|
|
90
|
-
|
|
123
|
+
const argumentsByStatement = statements.map((stmt) => setStatementArguments(stmt, sourceCode));
|
|
124
|
+
return {
|
|
125
|
+
enabled: enabledAfter(shebangChanges, argumentsByStatement.map((args) => args?.changes ?? [])),
|
|
126
|
+
hasUnknownOptions: argumentsByStatement.some((args) => args?.stoppedBy === 'unknown'),
|
|
127
|
+
};
|
|
91
128
|
}
|
|
@@ -230,7 +230,8 @@ entry has:
|
|
|
230
230
|
- `node`: the `CallExpr` node, the same object a `CallExpr` listener receives, so you can report
|
|
231
231
|
on it.
|
|
232
232
|
- `context`: the frames that lead to the command, outermost first. `date` in
|
|
233
|
-
`f() { echo "$(date)"; }` has `[{ kind: 'function',
|
|
233
|
+
`f() { echo "$(date)"; }` has `[{ kind: 'function', names: ['f'] }, { kind: 'cmdSubst' }]`.
|
|
234
|
+
Function frames list names in declaration order. Anonymous functions have an empty list.
|
|
234
235
|
- `argv0` and `argv`: the command's words, resolved like `resolveWord` resolves them.
|
|
235
236
|
- `resolveInvokedCommand()`: the command that actually runs after transparent wrappers such as
|
|
236
237
|
`sudo` or `env`. Its `chainIndices` say where each word of that chain sits in the site's `argv`,
|
|
@@ -268,6 +269,7 @@ const noCurlInFunctions = defineShellRule({
|
|
|
268
269
|
schema: [],
|
|
269
270
|
messages: {
|
|
270
271
|
curlInFunction: 'Function `{{name}}` runs `curl`; fetch data before calling it.',
|
|
272
|
+
curlInUnnamedFunction: 'This function runs `curl`; fetch data before calling it.',
|
|
271
273
|
},
|
|
272
274
|
},
|
|
273
275
|
create(context) {
|
|
@@ -283,11 +285,12 @@ const noCurlInFunctions = defineShellRule({
|
|
|
283
285
|
// function that directly contains this command.
|
|
284
286
|
const fn = site.context.findLast((frame) => frame.kind === 'function');
|
|
285
287
|
if (fn) {
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
messageId: 'curlInFunction',
|
|
289
|
-
|
|
290
|
-
|
|
288
|
+
const [name] = fn.names;
|
|
289
|
+
if (typeof name === 'string') {
|
|
290
|
+
context.report({ node: site.node, messageId: 'curlInFunction', data: { name } });
|
|
291
|
+
} else {
|
|
292
|
+
context.report({ node: site.node, messageId: 'curlInUnnamedFunction' });
|
|
293
|
+
}
|
|
291
294
|
}
|
|
292
295
|
}
|
|
293
296
|
},
|
|
@@ -413,7 +416,7 @@ import { walk, printSync, type Node, type PrintOptions } from 'sh-ast';
|
|
|
413
416
|
import { enumerateCommands, resolveArgv0, type CommandInventory } from 'sh-analysis';
|
|
414
417
|
```
|
|
415
418
|
|
|
416
|
-
The supported dependency ranges are `sh-ast@^0.
|
|
419
|
+
The supported dependency ranges are `sh-ast@^0.13.0` and `sh-analysis@^0.4.0`.
|
|
417
420
|
There are no plugin or umbrella syntax forwarding subpaths. Keep using source helpers when you
|
|
418
421
|
need ESLint nodes, UTF-16 locations or fixes: they map the original syntax by identity.
|
|
419
422
|
|
package/docs/error-codes.md
CHANGED
|
@@ -67,6 +67,10 @@ plugin's source helpers also preserve analysis resource/invalid-wrapper failures
|
|
|
67
67
|
and printed replacements preserve upstream printer errors. Returning a partial
|
|
68
68
|
command inventory on a depth refusal would silently omit real commands.
|
|
69
69
|
|
|
70
|
+
Match dependency failures by `code`. An analysis helper can throw an error from
|
|
71
|
+
the `sh-ast` version installed under `sh-analysis`, so constructor identity can
|
|
72
|
+
differ from the version imported by a rule or by the plugin.
|
|
73
|
+
|
|
70
74
|
| Dependency code | Meaning |
|
|
71
75
|
| --- | --- |
|
|
72
76
|
| `SH_AST_SYNTAX_PARSE` | Invalid shell source. The language presents fatal diagnostics; raw syntax calls retain the error. |
|