@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.
Files changed (91) hide show
  1. package/README.md +41 -34
  2. package/dist/api.d.ts +92 -0
  3. package/dist/api.js +83 -0
  4. package/dist/command-harvest/harvest-commands.d.ts +2 -2
  5. package/dist/command-harvest/harvest-commands.js +2 -2
  6. package/dist/configs/posix-portable.d.ts +6 -10
  7. package/dist/configs/posix-portable.js +4 -6
  8. package/dist/configuration-message.d.ts +14 -0
  9. package/dist/configuration-message.js +33 -0
  10. package/dist/declaration-conventions/get-declared-commands.d.ts +41 -0
  11. package/dist/declaration-conventions/{read-declared-commands.js → get-declared-commands.js} +43 -43
  12. package/dist/eslint-plugin-api.d.ts +1506 -0
  13. package/dist/eslint-plugin-syntax-analyze.d.ts +3 -3
  14. package/dist/eslint-plugin-syntax.d.ts +3 -4
  15. package/dist/eslint-plugin.d.ts +16 -1560
  16. package/dist/index.d.ts +14 -81
  17. package/dist/index.js +22 -76
  18. package/dist/language/command-resolution.d.ts +5 -5
  19. package/dist/language/command-resolution.js +11 -7
  20. package/dist/language/selector-classes.d.ts +7 -3
  21. package/dist/language/selector-classes.js +13 -4
  22. package/dist/language/shell-language-options.d.ts +3 -3
  23. package/dist/language/shell-language-options.js +12 -17
  24. package/dist/language/shell-language.d.ts +3 -1
  25. package/dist/language/shell-language.js +6 -11
  26. package/dist/language/shell-source-code.d.ts +59 -42
  27. package/dist/language/shell-source-code.js +86 -60
  28. package/dist/language/source-coordinates.d.ts +2 -0
  29. package/dist/language/source-coordinates.js +8 -4
  30. package/dist/language/syntax-view.js +19 -7
  31. package/dist/language/word-resolution.d.ts +2 -1
  32. package/dist/language/word-resolution.js +8 -3
  33. package/dist/rules/create-wrapper-rule.d.ts +1 -1
  34. package/dist/rules/create-wrapper-rule.js +11 -9
  35. package/dist/rules/define-shell-rule.js +1 -1
  36. package/dist/rules/no-masked-exit-status.d.ts +4 -3
  37. package/dist/rules/no-masked-exit-status.js +3 -2
  38. package/dist/rules/no-secret-env-reads.d.ts +2 -0
  39. package/dist/rules/no-secret-env-reads.js +13 -2
  40. package/dist/rules/no-secret-flags.d.ts +2 -0
  41. package/dist/rules/no-secret-flags.js +14 -3
  42. package/dist/rules/no-unchecked-cd.js +3 -2
  43. package/dist/rules/no-undeclared-commands.js +8 -3
  44. package/dist/rules/no-unused-declarations.js +6 -2
  45. package/dist/rules/prefer-local.d.ts +3 -2
  46. package/dist/rules/prefer-local.js +4 -2
  47. package/dist/rules/require-network-wrapper.d.ts +1 -1
  48. package/dist/rules/require-network-wrapper.js +11 -7
  49. package/dist/rules/require-strict-mode.d.ts +5 -46
  50. package/dist/rules/require-strict-mode.js +17 -107
  51. package/dist/rules/require-tty-guard.d.ts +2 -0
  52. package/dist/rules/require-tty-guard.js +8 -1
  53. package/dist/rules/resolve-declaration-convention.d.ts +9 -88
  54. package/dist/rules/resolve-declaration-convention.js +19 -71
  55. package/dist/rules/resolve-network-wrapper.d.ts +1 -2
  56. package/dist/rules/resolve-network-wrapper.js +18 -14
  57. package/dist/rules/resolve-transparent-wrappers.d.ts +2 -7
  58. package/dist/rules/resolve-transparent-wrappers.js +19 -9
  59. package/dist/rules/set-options.d.ts +1 -1
  60. package/dist/rules/set-options.js +1 -1
  61. package/dist/rules/strict-mode-prologue.d.ts +43 -0
  62. package/dist/rules/strict-mode-prologue.js +91 -0
  63. package/dist/rules/user-pattern.d.ts +4 -3
  64. package/dist/rules/user-pattern.js +10 -8
  65. package/dist/shell-dialect.d.ts +8 -0
  66. package/dist/shell-dialect.js +22 -0
  67. package/dist/syntax/analyze.d.ts +3 -3
  68. package/dist/syntax/analyze.js +3 -3
  69. package/dist/syntax/index.d.ts +3 -4
  70. package/dist/syntax/index.js +3 -4
  71. package/dist/version.js +2 -1
  72. package/docs/compat.md +5 -6
  73. package/docs/custom-rule-tutorial.md +22 -18
  74. package/docs/declaration-conventions.md +20 -23
  75. package/docs/error-codes.md +93 -0
  76. package/docs/examples/no-rm-rf-rule.mjs +8 -10
  77. package/docs/examples/sorted-declarations-rule.mjs +6 -9
  78. package/docs/examples/tsconfig.json +10 -0
  79. package/docs/rules/README.md +12 -4
  80. package/docs/rules/compat/no-nounset-empty-array.md +1 -1
  81. package/docs/rules/no-masked-exit-status.md +2 -2
  82. package/docs/rules/no-secret-env-reads.md +13 -3
  83. package/docs/rules/no-secret-flags.md +14 -5
  84. package/docs/rules/no-unchecked-cd.md +15 -2
  85. package/docs/rules/require-network-wrapper.md +5 -5
  86. package/docs/rules/require-strict-mode.md +25 -23
  87. package/docs/rules/require-tty-guard.md +12 -3
  88. package/docs/selectors.md +7 -2
  89. package/docs/wrapper-rules.md +2 -0
  90. package/package.json +3 -3
  91. package/dist/declaration-conventions/read-declared-commands.d.ts +0 -70
