@amritk/lint 0.3.2 → 0.3.3
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/dist/core/document.js +12 -10
- package/dist/core/formats.js +10 -13
- package/dist/core/glob.js +96 -118
- package/dist/core/index.js +31 -11
- package/dist/core/jsonpath.js +487 -561
- package/dist/core/lint.js +78 -85
- package/dist/core/plugin.js +21 -29
- package/dist/core/pointers.js +107 -151
- package/dist/core/ruleset.js +0 -0
- package/dist/core/runner.js +214 -272
- package/dist/core/types.js +4 -1
- package/dist/core/validate-ruleset.js +98 -112
- package/dist/fix/apply.js +58 -96
- package/dist/fix/index.js +7 -2
- package/dist/fix/plugin.js +15 -20
- package/dist/functions/alphabetical.js +39 -50
- package/dist/functions/casing.js +39 -45
- package/dist/functions/defined.js +7 -5
- package/dist/functions/enumeration.js +15 -25
- package/dist/functions/falsy.js +7 -5
- package/dist/functions/index.js +60 -44
- package/dist/functions/length.js +22 -32
- package/dist/functions/or.js +18 -24
- package/dist/functions/pattern.js +39 -49
- package/dist/functions/schema.js +93 -119
- package/dist/functions/truthy.js +7 -5
- package/dist/functions/typed-enum.js +33 -36
- package/dist/functions/undefined.js +7 -8
- package/dist/functions/unreferenced-reusable-object.js +35 -47
- package/dist/functions/xor.js +13 -16
- package/dist/index.js +114 -164
- package/dist/parsers/edit-model.js +316 -444
- package/dist/parsers/index.js +22 -19
- package/dist/parsers/json.js +39 -37
- package/dist/parsers/lines.js +23 -26
- package/dist/parsers/types.js +9 -7
- package/dist/parsers/yaml.js +137 -204
- package/dist/rules/openapi/fixers.js +166 -221
- package/dist/rules/openapi/formats.js +26 -27
- package/dist/rules/openapi/functions/example-validation.js +98 -138
- package/dist/rules/openapi/functions/helpers.js +8 -10
- package/dist/rules/openapi/functions/index.js +98 -72
- package/dist/rules/openapi/functions/oas-additional-operations.js +16 -22
- package/dist/rules/openapi/functions/oas-discriminator.js +24 -22
- package/dist/rules/openapi/functions/oas-example-external-value.js +13 -21
- package/dist/rules/openapi/functions/oas-example-value.js +24 -27
- package/dist/rules/openapi/functions/oas-mutually-exclusive.js +15 -19
- package/dist/rules/openapi/functions/oas-no-nullable.js +13 -21
- package/dist/rules/openapi/functions/oas-op-form-data-consume-check.js +21 -19
- package/dist/rules/openapi/functions/oas-op-id-unique.js +27 -27
- package/dist/rules/openapi/functions/oas-op-params.js +36 -42
- package/dist/rules/openapi/functions/oas-op-security-defined.js +40 -39
- package/dist/rules/openapi/functions/oas-op-success-response.js +11 -13
- package/dist/rules/openapi/functions/oas-path-param.js +75 -96
- package/dist/rules/openapi/functions/oas-schema-example-deprecated.js +33 -40
- package/dist/rules/openapi/functions/oas-schema.js +9 -14
- package/dist/rules/openapi/functions/oas-server-name-unique.js +21 -19
- package/dist/rules/openapi/functions/oas-server-variables.js +45 -49
- package/dist/rules/openapi/functions/oas-tag-defined.js +20 -20
- package/dist/rules/openapi/functions/oas-tag-kind.js +16 -16
- package/dist/rules/openapi/functions/oas-tag-parent-defined.js +40 -43
- package/dist/rules/openapi/functions/oas-tags-unique.js +18 -16
- package/dist/rules/openapi/functions/oas-unused-component.js +48 -58
- package/dist/rules/openapi/functions/ref-siblings.js +13 -11
- package/dist/rules/openapi/index.js +99 -117
- package/dist/rules/openapi/oas.js +524 -536
- package/dist/rules/openapi/schemas/index.js +17 -33
- package/dist/rules/openapi/schemas/oas20.json +1 -1592
- package/dist/rules/openapi/schemas/oas30.json +1 -1651
- package/dist/rules/openapi/schemas/oas31.json +1 -1412
- package/dist/rules/openapi/schemas/oas32.json +1 -1684
- package/package.json +5 -5
|
@@ -1,127 +1,109 @@
|
|
|
1
|
-
import { readFileSync } from
|
|
2
|
-
import { createRequire } from
|
|
3
|
-
import { dirname, isAbsolute, resolve as resolvePath } from
|
|
4
|
-
import { createRuleset as createCoreRuleset
|
|
5
|
-
import { builtinFunctions } from
|
|
6
|
-
import { parseWithPointers } from
|
|
7
|
-
import { oasFormats } from
|
|
8
|
-
import { oasFunctions } from
|
|
9
|
-
import { oas } from
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const
|
|
18
|
-
/** The names that resolve to the built-in OpenAPI ruleset (incl. the legacy Spectral alias). */
|
|
19
|
-
const OAS_RULESET_NAMES = new Set(['oas', 'loupe:oas', 'spectral:oas']);
|
|
20
|
-
/** Loads a ruleset definition from a file path by extension (YAML/JSON parsed, JS/CJS/MJS required). */
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { dirname, isAbsolute, resolve as resolvePath } from "node:path";
|
|
4
|
+
import { createRuleset as createCoreRuleset } from "../../core/index.js";
|
|
5
|
+
import { builtinFunctions } from "../../functions/index.js";
|
|
6
|
+
import { parseWithPointers } from "../../parsers/index.js";
|
|
7
|
+
import { oasFormats } from "./formats.js";
|
|
8
|
+
import { oasFunctions } from "./functions/index.js";
|
|
9
|
+
import { oas } from "./oas.js";
|
|
10
|
+
import { oasFixers } from "./fixers.js";
|
|
11
|
+
import { oas2, oas3, oas3_0, oas3_1, oas3_2, oasFormats as oasFormats2 } from "./formats.js";
|
|
12
|
+
import { oasFunctions as oasFunctions2 } from "./functions/index.js";
|
|
13
|
+
import { oas as oas4 } from "./oas.js";
|
|
14
|
+
import { loadOasSchema } from "./schemas/index.js";
|
|
15
|
+
const allFunctions = { ...builtinFunctions, ...oasFunctions };
|
|
16
|
+
const require2 = createRequire(import.meta.url);
|
|
17
|
+
const OAS_RULESET_NAMES = /* @__PURE__ */ new Set(["oas", "loupe:oas", "spectral:oas"]);
|
|
21
18
|
const loadRulesetFile = (file) => {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
19
|
+
if (/\.(ya?ml|json)$/i.test(file)) {
|
|
20
|
+
return parseWithPointers(readFileSync(file, "utf8")).data;
|
|
21
|
+
}
|
|
22
|
+
const module = require2(file);
|
|
23
|
+
return module.default ?? module;
|
|
27
24
|
};
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
let file;
|
|
43
|
-
try {
|
|
44
|
-
file = require.resolve(name, { paths: [basePath] });
|
|
45
|
-
}
|
|
46
|
-
catch {
|
|
47
|
-
throw new Error(`Cannot resolve extended ruleset "${name}" from ${basePath}`);
|
|
48
|
-
}
|
|
49
|
-
return { definition: loadRulesetFile(file), basePath: dirname(file) };
|
|
25
|
+
const resolveOpenApiRuleset = (name, basePath = process.cwd()) => {
|
|
26
|
+
if (OAS_RULESET_NAMES.has(name))
|
|
27
|
+
return { definition: oas, basePath };
|
|
28
|
+
if (name.startsWith(".") || isAbsolute(name)) {
|
|
29
|
+
const file2 = resolvePath(basePath, name);
|
|
30
|
+
return { definition: loadRulesetFile(file2), basePath: dirname(file2) };
|
|
31
|
+
}
|
|
32
|
+
let file;
|
|
33
|
+
try {
|
|
34
|
+
file = require2.resolve(name, { paths: [basePath] });
|
|
35
|
+
} catch {
|
|
36
|
+
throw new Error(`Cannot resolve extended ruleset "${name}" from ${basePath}`);
|
|
37
|
+
}
|
|
38
|
+
return { definition: loadRulesetFile(file), basePath: dirname(file) };
|
|
50
39
|
};
|
|
51
|
-
/** Loads a single custom function module (`<dir>/<name>.{js,cjs,mjs}` or a bare path). */
|
|
52
40
|
const loadFunctionByName = (basePath, dir, name) => {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
throw error;
|
|
66
|
-
}
|
|
41
|
+
const baseFile = resolvePath(basePath, dir, name);
|
|
42
|
+
for (const candidate of [baseFile, `${baseFile}.js`, `${baseFile}.cjs`, `${baseFile}.mjs`]) {
|
|
43
|
+
try {
|
|
44
|
+
const resolvedFile = require2.resolve(candidate);
|
|
45
|
+
const module = require2(resolvedFile);
|
|
46
|
+
const fn = module.default ?? module;
|
|
47
|
+
if (typeof fn !== "function")
|
|
48
|
+
throw new Error(`"${name}" did not export a function`);
|
|
49
|
+
return fn;
|
|
50
|
+
} catch (error) {
|
|
51
|
+
if (error.code !== "MODULE_NOT_FOUND")
|
|
52
|
+
throw error;
|
|
67
53
|
}
|
|
68
|
-
|
|
54
|
+
}
|
|
55
|
+
throw new Error(`Cannot resolve custom function "${name}" from ${resolvePath(basePath, dir)}`);
|
|
69
56
|
};
|
|
70
|
-
/**
|
|
71
|
-
* Walks a ruleset definition (and its string `extends`) collecting custom
|
|
72
|
-
* functions declared via `functions` / `functionsDir`, each loaded relative to
|
|
73
|
-
* the directory of the ruleset that declared it. The built-in `oas` names carry
|
|
74
|
-
* no custom functions, so they are skipped.
|
|
75
|
-
*/
|
|
76
57
|
const collectCustomFunctions = (definition, basePath, into, seen) => {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
if (Array.isArray(definition.functions)) {
|
|
96
|
-
const dir = definition.functionsDir ?? 'functions';
|
|
97
|
-
for (const name of definition.functions)
|
|
98
|
-
into[name] = loadFunctionByName(basePath, dir, name);
|
|
58
|
+
if (seen.has(definition))
|
|
59
|
+
return;
|
|
60
|
+
seen.add(definition);
|
|
61
|
+
if (definition.extends) {
|
|
62
|
+
const entries = Array.isArray(definition.extends) ? definition.extends : [definition.extends];
|
|
63
|
+
for (const entry of entries) {
|
|
64
|
+
const target = Array.isArray(entry) ? entry[0] : entry;
|
|
65
|
+
if (typeof target === "string") {
|
|
66
|
+
if (OAS_RULESET_NAMES.has(target))
|
|
67
|
+
continue;
|
|
68
|
+
const resolved = resolveOpenApiRuleset(target, basePath);
|
|
69
|
+
collectCustomFunctions(resolved.definition, resolved.basePath, into, seen);
|
|
70
|
+
} else {
|
|
71
|
+
collectCustomFunctions(target, basePath, into, seen);
|
|
72
|
+
}
|
|
99
73
|
}
|
|
74
|
+
}
|
|
75
|
+
if (Array.isArray(definition.functions)) {
|
|
76
|
+
const dir = definition.functionsDir ?? "functions";
|
|
77
|
+
for (const name of definition.functions)
|
|
78
|
+
into[name] = loadFunctionByName(basePath, dir, name);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
const createOpenApiRuleset = (definition, basePath) => {
|
|
82
|
+
const resolved = definition ?? { extends: [oas] };
|
|
83
|
+
let functions = allFunctions;
|
|
84
|
+
const custom = {};
|
|
85
|
+
collectCustomFunctions(resolved, basePath ?? process.cwd(), custom, /* @__PURE__ */ new Set());
|
|
86
|
+
if (Object.keys(custom).length > 0)
|
|
87
|
+
functions = { ...allFunctions, ...custom };
|
|
88
|
+
return createCoreRuleset(resolved, {
|
|
89
|
+
functions,
|
|
90
|
+
formats: oasFormats,
|
|
91
|
+
resolve: resolveOpenApiRuleset,
|
|
92
|
+
...basePath !== void 0 ? { basePath } : {}
|
|
93
|
+
});
|
|
100
94
|
};
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
// to the declaring ruleset's directory and layered over the built-ins.
|
|
116
|
-
let functions = allFunctions;
|
|
117
|
-
const custom = {};
|
|
118
|
-
collectCustomFunctions(resolved, basePath ?? process.cwd(), custom, new Set());
|
|
119
|
-
if (Object.keys(custom).length > 0)
|
|
120
|
-
functions = { ...allFunctions, ...custom };
|
|
121
|
-
return createCoreRuleset(resolved, {
|
|
122
|
-
functions,
|
|
123
|
-
formats: oasFormats,
|
|
124
|
-
resolve: resolveOpenApiRuleset,
|
|
125
|
-
...(basePath !== undefined ? { basePath } : {}),
|
|
126
|
-
});
|
|
95
|
+
export {
|
|
96
|
+
allFunctions,
|
|
97
|
+
createOpenApiRuleset,
|
|
98
|
+
loadOasSchema,
|
|
99
|
+
oas4 as oas,
|
|
100
|
+
oas2,
|
|
101
|
+
oas3,
|
|
102
|
+
oas3_0,
|
|
103
|
+
oas3_1,
|
|
104
|
+
oas3_2,
|
|
105
|
+
oasFixers,
|
|
106
|
+
oasFormats2 as oasFormats,
|
|
107
|
+
oasFunctions2 as oasFunctions,
|
|
108
|
+
resolveOpenApiRuleset
|
|
127
109
|
};
|