@eslint-sh/eslint-plugin 0.8.0 → 0.9.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 +41 -34
- package/dist/api.d.ts +92 -0
- package/dist/api.js +83 -0
- package/dist/command-harvest/harvest-commands.d.ts +2 -2
- package/dist/command-harvest/harvest-commands.js +2 -2
- package/dist/configs/posix-portable.d.ts +6 -10
- package/dist/configs/posix-portable.js +4 -6
- package/dist/configuration-message.d.ts +14 -0
- package/dist/configuration-message.js +33 -0
- package/dist/declaration-conventions/get-declared-commands.d.ts +41 -0
- package/dist/declaration-conventions/{read-declared-commands.js → get-declared-commands.js} +43 -43
- package/dist/eslint-plugin-api.d.ts +1506 -0
- package/dist/eslint-plugin-syntax-analyze.d.ts +3 -3
- package/dist/eslint-plugin-syntax.d.ts +3 -4
- package/dist/eslint-plugin.d.ts +16 -1560
- package/dist/index.d.ts +14 -81
- package/dist/index.js +22 -76
- package/dist/language/command-resolution.d.ts +5 -5
- package/dist/language/command-resolution.js +11 -7
- package/dist/language/selector-classes.d.ts +7 -3
- package/dist/language/selector-classes.js +13 -4
- package/dist/language/shell-language-options.d.ts +3 -3
- package/dist/language/shell-language-options.js +12 -17
- package/dist/language/shell-language.d.ts +3 -1
- package/dist/language/shell-language.js +6 -11
- package/dist/language/shell-source-code.d.ts +59 -42
- package/dist/language/shell-source-code.js +86 -60
- package/dist/language/source-coordinates.d.ts +2 -0
- package/dist/language/source-coordinates.js +8 -4
- package/dist/language/syntax-view.js +19 -7
- package/dist/language/word-resolution.d.ts +2 -1
- package/dist/language/word-resolution.js +8 -3
- package/dist/rules/create-wrapper-rule.d.ts +1 -1
- package/dist/rules/create-wrapper-rule.js +11 -9
- package/dist/rules/define-shell-rule.js +1 -1
- package/dist/rules/no-masked-exit-status.d.ts +4 -3
- package/dist/rules/no-masked-exit-status.js +3 -2
- package/dist/rules/no-secret-env-reads.d.ts +2 -0
- package/dist/rules/no-secret-env-reads.js +13 -2
- package/dist/rules/no-secret-flags.d.ts +2 -0
- package/dist/rules/no-secret-flags.js +14 -3
- package/dist/rules/no-unchecked-cd.js +3 -2
- package/dist/rules/no-undeclared-commands.js +8 -3
- package/dist/rules/no-unused-declarations.js +6 -2
- package/dist/rules/prefer-local.d.ts +3 -2
- package/dist/rules/prefer-local.js +4 -2
- package/dist/rules/require-network-wrapper.d.ts +1 -1
- package/dist/rules/require-network-wrapper.js +11 -7
- package/dist/rules/require-strict-mode.d.ts +5 -46
- package/dist/rules/require-strict-mode.js +17 -107
- package/dist/rules/require-tty-guard.d.ts +2 -0
- package/dist/rules/require-tty-guard.js +8 -1
- package/dist/rules/resolve-declaration-convention.d.ts +9 -88
- package/dist/rules/resolve-declaration-convention.js +19 -71
- package/dist/rules/resolve-network-wrapper.d.ts +1 -2
- package/dist/rules/resolve-network-wrapper.js +18 -14
- package/dist/rules/resolve-transparent-wrappers.d.ts +2 -7
- package/dist/rules/resolve-transparent-wrappers.js +19 -9
- package/dist/rules/set-options.d.ts +1 -1
- package/dist/rules/set-options.js +1 -1
- package/dist/rules/strict-mode-prologue.d.ts +43 -0
- package/dist/rules/strict-mode-prologue.js +91 -0
- package/dist/rules/user-pattern.d.ts +4 -3
- package/dist/rules/user-pattern.js +10 -8
- package/dist/shell-dialect.d.ts +8 -0
- package/dist/shell-dialect.js +22 -0
- package/dist/syntax/analyze.d.ts +3 -3
- package/dist/syntax/analyze.js +3 -3
- package/dist/syntax/index.d.ts +3 -4
- package/dist/syntax/index.js +3 -4
- package/dist/version.js +2 -1
- package/docs/compat.md +5 -6
- package/docs/custom-rule-tutorial.md +22 -18
- package/docs/declaration-conventions.md +20 -23
- package/docs/error-codes.md +93 -0
- package/docs/examples/no-rm-rf-rule.mjs +8 -10
- package/docs/examples/sorted-declarations-rule.mjs +6 -9
- package/docs/examples/tsconfig.json +10 -0
- package/docs/rules/README.md +12 -4
- package/docs/rules/compat/no-nounset-empty-array.md +1 -1
- package/docs/rules/no-masked-exit-status.md +2 -2
- package/docs/rules/no-secret-env-reads.md +13 -3
- package/docs/rules/no-secret-flags.md +14 -5
- package/docs/rules/no-unchecked-cd.md +15 -2
- package/docs/rules/require-network-wrapper.md +5 -5
- package/docs/rules/require-strict-mode.md +25 -23
- package/docs/rules/require-tty-guard.md +12 -3
- package/docs/selectors.md +7 -2
- package/docs/wrapper-rules.md +2 -0
- package/package.json +3 -3
- package/dist/declaration-conventions/read-declared-commands.d.ts +0 -70
|
@@ -7,6 +7,7 @@ import type { ShellNodeOf } from './syntax-view.js';
|
|
|
7
7
|
* are rejected instead of being treated as known or unknown shell values.
|
|
8
8
|
* A static word that ends a line of a CRLF file resolves without the line
|
|
9
9
|
* terminator's carriage return, as it would on LF input.
|
|
10
|
-
* @
|
|
10
|
+
* @throws TypeError with `ESLINT_SH_WORD_NODE` for a word without original syntax.
|
|
11
|
+
* @internal
|
|
11
12
|
*/
|
|
12
13
|
export declare function resolveWord(word: ShellNodeOf<'Word'>, options?: ResolveWordOptions): WordResolution;
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* Bridges lint-word identity to faithful syntax for source-code methods.
|
|
3
|
+
* ESLINT_SH_WORD_NODE rejects a missing source association; dynamic words
|
|
4
|
+
* remain ordinary analysis results rather than errors.
|
|
5
|
+
*/
|
|
2
6
|
import { resolveWord as resolveSyntaxWord } from 'sh-ast/analyze';
|
|
3
7
|
import { syntaxForNode, withoutCrlfCarriageReturn } from './syntax-view.js';
|
|
4
8
|
/**
|
|
@@ -8,12 +12,13 @@ import { syntaxForNode, withoutCrlfCarriageReturn } from './syntax-view.js';
|
|
|
8
12
|
* are rejected instead of being treated as known or unknown shell values.
|
|
9
13
|
* A static word that ends a line of a CRLF file resolves without the line
|
|
10
14
|
* terminator's carriage return, as it would on LF input.
|
|
11
|
-
* @
|
|
15
|
+
* @throws TypeError with `ESLINT_SH_WORD_NODE` for a word without original syntax.
|
|
16
|
+
* @internal
|
|
12
17
|
*/
|
|
13
18
|
export function resolveWord(word, options) {
|
|
14
19
|
const syntax = syntaxForNode(word);
|
|
15
20
|
if (syntax?.Type !== 'Word') {
|
|
16
|
-
throw new TypeError('resolveWord expects a parsed shell Word from this language adapter');
|
|
21
|
+
throw Object.assign(new TypeError('resolveWord expects a parsed shell Word from this language adapter'), { code: 'ESLINT_SH_WORD_NODE' });
|
|
17
22
|
}
|
|
18
23
|
return withoutCrlfCarriageReturn(syntax, resolveSyntaxWord(syntax, options));
|
|
19
24
|
}
|
|
@@ -157,7 +157,7 @@ export declare function buildWrapperRule<RuleOptions extends unknown[] = []>(con
|
|
|
157
157
|
* subcommand restrictions, and optional `meta.docs` overrides.
|
|
158
158
|
* @returns A {@link ShellRule}: an ESLint rule for the `sh/sh` language,
|
|
159
159
|
* typed so a pack's own declarations need no `@eslint/core` import to name it.
|
|
160
|
-
* @throws TypeError when `config` is malformed (no `commands` array, an empty
|
|
160
|
+
* @throws TypeError with `ESLINT_SH_WRAPPER_CONFIG` when `config` is malformed (no `commands` array, an empty
|
|
161
161
|
* wrapper name, a subcommand restriction without `names`, or an empty or
|
|
162
162
|
* non-string `docs.url` / `docs.description`).
|
|
163
163
|
*
|
|
@@ -36,6 +36,7 @@
|
|
|
36
36
|
* static. Commands inside command strings (`bash -c '…'`) are not commands to
|
|
37
37
|
* this analysis and are out of scope.
|
|
38
38
|
*/
|
|
39
|
+
import { configurationMessage } from '../configuration-message.js';
|
|
39
40
|
import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-ast/analyze';
|
|
40
41
|
import { resolveTransparentWrappers } from './resolve-transparent-wrappers.js';
|
|
41
42
|
import { definedFunctionNames } from '../command-harvest/harvest-commands.js';
|
|
@@ -91,14 +92,15 @@ function isStringArray(value) {
|
|
|
91
92
|
* offending field, so a malformed binding fails when a rule pack is loaded
|
|
92
93
|
* rather than in the middle of linting a file.
|
|
93
94
|
*/
|
|
95
|
+
// ESLINT_SH_WRAPPER_CONFIG rejects factory bindings before any rule state is created.
|
|
94
96
|
function compileWrapperRule(config) {
|
|
95
97
|
const { commands, wrapper, subcommands = {}, localInvocations = {}, docs = {}, } = config;
|
|
96
98
|
if (!isStringArray(commands)) {
|
|
97
|
-
throw new TypeError('createWrapperRule
|
|
99
|
+
throw Object.assign(new TypeError(configurationMessage('createWrapperRule config.commands', 'must be an array of non-empty strings', commands)), { code: 'ESLINT_SH_WRAPPER_CONFIG' });
|
|
98
100
|
}
|
|
99
101
|
for (const field of ['url', 'description']) {
|
|
100
102
|
if (docs[field] !== undefined && !isNonEmptyString(docs[field])) {
|
|
101
|
-
throw new TypeError(`createWrapperRule
|
|
103
|
+
throw Object.assign(new TypeError(configurationMessage(`createWrapperRule config.docs.${field}`, 'must be a non-empty string', docs[field])), { code: 'ESLINT_SH_WRAPPER_CONFIG' });
|
|
102
104
|
}
|
|
103
105
|
}
|
|
104
106
|
let wrapperSpec;
|
|
@@ -113,13 +115,13 @@ function compileWrapperRule(config) {
|
|
|
113
115
|
wrapperSpec = wrapper;
|
|
114
116
|
}
|
|
115
117
|
else {
|
|
116
|
-
throw new TypeError('createWrapperRule
|
|
118
|
+
throw Object.assign(new TypeError(configurationMessage('createWrapperRule config.wrapper', 'must be a non-empty string or a WrapperSpec with non-empty `names`', wrapper)), { code: 'ESLINT_SH_WRAPPER_CONFIG' });
|
|
117
119
|
}
|
|
118
120
|
const subcommandSpecs = [];
|
|
119
121
|
const subcommandNames = new Map();
|
|
120
122
|
for (const [command, restriction] of Object.entries(subcommands)) {
|
|
121
123
|
if (!isStringArray(restriction.names) || restriction.names.length === 0) {
|
|
122
|
-
throw new TypeError(`createWrapperRule
|
|
124
|
+
throw Object.assign(new TypeError(configurationMessage(`createWrapperRule config.subcommands.${command}.names`, 'must be a non-empty array of non-empty strings', restriction.names)), { code: 'ESLINT_SH_WRAPPER_CONFIG' });
|
|
123
125
|
}
|
|
124
126
|
subcommandNames.set(command, new Set(restriction.names));
|
|
125
127
|
// Modeling the command as a transparent wrapper makes `resolveArgv0`
|
|
@@ -135,18 +137,18 @@ function compileWrapperRule(config) {
|
|
|
135
137
|
for (const [label, local] of Object.entries(localInvocations)) {
|
|
136
138
|
const where = `localInvocations.${label}`;
|
|
137
139
|
if (!isRecord(local)) {
|
|
138
|
-
throw new TypeError(`createWrapperRule
|
|
140
|
+
throw Object.assign(new TypeError(configurationMessage(`createWrapperRule config.${where}`, 'must be an object', local)), { code: 'ESLINT_SH_WRAPPER_CONFIG' });
|
|
139
141
|
}
|
|
140
142
|
const { argLists, localOperand } = local;
|
|
141
143
|
if (argLists !== undefined &&
|
|
142
144
|
!(Array.isArray(argLists) &&
|
|
143
145
|
argLists.every((list) => Array.isArray(list) && isStringArray(list)))) {
|
|
144
|
-
throw new TypeError(`createWrapperRule
|
|
146
|
+
throw Object.assign(new TypeError(configurationMessage(`createWrapperRule config.${where}.argLists`, 'must be an array of arrays of non-empty strings', argLists)), { code: 'ESLINT_SH_WRAPPER_CONFIG' });
|
|
145
147
|
}
|
|
146
148
|
if (localOperand === undefined)
|
|
147
149
|
continue;
|
|
148
150
|
if (!isRecord(localOperand)) {
|
|
149
|
-
throw new TypeError(`createWrapperRule
|
|
151
|
+
throw Object.assign(new TypeError(configurationMessage(`createWrapperRule config.${where}.localOperand`, 'must be an object', localOperand)), { code: 'ESLINT_SH_WRAPPER_CONFIG' });
|
|
150
152
|
}
|
|
151
153
|
for (const field of ['localPrefixes', 'noArgFlags', 'argFlags']) {
|
|
152
154
|
const value = localOperand[field];
|
|
@@ -154,7 +156,7 @@ function compileWrapperRule(config) {
|
|
|
154
156
|
? isStringArray(value)
|
|
155
157
|
: value === undefined || isStringArray(value);
|
|
156
158
|
if (!valid) {
|
|
157
|
-
throw new TypeError(`createWrapperRule
|
|
159
|
+
throw Object.assign(new TypeError(configurationMessage(`createWrapperRule config.${where}.localOperand.${field}`, 'must be an array of non-empty strings', value)), { code: 'ESLINT_SH_WRAPPER_CONFIG' });
|
|
158
160
|
}
|
|
159
161
|
}
|
|
160
162
|
}
|
|
@@ -401,7 +403,7 @@ export function buildWrapperRule(config) {
|
|
|
401
403
|
* subcommand restrictions, and optional `meta.docs` overrides.
|
|
402
404
|
* @returns A {@link ShellRule}: an ESLint rule for the `sh/sh` language,
|
|
403
405
|
* typed so a pack's own declarations need no `@eslint/core` import to name it.
|
|
404
|
-
* @throws TypeError when `config` is malformed (no `commands` array, an empty
|
|
406
|
+
* @throws TypeError with `ESLINT_SH_WRAPPER_CONFIG` when `config` is malformed (no `commands` array, an empty
|
|
405
407
|
* wrapper name, a subcommand restriction without `names`, or an empty or
|
|
406
408
|
* non-string `docs.url` / `docs.description`).
|
|
407
409
|
*
|
|
@@ -43,7 +43,7 @@ export function defineShellRule(spec) {
|
|
|
43
43
|
// The type requires `meta`, but ESLint accepts a rule without one, so an
|
|
44
44
|
// untyped (JavaScript) caller's missing `meta` is read as empty rather than
|
|
45
45
|
// crashing. The types cannot see that caller, hence the suppression.
|
|
46
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
46
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- Untyped JavaScript callers may omit the required metadata.
|
|
47
47
|
const meta = spec.meta ?? {};
|
|
48
48
|
// Drop the deprecated singular key an untyped caller may have supplied, so
|
|
49
49
|
// the only language declaration ESLint sees is the one set here.
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @internal
|
|
5
5
|
*/
|
|
6
|
-
|
|
6
|
+
interface NoMaskedExitStatusOptions {
|
|
7
7
|
/** Report only where `errexit` is known to be on. Defaults to `false`. */
|
|
8
8
|
readonly onlyWithErrexit?: boolean;
|
|
9
9
|
/**
|
|
@@ -16,6 +16,7 @@ export interface NoMaskedExitStatusOptions {
|
|
|
16
16
|
readonly builtins?: readonly string[];
|
|
17
17
|
}
|
|
18
18
|
/** Message ids this rule reports with. @internal */
|
|
19
|
-
|
|
19
|
+
type NoMaskedExitStatusMessageId = 'maskedExitStatus' | 'declareThenAssign' | 'assignThenExport' | 'assignThenReadonly';
|
|
20
20
|
/** Built-in rule `sh/no-masked-exit-status`. @internal */
|
|
21
|
-
|
|
21
|
+
declare const noMaskedExitStatus: import("./define-shell-rule.js").ShellRule<[NoMaskedExitStatusOptions], NoMaskedExitStatusMessageId>;
|
|
22
|
+
export { noMaskedExitStatus };
|
|
@@ -90,7 +90,7 @@ import { childNodes } from '../language/syntax-view.js';
|
|
|
90
90
|
import { lineBreakOf } from '../language/line-breaks.js';
|
|
91
91
|
import { createRule } from './create-rule.js';
|
|
92
92
|
import { batsErrexitIsOn, isStatusChecked } from './no-unchecked-cd.js';
|
|
93
|
-
import { setStatementChanges } from './
|
|
93
|
+
import { setStatementChanges } from './strict-mode-prologue.js';
|
|
94
94
|
import { applySetOptionChanges, shebangOptionChanges } from './set-options.js';
|
|
95
95
|
import { BUILTIN_WRAPPER } from './transparent-wrappers.js';
|
|
96
96
|
/** Every declaration builtin the rule can check; the default for `builtins`. */
|
|
@@ -535,7 +535,7 @@ function splitText(declaration, scoped, separator, functionNames, sourceCode, tr
|
|
|
535
535
|
};
|
|
536
536
|
}
|
|
537
537
|
/** Built-in rule `sh/no-masked-exit-status`. @internal */
|
|
538
|
-
|
|
538
|
+
const noMaskedExitStatus = createRule({
|
|
539
539
|
name: 'no-masked-exit-status',
|
|
540
540
|
meta: {
|
|
541
541
|
type: 'problem',
|
|
@@ -672,3 +672,4 @@ export const noMaskedExitStatus = createRule({
|
|
|
672
672
|
};
|
|
673
673
|
},
|
|
674
674
|
});
|
|
675
|
+
export { noMaskedExitStatus };
|
|
@@ -10,6 +10,8 @@ export interface NoSecretEnvReadsOptions {
|
|
|
10
10
|
* {@link DEFAULT_PATTERNS} entirely.
|
|
11
11
|
*/
|
|
12
12
|
readonly patterns?: readonly string[];
|
|
13
|
+
/** Regex sources added to `patterns`, or to the built-in defaults when it is omitted. */
|
|
14
|
+
readonly additionalPatterns?: readonly string[];
|
|
13
15
|
/**
|
|
14
16
|
* Command names whose own arguments may read a secret-shaped variable. Wrapper-owned operands are still checked normally. A command
|
|
15
17
|
* run by path matches by basename.
|
|
@@ -71,12 +71,17 @@ export const noSecretEnvReads = createRule({
|
|
|
71
71
|
{
|
|
72
72
|
type: 'object',
|
|
73
73
|
properties: {
|
|
74
|
+
additionalPatterns: {
|
|
75
|
+
type: 'array',
|
|
76
|
+
// Validate with the rule's actual Unicode/case flags in create(),
|
|
77
|
+
// so syntax and safety errors identify the original option and source.
|
|
78
|
+
items: { type: 'string' },
|
|
79
|
+
},
|
|
74
80
|
patterns: {
|
|
75
81
|
type: 'array',
|
|
76
82
|
// The shared compiler validates Unicode syntax and identifies
|
|
77
83
|
// the option index and source; flagless schema checks cannot.
|
|
78
84
|
items: { type: 'string' },
|
|
79
|
-
minItems: 1,
|
|
80
85
|
},
|
|
81
86
|
allowedSinks: {
|
|
82
87
|
type: 'array',
|
|
@@ -93,7 +98,13 @@ export const noSecretEnvReads = createRule({
|
|
|
93
98
|
},
|
|
94
99
|
create(context) {
|
|
95
100
|
const options = context.options[0];
|
|
96
|
-
|
|
101
|
+
// An explicit replacement, including [], owns the base policy. Additions
|
|
102
|
+
// extend that base per file; compile separately to retain each option's identity.
|
|
103
|
+
// Diagnostic rule paths follow the configured ID, including plugin registration aliases.
|
|
104
|
+
const patterns = [
|
|
105
|
+
...(options?.patterns ?? DEFAULT_PATTERNS).map((source, index) => compileUserPattern(context.id, `patterns[${String(index)}]`, source, 'u')),
|
|
106
|
+
...(options?.additionalPatterns ?? []).map((source, index) => compileUserPattern(context.id, `additionalPatterns[${String(index)}]`, source, 'u')),
|
|
107
|
+
];
|
|
97
108
|
const allowedSinks = new Set(options?.allowedSinks ?? []);
|
|
98
109
|
const { sourceCode } = context;
|
|
99
110
|
const transparentWrappers = resolveTransparentWrappers(context);
|
|
@@ -11,6 +11,8 @@ export interface NoSecretFlagsOptions {
|
|
|
11
11
|
* Replaces {@link DEFAULT_PATTERNS} entirely.
|
|
12
12
|
*/
|
|
13
13
|
readonly patterns?: readonly string[];
|
|
14
|
+
/** Regex sources added to `patterns`, or to the built-in defaults when it is omitted. */
|
|
15
|
+
readonly additionalPatterns?: readonly string[];
|
|
14
16
|
}
|
|
15
17
|
/**
|
|
16
18
|
* One secret-shaped flag found in a command's arguments.
|
|
@@ -95,12 +95,17 @@ export const noSecretFlags = createRule({
|
|
|
95
95
|
{
|
|
96
96
|
type: 'object',
|
|
97
97
|
properties: {
|
|
98
|
+
additionalPatterns: {
|
|
99
|
+
type: 'array',
|
|
100
|
+
// Validate with the rule's actual Unicode/case flags in create(),
|
|
101
|
+
// so syntax and safety errors identify the original option and source.
|
|
102
|
+
items: { type: 'string' },
|
|
103
|
+
},
|
|
98
104
|
patterns: {
|
|
99
105
|
type: 'array',
|
|
100
106
|
// Syntax is checked with the actual flags by compileUserPattern,
|
|
101
107
|
// which also names the option index and source in errors.
|
|
102
108
|
items: { type: 'string' },
|
|
103
|
-
minItems: 1,
|
|
104
109
|
},
|
|
105
110
|
},
|
|
106
111
|
additionalProperties: false,
|
|
@@ -111,8 +116,14 @@ export const noSecretFlags = createRule({
|
|
|
111
116
|
},
|
|
112
117
|
},
|
|
113
118
|
create(context) {
|
|
114
|
-
const
|
|
115
|
-
|
|
119
|
+
const options = context.options[0];
|
|
120
|
+
// An explicit replacement, including [], owns the base policy. Additions
|
|
121
|
+
// extend that base per file; compile separately to retain each option's identity.
|
|
122
|
+
// Diagnostic rule paths follow the configured ID, including plugin registration aliases.
|
|
123
|
+
const patterns = [
|
|
124
|
+
...(options?.patterns ?? DEFAULT_PATTERNS).map((source, index) => compileUserPattern(context.id, `patterns[${String(index)}]`, source, PATTERN_FLAGS)),
|
|
125
|
+
...(options?.additionalPatterns ?? []).map((source, index) => compileUserPattern(context.id, `additionalPatterns[${String(index)}]`, source, PATTERN_FLAGS)),
|
|
126
|
+
];
|
|
116
127
|
const isSecretFlag = (name) => patterns.some((pattern) => pattern.test(name));
|
|
117
128
|
const { sourceCode } = context;
|
|
118
129
|
/** Finds every secret-shaped flag with a literal value among `args` (argv, command word first). */
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-ast/analyze';
|
|
51
51
|
import { resolveTransparentWrappers } from './resolve-transparent-wrappers.js';
|
|
52
52
|
import { createRule } from './create-rule.js';
|
|
53
|
-
import { prologueEnabledOptions, setStatementChanges } from './
|
|
53
|
+
import { prologueEnabledOptions, resolveStrictModePrologue, setStatementChanges, } from './strict-mode-prologue.js';
|
|
54
54
|
import { BUILTIN_WRAPPER } from './transparent-wrappers.js';
|
|
55
55
|
/** The commands that change the working directory and can fail doing so. */
|
|
56
56
|
const DIRECTORY_COMMANDS = new Set(['cd', 'pushd', 'popd']);
|
|
@@ -203,13 +203,14 @@ export const noUncheckedCd = createRule({
|
|
|
203
203
|
create(context) {
|
|
204
204
|
const [options] = context.options;
|
|
205
205
|
const { sourceCode } = context;
|
|
206
|
+
const prologue = resolveStrictModePrologue(context.settings);
|
|
206
207
|
const transparentWrappers = resolveTransparentWrappers(context, TRANSPARENT_WRAPPERS);
|
|
207
208
|
let errexitInForce = false;
|
|
208
209
|
return {
|
|
209
210
|
File(node) {
|
|
210
211
|
errexitInForce =
|
|
211
212
|
options.ignoreWhenErrexit !== false &&
|
|
212
|
-
prologueEnabledOptions(node, sourceCode).has('errexit');
|
|
213
|
+
prologueEnabledOptions(node, sourceCode, prologue).has('errexit');
|
|
213
214
|
},
|
|
214
215
|
CallExpr(node) {
|
|
215
216
|
if (errexitInForce)
|
|
@@ -28,10 +28,11 @@
|
|
|
28
28
|
* convention's own words when it has a `declarationHint` hook, since only the
|
|
29
29
|
* convention knows its syntax.
|
|
30
30
|
*/
|
|
31
|
+
import { configurationMessage } from '../configuration-message.js';
|
|
31
32
|
import { harvestCommands } from '../command-harvest/harvest-commands.js';
|
|
32
33
|
import { harvestWrapperTable } from '../command-harvest/wrapper-table.js';
|
|
33
34
|
import { createRule } from './create-rule.js';
|
|
34
|
-
import {
|
|
35
|
+
import { getDeclaredCommands, toLintedFile, } from '../declaration-conventions/get-declared-commands.js';
|
|
35
36
|
import { resolveDeclarationConvention } from './resolve-declaration-convention.js';
|
|
36
37
|
import { resolveTransparentWrappers } from './resolve-transparent-wrappers.js';
|
|
37
38
|
/** The registry name of this rule (`sh/no-undeclared-commands` in a user's config). */
|
|
@@ -51,7 +52,7 @@ function howToDeclare(convention, command, sourceCode, file) {
|
|
|
51
52
|
if (hint === null)
|
|
52
53
|
return GENERIC_HINT;
|
|
53
54
|
if (typeof hint !== 'string' || hint.trim() === '' || /[\r\n\u2028\u2029]/u.test(hint)) {
|
|
54
|
-
throw new Error(
|
|
55
|
+
throw Object.assign(new Error(configurationMessage('settings.sh.declarationConvention.declarationHint', `must return null or a non-empty single-line string for ${JSON.stringify(command)}`, hint)), { code: 'ESLINT_SH_DECLARATION_OUTPUT' });
|
|
55
56
|
}
|
|
56
57
|
return hint;
|
|
57
58
|
}
|
|
@@ -82,7 +83,11 @@ export const noUndeclaredCommands = createRule({
|
|
|
82
83
|
return {
|
|
83
84
|
File() {
|
|
84
85
|
// Only names matter here, so every kind of declaration counts the same.
|
|
85
|
-
const
|
|
86
|
+
const declarations = getDeclaredCommands(context);
|
|
87
|
+
if (declarations === null) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const declared = new Set(declarations.map(({ name }) => name));
|
|
86
91
|
for (const { name, node } of harvestCommands(sourceCode, {
|
|
87
92
|
transparentWrappers,
|
|
88
93
|
}).values()) {
|
|
@@ -2,7 +2,7 @@ import { resolveTransparentWrappers } from './resolve-transparent-wrappers.js';
|
|
|
2
2
|
import { harvestCommands } from '../command-harvest/harvest-commands.js';
|
|
3
3
|
import { harvestWrapperTable } from '../command-harvest/wrapper-table.js';
|
|
4
4
|
import { createRule } from './create-rule.js';
|
|
5
|
-
import {
|
|
5
|
+
import { getDeclaredCommands } from '../declaration-conventions/get-declared-commands.js';
|
|
6
6
|
import { resolveDeclarationConvention } from './resolve-declaration-convention.js';
|
|
7
7
|
import { SHELL_WRAPPER } from './transparent-wrappers.js';
|
|
8
8
|
/** The registry name of this rule (`sh/no-unused-declarations` in a user's config). */
|
|
@@ -204,7 +204,11 @@ export const noUnusedDeclarations = createRule({
|
|
|
204
204
|
used.add(each);
|
|
205
205
|
}
|
|
206
206
|
}
|
|
207
|
-
|
|
207
|
+
const declarations = getDeclaredCommands(context);
|
|
208
|
+
if (declarations === null) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
for (const declared of declarations) {
|
|
208
212
|
if (used.has(declared.name))
|
|
209
213
|
continue;
|
|
210
214
|
if (declared.kind === 'script') {
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @internal
|
|
5
5
|
*/
|
|
6
|
-
|
|
6
|
+
interface PreferLocalOptions {
|
|
7
7
|
/** Variable names that may be assigned in a function without `local`. */
|
|
8
8
|
readonly allow?: readonly string[];
|
|
9
9
|
/**
|
|
@@ -13,4 +13,5 @@ export interface PreferLocalOptions {
|
|
|
13
13
|
readonly allowPattern?: string;
|
|
14
14
|
}
|
|
15
15
|
/** Built-in rule `sh/prefer-local`. @internal */
|
|
16
|
-
|
|
16
|
+
declare const preferLocal: import("./define-shell-rule.js").ShellRule<[PreferLocalOptions], "preferLocal">;
|
|
17
|
+
export { preferLocal };
|
|
@@ -140,7 +140,7 @@ function scopeBindings(root, sourceCode, lastpipe) {
|
|
|
140
140
|
return found.sort((a, b) => a.node.range[0] - b.node.range[0]);
|
|
141
141
|
}
|
|
142
142
|
/** Built-in rule `sh/prefer-local`. @internal */
|
|
143
|
-
|
|
143
|
+
const preferLocal = createRule({
|
|
144
144
|
name: 'prefer-local',
|
|
145
145
|
meta: {
|
|
146
146
|
type: 'suggestion',
|
|
@@ -167,7 +167,8 @@ export const preferLocal = createRule({
|
|
|
167
167
|
const { sourceCode } = context;
|
|
168
168
|
const [options] = context.options;
|
|
169
169
|
const allowed = new Set(options.allow ?? []);
|
|
170
|
-
|
|
170
|
+
// The configured ID owns option paths, independently of the rule's registry name.
|
|
171
|
+
const allowPattern = compileUserPattern(context.id, 'allowPattern', options.allowPattern ?? DEFAULT_ALLOW_PATTERN, 'u');
|
|
171
172
|
// Validate configuration in every dialect, even where the rule is inert.
|
|
172
173
|
// `local` is not POSIX: under `posix` there is nothing to declare with.
|
|
173
174
|
if (sourceCode.dialect === 'posix')
|
|
@@ -248,3 +249,4 @@ export const preferLocal = createRule({
|
|
|
248
249
|
};
|
|
249
250
|
},
|
|
250
251
|
});
|
|
252
|
+
export { preferLocal };
|
|
@@ -15,7 +15,7 @@ export interface RequireNetworkWrapperOptions {
|
|
|
15
15
|
*/
|
|
16
16
|
readonly wrapper?: string | WrapperSpec;
|
|
17
17
|
/** Commands checked in addition to `commands` (or its default). */
|
|
18
|
-
readonly
|
|
18
|
+
readonly additionalCommands?: readonly string[];
|
|
19
19
|
}
|
|
20
20
|
/**
|
|
21
21
|
* The `sh/require-network-wrapper` rule module.
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
* `clone`, `pull`, `push`), found after git's own global options
|
|
15
15
|
* (`git -C dir fetch`).
|
|
16
16
|
*/
|
|
17
|
+
import { configurationMessage } from '../configuration-message.js';
|
|
17
18
|
import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-ast/analyze';
|
|
18
19
|
import { createRule } from './create-rule.js';
|
|
19
20
|
import { WRAPPER_RULE_MESSAGES, buildWrapperRule } from './create-wrapper-rule.js';
|
|
@@ -88,7 +89,7 @@ const GIT_NETWORK_SUBCOMMANDS = {
|
|
|
88
89
|
/**
|
|
89
90
|
* The subcommand restrictions this rule always applies: `git` counts only for
|
|
90
91
|
* its network subcommands, whether it comes from the default `commands`, a
|
|
91
|
-
* configured `commands`, or `
|
|
92
|
+
* configured `commands`, or `additionalCommands`.
|
|
92
93
|
*/
|
|
93
94
|
const NETWORK_SUBCOMMANDS = {
|
|
94
95
|
git: GIT_NETWORK_SUBCOMMANDS,
|
|
@@ -206,9 +207,10 @@ const WRAPPER_SPEC_SCHEMA = {
|
|
|
206
207
|
};
|
|
207
208
|
/**
|
|
208
209
|
* Resolves the factory config this rule enforces for one set of options, or
|
|
209
|
-
* `null` when no wrapper is configured.
|
|
210
|
+
* `null` when no wrapper is configured. The configured rule ID names local
|
|
211
|
+
* options; shared project facts always retain the canonical settings.sh path.
|
|
210
212
|
*/
|
|
211
|
-
function wrapperRuleConfig(options, settings) {
|
|
213
|
+
function wrapperRuleConfig(options, settings, ruleId) {
|
|
212
214
|
const shared = readNetworkWrapperSetting(settings);
|
|
213
215
|
if (options?.wrapper !== undefined && shared !== undefined) {
|
|
214
216
|
const configured = options.wrapper;
|
|
@@ -219,16 +221,18 @@ function wrapperRuleConfig(options, settings) {
|
|
|
219
221
|
: configured;
|
|
220
222
|
const network = networkWrapperSpec(settings);
|
|
221
223
|
if (network !== null && !sameWrapperSpec(option, network)) {
|
|
222
|
-
throw new Error(
|
|
224
|
+
throw Object.assign(new Error(configurationMessage(`rules[${JSON.stringify(ruleId)}].wrapper`, 'conflicts with settings.sh.networkWrapper; configure one shared network policy', configured)), { code: 'ESLINT_SH_WRAPPER_CONFLICT' });
|
|
223
225
|
}
|
|
224
226
|
}
|
|
225
227
|
const wrapper = shared ?? options?.wrapper;
|
|
226
228
|
if (wrapper === undefined)
|
|
227
229
|
return null;
|
|
228
230
|
return {
|
|
231
|
+
// An explicit replacement (even []) selects the base; additions extend it.
|
|
232
|
+
// The factory normalizes names to a set, so overlap never multiplies reports.
|
|
229
233
|
commands: [
|
|
230
234
|
...(options?.commands ?? DEFAULT_NETWORK_COMMANDS),
|
|
231
|
-
...(options?.
|
|
235
|
+
...(options?.additionalCommands ?? []),
|
|
232
236
|
],
|
|
233
237
|
wrapper,
|
|
234
238
|
subcommands: NETWORK_SUBCOMMANDS,
|
|
@@ -253,7 +257,7 @@ export const requireNetworkWrapper = createRule({
|
|
|
253
257
|
properties: {
|
|
254
258
|
commands: STRING_LIST_SCHEMA,
|
|
255
259
|
wrapper: { oneOf: [{ type: 'string', minLength: 1 }, WRAPPER_SPEC_SCHEMA] },
|
|
256
|
-
|
|
260
|
+
additionalCommands: STRING_LIST_SCHEMA,
|
|
257
261
|
},
|
|
258
262
|
additionalProperties: false,
|
|
259
263
|
},
|
|
@@ -261,7 +265,7 @@ export const requireNetworkWrapper = createRule({
|
|
|
261
265
|
messages: WRAPPER_RULE_MESSAGES,
|
|
262
266
|
},
|
|
263
267
|
create(context) {
|
|
264
|
-
const config = wrapperRuleConfig(context.options[0], context.settings);
|
|
268
|
+
const config = wrapperRuleConfig(context.options[0], context.settings, context.id);
|
|
265
269
|
if (config === null)
|
|
266
270
|
return {};
|
|
267
271
|
// The check itself is the rule `createWrapperRule` makes for this config
|
|
@@ -1,52 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
import type { ShellSourceCode } from '../language/shell-source-code.js';
|
|
3
|
-
import type { SetOptionChange } from './set-options.js';
|
|
4
|
-
/**
|
|
5
|
-
* The rule's single options object.
|
|
6
|
-
*
|
|
7
|
-
* @internal
|
|
8
|
-
*/
|
|
1
|
+
/** The policy's required options; prologue recognition is a shared project fact. @internal */
|
|
9
2
|
export interface RequireStrictModeOptions {
|
|
10
3
|
/**
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* (`["errexit", "nounset"]`), for which the autofix inserts a `set` command
|
|
15
|
-
* spelled from that list. When omitted, it is `"set -eu"` for a file
|
|
16
|
-
* whose dialect is `posix` and `"set -euo pipefail"` for every other
|
|
17
|
-
* dialect.
|
|
4
|
+
* Nonempty, unique long option names from the existing `set -o` domain.
|
|
5
|
+
* When absent, POSIX requires errexit/nounset and other dialects also require
|
|
6
|
+
* pipefail. The formatter, rather than consumer text, owns autofix spelling.
|
|
18
7
|
*/
|
|
19
|
-
readonly
|
|
20
|
-
/**
|
|
21
|
-
* When `true`, options passed to the shell on the shebang line
|
|
22
|
-
* (`#!/bin/bash -e`, `#!/usr/bin/env -S bash -euo pipefail`) count as
|
|
23
|
-
* enabled, so a shebang alone can satisfy the rule. Off by default, because
|
|
24
|
-
* those options are ignored when the script is run as `bash script.sh`.
|
|
25
|
-
*/
|
|
26
|
-
readonly allowShebangOnly?: boolean;
|
|
27
|
-
/** How many top-level statements, from the top, the prologue may take to appear. */
|
|
28
|
-
readonly withinStatements?: number;
|
|
8
|
+
readonly requiredOptions?: readonly string[];
|
|
29
9
|
}
|
|
30
|
-
/**
|
|
31
|
-
* Whether `stmt` is a plain, foreground, top-level `set` command, and if so
|
|
32
|
-
* the option changes its arguments make (read through `sourceCode`, so a
|
|
33
|
-
* non-static argument ends the reading).
|
|
34
|
-
*
|
|
35
|
-
* @internal
|
|
36
|
-
*/
|
|
37
|
-
export declare function setStatementChanges(stmt: ShellNodeOf<'Stmt'>, sourceCode: ShellSourceCode): readonly SetOptionChange[];
|
|
38
|
-
/**
|
|
39
|
-
* The options a file's prologue leaves enabled: the state after the first
|
|
40
|
-
* `withinStatements` top-level statements of `file`, plus the shebang's
|
|
41
|
-
* options when `allowShebangOnly` is set. This is the single definition of
|
|
42
|
-
* "what the prologue enables", shared with other rules (such as
|
|
43
|
-
* `no-unchecked-cd`) that need to know whether strict mode is in force.
|
|
44
|
-
*
|
|
45
|
-
* @internal
|
|
46
|
-
*/
|
|
47
|
-
export declare function prologueEnabledOptions(file: ShellFile, sourceCode: ShellSourceCode, config?: {
|
|
48
|
-
readonly allowShebangOnly?: boolean;
|
|
49
|
-
readonly withinStatements?: number;
|
|
50
|
-
}): ReadonlySet<string>;
|
|
51
10
|
/** Built-in rule `sh/require-strict-mode`. @internal */
|
|
52
11
|
export declare const requireStrictMode: import("./define-shell-rule.js").ShellRule<[RequireStrictModeOptions], "missingStrictMode">;
|