@eslint-sh/eslint-plugin 0.11.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 +10 -4
- package/dist/command-harvest/harvest-commands.js +39 -10
- package/dist/eslint-plugin-api.d.ts +36 -14
- package/dist/language/command-resolution.d.ts +16 -6
- package/dist/language/command-resolution.js +44 -17
- package/dist/language/line-breaks.d.ts +10 -4
- package/dist/language/line-breaks.js +14 -5
- package/dist/language/shell-language.d.ts +2 -0
- package/dist/language/shell-language.js +10 -1
- package/dist/language/shell-source-code.d.ts +29 -13
- package/dist/language/shell-source-code.js +126 -55
- 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 +34 -46
- 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 +127 -30
- package/dist/rules/prefer-local.js +146 -52
- package/dist/rules/require-shebang.js +23 -21
- package/dist/rules/require-strict-mode.d.ts +2 -2
- package/dist/rules/require-strict-mode.js +28 -16
- package/dist/rules/require-tty-guard.d.ts +2 -0
- package/dist/rules/require-tty-guard.js +205 -50
- package/dist/rules/set-options.d.ts +5 -16
- package/dist/rules/set-options.js +17 -45
- package/dist/rules/shebang-interpreter.d.ts +33 -10
- package/dist/rules/shebang-interpreter.js +522 -53
- package/dist/rules/shebang-matches-dialect.js +4 -4
- package/dist/rules/strict-mode-prologue.d.ts +9 -2
- package/dist/rules/strict-mode-prologue.js +45 -8
- package/dist/rules/variable-reads.d.ts +12 -0
- package/dist/rules/variable-reads.js +31 -0
- package/docs/custom-rule-tutorial.md +19 -10
- package/docs/error-codes.md +6 -1
- package/docs/examples/no-curl-in-functions-rule.mjs +7 -5
- package/docs/rules/README.md +5 -5
- 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 +4 -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 +18 -9
- package/docs/rules/prefer-local.md +15 -33
- package/docs/rules/require-network-wrapper.md +1 -1
- package/docs/rules/require-shebang.md +46 -3
- package/docs/rules/require-strict-mode.md +40 -15
- package/docs/rules/require-tty-guard.md +27 -4
- package/docs/rules/shebang-matches-dialect.md +3 -0
- package/docs/shellcheck.md +1 -1
- package/docs/wrapper-rules.md +1 -1
- package/package.json +4 -4
|
@@ -1,7 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `sh/prefer-local`: a variable assigned inside a function must be declared
|
|
3
|
+
* `local` (or `declare`/`typeset`) in that function, so the function does not
|
|
4
|
+
* overwrite a global its caller may depend on. Shell variables are global by
|
|
5
|
+
* default, and a helper that assigns `i`, `tmp` or `result` silently corrupts
|
|
6
|
+
* the same-named variable of every caller.
|
|
7
|
+
*
|
|
8
|
+
* **What counts as an assignment** inside a function body:
|
|
9
|
+
*
|
|
10
|
+
* - a plain assignment statement (`x=1`, `x+=1`, `x=$(cmd)`); an assignment
|
|
11
|
+
* that prefixes a command (`x=1 cmd`) only reaches that command's
|
|
12
|
+
* environment and is not reported;
|
|
13
|
+
* - the variables named by `read` (`read -r a b`, `read -a arr`);
|
|
14
|
+
* - the loop variable of `for x in …` / `select x in …`.
|
|
15
|
+
*
|
|
16
|
+
* **What is exempt:** a name that is declared `local`, `declare` or `typeset`
|
|
17
|
+
* earlier in the function; a name assigned at top level before the function is
|
|
18
|
+
* defined (an intentional global); a name declared with `declare -g`/
|
|
19
|
+
* `typeset -g` or by `export`/`readonly` (an explicit global); and a name the
|
|
20
|
+
* `allow` option lists or the `allowPattern` option matches (UPPER_CASE
|
|
21
|
+
* globals by default). Each function is its own scope: nested function
|
|
22
|
+
* definitions are checked on their own, and subshells, command substitutions
|
|
23
|
+
* and process substitutions are skipped because their assignments cannot leak.
|
|
24
|
+
* So is every element of a pipeline (`cmd | read x`, `ls | while read l; …`):
|
|
25
|
+
* pipeline elements run in subshells, except that the last one runs in the
|
|
26
|
+
* current shell when the file enables `shopt -s lastpipe`.
|
|
27
|
+
* A function whose body is a subshell (`f() ( … )`) is skipped for the same
|
|
28
|
+
* reason.
|
|
29
|
+
*
|
|
30
|
+
* Only the first assignment to a name in a function is reported: the
|
|
31
|
+
* declaration that fixes it covers the rest.
|
|
32
|
+
*
|
|
33
|
+
* Adding a local declaration can change reads through dynamic scope and
|
|
34
|
+
* which writes remain visible to the caller. Choosing that scope is a manual
|
|
35
|
+
* edit.
|
|
36
|
+
*
|
|
37
|
+
* `local` is not POSIX, so the rule is inert under `dialect: "posix"`.
|
|
38
|
+
*
|
|
39
|
+
* The language resolves command names and words in their AST positions.
|
|
40
|
+
*/
|
|
41
|
+
import { resolveWordTextAtPosition } from '../language/word-resolution.js';
|
|
1
42
|
import { childNodes, walkShell } from '../language/syntax-view.js';
|
|
2
43
|
import { createRule } from './create-rule.js';
|
|
3
44
|
import { compileUserPattern } from './user-pattern.js';
|
|
4
|
-
import { lineBreakOf } from '../language/line-breaks.js';
|
|
5
45
|
/** The `allowPattern` used when none is configured: UPPER_CASE globals. */
|
|
6
46
|
const DEFAULT_ALLOW_PATTERN = '^[A-Z][A-Z0-9_]*$';
|
|
7
47
|
/** A valid shell variable name; anything else is not a binding target. */
|
|
@@ -24,18 +64,24 @@ const READ_OPTIONS_WITH_ARGUMENT = new Set([
|
|
|
24
64
|
* the rest of the cluster, or else the next word. `-a`'s array name is an
|
|
25
65
|
* ordinary target word. Words that are not static are not guessed at.
|
|
26
66
|
*/
|
|
27
|
-
function readTargets(args, sourceCode) {
|
|
67
|
+
function readTargets(args, sourceCode, canReadCoprocess) {
|
|
28
68
|
const targets = [];
|
|
29
69
|
let optionsEnded = false;
|
|
30
|
-
let
|
|
70
|
+
let argumentTo;
|
|
71
|
+
const unprovenMkshCoprocess = sourceCode.dialect === 'mksh' && !canReadCoprocess;
|
|
31
72
|
for (const word of args.slice(1)) {
|
|
32
|
-
const text = sourceCode
|
|
33
|
-
if (
|
|
34
|
-
|
|
73
|
+
const text = resolveWordTextAtPosition(sourceCode, word);
|
|
74
|
+
if (argumentTo !== undefined) {
|
|
75
|
+
if (unprovenMkshCoprocess && argumentTo === 'u' && (text === null || text === 'p'))
|
|
76
|
+
return [];
|
|
77
|
+
argumentTo = undefined;
|
|
35
78
|
continue;
|
|
36
79
|
}
|
|
37
|
-
if (text === null)
|
|
80
|
+
if (text === null) {
|
|
81
|
+
if (unprovenMkshCoprocess && !optionsEnded)
|
|
82
|
+
return [];
|
|
38
83
|
continue;
|
|
84
|
+
}
|
|
39
85
|
if (!optionsEnded && text === '--') {
|
|
40
86
|
optionsEnded = true;
|
|
41
87
|
continue;
|
|
@@ -43,8 +89,15 @@ function readTargets(args, sourceCode) {
|
|
|
43
89
|
if (!optionsEnded && text.startsWith('-') && text.length > 1) {
|
|
44
90
|
for (let index = 1; index < text.length; index += 1) {
|
|
45
91
|
const letter = text.charAt(index);
|
|
46
|
-
|
|
47
|
-
|
|
92
|
+
const coprocess = letter === 'p' && (sourceCode.dialect === 'zsh' || sourceCode.dialect === 'mksh');
|
|
93
|
+
if (coprocess && !canReadCoprocess)
|
|
94
|
+
return [];
|
|
95
|
+
if (!coprocess && READ_OPTIONS_WITH_ARGUMENT.has(letter)) {
|
|
96
|
+
const attached = text.slice(index + 1);
|
|
97
|
+
if (unprovenMkshCoprocess && letter === 'u' && attached === 'p')
|
|
98
|
+
return [];
|
|
99
|
+
if (attached === '')
|
|
100
|
+
argumentTo = letter;
|
|
48
101
|
break;
|
|
49
102
|
}
|
|
50
103
|
}
|
|
@@ -60,7 +113,7 @@ function readTargets(args, sourceCode) {
|
|
|
60
113
|
* `declare`/`typeset` with a `-g` flag, and every non-scoped declaration
|
|
61
114
|
* (`export`, `readonly`), bind their names as intentional globals.
|
|
62
115
|
*/
|
|
63
|
-
function bindingsOf(node, sourceCode) {
|
|
116
|
+
function bindingsOf(node, sourceCode, coprocessReads) {
|
|
64
117
|
if (node.type === 'ForClause') {
|
|
65
118
|
const { loop } = node;
|
|
66
119
|
if (loop?.type !== 'WordIter' || loop.name === undefined)
|
|
@@ -73,13 +126,15 @@ function bindingsOf(node, sourceCode) {
|
|
|
73
126
|
? []
|
|
74
127
|
: [{ name: assign.name.value, kind: 'assign', node: assign }]);
|
|
75
128
|
}
|
|
76
|
-
return sourceCode.getCommandName(node) === 'read'
|
|
129
|
+
return sourceCode.getCommandName(node) === 'read'
|
|
130
|
+
? readTargets(node.args, sourceCode, sourceCode.dialect !== 'mksh' || coprocessReads.has(node))
|
|
131
|
+
: [];
|
|
77
132
|
}
|
|
78
133
|
if (node.type === 'DeclClause') {
|
|
79
134
|
const variant = node.variant?.value;
|
|
80
135
|
const flags = node.args
|
|
81
136
|
.filter((arg) => arg.naked && arg.name === undefined && arg.value !== undefined)
|
|
82
|
-
.map((arg) =>
|
|
137
|
+
.map((arg) => arg.value === undefined ? null : resolveWordTextAtPosition(sourceCode, arg.value));
|
|
83
138
|
const isGlobal = variant === undefined ||
|
|
84
139
|
!SCOPED_DECLARATIONS.has(variant) ||
|
|
85
140
|
flags.some((flag) => flag !== null && /^-[A-Za-z]*g/u.test(flag));
|
|
@@ -96,7 +151,12 @@ function bindingsOf(node, sourceCode) {
|
|
|
96
151
|
return [];
|
|
97
152
|
}
|
|
98
153
|
/** Node types whose contents run in a child process and cannot leak assignments. */
|
|
99
|
-
const ISOLATED_SCOPES = new Set([
|
|
154
|
+
const ISOLATED_SCOPES = new Set([
|
|
155
|
+
'Subshell',
|
|
156
|
+
'CmdSubst',
|
|
157
|
+
'ProcSubst',
|
|
158
|
+
'CoprocClause',
|
|
159
|
+
]);
|
|
100
160
|
/** `BinaryCmd.op` values for `|` and `|&`, the pipeline operators. */
|
|
101
161
|
const PIPE_OPS = new Set([13, 14]);
|
|
102
162
|
/**
|
|
@@ -110,31 +170,97 @@ function enablesLastpipe(root, sourceCode) {
|
|
|
110
170
|
walkShell(root, (node) => {
|
|
111
171
|
if (node.type !== 'CallExpr' || sourceCode.getCommandName(node) !== 'shopt')
|
|
112
172
|
return;
|
|
113
|
-
const words = node.args.slice(1).map((word) => sourceCode
|
|
173
|
+
const words = node.args.slice(1).map((word) => resolveWordTextAtPosition(sourceCode, word));
|
|
114
174
|
if (words.includes('-s') && words.includes('lastpipe'))
|
|
115
175
|
enabled = true;
|
|
116
176
|
});
|
|
117
177
|
return enabled;
|
|
118
178
|
}
|
|
179
|
+
/** An unconditional statement in the current shell, with no redirection to set up. */
|
|
180
|
+
function plainStatement(stmt) {
|
|
181
|
+
return !stmt.background && !stmt.disown && !stmt.negated && stmt.redirs.length === 0;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* mksh starts with no coprocess. A direct launch can supply a following read
|
|
185
|
+
* before an intervening command can replace or consume its pipe. Follow only
|
|
186
|
+
* the first command of a directly invoked function, and stop at opaque code.
|
|
187
|
+
*/
|
|
188
|
+
function provenMkshCoprocessReads(source) {
|
|
189
|
+
const reads = new Set();
|
|
190
|
+
const functions = new Map();
|
|
191
|
+
if (source.dialect !== 'mksh')
|
|
192
|
+
return reads;
|
|
193
|
+
function literalCall(stmt) {
|
|
194
|
+
if (stmt === undefined || !plainStatement(stmt) || stmt.cmd?.type !== 'CallExpr')
|
|
195
|
+
return undefined;
|
|
196
|
+
const call = stmt.cmd;
|
|
197
|
+
return call.assigns.length === 0 &&
|
|
198
|
+
call.args.every((word) => resolveWordTextAtPosition(source, word) !== null)
|
|
199
|
+
? call
|
|
200
|
+
: undefined;
|
|
201
|
+
}
|
|
202
|
+
let live = false;
|
|
203
|
+
for (const stmt of source.ast.stmts) {
|
|
204
|
+
if (!plainStatement(stmt))
|
|
205
|
+
break;
|
|
206
|
+
if (!stmt.coprocess && stmt.cmd?.type === 'FuncDecl') {
|
|
207
|
+
const { name, body } = stmt.cmd;
|
|
208
|
+
if (name === undefined || body === undefined)
|
|
209
|
+
break;
|
|
210
|
+
functions.set(name.value, body);
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
const call = literalCall(stmt);
|
|
214
|
+
if (call === undefined)
|
|
215
|
+
break;
|
|
216
|
+
if (stmt.coprocess) {
|
|
217
|
+
live = true;
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
const name = source.getCommandName(call);
|
|
221
|
+
if (name === null)
|
|
222
|
+
break;
|
|
223
|
+
const body = functions.get(name);
|
|
224
|
+
if (body === undefined || !plainStatement(body) || body.coprocess)
|
|
225
|
+
break;
|
|
226
|
+
const statements = body.cmd?.type === 'Block' ? body.cmd.stmts : [body];
|
|
227
|
+
const first = statements.at(0);
|
|
228
|
+
const startsCoprocess = (first?.coprocess ?? false) && literalCall(first) !== undefined;
|
|
229
|
+
const readStatement = statements[startsCoprocess ? 1 : 0];
|
|
230
|
+
const read = literalCall(readStatement);
|
|
231
|
+
if ((live || startsCoprocess) &&
|
|
232
|
+
read !== undefined &&
|
|
233
|
+
!readStatement.coprocess &&
|
|
234
|
+
source.getCommandName(read) === 'read' &&
|
|
235
|
+
!functions.has('read')) {
|
|
236
|
+
reads.add(read);
|
|
237
|
+
}
|
|
238
|
+
break;
|
|
239
|
+
}
|
|
240
|
+
return reads;
|
|
241
|
+
}
|
|
119
242
|
/**
|
|
120
243
|
* Every binding in `root`'s own scope, in source order. Nested function
|
|
121
|
-
* definitions and isolated scopes (subshells, substitutions
|
|
244
|
+
* definitions and isolated scopes (subshells, substitutions, background jobs)
|
|
245
|
+
* are not entered.
|
|
122
246
|
* Pipeline elements run in subshells, so only the last element of a pipeline
|
|
123
247
|
* is entered, and only when `lastpipe` is enabled.
|
|
124
248
|
*/
|
|
125
|
-
function scopeBindings(root, sourceCode, lastpipe) {
|
|
249
|
+
function scopeBindings(root, sourceCode, lastpipe, coprocessReads) {
|
|
126
250
|
const found = [];
|
|
127
251
|
// Pre-order with an explicit stack: children are pushed in reverse so they pop in source order.
|
|
128
252
|
const pending = [root];
|
|
129
253
|
for (let node = pending.pop(); node !== undefined; node = pending.pop()) {
|
|
130
254
|
if (node.type === 'FuncDecl' || ISOLATED_SCOPES.has(node.type))
|
|
131
255
|
continue;
|
|
256
|
+
if (node.type === 'Stmt' && (node.background || node.coprocess || node.disown))
|
|
257
|
+
continue;
|
|
132
258
|
if (node.type === 'BinaryCmd' && PIPE_OPS.has(node.op)) {
|
|
133
259
|
if (lastpipe && node.y !== undefined)
|
|
134
260
|
pending.push(node.y);
|
|
135
261
|
continue;
|
|
136
262
|
}
|
|
137
|
-
found.push(...bindingsOf(node, sourceCode));
|
|
263
|
+
found.push(...bindingsOf(node, sourceCode, coprocessReads));
|
|
138
264
|
const children = childNodes(node);
|
|
139
265
|
for (let index = children.length - 1; index >= 0; index--)
|
|
140
266
|
pending.push(children[index]);
|
|
@@ -149,7 +275,6 @@ const preferLocal = createRule({
|
|
|
149
275
|
docs: {
|
|
150
276
|
description: 'require variables assigned inside a function to be declared `local`, unless intentionally global',
|
|
151
277
|
},
|
|
152
|
-
fixable: 'code',
|
|
153
278
|
schema: [
|
|
154
279
|
{
|
|
155
280
|
type: 'object',
|
|
@@ -175,8 +300,8 @@ const preferLocal = createRule({
|
|
|
175
300
|
// `local` is not POSIX: under `posix` there is nothing to declare with.
|
|
176
301
|
if (sourceCode.dialect === 'posix')
|
|
177
302
|
return {};
|
|
178
|
-
const text = sourceCode.text;
|
|
179
303
|
const lastpipe = enablesLastpipe(sourceCode.ast, sourceCode);
|
|
304
|
+
const coprocessReads = provenMkshCoprocessReads(sourceCode);
|
|
180
305
|
// Names assigned or declared outside every function, with where each
|
|
181
306
|
// binding ends: assigned before a function is defined, they are globals
|
|
182
307
|
// the function is meant to share.
|
|
@@ -184,31 +309,11 @@ const preferLocal = createRule({
|
|
|
184
309
|
let topLevelCollected = false;
|
|
185
310
|
const topLevelBefore = (position) => {
|
|
186
311
|
if (!topLevelCollected) {
|
|
187
|
-
topLevelBindings.push(...scopeBindings(sourceCode.ast, sourceCode, lastpipe));
|
|
312
|
+
topLevelBindings.push(...scopeBindings(sourceCode.ast, sourceCode, lastpipe, coprocessReads));
|
|
188
313
|
topLevelCollected = true;
|
|
189
314
|
}
|
|
190
315
|
return new Set(topLevelBindings.filter((b) => b.node.range[1] <= position).map((b) => b.name));
|
|
191
316
|
};
|
|
192
|
-
/**
|
|
193
|
-
* The one `local` declaration that covers `names`, as `[index, text]`, or
|
|
194
|
-
* `null` when the function has no `{ … }` block with a first statement to
|
|
195
|
-
* insert before. Every report of a function carries this same insertion
|
|
196
|
-
* (with the function's whole name list), because fixes that insert at one
|
|
197
|
-
* offset conflict: ESLint applies one per pass and stops after 10, so a
|
|
198
|
-
* function with more than 10 names would otherwise never settle.
|
|
199
|
-
*/
|
|
200
|
-
const insertionFor = (func, names) => {
|
|
201
|
-
const block = func.body?.cmd;
|
|
202
|
-
if (block?.type !== 'Block')
|
|
203
|
-
return null;
|
|
204
|
-
const first = block.stmts.at(0);
|
|
205
|
-
if (first === undefined)
|
|
206
|
-
return null;
|
|
207
|
-
const lineStart = Math.max(text.lastIndexOf('\n', first.range[0] - 1), text.lastIndexOf('\r', first.range[0] - 1));
|
|
208
|
-
const indent = text.slice(lineStart + 1, first.range[0]);
|
|
209
|
-
const separator = /^[ \t]*$/u.test(indent) ? lineBreakOf(text) + indent : '; ';
|
|
210
|
-
return [first.range[0], `local ${names.join(' ')}${separator}`];
|
|
211
|
-
};
|
|
212
317
|
return {
|
|
213
318
|
FuncDecl(func) {
|
|
214
319
|
const { body } = func;
|
|
@@ -217,8 +322,7 @@ const preferLocal = createRule({
|
|
|
217
322
|
const global = topLevelBefore(func.range[0]);
|
|
218
323
|
const declared = new Set();
|
|
219
324
|
const functionName = func.name?.value ?? '';
|
|
220
|
-
const
|
|
221
|
-
for (const binding of scopeBindings(body, sourceCode, lastpipe)) {
|
|
325
|
+
for (const binding of scopeBindings(body, sourceCode, lastpipe, coprocessReads)) {
|
|
222
326
|
const { name } = binding;
|
|
223
327
|
if (binding.kind !== 'assign') {
|
|
224
328
|
declared.add(name);
|
|
@@ -231,20 +335,10 @@ const preferLocal = createRule({
|
|
|
231
335
|
continue;
|
|
232
336
|
}
|
|
233
337
|
declared.add(name);
|
|
234
|
-
leaks.push(binding);
|
|
235
|
-
}
|
|
236
|
-
if (leaks.length === 0)
|
|
237
|
-
return;
|
|
238
|
-
const insertion = insertionFor(func, leaks.map((binding) => binding.name));
|
|
239
|
-
for (const binding of leaks) {
|
|
240
|
-
const { name } = binding;
|
|
241
338
|
context.report({
|
|
242
339
|
node: binding.node,
|
|
243
340
|
messageId: 'preferLocal',
|
|
244
341
|
data: { name, function: functionName },
|
|
245
|
-
fix: insertion === null
|
|
246
|
-
? null
|
|
247
|
-
: (fixer) => fixer.insertTextBeforeRange([insertion[0], insertion[0]], insertion[1]),
|
|
248
342
|
});
|
|
249
343
|
}
|
|
250
344
|
},
|
|
@@ -1,19 +1,6 @@
|
|
|
1
|
+
import { sourceHasBOM } from '../language/shell-language.js';
|
|
1
2
|
import { createRule } from './create-rule.js';
|
|
2
|
-
import { shebangShell } from './shebang-interpreter.js';
|
|
3
|
-
/**
|
|
4
|
-
* Interpreter basenames this rule accepts in a shebang. Any of them makes the
|
|
5
|
-
* file's interpreter explicit; which one is right for the configured dialect
|
|
6
|
-
* is a separate rule's question.
|
|
7
|
-
*/
|
|
8
|
-
const RECOGNIZED_SHELLS = new Set([
|
|
9
|
-
'bash',
|
|
10
|
-
'sh',
|
|
11
|
-
'dash',
|
|
12
|
-
'ksh',
|
|
13
|
-
'mksh',
|
|
14
|
-
'zsh',
|
|
15
|
-
'bats',
|
|
16
|
-
]);
|
|
3
|
+
import { kernelShebangLine, RECOGNIZED_SHELLS, shebangShell } from './shebang-interpreter.js';
|
|
17
4
|
/**
|
|
18
5
|
* The interpreter the autofix names for each configured dialect. `posix` has
|
|
19
6
|
* no shell of its own name, so it maps to `sh`.
|
|
@@ -27,6 +14,22 @@ const SHELL_FOR_DIALECT = {
|
|
|
27
14
|
};
|
|
28
15
|
/** The UTF-8 byte order mark as it appears in a decoded string. */
|
|
29
16
|
const BOM = '\uFEFF';
|
|
17
|
+
const WHITESPACE_ESCAPES = new Map([
|
|
18
|
+
[' ', '\\x20'],
|
|
19
|
+
['\t', '\\t'],
|
|
20
|
+
['\v', '\\v'],
|
|
21
|
+
['\f', '\\f'],
|
|
22
|
+
['\r', '\\r'],
|
|
23
|
+
]);
|
|
24
|
+
function displayShebang(text) {
|
|
25
|
+
const line = kernelShebangLine(text);
|
|
26
|
+
let end = line.length;
|
|
27
|
+
while (end > 0 && /[\p{White_Space}\uFEFF]/u.test(line[end - 1]))
|
|
28
|
+
end--;
|
|
29
|
+
const suffix = Array.from(line.slice(end), (character) => WHITESPACE_ESCAPES.get(character) ??
|
|
30
|
+
'\\u' + character.charCodeAt(0).toString(16).padStart(4, '0')).join('');
|
|
31
|
+
return line.slice(0, end).replaceAll('\r', '\\r') + suffix;
|
|
32
|
+
}
|
|
30
33
|
/** The shebang line the fix inserts for `dialect`, in the configured `style`. */
|
|
31
34
|
function shebangFor(dialect, style) {
|
|
32
35
|
const shell = SHELL_FOR_DIALECT[dialect];
|
|
@@ -75,25 +78,24 @@ export const requireShebang = createRule({
|
|
|
75
78
|
end: { line: 1, column: firstLine.length + 1 },
|
|
76
79
|
};
|
|
77
80
|
if (text.startsWith('#!')) {
|
|
78
|
-
const
|
|
79
|
-
const shell = shebangShell(shebangLine.slice('#!'.length));
|
|
81
|
+
const shell = shebangShell(text.slice('#!'.length));
|
|
80
82
|
if (shell === undefined || !RECOGNIZED_SHELLS.has(shell)) {
|
|
81
83
|
context.report({
|
|
82
84
|
loc,
|
|
83
85
|
messageId: 'unrecognizedInterpreter',
|
|
84
|
-
data: { shebang:
|
|
86
|
+
data: { shebang: displayShebang(text) },
|
|
85
87
|
});
|
|
86
88
|
}
|
|
87
89
|
return;
|
|
88
90
|
}
|
|
89
91
|
const shebang = shebangFor(sourceCode.dialect, style);
|
|
90
|
-
// Keep the file's own line-ending style: a CRLF file gets a CRLF shebang.
|
|
91
|
-
const lineBreak = /^[^\n]*\r\n/u.test(sourceCode.text) ? '\r\n' : '\n';
|
|
92
92
|
context.report({
|
|
93
93
|
loc,
|
|
94
94
|
messageId: 'missingShebang',
|
|
95
95
|
data: { shebang },
|
|
96
|
-
|
|
96
|
+
// ESLint strips the BOM from source coordinates. A range crossing
|
|
97
|
+
// zero tells its fixer to remove the BOM. The kernel needs LF here.
|
|
98
|
+
fix: (fixer) => fixer.replaceTextRange([sourceHasBOM(sourceCode) ? -1 : 0, 0], `${shebang}\n`),
|
|
97
99
|
});
|
|
98
100
|
},
|
|
99
101
|
};
|
|
@@ -3,9 +3,9 @@ export interface RequireStrictModeOptions {
|
|
|
3
3
|
/**
|
|
4
4
|
* Nonempty, unique long option names from the existing `set -o` domain.
|
|
5
5
|
* When absent, POSIX requires errexit/nounset and other dialects also require
|
|
6
|
-
* pipefail. The formatter, rather than consumer text, owns
|
|
6
|
+
* pipefail. The formatter, rather than consumer text, owns the inserted spelling.
|
|
7
7
|
*/
|
|
8
8
|
readonly requiredOptions?: readonly string[];
|
|
9
9
|
}
|
|
10
10
|
/** Built-in rule `sh/require-strict-mode`. @internal */
|
|
11
|
-
export declare const requireStrictMode: import("./define-shell-rule.js").ShellRule<[RequireStrictModeOptions], "missingStrictMode">;
|
|
11
|
+
export declare const requireStrictMode: import("./define-shell-rule.js").ShellRule<[RequireStrictModeOptions], "missingStrictMode" | "enableStrictMode">;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { createRule } from './create-rule.js';
|
|
2
2
|
import { SET_OPTION_NAMES, formatSetCommand, shebangOptionChanges } from './set-options.js';
|
|
3
|
-
import { enabledAfter, resolveStrictModePrologue,
|
|
4
|
-
import {
|
|
3
|
+
import { enabledAfter, resolveStrictModePrologue, setStatementArguments, } from './strict-mode-prologue.js';
|
|
4
|
+
import { shellLineBreakOf } from '../language/line-breaks.js';
|
|
5
5
|
/**
|
|
6
6
|
* Dialect-aware policy defaults. Bats runs under Bash; POSIX omits pipefail
|
|
7
7
|
* because older dash and BusyBox ash reject it. Explicit requirements win.
|
|
@@ -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,17 +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
|
-
|
|
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
|
+
: [],
|
|
83
95
|
});
|
|
84
96
|
},
|
|
85
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>;
|