contractdiff 0.0.0-stage → 0.4.2
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/LICENSE +21 -0
- package/README.md +228 -2
- package/dist/bin.js +4 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.js +24 -0
- package/dist/command-diff.js +310 -0
- package/dist/command-explain-cap.js +36 -0
- package/dist/command-explain-inline.js +17 -0
- package/dist/command-explain-name.js +31 -0
- package/dist/command-explain-operation.js +85 -0
- package/dist/command-explain-text.js +21 -0
- package/dist/command-explain.js +54 -0
- package/dist/command-lint.js +280 -0
- package/dist/command-migrate.js +70 -0
- package/dist/command-plugin.js +32 -0
- package/dist/command-props.js +116 -0
- package/dist/commands.js +7 -0
- package/dist/core/index.d.ts +2 -0
- package/dist/core/index.js +2 -0
- package/dist/core/model.d.ts +46 -0
- package/dist/core/model.js +1 -0
- package/dist/core/resolve.d.ts +22 -0
- package/dist/core/resolve.js +71 -0
- package/dist/diff-classify.js +30 -0
- package/dist/diff-contract.js +176 -0
- package/dist/diff-decisions.js +446 -0
- package/dist/diff-normalise.js +143 -0
- package/dist/diff-rules.js +109 -0
- package/dist/diff-schema.js +335 -0
- package/dist/diff-security.js +33 -0
- package/dist/diff-transitive.js +26 -0
- package/dist/diff-usage.js +59 -0
- package/dist/extract-normalise.js +85 -0
- package/dist/extract-roundtrip.js +28 -0
- package/dist/extract-standard.js +79 -0
- package/dist/gen-ts-ast.d.ts +135 -0
- package/dist/gen-ts-ast.js +52 -0
- package/dist/gen-ts-bundle.js +233 -0
- package/dist/gen-ts-comment.js +48 -0
- package/dist/gen-ts-context.d.ts +83 -0
- package/dist/gen-ts-context.js +45 -0
- package/dist/gen-ts-dict.d.ts +14 -0
- package/dist/gen-ts-dict.js +14 -0
- package/dist/gen-ts-discriminators.d.ts +7 -0
- package/dist/gen-ts-discriminators.js +117 -0
- package/dist/gen-ts-document.d.ts +12 -0
- package/dist/gen-ts-document.js +82 -0
- package/dist/gen-ts-enums.js +139 -0
- package/dist/gen-ts-helpers.js +30 -0
- package/dist/gen-ts-index.d.ts +49 -0
- package/dist/gen-ts-index.js +110 -0
- package/dist/gen-ts-operations.js +277 -0
- package/dist/gen-ts-print.js +113 -0
- package/dist/gen-ts-ref.d.ts +30 -0
- package/dist/gen-ts-ref.js +75 -0
- package/dist/gen-ts-schema.js +283 -0
- package/dist/gen-ts-text.d.ts +24 -0
- package/dist/gen-ts-text.js +98 -0
- package/dist/host.d.ts +145 -0
- package/dist/host.js +273 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +5 -0
- package/dist/input-asyncapi.js +167 -0
- package/dist/input-buf.js +58 -0
- package/dist/input-jsonschema.js +165 -0
- package/dist/input-mcp.js +111 -0
- package/dist/input-overlay-jsonpath.js +265 -0
- package/dist/input-overlay.js +138 -0
- package/dist/input-protobuf.js +212 -0
- package/dist/input-swagger2.js +331 -0
- package/dist/inputs.js +8 -0
- package/dist/lint-code-read.js +129 -0
- package/dist/lint-code.d.ts +56 -0
- package/dist/lint-code.js +64 -0
- package/dist/lint-engine.d.ts +93 -0
- package/dist/lint-engine.js +87 -0
- package/dist/lint-functions.js +95 -0
- package/dist/lint-history.js +133 -0
- package/dist/lint-jsonpath.js +92 -0
- package/dist/lint-names.d.ts +11 -0
- package/dist/lint-names.js +2 -0
- package/dist/lint-redocly.js +141 -0
- package/dist/lint-rules.js +189 -0
- package/dist/lint-select.d.ts +33 -0
- package/dist/lint-select.js +129 -0
- package/dist/lint-since.js +20 -0
- package/dist/lint-spectral.js +167 -0
- package/dist/lint-stats.js +58 -0
- package/dist/migrate-hey-api.js +144 -0
- package/dist/migrate-literal.js +139 -0
- package/dist/migrate-openapi-fetch.js +62 -0
- package/dist/migrate-openapi-typescript.js +159 -0
- package/dist/migrate-orval.js +119 -0
- package/dist/migrate-shared.js +31 -0
- package/dist/openapi-typescript.d.ts +9 -0
- package/dist/openapi-typescript.js +36 -0
- package/dist/openapi.d.ts +20 -0
- package/dist/openapi.js +94 -0
- package/dist/plugin-express.js +66 -0
- package/dist/plugin-extract-hono.js +1 -0
- package/dist/plugin-extract-nest.js +1 -0
- package/dist/plugin-extract-openapi.js +246 -0
- package/dist/plugin-extract-standard-schema.js +42 -0
- package/dist/plugin-extract-trpc.js +1 -0
- package/dist/plugin-extract-ts-rest.js +77 -0
- package/dist/plugin-fastify.js +68 -0
- package/dist/plugin-fetch-client.js +172 -0
- package/dist/plugin-fetch-routes.js +110 -0
- package/dist/plugin-fetch-sample.js +155 -0
- package/dist/plugin-fetch-text.js +5 -0
- package/dist/plugin-fetch.js +19 -0
- package/dist/plugin-hono.js +42 -0
- package/dist/plugin-json-schema-build.js +289 -0
- package/dist/plugin-json-schema-dialect-anthropic.js +41 -0
- package/dist/plugin-json-schema-dialect-gemini.js +29 -0
- package/dist/plugin-json-schema-dialect-mcp-elicitation.js +32 -0
- package/dist/plugin-json-schema-dialect-mcp.js +13 -0
- package/dist/plugin-json-schema-dialect-openai.js +37 -0
- package/dist/plugin-json-schema-dialect.js +180 -0
- package/dist/plugin-json-schema.js +125 -0
- package/dist/plugin-markdown.js +114 -0
- package/dist/plugin-mcp-code.js +28 -0
- package/dist/plugin-mcp-tools.js +152 -0
- package/dist/plugin-mcp.js +107 -0
- package/dist/plugin-msw-files.js +50 -0
- package/dist/plugin-msw-sample.js +45 -0
- package/dist/plugin-msw-synth.js +116 -0
- package/dist/plugin-msw.js +30 -0
- package/dist/plugin-nest-code.js +68 -0
- package/dist/plugin-nest-contract.js +23 -0
- package/dist/plugin-nest.js +44 -0
- package/dist/plugin-server-code.js +61 -0
- package/dist/plugin-server-contract.js +48 -0
- package/dist/plugin-server-example.js +62 -0
- package/dist/plugin-server-router.js +77 -0
- package/dist/plugin-server-routes.js +155 -0
- package/dist/plugin-server-runtime.js +3 -0
- package/dist/plugin-server-sample.js +40 -0
- package/dist/plugin-swr-sample.js +21 -0
- package/dist/plugin-swr.js +40 -0
- package/dist/plugin-tanstack-query-sample.js +21 -0
- package/dist/plugin-tanstack-query.js +59 -0
- package/dist/plugin-types.js +80 -0
- package/dist/plugin-validator-code.js +41 -0
- package/dist/plugin-validator-names.js +27 -0
- package/dist/plugin-validator.js +103 -0
- package/dist/plugins.js +21 -0
- package/dist/props-emit.js +194 -0
- package/dist/props-generate.js +409 -0
- package/dist/props-judge.js +61 -0
- package/dist/props-mutate.js +144 -0
- package/dist/props-random.js +25 -0
- package/dist/props-run.js +176 -0
- package/dist/props-shrink.js +75 -0
- package/dist/run-apply.js +25 -0
- package/dist/run-command.js +31 -0
- package/dist/run-commit.d.ts +51 -0
- package/dist/run-commit.js +48 -0
- package/dist/run-config.d.ts +193 -0
- package/dist/run-config.js +256 -0
- package/dist/run-diff.js +24 -0
- package/dist/run-extract.d.ts +13 -0
- package/dist/run-extract.js +21 -0
- package/dist/run-gen.d.ts +15 -0
- package/dist/run-gen.js +43 -0
- package/dist/run-header.js +37 -0
- package/dist/run-input.js +31 -0
- package/dist/run-load.js +32 -0
- package/dist/run-map.d.ts +41 -0
- package/dist/run-map.js +52 -0
- package/dist/run-plan.d.ts +31 -0
- package/dist/run-plan.js +35 -0
- package/dist/scaffold-seed.js +43 -0
- package/dist/scaffold-write.js +17 -0
- package/dist/scaffold.js +196 -0
- package/dist/testing-lint.js +43 -0
- package/dist/testing-owner.js +17 -0
- package/dist/testing-probe.js +25 -0
- package/dist/testing-rules.d.ts +65 -0
- package/dist/testing-rules.js +115 -0
- package/dist/testing.d.ts +32 -0
- package/dist/testing.js +79 -0
- package/package.json +108 -4
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
const isObject = (value) => typeof value === 'object' && value !== null;
|
|
2
|
+
const token = (key) => key.replaceAll('~', '~0').replaceAll('/', '~1');
|
|
3
|
+
const NAME = /^[^.[\]]+/u;
|
|
4
|
+
const MEMBER = /^[\w$-]+$/u;
|
|
5
|
+
const KEY = /^\s*(?:'([^']*)'|"([^"]*)"|([\w$-]+))\s*$/u;
|
|
6
|
+
const literalOf = (text) => {
|
|
7
|
+
const quoted = /^'([^']*)'$|^"([^"]*)"$/u.exec(text);
|
|
8
|
+
if (quoted !== null)
|
|
9
|
+
return quoted[1] ?? quoted[2];
|
|
10
|
+
if (text === 'true' || text === 'false')
|
|
11
|
+
return text === 'true';
|
|
12
|
+
return /^-?\d+(?:\.\d+)?$/u.test(text) ? Number(text) : undefined;
|
|
13
|
+
};
|
|
14
|
+
const filterOf = (inside, path) => {
|
|
15
|
+
if (!inside.startsWith('?(@.') || !inside.endsWith(')'))
|
|
16
|
+
return undefined;
|
|
17
|
+
const [left, right, ...more] = inside.slice('?(@.'.length, -1).split('==');
|
|
18
|
+
const field = left.trim().split('.');
|
|
19
|
+
if (more.length > 0 || !field.every((name) => MEMBER.test(name)))
|
|
20
|
+
return undefined;
|
|
21
|
+
if (right === undefined)
|
|
22
|
+
return { kind: 'filter', field };
|
|
23
|
+
const equals = literalOf(right.trim());
|
|
24
|
+
if (equals === undefined)
|
|
25
|
+
throw new Error(`${path}: the filter compares with \`${right.trim()}\`, which is not a string, number or boolean`);
|
|
26
|
+
return { kind: 'filter', field, equals };
|
|
27
|
+
};
|
|
28
|
+
const bracketOf = (inside, path) => {
|
|
29
|
+
if (inside.trim() === '*')
|
|
30
|
+
return { kind: 'wildcard' };
|
|
31
|
+
const filter = filterOf(inside.trim(), path);
|
|
32
|
+
if (filter !== undefined)
|
|
33
|
+
return filter;
|
|
34
|
+
const keys = inside.split(',').map((part) => KEY.exec(part));
|
|
35
|
+
if (keys.some((key) => key === null))
|
|
36
|
+
throw new Error(`${path}: \`[${inside}]\` is outside the subset (keys, \`*\`, \`?(@.x)\` and \`?(@.x == 'y')\`)`);
|
|
37
|
+
return { kind: 'keys', keys: keys.map((key) => key.slice(1).find((part) => part !== undefined)) };
|
|
38
|
+
};
|
|
39
|
+
const stepAt = (rest, path) => {
|
|
40
|
+
if (rest.startsWith('..'))
|
|
41
|
+
return [{ kind: 'descend' }, rest.startsWith('..[') ? rest.slice(2) : rest.slice(1)];
|
|
42
|
+
if (rest.startsWith('[')) {
|
|
43
|
+
const end = rest.indexOf(']');
|
|
44
|
+
if (end < 0)
|
|
45
|
+
throw new Error(`${path}: a \`[\` is never closed`);
|
|
46
|
+
return [bracketOf(rest.slice(1, end), path), rest.slice(end + 1)];
|
|
47
|
+
}
|
|
48
|
+
const key = rest.startsWith('.') ? NAME.exec(rest.slice(1))?.[0] : undefined;
|
|
49
|
+
if (key === undefined || /[~^@()?]/u.test(key))
|
|
50
|
+
throw new Error(`${path}: \`${rest}\` is outside the subset`);
|
|
51
|
+
return [key === '*' ? { kind: 'wildcard' } : { kind: 'keys', keys: [key] }, rest.slice(1 + key.length)];
|
|
52
|
+
};
|
|
53
|
+
export const parse = (path) => {
|
|
54
|
+
if (!path.startsWith('$'))
|
|
55
|
+
throw new Error(`${path}: a path starts at the root, \`$\``);
|
|
56
|
+
const steps = [];
|
|
57
|
+
let rest = path.slice(1);
|
|
58
|
+
while (rest.length > 0) {
|
|
59
|
+
const [step, after] = stepAt(rest, path);
|
|
60
|
+
steps.push(step);
|
|
61
|
+
rest = after;
|
|
62
|
+
}
|
|
63
|
+
return steps;
|
|
64
|
+
};
|
|
65
|
+
const childrenOf = (match) => isObject(match.value) ? Object.entries(match.value).map(([key, value]) => ({ pointer: `${match.pointer}/${token(key)}`, value })) : [];
|
|
66
|
+
const descendantsOf = (match) => [match, ...childrenOf(match).flatMap((child) => descendantsOf(child))];
|
|
67
|
+
const memberAt = (value, field) => field.reduce((at, key) => (isObject(at) ? at[key] : undefined), value);
|
|
68
|
+
const stepOn = (step, match) => {
|
|
69
|
+
switch (step.kind) {
|
|
70
|
+
case 'keys': {
|
|
71
|
+
const members = new Map(childrenOf(match).map((child) => [child.pointer, child]));
|
|
72
|
+
return step.keys.flatMap((key) => members.get(`${match.pointer}/${token(key)}`) ?? []);
|
|
73
|
+
}
|
|
74
|
+
case 'wildcard': {
|
|
75
|
+
return childrenOf(match);
|
|
76
|
+
}
|
|
77
|
+
case 'descend': {
|
|
78
|
+
return descendantsOf(match);
|
|
79
|
+
}
|
|
80
|
+
case 'filter': {
|
|
81
|
+
return childrenOf(match).filter((child) => {
|
|
82
|
+
const member = memberAt(child.value, step.field);
|
|
83
|
+
return step.equals === undefined ? member !== undefined && member !== null && member !== false : member === step.equals;
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
export const query = (document, steps) => {
|
|
89
|
+
const matches = steps.reduce((at, step) => at.flatMap((match) => stepOn(step, match)), [{ pointer: '#', value: document }]);
|
|
90
|
+
const seen = new Set();
|
|
91
|
+
return matches.filter((match) => !seen.has(match.pointer) && Boolean(seen.add(match.pointer)));
|
|
92
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The names `lint`'s rules are written with: the built-in `then` functions. Apart from
|
|
3
|
+
* `lint-functions.ts`, so the plugin host can check a contributed rule without loading the
|
|
4
|
+
* functions' validator.
|
|
5
|
+
*/
|
|
6
|
+
export declare const FUNCTIONS: readonly ["truthy", "falsy", "defined", "undefined", "pattern", "casing", "enumeration", "length", "xor", "schema"];
|
|
7
|
+
export type FunctionName = (typeof FUNCTIONS)[number];
|
|
8
|
+
/** The functions on code (R14): they judge a handler, so only a rule with `given.implementation` names them. */
|
|
9
|
+
export declare const CODE_FUNCTIONS: readonly ["calls", "validates-input", "returns-error-shape"];
|
|
10
|
+
export type CodeFunctionName = (typeof CODE_FUNCTIONS)[number];
|
|
11
|
+
export type ThenName = FunctionName | CodeFunctionName;
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { patternOf, plainMessage, uncarried } from './lint-spectral.js';
|
|
2
|
+
export const REDOCLY_RECOMMENDED = {
|
|
3
|
+
'info-description': { id: 'info-description', level: 'warn' },
|
|
4
|
+
'operation-description': { id: 'operation-description', level: 'warn' },
|
|
5
|
+
'operation-operationId': { id: 'operation-operation-id', level: 'warn' },
|
|
6
|
+
'operation-operationId-unique': { id: 'operation-operation-id-unique', level: 'error' },
|
|
7
|
+
'operation-2xx-response': { id: 'operation-success-response', level: 'warn' },
|
|
8
|
+
'parameter-description': { id: 'parameter-description', level: 'warn' },
|
|
9
|
+
'no-path-trailing-slash': { id: 'path-no-trailing-slash', level: 'error' },
|
|
10
|
+
'no-unresolved-refs': { id: 'no-unresolved-ref', level: 'error' },
|
|
11
|
+
'no-unused-components': { id: 'no-unused-component', level: 'warn' },
|
|
12
|
+
'no-invalid-media-type-examples': { id: 'example-valid', level: 'warn' },
|
|
13
|
+
};
|
|
14
|
+
const SUBJECTS = {
|
|
15
|
+
Root: { select: 'document' },
|
|
16
|
+
Info: { select: 'path', path: '$.info' },
|
|
17
|
+
Operation: { select: 'operation' },
|
|
18
|
+
Parameter: { select: 'parameter' },
|
|
19
|
+
Schema: { select: 'schema' },
|
|
20
|
+
};
|
|
21
|
+
const CASINGS = {
|
|
22
|
+
camelCase: 'camel',
|
|
23
|
+
PascalCase: 'pascal',
|
|
24
|
+
'kebab-case': 'kebab',
|
|
25
|
+
snake_case: 'snake',
|
|
26
|
+
MACRO_CASE: 'macro',
|
|
27
|
+
flatcase: 'flat',
|
|
28
|
+
};
|
|
29
|
+
const LEVELS = new Map([
|
|
30
|
+
['error', 'error'],
|
|
31
|
+
['warn', 'warn'],
|
|
32
|
+
['off', 'off'],
|
|
33
|
+
]);
|
|
34
|
+
const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
35
|
+
const token = (key) => key.replaceAll('~', '~0').replaceAll('/', '~1');
|
|
36
|
+
const levelOf = (severity) => LEVELS.get(severity ?? 'error') ?? uncarried(`severity \`${String(severity)}\` is not error, warn or off`);
|
|
37
|
+
const thenOf = (name, value) => {
|
|
38
|
+
switch (name) {
|
|
39
|
+
case 'defined': {
|
|
40
|
+
return { function: value === false ? 'undefined' : 'defined' };
|
|
41
|
+
}
|
|
42
|
+
case 'nonEmpty': {
|
|
43
|
+
return { function: 'length', functionOptions: { min: 1 } };
|
|
44
|
+
}
|
|
45
|
+
case 'pattern': {
|
|
46
|
+
return { function: 'pattern', functionOptions: { match: patternOf(value, 'match') } };
|
|
47
|
+
}
|
|
48
|
+
case 'notPattern': {
|
|
49
|
+
return { function: 'pattern', functionOptions: { notMatch: patternOf(value, 'notMatch') } };
|
|
50
|
+
}
|
|
51
|
+
case 'casing': {
|
|
52
|
+
const type = CASINGS[String(value)] ?? uncarried(`casing \`${String(value)}\` is not one of ${Object.keys(CASINGS).join(', ')}`);
|
|
53
|
+
return { function: 'casing', functionOptions: { type } };
|
|
54
|
+
}
|
|
55
|
+
case 'enum': {
|
|
56
|
+
return Array.isArray(value) ? { function: 'enumeration', functionOptions: { values: value } } : uncarried('`enum` is not a list');
|
|
57
|
+
}
|
|
58
|
+
case 'const': {
|
|
59
|
+
return { function: 'enumeration', functionOptions: { values: [value] } };
|
|
60
|
+
}
|
|
61
|
+
case 'minLength':
|
|
62
|
+
case 'maxLength': {
|
|
63
|
+
if (typeof value !== 'number')
|
|
64
|
+
return uncarried(`\`${name}\` is not a number`);
|
|
65
|
+
return { function: 'length', functionOptions: name === 'minLength' ? { min: value } : { max: value } };
|
|
66
|
+
}
|
|
67
|
+
default: {
|
|
68
|
+
return uncarried(`assertion \`${name}\` has no core function that means the same`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
const ruleOf = (id, value, file) => {
|
|
73
|
+
for (const key of ['where', 'function'])
|
|
74
|
+
if (value[key] !== undefined)
|
|
75
|
+
uncarried(`\`${key}\` is not carried`);
|
|
76
|
+
const subject = isRecord(value.subject) ? value.subject : uncarried('it has no `subject`');
|
|
77
|
+
const given = SUBJECTS[String(subject.type)] ?? uncarried(`subject type \`${String(subject.type)}\` is not one of ${Object.keys(SUBJECTS).join(', ')}`);
|
|
78
|
+
for (const key of Object.keys(subject))
|
|
79
|
+
if (key !== 'type' && key !== 'property')
|
|
80
|
+
uncarried(`\`subject.${key}\` is not carried`);
|
|
81
|
+
const fields = subject.property === undefined ? [undefined] : [subject.property].flat().map((field) => (typeof field === 'string' ? field : uncarried('`subject.property` is not a name or a list of names')));
|
|
82
|
+
if (fields.length === 0)
|
|
83
|
+
uncarried('`subject.property` is an empty list');
|
|
84
|
+
const assertions = isRecord(value.assertions) && Object.keys(value.assertions).length > 0 ? value.assertions : uncarried('it has no `assertions`');
|
|
85
|
+
const thens = fields.flatMap((field) => Object.entries(assertions).map(([name, option]) => ({ ...thenOf(name, option), ...(field === undefined ? {} : { field }) })));
|
|
86
|
+
const description = plainMessage(value.message) ?? id;
|
|
87
|
+
return {
|
|
88
|
+
rule: { description, given, then: thens.length === 1 ? thens[0] : thens, fix: `make it hold, as \`${id}\` in ${file} asks: ${description}` },
|
|
89
|
+
level: levelOf(value.severity),
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
const extendsOf = (value, file) => {
|
|
93
|
+
const levels = new Map();
|
|
94
|
+
const notCarried = [];
|
|
95
|
+
for (const [index, name] of [value ?? []].flat().entries()) {
|
|
96
|
+
const pointer = `#/extends/${String(index)}`;
|
|
97
|
+
if (name !== 'recommended') {
|
|
98
|
+
notCarried.push({ id: String(name), file, pointer, reason: `extends \`${String(name)}\`, which contractdiff does not read`, fix: `name the rules you need from \`${String(name)}\` in ${file}` });
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
for (const { id, level } of Object.values(REDOCLY_RECOMMENDED))
|
|
102
|
+
levels.set(id, level);
|
|
103
|
+
notCarried.push({
|
|
104
|
+
id: 'recommended',
|
|
105
|
+
file,
|
|
106
|
+
pointer,
|
|
107
|
+
reason: `carries ${String(Object.keys(REDOCLY_RECOMMENDED).length)} of its rules as contractdiff equivalents (${Object.keys(REDOCLY_RECOMMENDED).join(', ')}); the others are not carried`,
|
|
108
|
+
fix: 'write the others you rely on as `rule/…` assertions in this file, or run `contractdiff lint --ruleset recommended` for the built-in set',
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
return { levels, notCarried };
|
|
112
|
+
};
|
|
113
|
+
const IGNORED = ['plugins', 'decorators', 'preprocessors', 'apis', 'configurable-rules'];
|
|
114
|
+
const entryOf = (id, definition, into) => {
|
|
115
|
+
if (isRecord(definition) && id.startsWith('rule/')) {
|
|
116
|
+
const carried = ruleOf(id, definition, into.file);
|
|
117
|
+
into.rules.set(id, carried.rule);
|
|
118
|
+
into.levels.set(id, carried.level);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
const severity = isRecord(definition) ? definition.severity : definition;
|
|
122
|
+
const target = REDOCLY_RECOMMENDED[id]?.id ?? (into.known.has(id) ? id : uncarried(`it sets the level of \`${id}\`, which no carried ruleset defines`));
|
|
123
|
+
into.levels.set(target, levelOf(severity));
|
|
124
|
+
};
|
|
125
|
+
export const readRedocly = (document, file, known) => {
|
|
126
|
+
const value = isRecord(document) ? document : {};
|
|
127
|
+
const { levels, notCarried } = extendsOf(value.extends, file);
|
|
128
|
+
for (const key of IGNORED.filter((each) => value[each] !== undefined)) {
|
|
129
|
+
notCarried.push({ id: key, file, pointer: `#/${key}`, reason: `\`${key}\` is not carried`, fix: `keep \`${key}\` running in Redocly; lint reads \`rules\` and \`extends\`` });
|
|
130
|
+
}
|
|
131
|
+
const rules = new Map();
|
|
132
|
+
for (const [id, definition] of Object.entries(isRecord(value.rules) ? value.rules : {})) {
|
|
133
|
+
try {
|
|
134
|
+
entryOf(id, definition, { file, known, rules, levels });
|
|
135
|
+
}
|
|
136
|
+
catch (error) {
|
|
137
|
+
notCarried.push({ id, file, pointer: `#/rules/${token(id)}`, reason: error.message, fix: 'rewrite it as a `rule/…` with a subject contractdiff selects and assertions it carries, or keep it in Redocly' });
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return { rules: Object.fromEntries(rules), levels: Object.fromEntries(levels), notCarried };
|
|
141
|
+
};
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { inline } from './core/resolve.js';
|
|
2
|
+
import { validAgainst } from './lint-functions.js';
|
|
3
|
+
import { at } from './lint-select.js';
|
|
4
|
+
const HTTP = ['openapi', 'swagger2'];
|
|
5
|
+
const membersOf = (value) => Object(value);
|
|
6
|
+
export const MAX_DESCRIPTION = 300;
|
|
7
|
+
export const OVERLAP = 0.8;
|
|
8
|
+
const MIN_WORD = 4;
|
|
9
|
+
const textOf = (value) => (typeof value === 'string' ? value.trim() : '');
|
|
10
|
+
const proseOf = (node) => textOf(node.value.description) || textOf(node.value.summary);
|
|
11
|
+
export const wordsOf = (text) => new Set(text.toLowerCase().split(/[^\p{L}\p{N}]+/u).filter((word) => word.length >= MIN_WORD));
|
|
12
|
+
export const overlapOf = (a, b) => {
|
|
13
|
+
const shared = [...a].filter((word) => b.has(word)).length;
|
|
14
|
+
const union = a.size + b.size - shared;
|
|
15
|
+
return union === 0 ? 0 : shared / union;
|
|
16
|
+
};
|
|
17
|
+
const refsIn = (value) => {
|
|
18
|
+
if (typeof value !== 'object' || value === null)
|
|
19
|
+
return [];
|
|
20
|
+
return Object.entries(value).flatMap(([key, child]) => (key === '$ref' && typeof child === 'string' ? [child] : refsIn(child)));
|
|
21
|
+
};
|
|
22
|
+
const resolves = (document, ref) => at(document, ref) !== undefined;
|
|
23
|
+
const PERCENT = 100;
|
|
24
|
+
const draftOf = (document) => {
|
|
25
|
+
const { openapi, swagger } = membersOf(document);
|
|
26
|
+
return swagger !== undefined || (typeof openapi === 'string' && openapi.startsWith('3.0')) ? 'openapi-3.0' : '2020-12';
|
|
27
|
+
};
|
|
28
|
+
const inlinedOf = (document, schema) => {
|
|
29
|
+
try {
|
|
30
|
+
return inline(document, schema);
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
const badExamples = (node, document) => {
|
|
37
|
+
const { example, examples } = node.value;
|
|
38
|
+
const samples = [...(example === undefined ? [] : [example]), ...(Array.isArray(examples) ? examples : [])];
|
|
39
|
+
if (samples.length === 0)
|
|
40
|
+
return [];
|
|
41
|
+
const inlined = inlinedOf(document, node.value);
|
|
42
|
+
if (inlined === undefined)
|
|
43
|
+
return [];
|
|
44
|
+
const unreadable = [...inlined.cycles, ...inlined.external];
|
|
45
|
+
if (unreadable.length > 0)
|
|
46
|
+
return [`example cannot be checked: its schema reaches ${unreadable.join(', ')}, which repeats or is in another file`];
|
|
47
|
+
const schema = inlined.value;
|
|
48
|
+
return samples.flatMap((sample) => {
|
|
49
|
+
try {
|
|
50
|
+
const problem = validAgainst(schema, sample, draftOf(document));
|
|
51
|
+
return problem === undefined ? [] : [`example ${problem}`];
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
return [`example cannot be checked: ${error.message}`];
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
export const rules = {
|
|
59
|
+
'operation-operation-id': {
|
|
60
|
+
description: 'Every HTTP operation has an operationId: generated clients, tools and docs are named by it',
|
|
61
|
+
given: { select: 'operation', formats: HTTP },
|
|
62
|
+
then: { field: 'operationId', function: 'truthy' },
|
|
63
|
+
fix: 'add an `operationId`, a verb and a noun in camelCase such as `listPets`',
|
|
64
|
+
},
|
|
65
|
+
'operation-operation-id-unique': {
|
|
66
|
+
description: 'No two operations share an operationId',
|
|
67
|
+
given: { select: 'document', formats: HTTP },
|
|
68
|
+
check: (_node, { contract }) => {
|
|
69
|
+
const ids = contract.operations.map((operation) => membersOf(at(contract.document, operation.source)).operationId).filter((id) => typeof id === 'string');
|
|
70
|
+
return [...new Set(ids.filter((id, index) => ids.indexOf(id) !== index))].map((id) => `operationId \`${id}\` is used more than once`);
|
|
71
|
+
},
|
|
72
|
+
fix: 'give each operation its own `operationId`',
|
|
73
|
+
},
|
|
74
|
+
'operation-description': {
|
|
75
|
+
description: 'Every operation says what it does, in a description or a summary',
|
|
76
|
+
given: { select: 'operation' },
|
|
77
|
+
check: (node) => (proseOf(node) === '' ? ['has no description or summary'] : []),
|
|
78
|
+
fix: 'add a `description` saying what the operation does and when to call it',
|
|
79
|
+
},
|
|
80
|
+
'operation-tags': {
|
|
81
|
+
description: 'Every HTTP operation has at least one tag: tags group docs and select tools',
|
|
82
|
+
given: { select: 'operation', formats: HTTP },
|
|
83
|
+
then: { field: 'tags', function: 'length', functionOptions: { min: 1 } },
|
|
84
|
+
fix: 'add `tags` naming the resource the operation belongs to',
|
|
85
|
+
},
|
|
86
|
+
'operation-success-response': {
|
|
87
|
+
description: 'Every HTTP operation declares a 2xx or 3xx response',
|
|
88
|
+
given: { select: 'operation', formats: HTTP },
|
|
89
|
+
check: (node) => (Object.keys(membersOf(node.value.responses)).some((code) => /^(?:[23]\d\d|[23]XX)$/u.test(code)) ? [] : ['declares no 2xx or 3xx response']),
|
|
90
|
+
fix: 'declare the response a successful call returns',
|
|
91
|
+
},
|
|
92
|
+
'parameter-description': {
|
|
93
|
+
description: 'Every parameter is described',
|
|
94
|
+
given: { select: 'parameter' },
|
|
95
|
+
then: { field: 'description', function: 'truthy' },
|
|
96
|
+
fix: 'add a `description` saying what the parameter does and what values it takes',
|
|
97
|
+
},
|
|
98
|
+
'property-description': {
|
|
99
|
+
description: 'Every property of every payload is described',
|
|
100
|
+
given: { select: 'property' },
|
|
101
|
+
then: { field: 'description', function: 'truthy' },
|
|
102
|
+
fix: 'add a `description` saying what the property holds, with its unit if it has one',
|
|
103
|
+
},
|
|
104
|
+
'info-description': {
|
|
105
|
+
description: 'The document says what the API is for',
|
|
106
|
+
given: { select: 'document', formats: ['openapi', 'swagger2', 'asyncapi'] },
|
|
107
|
+
then: { field: 'info.description', function: 'truthy' },
|
|
108
|
+
fix: 'add `info.description`',
|
|
109
|
+
},
|
|
110
|
+
'no-unresolved-ref': {
|
|
111
|
+
description: 'Every local `$ref` points at something the document defines',
|
|
112
|
+
given: { select: 'document' },
|
|
113
|
+
check: (node) => [...new Set(refsIn(node.value).filter((ref) => ref.startsWith('#') && !resolves(node.value, ref)))].map((ref) => `\`${ref}\` points at nothing`),
|
|
114
|
+
fix: 'point the `$ref` at a definition that exists, or add the definition',
|
|
115
|
+
},
|
|
116
|
+
'no-unused-component': {
|
|
117
|
+
description: 'Every schema under components is used',
|
|
118
|
+
given: { select: 'document', formats: ['openapi'] },
|
|
119
|
+
check: (node) => {
|
|
120
|
+
const refs = refsIn(node.value);
|
|
121
|
+
return Object.keys(membersOf(membersOf(node.value.components).schemas))
|
|
122
|
+
.filter((name) => {
|
|
123
|
+
const target = `#/components/schemas/${name.replaceAll('~', '~0').replaceAll('/', '~1')}`;
|
|
124
|
+
return !refs.some((ref) => ref === target || ref.startsWith(`${target}/`));
|
|
125
|
+
})
|
|
126
|
+
.map((name) => `\`components.schemas.${name}\` is never used`);
|
|
127
|
+
},
|
|
128
|
+
fix: 'remove the unused schema, or reference it where it is meant to be used',
|
|
129
|
+
},
|
|
130
|
+
'path-no-trailing-slash': {
|
|
131
|
+
description: 'No path ends with a slash',
|
|
132
|
+
given: { select: 'document', formats: HTTP },
|
|
133
|
+
check: (node) => Object.keys(membersOf(node.value.paths)).filter((path) => path.length > 1 && path.endsWith('/')).map((path) => `path \`${path}\` ends with a slash`),
|
|
134
|
+
fix: 'drop the trailing slash',
|
|
135
|
+
},
|
|
136
|
+
'example-valid': {
|
|
137
|
+
description: 'Every example is valid against the schema it sits in',
|
|
138
|
+
given: { select: 'schema' },
|
|
139
|
+
check: (node, { contract }) => badExamples(node, contract.document),
|
|
140
|
+
fix: 'correct the example, or the schema if the example is right',
|
|
141
|
+
},
|
|
142
|
+
'description-length': {
|
|
143
|
+
description: `An operation's description fits in a tool list: at most ${String(MAX_DESCRIPTION)} characters`,
|
|
144
|
+
given: { select: 'operation' },
|
|
145
|
+
check: (node) => {
|
|
146
|
+
const prose = proseOf(node);
|
|
147
|
+
return prose.length > MAX_DESCRIPTION ? [`description has ${String(prose.length)} characters, more than ${String(MAX_DESCRIPTION)}`] : [];
|
|
148
|
+
},
|
|
149
|
+
fix: `say what the operation does and when to call it in ${String(MAX_DESCRIPTION)} characters; move the rest to the parameters' descriptions or the docs`,
|
|
150
|
+
},
|
|
151
|
+
'description-overlap': {
|
|
152
|
+
description: `No two operations are described in words an agent cannot tell apart (${String(OVERLAP * PERCENT)}% shared or more)`,
|
|
153
|
+
given: { select: 'operation' },
|
|
154
|
+
check: (node, { contract }) => {
|
|
155
|
+
const mine = wordsOf(proseOf(node));
|
|
156
|
+
const index = contract.operations.indexOf(node.operation);
|
|
157
|
+
const earlier = contract.operations.filter((_, position) => position < index);
|
|
158
|
+
return earlier.flatMap((other) => {
|
|
159
|
+
const share = overlapOf(mine, wordsOf(proseOf({ pointer: other.source, value: membersOf(at(contract.document, other.source)) })));
|
|
160
|
+
return share >= OVERLAP ? [`description shares ${String(Math.round(share * PERCENT))}% of its words with \`${other.id}\``] : [];
|
|
161
|
+
});
|
|
162
|
+
},
|
|
163
|
+
fix: 'say in each description what makes the operation different: what it returns, what it changes, and when to call it instead of the other',
|
|
164
|
+
},
|
|
165
|
+
};
|
|
166
|
+
export const rulesets = {
|
|
167
|
+
recommended: {
|
|
168
|
+
'operation-operation-id': 'warn',
|
|
169
|
+
'operation-operation-id-unique': 'warn',
|
|
170
|
+
'operation-description': 'warn',
|
|
171
|
+
'operation-tags': 'warn',
|
|
172
|
+
'operation-success-response': 'warn',
|
|
173
|
+
'parameter-description': 'warn',
|
|
174
|
+
'info-description': 'warn',
|
|
175
|
+
'no-unresolved-ref': 'warn',
|
|
176
|
+
'no-unused-component': 'warn',
|
|
177
|
+
'path-no-trailing-slash': 'warn',
|
|
178
|
+
'example-valid': 'warn',
|
|
179
|
+
},
|
|
180
|
+
'agent-ready': {
|
|
181
|
+
'operation-operation-id': 'warn',
|
|
182
|
+
'operation-description': 'warn',
|
|
183
|
+
'description-length': 'warn',
|
|
184
|
+
'description-overlap': 'warn',
|
|
185
|
+
'parameter-description': 'warn',
|
|
186
|
+
'property-description': 'warn',
|
|
187
|
+
'example-valid': 'warn',
|
|
188
|
+
},
|
|
189
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type Contract, type Operation } from './core/model.js';
|
|
2
|
+
export declare const SELECTS: readonly ["document", "operation", "parameter", "property", "schema", "path"];
|
|
3
|
+
export type Select = (typeof SELECTS)[number];
|
|
4
|
+
export interface Given {
|
|
5
|
+
readonly select: Select;
|
|
6
|
+
/** Only these input formats, e.g. `["openapi", "swagger2"]`. */
|
|
7
|
+
readonly formats?: readonly string[];
|
|
8
|
+
/** Only operations, or members of operations, carrying this tag. */
|
|
9
|
+
readonly tag?: string;
|
|
10
|
+
/** Only operations, or members of operations, with this HTTP method, upper case. */
|
|
11
|
+
readonly method?: string;
|
|
12
|
+
/** With `select: 'path'`: JSONPath into the source document (the R9 subset), one or several. */
|
|
13
|
+
readonly path?: string | readonly string[];
|
|
14
|
+
/** With `select: 'operation'`: judge each operation's handler, the code the user owns (R14), not the spec. */
|
|
15
|
+
readonly implementation?: boolean;
|
|
16
|
+
}
|
|
17
|
+
export interface LintNode {
|
|
18
|
+
/** A JSON Pointer into the source document, or a logical one for an inline payload schema. */
|
|
19
|
+
readonly pointer: string;
|
|
20
|
+
readonly value: Readonly<Record<string, unknown>>;
|
|
21
|
+
/** The operation the node belongs to; absent for the document. */
|
|
22
|
+
readonly operation?: Operation;
|
|
23
|
+
/** A property's or parameter's name. */
|
|
24
|
+
readonly name?: string;
|
|
25
|
+
/** What a `path` selected, as it is: a `then` with no `field` reads this, not `value`. */
|
|
26
|
+
readonly matched?: unknown;
|
|
27
|
+
}
|
|
28
|
+
/** What a pointer names in the document, or `undefined` when it names nothing. */
|
|
29
|
+
export declare const at: (document: unknown, pointer: string) => unknown;
|
|
30
|
+
/** The HTTP method of an operation read from `#/paths/<path>/<method>`, upper case. */
|
|
31
|
+
export declare const methodOf: (operation: Operation) => string | undefined;
|
|
32
|
+
/** The nodes a `given` selects in a contract. */
|
|
33
|
+
export declare const select: (contract: Contract, given: Given) => LintNode[];
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { tagsOf } from './command-explain-operation.js';
|
|
2
|
+
import { pointerGet } from './core/resolve.js';
|
|
3
|
+
import { parse, query } from './lint-jsonpath.js';
|
|
4
|
+
export const SELECTS = ['document', 'operation', 'parameter', 'property', 'schema', 'path'];
|
|
5
|
+
const membersOf = (value) => Object(value);
|
|
6
|
+
function isRecord(value) {
|
|
7
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
8
|
+
}
|
|
9
|
+
const token = (key) => key.replaceAll('~', '~0').replaceAll('/', '~1');
|
|
10
|
+
export const at = (document, pointer) => {
|
|
11
|
+
try {
|
|
12
|
+
return pointerGet(document, pointer);
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
const follow = (document, node, pointer, seen = new Set()) => {
|
|
19
|
+
const ref = membersOf(node).$ref;
|
|
20
|
+
if (typeof ref !== 'string' || seen.has(ref))
|
|
21
|
+
return { node, pointer };
|
|
22
|
+
seen.add(ref);
|
|
23
|
+
const target = at(document, ref);
|
|
24
|
+
return target === undefined ? { node, pointer } : follow(document, target, ref, seen);
|
|
25
|
+
};
|
|
26
|
+
const METHOD_TOKENS = 4;
|
|
27
|
+
export const methodOf = (operation) => {
|
|
28
|
+
const tokens = operation.source.split('/');
|
|
29
|
+
return tokens[1] === 'paths' && tokens.length >= METHOD_TOKENS ? tokens.at(-1).toUpperCase() : undefined;
|
|
30
|
+
};
|
|
31
|
+
const operationsOf = (contract, given) => contract.operations.filter((operation) => (given.tag === undefined || tagsOf(contract, operation).includes(given.tag)) && (given.method === undefined || methodOf(operation) === given.method.toUpperCase()));
|
|
32
|
+
const operationNodes = (contract, given) => operationsOf(contract, given).map((operation) => ({ pointer: operation.source, value: membersOf(at(contract.document, operation.source)), operation }));
|
|
33
|
+
const parameterNodes = (contract, given) => {
|
|
34
|
+
const seen = new Set();
|
|
35
|
+
return operationsOf(contract, given).flatMap((operation) => {
|
|
36
|
+
const item = operation.source.split('/').slice(0, -1).join('/');
|
|
37
|
+
return [item, operation.source].flatMap((owner) => {
|
|
38
|
+
const list = membersOf(at(contract.document, owner)).parameters;
|
|
39
|
+
if (!Array.isArray(list))
|
|
40
|
+
return [];
|
|
41
|
+
return list.flatMap((parameter, index) => {
|
|
42
|
+
const found = follow(contract.document, parameter, `${owner}/parameters/${String(index)}`);
|
|
43
|
+
if (seen.has(found.pointer) || !isRecord(found.node))
|
|
44
|
+
return [];
|
|
45
|
+
seen.add(found.pointer);
|
|
46
|
+
return [{ pointer: found.pointer, value: found.node, operation, name: String(found.node.name) }];
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
const ONE = ['items', 'additionalProperties', 'not', 'contains', 'if', 'then', 'else', 'propertyNames', 'unevaluatedItems', 'unevaluatedProperties'];
|
|
52
|
+
const LIST = ['allOf', 'anyOf', 'oneOf', 'prefixItems'];
|
|
53
|
+
const MAP = ['properties', 'patternProperties', '$defs', 'definitions', 'dependentSchemas'];
|
|
54
|
+
const childrenOf = (schema, pointer) => [
|
|
55
|
+
...ONE.map((keyword) => ({ node: schema[keyword], pointer: `${pointer}/${keyword}` })),
|
|
56
|
+
...LIST.flatMap((keyword) => {
|
|
57
|
+
const list = schema[keyword];
|
|
58
|
+
return Array.isArray(list) ? list.map((item, index) => ({ node: item, pointer: `${pointer}/${keyword}/${String(index)}` })) : [];
|
|
59
|
+
}),
|
|
60
|
+
...MAP.flatMap((keyword) => {
|
|
61
|
+
const map = schema[keyword];
|
|
62
|
+
if (!isRecord(map))
|
|
63
|
+
return [];
|
|
64
|
+
return Object.entries(map).map(([key, item]) => ({ node: item, pointer: `${pointer}/${keyword}/${token(key)}`, ...(keyword === 'properties' ? { name: key } : {}) }));
|
|
65
|
+
}),
|
|
66
|
+
];
|
|
67
|
+
const walk = (state, at, operation) => {
|
|
68
|
+
const found = follow(state.document, at.node, at.pointer);
|
|
69
|
+
if (state.seen.has(found.pointer) || !isRecord(found.node))
|
|
70
|
+
return;
|
|
71
|
+
state.seen.add(found.pointer);
|
|
72
|
+
const schema = found.node;
|
|
73
|
+
const value = { ...schema, ...at.node };
|
|
74
|
+
state.schemas.push({ pointer: found.pointer, value, operation });
|
|
75
|
+
if (at.name !== undefined)
|
|
76
|
+
state.properties.push({ pointer: found.pointer, value, operation, name: at.name });
|
|
77
|
+
for (const child of childrenOf(schema, found.pointer))
|
|
78
|
+
walk(state, child, operation);
|
|
79
|
+
};
|
|
80
|
+
const schemaWalk = (contract, given) => {
|
|
81
|
+
const state = { document: contract.document, seen: new Set(), schemas: [], properties: [] };
|
|
82
|
+
for (const operation of operationsOf(contract, given)) {
|
|
83
|
+
for (const payload of operation.payloads)
|
|
84
|
+
walk(state, { node: payload.schema, pointer: `${operation.source} ${payload.name}` }, operation);
|
|
85
|
+
}
|
|
86
|
+
return state;
|
|
87
|
+
};
|
|
88
|
+
const pathNodes = (contract, given) => {
|
|
89
|
+
const seen = new Set();
|
|
90
|
+
return [given.path ?? []].flat().flatMap((path) => {
|
|
91
|
+
let steps;
|
|
92
|
+
try {
|
|
93
|
+
steps = parse(path);
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
return [];
|
|
97
|
+
}
|
|
98
|
+
return query(contract.document, steps).flatMap((match) => {
|
|
99
|
+
if (seen.has(match.pointer))
|
|
100
|
+
return [];
|
|
101
|
+
seen.add(match.pointer);
|
|
102
|
+
return [{ pointer: match.pointer, value: isRecord(match.value) ? match.value : {}, matched: match.value }];
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
};
|
|
106
|
+
export const select = (contract, given) => {
|
|
107
|
+
if (given.formats !== undefined && !given.formats.includes(contract.format))
|
|
108
|
+
return [];
|
|
109
|
+
switch (given.select) {
|
|
110
|
+
case 'document': {
|
|
111
|
+
return [{ pointer: '#', value: membersOf(contract.document) }];
|
|
112
|
+
}
|
|
113
|
+
case 'operation': {
|
|
114
|
+
return operationNodes(contract, given);
|
|
115
|
+
}
|
|
116
|
+
case 'parameter': {
|
|
117
|
+
return parameterNodes(contract, given);
|
|
118
|
+
}
|
|
119
|
+
case 'property': {
|
|
120
|
+
return schemaWalk(contract, given).properties;
|
|
121
|
+
}
|
|
122
|
+
case 'schema': {
|
|
123
|
+
return schemaWalk(contract, given).schemas;
|
|
124
|
+
}
|
|
125
|
+
case 'path': {
|
|
126
|
+
return pathNodes(contract, given);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { compareContracts } from './diff-contract.js';
|
|
2
|
+
import { commitOf, SHORT } from './lint-history.js';
|
|
3
|
+
export const touchedOf = (old, nu) => {
|
|
4
|
+
const changed = new Set(compareContracts(old, nu).findings.map((finding) => finding.operation));
|
|
5
|
+
const operations = nu.operations.filter((operation) => changed.has(operation.id));
|
|
6
|
+
return { ids: new Set(operations.map((operation) => operation.id)), sources: operations.map((operation) => operation.source) };
|
|
7
|
+
};
|
|
8
|
+
export const skippedOf = (contract, touched) => contract.operations.filter((operation) => !touched.ids.has(operation.id)).length;
|
|
9
|
+
export const sinceOf = async (run) => {
|
|
10
|
+
const commit = await commitOf(run.git, '--since', run.ref);
|
|
11
|
+
if (commit === undefined)
|
|
12
|
+
return { inputs: run.inputs.map((each) => each.input), since: { unapplied: 'not a git repository, so every operation is linted' } };
|
|
13
|
+
const inputs = await Promise.all(run.inputs.map(async ({ input, path }) => {
|
|
14
|
+
const text = await run.git.show(commit, path);
|
|
15
|
+
const old = text === undefined ? undefined : run.read(text);
|
|
16
|
+
return old === undefined ? input : { ...input, touched: touchedOf(old, input.contract) };
|
|
17
|
+
}));
|
|
18
|
+
const skipped = inputs.reduce((sum, input) => sum + (input.touched === undefined ? 0 : skippedOf(input.contract, input.touched)), 0);
|
|
19
|
+
return { inputs, since: { from: `${run.ref} (${commit.slice(0, SHORT)})`, skipped } };
|
|
20
|
+
};
|