@amritk/lint 0.3.2 → 0.4.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 (119) hide show
  1. package/AI.md +55 -0
  2. package/dist/core/document.js +12 -10
  3. package/dist/core/formats.js +10 -13
  4. package/dist/core/glob.js +96 -118
  5. package/dist/core/index.d.ts +0 -1
  6. package/dist/core/index.js +30 -11
  7. package/dist/core/jsonpath.js +487 -561
  8. package/dist/core/lint.js +78 -85
  9. package/dist/core/plugin.js +21 -29
  10. package/dist/core/pointers.js +107 -151
  11. package/dist/core/ruleset.js +0 -0
  12. package/dist/core/runner.js +214 -272
  13. package/dist/core/types.js +4 -1
  14. package/dist/core/validate-ruleset.js +98 -112
  15. package/dist/fix/apply.d.ts +1 -1
  16. package/dist/fix/apply.js +58 -96
  17. package/dist/fix/index.js +7 -2
  18. package/dist/fix/plugin.js +15 -20
  19. package/dist/fix/types.d.ts +1 -1
  20. package/dist/functions/alphabetical.d.ts +1 -1
  21. package/dist/functions/alphabetical.js +39 -50
  22. package/dist/functions/casing.d.ts +1 -1
  23. package/dist/functions/casing.js +39 -45
  24. package/dist/functions/defined.d.ts +1 -1
  25. package/dist/functions/defined.js +7 -5
  26. package/dist/functions/enumeration.d.ts +1 -1
  27. package/dist/functions/enumeration.js +15 -25
  28. package/dist/functions/falsy.d.ts +1 -1
  29. package/dist/functions/falsy.js +7 -5
  30. package/dist/functions/index.d.ts +1 -1
  31. package/dist/functions/index.js +60 -44
  32. package/dist/functions/length.d.ts +1 -1
  33. package/dist/functions/length.js +22 -32
  34. package/dist/functions/or.d.ts +1 -1
  35. package/dist/functions/or.js +18 -24
  36. package/dist/functions/pattern.d.ts +1 -1
  37. package/dist/functions/pattern.js +39 -49
  38. package/dist/functions/schema.d.ts +1 -1
  39. package/dist/functions/schema.js +93 -119
  40. package/dist/functions/truthy.d.ts +1 -1
  41. package/dist/functions/truthy.js +7 -5
  42. package/dist/functions/typed-enum.d.ts +1 -1
  43. package/dist/functions/typed-enum.js +33 -36
  44. package/dist/functions/undefined.d.ts +1 -1
  45. package/dist/functions/undefined.js +7 -8
  46. package/dist/functions/unreferenced-reusable-object.d.ts +1 -1
  47. package/dist/functions/unreferenced-reusable-object.js +35 -47
  48. package/dist/functions/xor.d.ts +1 -1
  49. package/dist/functions/xor.js +13 -16
  50. package/dist/index.d.ts +17 -4
  51. package/dist/index.js +149 -164
  52. package/dist/parsers/edit-model.js +316 -444
  53. package/dist/parsers/index.d.ts +1 -1
  54. package/dist/parsers/index.js +23 -19
  55. package/dist/parsers/json.js +39 -37
  56. package/dist/parsers/lines.js +23 -26
  57. package/dist/parsers/types.js +9 -7
  58. package/dist/parsers/yaml.js +137 -204
  59. package/dist/rules/openapi/fixers.js +166 -221
  60. package/dist/rules/openapi/formats.js +26 -27
  61. package/dist/rules/openapi/functions/example-validation.d.ts +1 -1
  62. package/dist/rules/openapi/functions/example-validation.js +98 -138
  63. package/dist/rules/openapi/functions/helpers.js +8 -10
  64. package/dist/rules/openapi/functions/index.d.ts +1 -1
  65. package/dist/rules/openapi/functions/index.js +98 -72
  66. package/dist/rules/openapi/functions/oas-additional-operations.d.ts +1 -1
  67. package/dist/rules/openapi/functions/oas-additional-operations.js +16 -22
  68. package/dist/rules/openapi/functions/oas-discriminator.d.ts +1 -1
  69. package/dist/rules/openapi/functions/oas-discriminator.js +24 -22
  70. package/dist/rules/openapi/functions/oas-example-external-value.d.ts +1 -1
  71. package/dist/rules/openapi/functions/oas-example-external-value.js +13 -21
  72. package/dist/rules/openapi/functions/oas-example-value.d.ts +1 -1
  73. package/dist/rules/openapi/functions/oas-example-value.js +24 -27
  74. package/dist/rules/openapi/functions/oas-mutually-exclusive.d.ts +1 -1
  75. package/dist/rules/openapi/functions/oas-mutually-exclusive.js +15 -19
  76. package/dist/rules/openapi/functions/oas-no-nullable.d.ts +1 -1
  77. package/dist/rules/openapi/functions/oas-no-nullable.js +13 -21
  78. package/dist/rules/openapi/functions/oas-op-form-data-consume-check.d.ts +1 -1
  79. package/dist/rules/openapi/functions/oas-op-form-data-consume-check.js +21 -19
  80. package/dist/rules/openapi/functions/oas-op-id-unique.d.ts +1 -1
  81. package/dist/rules/openapi/functions/oas-op-id-unique.js +27 -27
  82. package/dist/rules/openapi/functions/oas-op-params.d.ts +1 -1
  83. package/dist/rules/openapi/functions/oas-op-params.js +36 -42
  84. package/dist/rules/openapi/functions/oas-op-security-defined.d.ts +1 -1
  85. package/dist/rules/openapi/functions/oas-op-security-defined.js +40 -39
  86. package/dist/rules/openapi/functions/oas-op-success-response.d.ts +1 -1
  87. package/dist/rules/openapi/functions/oas-op-success-response.js +11 -13
  88. package/dist/rules/openapi/functions/oas-path-param.d.ts +1 -1
  89. package/dist/rules/openapi/functions/oas-path-param.js +75 -96
  90. package/dist/rules/openapi/functions/oas-schema-example-deprecated.d.ts +1 -1
  91. package/dist/rules/openapi/functions/oas-schema-example-deprecated.js +33 -40
  92. package/dist/rules/openapi/functions/oas-schema.d.ts +1 -1
  93. package/dist/rules/openapi/functions/oas-schema.js +9 -14
  94. package/dist/rules/openapi/functions/oas-server-name-unique.d.ts +1 -1
  95. package/dist/rules/openapi/functions/oas-server-name-unique.js +21 -19
  96. package/dist/rules/openapi/functions/oas-server-variables.d.ts +1 -1
  97. package/dist/rules/openapi/functions/oas-server-variables.js +45 -49
  98. package/dist/rules/openapi/functions/oas-tag-defined.d.ts +1 -1
  99. package/dist/rules/openapi/functions/oas-tag-defined.js +20 -20
  100. package/dist/rules/openapi/functions/oas-tag-kind.d.ts +1 -1
  101. package/dist/rules/openapi/functions/oas-tag-kind.js +16 -16
  102. package/dist/rules/openapi/functions/oas-tag-parent-defined.d.ts +1 -1
  103. package/dist/rules/openapi/functions/oas-tag-parent-defined.js +40 -43
  104. package/dist/rules/openapi/functions/oas-tags-unique.d.ts +1 -1
  105. package/dist/rules/openapi/functions/oas-tags-unique.js +18 -16
  106. package/dist/rules/openapi/functions/oas-unused-component.d.ts +1 -1
  107. package/dist/rules/openapi/functions/oas-unused-component.js +48 -58
  108. package/dist/rules/openapi/functions/ref-siblings.d.ts +1 -1
  109. package/dist/rules/openapi/functions/ref-siblings.js +13 -11
  110. package/dist/rules/openapi/index.d.ts +2 -1
  111. package/dist/rules/openapi/index.js +99 -117
  112. package/dist/rules/openapi/oas.d.ts +1 -1
  113. package/dist/rules/openapi/oas.js +524 -536
  114. package/dist/rules/openapi/schemas/index.js +17 -33
  115. package/dist/rules/openapi/schemas/oas20.json +1 -1592
  116. package/dist/rules/openapi/schemas/oas30.json +1 -1651
  117. package/dist/rules/openapi/schemas/oas31.json +1 -1412
  118. package/dist/rules/openapi/schemas/oas32.json +1 -1684
  119. package/package.json +11 -6