@@ -1,8 +1,23 @@
1
1
  /**
2
- * The binding a validation error names. A convention can only be bound in
3
- * settings in practice: ESLint copies rule options with `structuredClone`,
4
- * which rejects an object with methods.
2
+ * The one path by which a rule reads a bound convention's declarations for
3
+ * the file it is linting. The built-in consumers (`sh/no-undeclared-commands`,
4
+ * `sh/no-unused-declarations`) and a pack's own rules all call
5
+ * {@link getDeclaredCommands}, so they hand the convention the same
6
+ * {@link LintedFile} and trust only declarations that passed the same checks.
7
+ *
8
+ * The checks exist because a declaration's kind decides where a report goes
9
+ * and whether a fix is offered. A declaration of an unknown kind, one missing
10
+ * its kind's fields, or a script range outside the text would otherwise be
11
+ * reported at a made-up location, crash deep inside a rule, or be silently
12
+ * accepted by a rule that only reads names. Failing with the declaration and
13
+ * the setting in the message makes the convention's bug the thing the user
14
+ * sees.
15
+ *
16
+ * ESLINT_SH_DECLARATION_OUTPUT identifies invalid extension results. Exceptions thrown by convention hooks belong to the extension and propagate unchanged.
5
17
  */
18
+ import { configurationMessage, describeConfigurationValue } from '../configuration-message.js';
19
+ import { resolveDeclarationConvention } from '../rules/resolve-declaration-convention.js';
20
+ /** Validation errors identify the same shared policy that supplied the declarations. */
6
21
  const BINDING = 'settings.sh.declarationConvention';
7
22
  /**
8
23
  * The only view of the linted file a convention's hooks receive: `filename`,
@@ -64,64 +79,49 @@ function invalidReason(value, textLength) {
64
79
  case undefined:
65
80
  return "`kind` is missing: it must be 'script', 'file' or 'described'";
66
81
  default:
67
- return `\`kind\` must be 'script', 'file' or 'described', not ${describe(declared.kind)}`;
68
- }
69
- }
70
- /** A short rendering of a value for an error message. */
71
- function describe(value) {
72
- try {
73
- // `JSON.stringify` returns `undefined` for a function or `undefined`.
74
- const text = JSON.stringify(value);
75
- return text ?? String(value);
76
- }
77
- catch {
78
- return String(value);
82
+ return `\`kind\` must be 'script', 'file' or 'described', not ${describeConfigurationValue(declared.kind)}`;
79
83
  }
80
84
  }
