@eslint-sh/eslint-plugin 0.8.0 → 0.10.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 +58 -57
- package/dist/api.d.ts +92 -0
- package/dist/api.js +83 -0
- package/dist/command-harvest/builtins/bats.d.ts +1 -1
- package/dist/command-harvest/harvest-commands.d.ts +18 -7
- package/dist/command-harvest/harvest-commands.js +11 -10
- package/dist/command-harvest/wrapper-table.d.ts +1 -1
- package/dist/command-harvest/wrapper-table.js +1 -1
- 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 +1516 -0
- 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 +47 -13
- package/dist/language/command-resolution.js +48 -18
- 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 +77 -77
- package/dist/language/shell-source-code.js +107 -98
- package/dist/language/source-coordinates.d.ts +2 -0
- package/dist/language/source-coordinates.js +8 -4
- package/dist/language/syntax-view.d.ts +1 -1
- package/dist/language/syntax-view.js +19 -7
- package/dist/language/word-resolution.d.ts +4 -3
- package/dist/language/word-resolution.js +10 -5
- package/dist/rules/compat/declaration-options.js +2 -2
- package/dist/rules/compat/no-brace-sequence-padding-step.js +1 -1
- package/dist/rules/compat/no-mapfile.js +2 -2
- package/dist/rules/compat/no-negative-array-index.js +2 -2
- package/dist/rules/compat/no-printf-time-format.js +2 -2
- package/dist/rules/compat/no-test-v.js +2 -2
- package/dist/rules/compat/no-wait-n.js +2 -2
- package/dist/rules/compat/posix-absent-command.js +2 -2
- package/dist/rules/compat/shopt-options.js +2 -2
- package/dist/rules/create-wrapper-rule.d.ts +4 -4
- package/dist/rules/create-wrapper-rule.js +19 -13
- package/dist/rules/define-shell-rule.js +1 -1
- package/dist/rules/no-dynamic-invocation.d.ts +1 -1
- package/dist/rules/no-dynamic-invocation.js +12 -4
- package/dist/rules/no-eval.js +6 -2
- package/dist/rules/no-masked-exit-status.d.ts +4 -3
- package/dist/rules/no-masked-exit-status.js +4 -3
- package/dist/rules/no-secret-env-reads.d.ts +2 -0
- package/dist/rules/no-secret-env-reads.js +14 -3
- 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 +4 -3
- package/dist/rules/no-undeclared-commands.js +10 -4
- package/dist/rules/no-unused-declarations.js +13 -4
- package/dist/rules/prefer-local.d.ts +3 -2
- package/dist/rules/prefer-local.js +4 -2
- package/dist/rules/quote-expansions.js +1 -1
- package/dist/rules/require-network-wrapper.d.ts +2 -2
- package/dist/rules/require-network-wrapper.js +21 -11
- 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 +9 -2
- 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 +3 -4
- package/dist/rules/resolve-network-wrapper.js +23 -19
- package/dist/rules/resolve-transparent-wrappers.d.ts +3 -8
- package/dist/rules/resolve-transparent-wrappers.js +21 -11
- 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/transparent-wrappers.d.ts +3 -3
- package/dist/rules/transparent-wrappers.js +1 -1
- 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/version.js +2 -1
- package/docs/compat.md +5 -6
- package/docs/custom-rule-tutorial.md +55 -51
- package/docs/declaration-conventions.md +20 -23
- package/docs/error-codes.md +94 -0
- package/docs/examples/no-backticks-rule.mjs +1 -1
- package/docs/examples/no-curl-in-functions-rule.mjs +2 -2
- package/docs/examples/no-listed-commands-rule.ts +1 -1
- 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-dynamic-invocation.md +5 -0
- 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/no-undeclared-commands.md +4 -0
- package/docs/rules/no-unused-declarations.md +4 -0
- package/docs/rules/require-network-wrapper.md +6 -6
- 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 +3 -1
- package/package.json +9 -20
- package/dist/declaration-conventions/read-declared-commands.d.ts +0 -70
- package/dist/eslint-plugin-syntax-analyze.d.ts +0 -23
- package/dist/eslint-plugin-syntax.d.ts +0 -31
- package/dist/syntax/analyze.d.ts +0 -19
- package/dist/syntax/analyze.js +0 -21
- package/dist/syntax/index.d.ts +0 -27
- package/dist/syntax/index.js +0 -29
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { WrapperSpec } from 'sh-
|
|
1
|
+
import type { WrapperSpec } from 'sh-analysis';
|
|
2
2
|
/**
|
|
3
3
|
* Checks a configured value and returns it typed: a non-empty command name,
|
|
4
4
|
* or a `WrapperSpec` object made of the JSON-expressible fields.
|
|
@@ -7,8 +7,7 @@ import type { WrapperSpec } from 'sh-ast/analyze';
|
|
|
7
7
|
export declare function validateWrapperSetting(value: unknown, setting: string): string | WrapperSpec;
|
|
8
8
|
/**
|
|
9
9
|
* Reads `settings.sh.networkWrapper`: the configured name or `WrapperSpec`,
|
|
10
|
-
* or `undefined` when it is not set. Throws
|
|
11
|
-
* `Invalid settings.sh.networkWrapper:` when the value is malformed. Call it
|
|
10
|
+
* or `undefined` when it is not set. Throws `Error` with `ESLINT_SH_WRAPPER_SETTING` and the offending path when the value is malformed. Call it
|
|
12
11
|
* from `create()`, so the error surfaces as ESLint's configuration message.
|
|
13
12
|
*
|
|
14
13
|
* @internal
|
|
@@ -17,7 +16,7 @@ export declare function readNetworkWrapperSetting(settings: Readonly<Record<stri
|
|
|
17
16
|
/**
|
|
18
17
|
* The `WrapperSpec` for the network wrapper set in `settings.sh.networkWrapper`,
|
|
19
18
|
* for a rule that looks through wrappers, or `null` when none is set. A name
|
|
20
|
-
* that is one of `sh-
|
|
19
|
+
* that is one of `sh-analysis`'s default wrappers (`timeout`) keeps that
|
|
21
20
|
* entry's known options; any other name is a wrapper with no options.
|
|
22
21
|
*
|
|
23
22
|
* @internal
|
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
* Reads the shared network policy and validates JSON-expressible wrapper specs.
|
|
3
3
|
* A network wrapper also supplies a transparent-wrapper fact to every consumer.
|
|
4
4
|
*/
|
|
5
|
-
import {
|
|
5
|
+
import { configurationMessage } from '../configuration-message.js';
|
|
6
|
+
import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-analysis';
|
|
6
7
|
/** The setting's dotted path, as error messages name it. */
|
|
7
8
|
const SETTING = 'settings.sh.networkWrapper';
|
|
8
9
|
/**
|
|
@@ -30,48 +31,51 @@ function isStringList(value) {
|
|
|
30
31
|
*/
|
|
31
32
|
export function validateWrapperSetting(value, setting) {
|
|
32
33
|
/** Names the configuration binding instead of exposing an analysis failure. */
|
|
33
|
-
function invalid(detail) {
|
|
34
|
-
|
|
34
|
+
function invalid(detail, field) {
|
|
35
|
+
// The setting owns malformed JSON specs; analysis errors keep their own identity.
|
|
36
|
+
const where = field === undefined ? setting : `${setting}.${field}`;
|
|
37
|
+
const received = field === undefined ? value : value[field];
|
|
38
|
+
throw Object.assign(new Error(configurationMessage(where, detail, received)), {
|
|
39
|
+
code: 'ESLINT_SH_WRAPPER_SETTING',
|
|
40
|
+
});
|
|
35
41
|
}
|
|
36
42
|
if (typeof value === 'string') {
|
|
37
43
|
if (value.length === 0)
|
|
38
|
-
invalid('expected a command name or a wrapper spec
|
|
44
|
+
invalid('expected a command name or a wrapper spec');
|
|
39
45
|
return value;
|
|
40
46
|
}
|
|
41
47
|
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
42
|
-
|
|
43
|
-
invalid(`expected a command name or a wrapper spec ({ names: [...] }), but got ${got}.`);
|
|
48
|
+
invalid('expected a command name or a wrapper spec ({ names: [...] })');
|
|
44
49
|
}
|
|
45
50
|
const spec = value;
|
|
46
51
|
for (const key of Object.keys(spec)) {
|
|
47
52
|
if (!KNOWN_FIELDS.has(key))
|
|
48
|
-
invalid(
|
|
53
|
+
invalid('unknown wrapper spec field', key);
|
|
49
54
|
}
|
|
50
55
|
if (!isStringList(spec.names) || spec.names.length === 0) {
|
|
51
|
-
invalid('
|
|
56
|
+
invalid('must be a non-empty array of non-empty strings', 'names');
|
|
52
57
|
}
|
|
53
58
|
for (const field of STRING_LIST_FIELDS) {
|
|
54
59
|
if (spec[field] !== undefined && !isStringList(spec[field])) {
|
|
55
|
-
invalid(
|
|
60
|
+
invalid('must be an array of non-empty strings', field);
|
|
56
61
|
}
|
|
57
62
|
}
|
|
58
63
|
if (spec.shellBuiltin !== undefined && typeof spec.shellBuiltin !== 'boolean') {
|
|
59
|
-
invalid('
|
|
64
|
+
invalid('must be a boolean', 'shellBuiltin');
|
|
60
65
|
}
|
|
61
66
|
const positional = spec.positionalOperandsBeforeCommand;
|
|
62
67
|
if (positional !== undefined && !(Number.isInteger(positional) && positional >= 0)) {
|
|
63
|
-
invalid('
|
|
68
|
+
invalid('must be a non-negative integer', 'positionalOperandsBeforeCommand');
|
|
64
69
|
}
|
|
65
70
|
const assignment = spec.assignmentOperands;
|
|
66
71
|
if (assignment !== undefined && !ASSIGNMENT_OPERANDS.includes(assignment)) {
|
|
67
|
-
invalid(
|
|
72
|
+
invalid(`must be one of ${ASSIGNMENT_OPERANDS.join(', ')}`, 'assignmentOperands');
|
|
68
73
|
}
|
|
69
74
|
return spec;
|
|
70
75
|
}
|
|
71
76
|
/**
|
|
72
77
|
* Reads `settings.sh.networkWrapper`: the configured name or `WrapperSpec`,
|
|
73
|
-
* or `undefined` when it is not set. Throws
|
|
74
|
-
* `Invalid settings.sh.networkWrapper:` when the value is malformed. Call it
|
|
78
|
+
* or `undefined` when it is not set. Throws `Error` with `ESLINT_SH_WRAPPER_SETTING` and the offending path when the value is malformed. Call it
|
|
75
79
|
* from `create()`, so the error surfaces as ESLint's configuration message.
|
|
76
80
|
*
|
|
77
81
|
* @internal
|
|
@@ -86,7 +90,7 @@ export function readNetworkWrapperSetting(settings) {
|
|
|
86
90
|
/**
|
|
87
91
|
* The `WrapperSpec` for the network wrapper set in `settings.sh.networkWrapper`,
|
|
88
92
|
* for a rule that looks through wrappers, or `null` when none is set. A name
|
|
89
|
-
* that is one of `sh-
|
|
93
|
+
* that is one of `sh-analysis`'s default wrappers (`timeout`) keeps that
|
|
90
94
|
* entry's known options; any other name is a wrapper with no options.
|
|
91
95
|
*
|
|
92
96
|
* @internal
|
|
@@ -102,11 +106,11 @@ export function networkWrapperSpec(settings) {
|
|
|
102
106
|
});
|
|
103
107
|
}
|
|
104
108
|
/**
|
|
105
|
-
* Canonical effective grammar from sh-
|
|
109
|
+
* Canonical effective grammar from sh-analysis's WrapperSpec matcher, excluding
|
|
106
110
|
* the names that select it. Flags are membership sets; omitted lists are empty,
|
|
107
111
|
* shellBuiltin defaults to false (utility matching), and positional count
|
|
108
|
-
* defaults to zero.
|
|
109
|
-
*
|
|
112
|
+
* defaults to zero. Assignment operands require an explicit grammar; absence
|
|
113
|
+
* means every operand remains available as a command candidate.
|
|
110
114
|
*
|
|
111
115
|
* Expanded built-ins may contain a flag pattern even though JSON settings may
|
|
112
116
|
* not. Compare its source and flags structurally, including absence, rather
|
|
@@ -123,7 +127,7 @@ function canonicalWrapperGrammar(spec) {
|
|
|
123
127
|
stopsChainFlags: flags(spec.stopsChainFlags),
|
|
124
128
|
shellBuiltin: spec.shellBuiltin ?? false,
|
|
125
129
|
positionalOperandsBeforeCommand: spec.positionalOperandsBeforeCommand ?? 0,
|
|
126
|
-
assignmentOperands: spec.assignmentOperands ??
|
|
130
|
+
assignmentOperands: spec.assignmentOperands ?? null,
|
|
127
131
|
noArgFlagPattern: pattern === undefined ? null : { source: pattern.source, flags: pattern.flags },
|
|
128
132
|
});
|
|
129
133
|
}
|
|
@@ -1,11 +1,5 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Resolves project wrapper facts once per consuming rule. Shared facts precede
|
|
3
|
-
* the rule's built-in table, so an explicit option shape replaces the default
|
|
4
|
-
* for that name. Rules retain their distinct built-in scopes, but never their
|
|
5
|
-
* own interpretation of a configured project wrapper.
|
|
6
|
-
*/
|
|
7
1
|
import type { SettingsConfig } from '@eslint/core';
|
|
8
|
-
import type { WrapperSpec } from 'sh-
|
|
2
|
+
import type { WrapperSpec } from 'sh-analysis';
|
|
9
3
|
import type { ShellSourceCode } from '../language/shell-source-code.js';
|
|
10
4
|
/** The rule-context fields needed to resolve shared wrappers and their path spellings. @public */
|
|
11
5
|
export interface TransparentWrappersBindingContext {
|
|
@@ -37,7 +31,8 @@ export interface TransparentWrappersBindingContext {
|
|
|
37
31
|
*
|
|
38
32
|
* @param context - The consuming rule's context.
|
|
39
33
|
* @param builtIns - The rule's own built-in wrapper scope.
|
|
40
|
-
* @throws Error
|
|
34
|
+
* @throws Error with `ESLINT_SH_WRAPPER_SETTING` for malformed bindings, or
|
|
35
|
+
* `ESLINT_SH_WRAPPER_CONFLICT` for duplicate names or contradictory shared policies.
|
|
41
36
|
* @public
|
|
42
37
|
*/
|
|
43
38
|
export declare function resolveTransparentWrappers(context: TransparentWrappersBindingContext, builtIns?: readonly WrapperSpec[]): readonly WrapperSpec[];
|
|
@@ -1,7 +1,16 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Resolves project wrapper facts once per consuming rule. Shared facts precede
|
|
3
|
+
* the rule's built-in table, so an explicit option shape replaces the default
|
|
4
|
+
* for that name. Rules retain their distinct built-in scopes, but never their
|
|
5
|
+
* own interpretation of a configured project wrapper.
|
|
6
|
+
*
|
|
7
|
+
* ESLINT_SH_WRAPPER_SETTING rejects malformed bindings; ESLINT_SH_WRAPPER_CONFLICT rejects contradictory shared facts instead of choosing a policy by list order.
|
|
8
|
+
*/
|
|
9
|
+
import { configurationMessage } from '../configuration-message.js';
|
|
10
|
+
import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-analysis';
|
|
2
11
|
import { networkWrapperSpec, sameWrapperOptions, validateWrapperSetting, } from './resolve-network-wrapper.js';
|
|
3
|
-
/** The
|
|
4
|
-
const SETTING = 'settings
|
|
12
|
+
/** The user-written setting path, extended with indices and fields in configuration errors. */
|
|
13
|
+
const SETTING = 'settings.sh.transparentWrappers';
|
|
5
14
|
/** A name-only entry preserves a known utility's default flag handling. */
|
|
6
15
|
function asSpec(value) {
|
|
7
16
|
if (typeof value !== 'string') {
|
|
@@ -32,7 +41,8 @@ function asSpec(value) {
|
|
|
32
41
|
*
|
|
33
42
|
* @param context - The consuming rule's context.
|
|
34
43
|
* @param builtIns - The rule's own built-in wrapper scope.
|
|
35
|
-
* @throws Error
|
|
44
|
+
* @throws Error with `ESLINT_SH_WRAPPER_SETTING` for malformed bindings, or
|
|
45
|
+
* `ESLINT_SH_WRAPPER_CONFLICT` for duplicate names or contradictory shared policies.
|
|
36
46
|
* @public
|
|
37
47
|
*/
|
|
38
48
|
export function resolveTransparentWrappers(context, builtIns = DEFAULT_TRANSPARENT_WRAPPERS) {
|
|
@@ -41,24 +51,24 @@ export function resolveTransparentWrappers(context, builtIns = DEFAULT_TRANSPARE
|
|
|
41
51
|
? sh.transparentWrappers
|
|
42
52
|
: undefined;
|
|
43
53
|
if (value !== undefined && !Array.isArray(value)) {
|
|
44
|
-
throw new Error(
|
|
54
|
+
throw Object.assign(new Error(configurationMessage(SETTING, 'expected an array of command names or wrapper specs', value)), { code: 'ESLINT_SH_WRAPPER_SETTING' });
|
|
45
55
|
}
|
|
46
|
-
const configured = (value ?? []).map((entry) => asSpec(validateWrapperSetting(entry, SETTING)));
|
|
56
|
+
const configured = (value ?? []).map((entry, index) => asSpec(validateWrapperSetting(entry, `${SETTING}[${String(index)}]`)));
|
|
47
57
|
const names = new Set();
|
|
48
|
-
for (const spec of configured) {
|
|
58
|
+
for (const [index, spec] of configured.entries()) {
|
|
49
59
|
for (const name of spec.names) {
|
|
50
60
|
if (names.has(name)) {
|
|
51
|
-
throw new Error(
|
|
61
|
+
throw Object.assign(new Error(configurationMessage(`${SETTING}[${String(index)}]`, `duplicate wrapper name ${JSON.stringify(name)}`, value?.[index])), { code: 'ESLINT_SH_WRAPPER_CONFLICT' });
|
|
52
62
|
}
|
|
53
63
|
names.add(name);
|
|
54
64
|
}
|
|
55
65
|
}
|
|
56
66
|
const network = networkWrapperSpec(context.settings);
|
|
57
|
-
for (const spec of configured) {
|
|
67
|
+
for (const [index, spec] of configured.entries()) {
|
|
58
68
|
if (network !== null &&
|
|
59
69
|
spec.names.some((name) => network.names.includes(name)) &&
|
|
60
70
|
!sameWrapperOptions(spec, network)) {
|
|
61
|
-
throw new Error(
|
|
71
|
+
throw Object.assign(new Error(configurationMessage(`${SETTING}[${String(index)}]`, 'wrapper spec conflicts with settings.sh.networkWrapper', value?.[index])), { code: 'ESLINT_SH_WRAPPER_CONFLICT' });
|
|
62
72
|
}
|
|
63
73
|
}
|
|
64
74
|
// Precedence applies to names, including their path-matching semantics.
|
|
@@ -79,7 +89,7 @@ export function resolveTransparentWrappers(context, builtIns = DEFAULT_TRANSPARE
|
|
|
79
89
|
// Relative utility paths need exact aliases in sh-ast. Derive them only from
|
|
80
90
|
// static argv words in this file; no path is resolved or executed here.
|
|
81
91
|
const paths = new Map();
|
|
82
|
-
for (const site of context.sourceCode.
|
|
92
|
+
for (const site of context.sourceCode.getCommandInventory().commands) {
|
|
83
93
|
for (const word of site.argv) {
|
|
84
94
|
if (!word.static || !word.text.includes('/') || paths.has(word.text)) {
|
|
85
95
|
continue;
|
|
@@ -90,7 +90,7 @@ export declare function applySetOptionChanges(enabled: Set<string>, changes: rea
|
|
|
90
90
|
export declare function formatSetCommand(options: readonly string[]): string;
|
|
91
91
|
/**
|
|
92
92
|
* The option changes the shebang line passes to the shell (read by
|
|
93
|
-
*
|
|
93
|
+
* the shared prologue definition and by
|
|
94
94
|
* `compat/no-nounset-empty-array`). Only spellings the kernel hands to the shell as written
|
|
95
95
|
* are read:
|
|
96
96
|
*
|
|
@@ -159,7 +159,7 @@ export function formatSetCommand(options) {
|
|
|
159
159
|
}
|
|
160
160
|
/**
|
|
161
161
|
* The option changes the shebang line passes to the shell (read by
|
|
162
|
-
*
|
|
162
|
+
* the shared prologue definition and by
|
|
163
163
|
* `compat/no-nounset-empty-array`). Only spellings the kernel hands to the shell as written
|
|
164
164
|
* are read:
|
|
165
165
|
*
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { SettingsConfig } from '@eslint/core';
|
|
2
|
+
import type { ShellFile, ShellNodeOf } from '../language/syntax-view.js';
|
|
3
|
+
import type { ShellSourceCode } from '../language/shell-source-code.js';
|
|
4
|
+
import type { SetOptionChange } from './set-options.js';
|
|
5
|
+
/** The normalized project definition, independent of any required option list. @internal */
|
|
6
|
+
export interface StrictModePrologue {
|
|
7
|
+
/** Whether interpreter flags count, despite being bypassed by `bash script.sh`. */
|
|
8
|
+
readonly allowShebangOnly: boolean;
|
|
9
|
+
/** Positive number of top-level statements whose final option state counts. */
|
|
10
|
+
readonly withinStatements: number;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Normalizes the shared project fact at rule creation, following the other
|
|
14
|
+
* shared-settings resolvers: absence uses defaults, malformed explicit values
|
|
15
|
+
* throw a named configuration error. No cache can leak a file's configuration.
|
|
16
|
+
* @throws Error with `ESLINT_SH_STRICT_MODE_SETTING` for malformed shared project facts.
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
export declare function resolveStrictModePrologue(settings: SettingsConfig): StrictModePrologue;
|
|
20
|
+
/**
|
|
21
|
+
* Whether `stmt` is a plain, foreground, top-level `set` command, and if so
|
|
22
|
+
* the option changes its arguments make (read through `sourceCode`, so a
|
|
23
|
+
* non-static argument ends the reading).
|
|
24
|
+
*
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
export declare function setStatementChanges(stmt: ShellNodeOf<'Stmt'>, sourceCode: ShellSourceCode): readonly SetOptionChange[];
|
|
28
|
+
/**
|
|
29
|
+
* The options enabled after applying `seed`, then each statement's `set`
|
|
30
|
+
* changes in order.
|
|
31
|
+
* @internal
|
|
32
|
+
*/
|
|
33
|
+
export declare function enabledAfter(seed: readonly SetOptionChange[], statementChanges: readonly (readonly SetOptionChange[])[]): Set<string>;
|
|
34
|
+
/**
|
|
35
|
+
* The options a file's prologue leaves enabled: the state after the first
|
|
36
|
+
* `withinStatements` top-level statements of `file`, plus the shebang's
|
|
37
|
+
* options when `allowShebangOnly` is set. This is the single definition of
|
|
38
|
+
* "what the prologue enables", shared with other rules (such as
|
|
39
|
+
* `no-unchecked-cd`) that need to know whether strict mode is in force.
|
|
40
|
+
*
|
|
41
|
+
* @internal
|
|
42
|
+
*/
|
|
43
|
+
export declare function prologueEnabledOptions(file: ShellFile, sourceCode: ShellSourceCode, config: StrictModePrologue): ReadonlySet<string>;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shared definition of where a script establishes shell options. Policy
|
|
3
|
+
* rules consume the same normalized project fact, without owning one another's
|
|
4
|
+
* requirements or retaining state across configs or files.
|
|
5
|
+
* ESLINT_SH_STRICT_MODE_SETTING identifies a malformed project prologue fact;
|
|
6
|
+
* rule-option schema failures remain ESLint responsibilities.
|
|
7
|
+
*/
|
|
8
|
+
import { configurationMessage } from '../configuration-message.js';
|
|
9
|
+
import { applySetOptionChanges, parseSetArguments, shebangOptionChanges } from './set-options.js';
|
|
10
|
+
/** The user-written path of the shared project fact, extended with its offending field. */
|
|
11
|
+
const SETTING = 'settings.sh.strictModePrologue';
|
|
12
|
+
/**
|
|
13
|
+
* Normalizes the shared project fact at rule creation, following the other
|
|
14
|
+
* shared-settings resolvers: absence uses defaults, malformed explicit values
|
|
15
|
+
* throw a named configuration error. No cache can leak a file's configuration.
|
|
16
|
+
* @throws Error with `ESLINT_SH_STRICT_MODE_SETTING` for malformed shared project facts.
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
export function resolveStrictModePrologue(settings) {
|
|
20
|
+
const sh = settings.sh;
|
|
21
|
+
const value = typeof sh === 'object' && sh !== null && 'strictModePrologue' in sh
|
|
22
|
+
? sh.strictModePrologue
|
|
23
|
+
: undefined;
|
|
24
|
+
if (value === undefined) {
|
|
25
|
+
return { allowShebangOnly: false, withinStatements: 5 };
|
|
26
|
+
}
|
|
27
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
28
|
+
throw Object.assign(new Error(configurationMessage(SETTING, 'expected a prologue definition object', value)), { code: 'ESLINT_SH_STRICT_MODE_SETTING' });
|
|
29
|
+
}
|
|
30
|
+
for (const key of Object.keys(value)) {
|
|
31
|
+
if (key !== 'allowShebangOnly' && key !== 'withinStatements') {
|
|
32
|
+
throw Object.assign(new Error(configurationMessage(`${SETTING}.${key}`, 'unknown prologue definition field', value[key])), { code: 'ESLINT_SH_STRICT_MODE_SETTING' });
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const allowShebangOnly = 'allowShebangOnly' in value ? value.allowShebangOnly : undefined;
|
|
36
|
+
const withinStatements = 'withinStatements' in value ? value.withinStatements : undefined;
|
|
37
|
+
if (allowShebangOnly !== undefined && typeof allowShebangOnly !== 'boolean') {
|
|
38
|
+
throw Object.assign(new Error(configurationMessage(`${SETTING}.allowShebangOnly`, 'must be a boolean', allowShebangOnly)), { code: 'ESLINT_SH_STRICT_MODE_SETTING' });
|
|
39
|
+
}
|
|
40
|
+
if (withinStatements !== undefined &&
|
|
41
|
+
(typeof withinStatements !== 'number' ||
|
|
42
|
+
!Number.isInteger(withinStatements) ||
|
|
43
|
+
withinStatements < 1)) {
|
|
44
|
+
throw Object.assign(new Error(configurationMessage(`${SETTING}.withinStatements`, 'must be a positive integer', withinStatements)), { code: 'ESLINT_SH_STRICT_MODE_SETTING' });
|
|
45
|
+
}
|
|
46
|
+
return { allowShebangOnly: allowShebangOnly ?? false, withinStatements: withinStatements ?? 5 };
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Whether `stmt` is a plain, foreground, top-level `set` command, and if so
|
|
50
|
+
* the option changes its arguments make (read through `sourceCode`, so a
|
|
51
|
+
* non-static argument ends the reading).
|
|
52
|
+
*
|
|
53
|
+
* @internal
|
|
54
|
+
*/
|
|
55
|
+
export function setStatementChanges(stmt, sourceCode) {
|
|
56
|
+
if (stmt.background) {
|
|
57
|
+
return [];
|
|
58
|
+
}
|
|
59
|
+
const { cmd } = stmt;
|
|
60
|
+
if (cmd?.type !== 'CallExpr' || sourceCode.getCommandName(cmd) !== 'set') {
|
|
61
|
+
return [];
|
|
62
|
+
}
|
|
63
|
+
const args = cmd.args.slice(1);
|
|
64
|
+
return parseSetArguments(args.map((word) => sourceCode.resolveWordText(word))).changes;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* The options enabled after applying `seed`, then each statement's `set`
|
|
68
|
+
* changes in order.
|
|
69
|
+
* @internal
|
|
70
|
+
*/
|
|
71
|
+
export function enabledAfter(seed, statementChanges) {
|
|
72
|
+
const enabled = applySetOptionChanges(new Set(), seed);
|
|
73
|
+
for (const changes of statementChanges) {
|
|
74
|
+
applySetOptionChanges(enabled, changes);
|
|
75
|
+
}
|
|
76
|
+
return enabled;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The options a file's prologue leaves enabled: the state after the first
|
|
80
|
+
* `withinStatements` top-level statements of `file`, plus the shebang's
|
|
81
|
+
* options when `allowShebangOnly` is set. This is the single definition of
|
|
82
|
+
* "what the prologue enables", shared with other rules (such as
|
|
83
|
+
* `no-unchecked-cd`) that need to know whether strict mode is in force.
|
|
84
|
+
*
|
|
85
|
+
* @internal
|
|
86
|
+
*/
|
|
87
|
+
export function prologueEnabledOptions(file, sourceCode, config) {
|
|
88
|
+
const shebangChanges = config.allowShebangOnly ? shebangOptionChanges(sourceCode.text) : [];
|
|
89
|
+
const statements = file.stmts.slice(0, config.withinStatements);
|
|
90
|
+
return enabledAfter(shebangChanges, statements.map((stmt) => setStatementChanges(stmt, sourceCode)));
|
|
91
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Wrapper data that built-in rules add to `sh-
|
|
2
|
+
* Wrapper data that built-in rules add to `sh-analysis`'s
|
|
3
3
|
* `DEFAULT_TRANSPARENT_WRAPPERS` when they ask
|
|
4
4
|
* `ShellSourceCode.resolveInvokedCommand` which command an invocation runs.
|
|
5
5
|
*
|
|
@@ -7,14 +7,14 @@
|
|
|
7
7
|
* find the word a wrapper runs. No rule logic lives here, and no entry makes
|
|
8
8
|
* a safety judgment. Each rule decides which of these entries it follows.
|
|
9
9
|
*/
|
|
10
|
-
import type { WrapperSpec } from 'sh-
|
|
10
|
+
import type { WrapperSpec } from 'sh-analysis';
|
|
11
11
|
/**
|
|
12
12
|
* Bash's `builtin [shell-builtin [args …]]`: runs the named builtin, bypassing
|
|
13
13
|
* any function of the same name. It takes no options of its own, so the word
|
|
14
14
|
* after it is the command it runs. Only the exact builtin name has these
|
|
15
15
|
* semantics; an executable path with the same basename is an ordinary program.
|
|
16
16
|
*
|
|
17
|
-
* `sh-
|
|
17
|
+
* `sh-analysis`'s default wrapper table leaves `builtin` out, so
|
|
18
18
|
* `builtin "$x"` would otherwise resolve to the static command `builtin`.
|
|
19
19
|
*
|
|
20
20
|
* @see https://www.gnu.org/software/bash/manual/bash.html#Bash-Builtins — `builtin`
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* after it is the command it runs. Only the exact builtin name has these
|
|
5
5
|
* semantics; an executable path with the same basename is an ordinary program.
|
|
6
6
|
*
|
|
7
|
-
* `sh-
|
|
7
|
+
* `sh-analysis`'s default wrapper table leaves `builtin` out, so
|
|
8
8
|
* `builtin "$x"` would otherwise resolve to the static command `builtin`.
|
|
9
9
|
*
|
|
10
10
|
* @see https://www.gnu.org/software/bash/manual/bash.html#Bash-Builtins — `builtin`
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
*
|
|
30
30
|
* @internal
|
|
31
31
|
*/
|
|
32
|
-
|
|
32
|
+
interface NestedRepetition {
|
|
33
33
|
/** The outer repeating quantifier as written, such as `*` or `{2,}`. */
|
|
34
34
|
readonly outer: string;
|
|
35
35
|
/** The text of the group the outer quantifier repeats, such as `([A-Z]+_?)`. */
|
|
@@ -45,7 +45,7 @@ export interface NestedRepetition {
|
|
|
45
45
|
*
|
|
46
46
|
* @internal
|
|
47
47
|
*/
|
|
48
|
-
|
|
48
|
+
declare function findNestedRepetition(source: string): NestedRepetition | undefined;
|
|
49
49
|
/**
|
|
50
50
|
* Compiles a regular-expression source from a rule option that will be tested
|
|
51
51
|
* against script-controlled text. Throws a `TypeError` naming the rule and the
|
|
@@ -54,7 +54,7 @@ export declare function findNestedRepetition(source: string): NestedRepetition |
|
|
|
54
54
|
* the error from the rule's `create()`, so it surfaces on the first file linted
|
|
55
55
|
* with the rule ("Occurred while linting <file>"), not when the config loads.
|
|
56
56
|
*
|
|
57
|
-
* @param rule - The rule
|
|
57
|
+
* @param rule - The configured rule ID (context.id), including the caller's plugin namespace.
|
|
58
58
|
* @param option - The option as the user wrote it, such as `patterns[0]`.
|
|
59
59
|
* @param source - The regular-expression source.
|
|
60
60
|
* @param flags - Rule-specific flags. Unicode semantics are always added;
|
|
@@ -63,3 +63,4 @@ export declare function findNestedRepetition(source: string): NestedRepetition |
|
|
|
63
63
|
* @internal
|
|
64
64
|
*/
|
|
65
65
|
export declare function compileUserPattern(rule: string, option: string, source: string, flags: string): RegExp;
|
|
66
|
+
export { findNestedRepetition };
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
*
|
|
25
25
|
* @see https://github.com/mike-north/eslint-sh/issues/221
|
|
26
26
|
*/
|
|
27
|
+
import { configurationMessage } from '../configuration-message.js';
|
|
27
28
|
/** Matches a `{n}`, `{n,}` or `{n,m}` quantifier at the start of the string. */
|
|
28
29
|
const BRACE_QUANTIFIER = /^\{\d+(?:,\d*)?\}/u;
|
|
29
30
|
/**
|
|
@@ -103,7 +104,7 @@ function skipClass(source, i) {
|
|
|
103
104
|
*
|
|
104
105
|
* @internal
|
|
105
106
|
*/
|
|
106
|
-
|
|
107
|
+
function findNestedRepetition(source) {
|
|
107
108
|
const stack = [{ start: 0, containsRepetition: false }];
|
|
108
109
|
let i = 0;
|
|
109
110
|
while (i < source.length) {
|
|
@@ -159,7 +160,7 @@ export function findNestedRepetition(source) {
|
|
|
159
160
|
* the error from the rule's `create()`, so it surfaces on the first file linted
|
|
160
161
|
* with the rule ("Occurred while linting <file>"), not when the config loads.
|
|
161
162
|
*
|
|
162
|
-
* @param rule - The rule
|
|
163
|
+
* @param rule - The configured rule ID (context.id), including the caller's plugin namespace.
|
|
163
164
|
* @param option - The option as the user wrote it, such as `patterns[0]`.
|
|
164
165
|
* @param source - The regular-expression source.
|
|
165
166
|
* @param flags - Rule-specific flags. Unicode semantics are always added;
|
|
@@ -172,15 +173,16 @@ export function compileUserPattern(rule, option, source, flags) {
|
|
|
172
173
|
try {
|
|
173
174
|
pattern = new RegExp(source, flags.includes('u') ? flags : `${flags}u`);
|
|
174
175
|
}
|
|
175
|
-
catch {
|
|
176
|
-
|
|
176
|
+
catch (cause) {
|
|
177
|
+
// Syntax and policy refusal are separate identities: only syntax has a compiler cause.
|
|
178
|
+
throw Object.assign(new TypeError(configurationMessage(`rules[${JSON.stringify(rule)}].${option}`, 'must be a valid regular expression', source), { cause }), { code: 'ESLINT_SH_PATTERN_SYNTAX' });
|
|
177
179
|
}
|
|
178
180
|
const nested = findNestedRepetition(source);
|
|
179
181
|
if (nested !== undefined) {
|
|
180
|
-
throw new TypeError(
|
|
181
|
-
'
|
|
182
|
-
'
|
|
183
|
-
'of repetition, for example `[A-Z0-9_]*` instead of `([A-Z]+_?)*`.');
|
|
182
|
+
throw Object.assign(new TypeError(configurationMessage(`rules[${JSON.stringify(rule)}].${option}`, `repeats ${nested.group} with \`${nested.outer}\`, and that group already repeats. ` +
|
|
183
|
+
'The pattern runs against names the linted script chooses, and a nested repetition can take exponential time on a long name. ' +
|
|
184
|
+
'Rewrite it with one level of repetition, for example `[A-Z0-9_]*` instead of `([A-Z]+_?)*`', source)), { code: 'ESLINT_SH_PATTERN_REPETITION' });
|
|
184
185
|
}
|
|
185
186
|
return pattern;
|
|
186
187
|
}
|
|
188
|
+
export { findNestedRepetition };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Project names for the five supported shell grammars. @public */
|
|
2
|
+
export type ShellDialect = 'bash' | 'posix' | 'mksh' | 'bats' | 'zsh';
|
|
3
|
+
/** The single project-label translation to the pinned parser's grammar constants. @internal */
|
|
4
|
+
export declare const SHELL_VARIANTS: Readonly<Record<ShellDialect, number>>;
|
|
5
|
+
/** Stable diagnostic ordering follows the canonical mapping, without another hand-maintained list. @internal */
|
|
6
|
+
export declare const SUPPORTED_SHELL_DIALECTS: readonly string[];
|
|
7
|
+
/** Labels are own keys; object-prototype names never select a grammar. @internal */
|
|
8
|
+
export declare function isShellDialect(value: unknown): value is ShellDialect;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Generated from shared/shell-dialect.ts; edit the canonical source.
|
|
2
|
+
/**
|
|
3
|
+
* Project shell vocabulary shared by linting and non-lint services. A label
|
|
4
|
+
* selects one definite grammar; parser constants are an implementation detail.
|
|
5
|
+
* This module is compiled into each consumer artifact, so shared semantics do
|
|
6
|
+
* not require a runtime dependency between separately released packages.
|
|
7
|
+
*/
|
|
8
|
+
import { LangBash, LangBats, LangMirBSDKorn, LangPOSIX, LangZsh } from 'sh-ast';
|
|
9
|
+
/** The single project-label translation to the pinned parser's grammar constants. @internal */
|
|
10
|
+
export const SHELL_VARIANTS = {
|
|
11
|
+
bash: LangBash,
|
|
12
|
+
posix: LangPOSIX,
|
|
13
|
+
mksh: LangMirBSDKorn,
|
|
14
|
+
bats: LangBats,
|
|
15
|
+
zsh: LangZsh,
|
|
16
|
+
};
|
|
17
|
+
/** Stable diagnostic ordering follows the canonical mapping, without another hand-maintained list. @internal */
|
|
18
|
+
export const SUPPORTED_SHELL_DIALECTS = Object.keys(SHELL_VARIANTS);
|
|
19
|
+
/** Labels are own keys; object-prototype names never select a grammar. @internal */
|
|
20
|
+
export function isShellDialect(value) {
|
|
21
|
+
return typeof value === 'string' && Object.hasOwn(SHELL_VARIANTS, value);
|
|
22
|
+
}
|
package/dist/version.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/** Plugin version metadata comes from its own package; ESLINT_SH_PACKAGE_METADATA identifies an invalid package shape. Filesystem/JSON exceptions retain their native identity. */
|
|
1
2
|
import { readFileSync } from 'node:fs';
|
|
2
3
|
import { dirname, join } from 'node:path';
|
|
3
4
|
import { fileURLToPath } from 'node:url';
|
|
@@ -24,5 +25,5 @@ export function readPackageVersion() {
|
|
|
24
25
|
typeof parsed.version === 'string') {
|
|
25
26
|
return parsed.version;
|
|
26
27
|
}
|
|
27
|
-
throw new Error(`@eslint-sh/eslint-plugin: package.json at ${packageJsonPath} is missing a string "version" field`);
|
|
28
|
+
throw Object.assign(new Error(`@eslint-sh/eslint-plugin: package.json at ${packageJsonPath} is missing a string "version" field`), { code: 'ESLINT_SH_PACKAGE_METADATA' });
|
|
28
29
|
}
|
package/docs/compat.md
CHANGED
|
@@ -67,7 +67,7 @@ instead of UTF-8. `sh/compat/no-nounset-empty-array` gates a behavior, not synta
|
|
|
67
67
|
`dialect: "posix"` can lack it: those three are the POSIX-only gates, and the other twenty-two are
|
|
68
68
|
the version gates. The `bash-3.2` preset enables twenty-one version gates, excluding the opt-in
|
|
69
69
|
`sh/compat/no-nounset-empty-array`, and the `posix-portable` preset
|
|
70
|
-
enables
|
|
70
|
+
enables twenty-four, omitting the Bash-only empty-array check that cannot fire there; see
|
|
71
71
|
[Presets](#presets).
|
|
72
72
|
|
|
73
73
|
## Setting required shell support
|
|
@@ -154,11 +154,10 @@ an explicit opt-in for Bash:
|
|
|
154
154
|
fail or read as unset on dash, but not `local`, `echo -e` or the others listed under
|
|
155
155
|
[What `posix-portable` does not report](#what-posix-portable-does-not-report), so a clean run does
|
|
156
156
|
not prove a script runs under every POSIX `sh`. It sets `languageOptions: { dialect: 'posix' }` and
|
|
157
|
-
enables
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
`#!/bin/sh` script.
|
|
157
|
+
enables twenty-four effective `compat/*` rules and `sh/shebang-matches-dialect`, all at `error`.
|
|
158
|
+
The shebang check verifies that the declared interpreter agrees with the target dialect.
|
|
159
|
+
It excludes the Bash-only empty-array nounset rule and imposes no strict-mode policy.
|
|
160
|
+
Compose `recommended` to require its dialect-aware POSIX `set -eu` default.
|
|
162
161
|
|
|
163
162
|
Both presets are single objects with no `files`; extend them from an entry that lists your
|
|
164
163
|
scripts, after `recommended` if you want its rules too:
|