package/AI.md ADDED
@@ -0,0 +1,55 @@
1
+ # @amritk/lint — notes for AI coding agents
2
+
3
+ A fast, format-agnostic JSON/YAML style-guide linter: JSON Schema validation
4
+ plus custom rules, emitting findings with exact `line:column` ranges. Full
5
+ reference is [README.md](./README.md).
6
+
7
+ > Pre-alpha: APIs change in **minor** versions.
8
+
9
+ ## Minimal example
10
+
11
+ ```ts
12
+ import { lintDocument } from '@amritk/lint'
13
+
14
+ const ruleset = {
15
+ rules: {
16
+ 'require-name': { given: '$', severity: 'error', then: { field: 'name', function: 'truthy' } },
17
+ },
18
+ }
19
+
20
+ const findings = await lintDocument('version: 1\n', { ruleset, source: 'service.yaml' })
21
+ ```
22
+
23
+ ## Gotchas — where agents fail
24
+
25
+ 1. **Two `severity` vocabularies.** In a **ruleset** you author strings
26
+ (`'error' | 'warn' | 'info' | 'hint' | 'off'`). In a **finding** `severity`
27
+ is **numeric** (`0` error, `1` warn, `2` info, `3` hint). Don't cross them.
28
+ 2. **Ranges are zero-based** (`{ line, character }` from 0). Add 1 to both to
29
+ print `file:line:col`.
30
+ 3. **The engine ships no resolver, no built-in ruleset, no fixers.** `$ref`s are
31
+ only dereferenced if you pass a `resolve` hook (e.g. wrapping
32
+ `@amritk/resolve-refs`); `fixDocument` is a no-op until you supply a
33
+ `FixerRegistry`; `createRuleset()` with no argument runs zero rules.
34
+ 4. **`extends` targets are file paths or npm packages only** — there are no
35
+ named built-in rulesets in core. String `extends` resolve relative to
36
+ `rulesetBasePath` (or the ruleset file's own directory).
37
+ 5. **OpenAPI support is a separate subpath**, `@amritk/lint/rules/openapi`
38
+ (`createOpenApiRuleset`, `oas`, `oasFixers`, …) — not the package root.
39
+
40
+ ## Exports
41
+
42
+ - `lintDocument(input, options?)` → findings only.
43
+ - `lintDocumentWithResult(input, options?)` → `{ diagnostics, output?, pluginData }`.
44
+ - `fixDocument(input, options?)` → `{ output, fixed, applied, remaining }` (needs `fixers`).
45
+ - `createRuleset(def?, basePath?)`, `resolveNamedRuleset(name, basePath?)`,
46
+ `builtinFunctions` (`alphabetical`, `casing`, `truthy`, `pattern`, `schema`, …).
47
+
48
+ ## Subpaths
49
+
50
+ | Import | Purpose |
51
+ |---|---|
52
+ | `@amritk/lint` | core engine, `lintDocument`/`fixDocument`, built-in functions |
53
+ | `@amritk/lint/rules/openapi` | ready-made OpenAPI preset (`createOpenApiRuleset`, `oas`) |
54
+
55
+ Install: `bun add @amritk/lint`.
@@ -1,11 +1,13 @@
1
- import { parseWithPointers, } from '../parsers/index.js';
2
- /** Parses `input` into a {@link Document} with a source map for position lookups. */
3
- export const createDocument = (input, options = {}) => {
4
- const parsed = parseWithPointers(input, options);
5
- return {
6
- source: options.source,
7
- data: parsed.data,
8
- diagnostics: parsed.diagnostics,
9
- getLocationForJsonPath: (path, closest = false) => parsed.getLocationForJsonPath(path, closest),
10
- };
1
+ import { parseWithPointers } from "../parsers/index.js";
2
+ const createDocument = (input, options = {}) => {
3
+ const parsed = parseWithPointers(input, options);
4
+ return {
5
+ source: options.source,
6
+ data: parsed.data,
7
+ diagnostics: parsed.diagnostics,
8
+ getLocationForJsonPath: (path, closest = false) => parsed.getLocationForJsonPath(path, closest)
9
+ };
10
+ };
11
+ export {
12
+ createDocument
11
13
  };
@@ -1,14 +1,11 @@
1
- /**
2
- * Returns the set of registered format names that match the document. The
3
- * `formats` registry is supplied by the caller (a preset provides its own
4
- * format detectors); the engine itself is format-agnostic, so the
5
- * default registry is empty and rules with no `formats` gate run regardless.
6
- */
7
- export const detectFormats = (document, formats = {}) => {
8
- const matched = new Set();
9
- for (const [name, detector] of Object.entries(formats)) {
10
- if (detector(document))
11
- matched.add(name);
12
- }
13
- return matched;
1
+ const detectFormats = (document, formats = {}) => {
2
+ const matched = /* @__PURE__ */ new Set();
3
+ for (const [name, detector] of Object.entries(formats)) {
4
+ if (detector(document))
5
+ matched.add(name);
6
+ }
7
+ return matched;
8
+ };
9
+ export {
10
+ detectFormats
14
11
  };
package/dist/core/glob.js CHANGED
@@ -1,132 +1,110 @@
1
- /**
2
- * Minimal glob → RegExp for matching override `files` patterns against document
3
- * paths. Supports `**` (any path segments), `*` (within a segment), `?`, and
4
- * brace expansion (`{a,b}`).
5
- */
6
1
  const REGEXP_SPECIAL = /[\\^$.*+?()[\]{}|/]/;
7
- /** Splits brace-group content on top-level commas (commas nested in inner braces stay put). */
8
2
  const splitTopLevel = (body) => {
9
- const parts = [];
10
- let depth = 0;
11
- let current = '';
12
- for (const ch of body) {
13
- if (ch === '{')
14
- depth++;
15
- else if (ch === '}')
16
- depth--;
17
- if (ch === ',' && depth === 0) {
18
- parts.push(current);
19
- current = '';
20
- continue;
21
- }
22
- current += ch;
3
+ const parts = [];
4
+ let depth = 0;
5
+ let current = "";
6
+ for (const ch of body) {
7
+ if (ch === "{")
8
+ depth++;
9
+ else if (ch === "}")
10
+ depth--;
11
+ if (ch === "," && depth === 0) {
12
+ parts.push(current);
13
+ current = "";
14
+ continue;
23
15
  }
24
- parts.push(current);
25
- return parts;
16
+ current += ch;
17
+ }
18
+ parts.push(current);
19
+ return parts;
26
20
  };
27
- /**
28
- * Expands brace alternations (`a.{yaml,yml}` → `a.yaml`, `a.yml`) into concrete
29
- * globs, cartesian across multiple groups. A group with no top-level comma is
30
- * left literal (mirroring minimatch), so `{}` in a path does not vanish.
31
- */
32
21
  const expandBraces = (glob) => {
33
- const open = glob.indexOf('{');
34
- if (open === -1)
35
- return [glob];
36
- let depth = 0;
37
- let close = -1;
38
- for (let i = open; i < glob.length; i++) {
39
- if (glob[i] === '{')
40
- depth++;
41
- else if (glob[i] === '}') {
42
- depth--;
43
- if (depth === 0) {
44
- close = i;
45
- break;
46
- }
47
- }
22
+ const open = glob.indexOf("{");
23
+ if (open === -1)
24
+ return [glob];
25
+ let depth = 0;
26
+ let close = -1;
27
+ for (let i = open; i < glob.length; i++) {
28
+ if (glob[i] === "{")
29
+ depth++;
30
+ else if (glob[i] === "}") {
31
+ depth--;
32
+ if (depth === 0) {
33
+ close = i;
34
+ break;
35
+ }
48
36
  }
49
- if (close === -1)
50
- return [glob];
51
- const prefix = glob.slice(0, open);
52
- const body = glob.slice(open + 1, close);
53
- const suffix = glob.slice(close + 1);
54
- const options = splitTopLevel(body);
55
- if (options.length === 1) {
56
- // No alternation keep the braces as literal characters.
57
- return expandBraces(suffix).map((rest) => `${prefix}{${body}}${rest}`);
37
+ }
38
+ if (close === -1)
39
+ return [glob];
40
+ const prefix = glob.slice(0, open);
41
+ const body = glob.slice(open + 1, close);
42
+ const suffix = glob.slice(close + 1);
43
+ const options = splitTopLevel(body);
44
+ if (options.length === 1) {
45
+ return expandBraces(suffix).map((rest) => `${prefix}{${body}}${rest}`);
46
+ }
47
+ const results = [];
48
+ for (const option of options) {
49
+ for (const expandedOption of expandBraces(option)) {
50
+ for (const expandedSuffix of expandBraces(suffix)) {
51
+ results.push(prefix + expandedOption + expandedSuffix);
52
+ }
58
53
  }
59
- const results = [];
60
- for (const option of options) {
61
- for (const expandedOption of expandBraces(option)) {
62
- for (const expandedSuffix of expandBraces(suffix)) {
63
- results.push(prefix + expandedOption + expandedSuffix);
64
- }
65
- }
66
- }
67
- return results;
54
+ }
55
+ return results;
68
56
  };
69
- /** Compiles a single (brace-free) glob into an un-anchored RegExp source. */
70
57
  const globInnerSource = (glob) => {
71
- let source = '';
72
- for (let i = 0; i < glob.length; i++) {
73
- const char = glob[i];
74
- if (char === '*') {
75
- if (glob[i + 1] === '*') {
76
- i++;
77
- if (glob[i + 1] === '/') {
78
- i++;
79
- source += '(?:.*/)?';
80
- }
81
- else {
82
- source += '.*';
83
- }
84
- }
85
- else {
86
- source += '[^/]*';
87
- }
88
- }
89
- else if (char === '?') {
90
- source += '[^/]';
91
- }
92
- else if (char && REGEXP_SPECIAL.test(char)) {
93
- source += `\\${char}`;
94
- }
95
- else {
96
- source += char;
58
+ let source = "";
59
+ for (let i = 0; i < glob.length; i++) {
60
+ const char = glob[i];
61
+ if (char === "*") {
62
+ if (glob[i + 1] === "*") {
63
+ i++;
64
+ if (glob[i + 1] === "/") {
65
+ i++;
66
+ source += "(?:.*/)?";
67
+ } else {
68
+ source += ".*";
97
69
  }
70
+ } else {
71
+ source += "[^/]*";
72
+ }
73
+ } else if (char === "?") {
74
+ source += "[^/]";
75
+ } else if (char && REGEXP_SPECIAL.test(char)) {
76
+ source += `\\${char}`;
77
+ } else {
78
+ source += char;
98
79
  }
99
- return source;
80
+ }
81
+ return source;
100
82
  };
101
- // Compiled patterns are cached by their glob string; the ruleset's override
102
- // globs are re-tested against every linted source, so compiling once matters.
103
- const regexCache = new Map();
104
- /** Compiles a glob pattern (`**`, `*`, `?`, `{a,b}`) into an anchored RegExp, cached by string. */
105
- export const globToRegExp = (glob) => {
106
- const cached = regexCache.get(glob);
107
- if (cached)
108
- return cached;
109
- const alternatives = expandBraces(glob).map(globInnerSource);
110
- const regex = new RegExp(`^(?:${alternatives.join('|')})$`);
111
- regexCache.set(glob, regex);
112
- return regex;
83
+ const regexCache = /* @__PURE__ */ new Map();
84
+ const globToRegExp = (glob) => {
85
+ const cached = regexCache.get(glob);
86
+ if (cached)
87
+ return cached;
88
+ const alternatives = expandBraces(glob).map(globInnerSource);
89
+ const regex = new RegExp(`^(?:${alternatives.join("|")})$`);
90
+ regexCache.set(glob, regex);
91
+ return regex;
113
92
  };
114
- /** Returns true if `path` matches any of the glob patterns. */
115
- export const matchesGlob = (path, patterns) => {
116
- const basename = path.split('/').pop() ?? path;
117
- return patterns.some((pattern) => {
118
- const regex = globToRegExp(pattern);
119
- if (regex.test(path))
120
- return true;
121
- // A pattern without a slash also matches the basename.
122
- if (!pattern.includes('/'))
123
- return regex.test(basename);
124
- // A relative pattern that contains a slash matches an absolute source by
125
- // suffix, so `src/api.yaml` matches `/home/user/repo/src/api.yaml` — the
126
- // minimatch/Spectral behavior for override globs against absolute paths.
127
- if (!pattern.startsWith('/') && !pattern.startsWith('**')) {
128
- return globToRegExp(`**/${pattern}`).test(path);
129
- }
130
- return false;
131
- });
93
+ const matchesGlob = (path, patterns) => {
94
+ const basename = path.split("/").pop() ?? path;
95
+ return patterns.some((pattern) => {
96
+ const regex = globToRegExp(pattern);
97
+ if (regex.test(path))
98
+ return true;
99
+ if (!pattern.includes("/"))
100
+ return regex.test(basename);
101
+ if (!pattern.startsWith("/") && !pattern.startsWith("**")) {
102
+ return globToRegExp(`**/${pattern}`).test(path);
103
+ }
104
+ return false;
105
+ });
106
+ };
107
+ export {
108
+ globToRegExp,
109
+ matchesGlob
132
110
  };
@@ -7,5 +7,4 @@ export { type LintPlugin, type LintPluginContext, type LintPluginResult, type Pl
7
7
  export { pointerToPath, resolveSourceOrigin, resolveSourceOriginFromMap, resolveSourcePath, } from './pointers.js';
8
8
  export { type AliasDefinition, createRuleset, type ExtendModifier, type ExtendResolver, type ResolvedExtend, type Ruleset, type RulesetOptions, } from './ruleset.js';
9
9
  export { createLinter, type IRunOptions, type Linter } from './runner.js';
10
- export * from './types.js';
11
10
  export { type IRulesetProblem, validateRuleset } from './validate-ruleset.js';
@@ -1,11 +1,30 @@
1
- export { createDocument } from './document.js';
2
- export { detectFormats } from './formats.js';
3
- export { globToRegExp, matchesGlob } from './glob.js';
4
- export { compileQuery, query, queryCompiled, queryMany } from './jsonpath.js';
5
- export { lint, lintWithResult, } from './lint.js';
6
- export { runPlugins, } from './plugin.js';
7
- export { pointerToPath, resolveSourceOrigin, resolveSourceOriginFromMap, resolveSourcePath, } from './pointers.js';
8
- export { createRuleset, } from './ruleset.js';
9
- export { createLinter } from './runner.js';
10
- export * from './types.js';
11
- export { validateRuleset } from './validate-ruleset.js';
1
+ import { createDocument } from "./document.js";
2
+ import { detectFormats } from "./formats.js";
3
+ import { globToRegExp, matchesGlob } from "./glob.js";
4
+ import { compileQuery, query, queryCompiled, queryMany } from "./jsonpath.js";
5
+ import { lint, lintWithResult } from "./lint.js";
6
+ import { runPlugins } from "./plugin.js";
7
+ import { pointerToPath, resolveSourceOrigin, resolveSourceOriginFromMap, resolveSourcePath } from "./pointers.js";
8
+ import { createRuleset } from "./ruleset.js";
9
+ import { createLinter } from "./runner.js";
10
+ import { validateRuleset } from "./validate-ruleset.js";
11
+ export {
12
+ compileQuery,
13
+ createDocument,
14
+ createLinter,
15
+ createRuleset,
16
+ detectFormats,
17
+ globToRegExp,
18
+ lint,
19
+ lintWithResult,
20
+ matchesGlob,
21
+ pointerToPath,
22
+ query,
23
+ queryCompiled,
24
+ queryMany,
25
+ resolveSourceOrigin,
26
+ resolveSourceOriginFromMap,
27
+ resolveSourcePath,
28
+ runPlugins,
29
+ validateRuleset
30
+ };