81
85
  /**
82
- * Returns the declarations `convention` makes for the file `context` is
83
- * linting, after checking each one against its kind. Call it from a rule's
84
- * listener with the convention `resolveDeclarationConvention` returned and
85
- * the rule's own `context`.
86
- *
87
- * The convention receives a frozen {@link LintedFile} holding only
88
- * `filename`, `physicalFilename` and `cwd`, never the whole context, so it
89
- * sees which file it is reading and nothing it could report through.
86
+ * Returns validated declarations for the linted file, or `null` when the project
87
+ * has no bound convention. An empty array means a convention is bound and the
88
+ * file declares no commands; consumers must preserve that distinction.
90
89
  *
91
- * Throws an `Error` naming the declaration-convention setting and showing the
92
- * offending value when
93
- * `declaredCommands` does not return an array, or returns a declaration with
94
- * a missing or unknown `kind`, without its kind's fields, or with a `range`
95
- * outside the linted text.
90
+ * The convention receives only a frozen {@link LintedFile}, never rule state.
91
+ * Invalid bindings, output arrays, declaration kinds, provenance or UTF-16 ranges
92
+ * throw an error naming the shared setting before a rule can trust the result.
96
93
  *
97
94
  * @example
98
95
  * ```ts
99
- * const convention = resolveDeclarationConvention(context);
100
- * if (convention === null) return {};
101
- * return {
102
- * File() {
103
- * for (const declared of readDeclaredCommands(convention, context)) {
104
- * if (declared.kind === 'script') {
105
- * // declared.range is an offset pair into context.sourceCode.text
106
- * }
107
- * }
108
- * },
109
- * };
96
+ * File() {
97
+ * const declarations = getDeclaredCommands(context);
98
+ * if (declarations === null) return;
99
+ * for (const declared of declarations) {
100
+ * // Each declaration has been checked against its kind's contract.
101
+ * }
102
+ * }
110
103
  * ```
111
104
  *
105
+ * @throws Error with `ESLINT_SH_DECLARATION_CONVENTION` for invalid shared bindings, or
106
+ * `ESLINT_SH_DECLARATION_OUTPUT` for invalid hook results.
107
+ * Exceptions thrown by the convention itself pass through unchanged, including their causes.
112
108
  * @public
113
109
  */
114
- export function readDeclaredCommands(convention, context) {
110
+ export function getDeclaredCommands(context) {
111
+ const convention = resolveDeclarationConvention(context);
112
+ if (convention === null) {
113
+ return null;
114
+ }
115
115
  const { sourceCode } = context;
116
116
  const file = toLintedFile(context);
117
117
  const declarations = convention.declaredCommands(sourceCode, file);
118
118
  if (!Array.isArray(declarations)) {
119
- throw new Error(`Invalid ${BINDING}: \`declaredCommands\` must return an array, but returned ${describe(declarations)}.`);
119
+ throw Object.assign(new Error(configurationMessage(`${BINDING}.declaredCommands`, 'must return an array', declarations)), { code: 'ESLINT_SH_DECLARATION_OUTPUT' });
120
120
  }
121
- for (const declared of declarations) {
121
+ for (const [index, declared] of declarations.entries()) {
122
122
  const reason = invalidReason(declared, sourceCode.text.length);
123
123
  if (reason !== null) {
124
- throw new Error(`Invalid ${BINDING}: \`declaredCommands\` returned the declaration ${describe(declared)}, but ${reason}.`);
124
+ throw Object.assign(new Error(configurationMessage(`${BINDING}.declaredCommands[${String(index)}]`, `returned an invalid declaration: ${reason}`, declared)), { code: 'ESLINT_SH_DECLARATION_OUTPUT' });
125
125
  }
126
126
  }
127
127
  return declarations;