@openpkg-ts/spec 0.3.0 → 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.
- package/dist/index.d.ts +179 -0
- package/dist/index.js +776 -0
- package/package.json +1 -1
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
declare const SCHEMA_VERSION = "0.2.0";
|
|
2
|
+
declare const SCHEMA_URL = "https://unpkg.com/@openpkg-ts/spec/schemas/v0.2.0/openpkg.schema.json";
|
|
3
|
+
declare const JSON_SCHEMA_DRAFT = "https://json-schema.org/draft/2020-12/schema";
|
|
4
|
+
type SpecTag = {
|
|
5
|
+
name: string;
|
|
6
|
+
text: string;
|
|
7
|
+
};
|
|
8
|
+
type SpecSource = {
|
|
9
|
+
file?: string;
|
|
10
|
+
line?: number;
|
|
11
|
+
url?: string;
|
|
12
|
+
};
|
|
13
|
+
type SpecSchema = unknown;
|
|
14
|
+
type SpecExample = Record<string, unknown>;
|
|
15
|
+
type SpecExtension = Record<string, unknown>;
|
|
16
|
+
type SpecDocSignal = "description" | "params" | "returns" | "examples";
|
|
17
|
+
type SpecDocDrift = {
|
|
18
|
+
type: "param-mismatch" | "param-type-mismatch" | "return-type-mismatch" | "generic-constraint-mismatch" | "optionality-mismatch" | "deprecated-mismatch" | "visibility-mismatch" | "async-mismatch" | "property-type-drift" | "example-drift" | "example-syntax-error" | "example-runtime-error" | "example-assertion-failed" | "broken-link";
|
|
19
|
+
target?: string;
|
|
20
|
+
issue: string;
|
|
21
|
+
suggestion?: string;
|
|
22
|
+
};
|
|
23
|
+
type SpecVisibility = "public" | "protected" | "private";
|
|
24
|
+
type SpecDocsMetadata = {
|
|
25
|
+
coverageScore?: number;
|
|
26
|
+
missing?: SpecDocSignal[];
|
|
27
|
+
drift?: SpecDocDrift[];
|
|
28
|
+
};
|
|
29
|
+
type SpecTypeParameter = {
|
|
30
|
+
name: string;
|
|
31
|
+
constraint?: string;
|
|
32
|
+
default?: string;
|
|
33
|
+
};
|
|
34
|
+
type SpecSignatureParameter = {
|
|
35
|
+
name: string;
|
|
36
|
+
required?: boolean;
|
|
37
|
+
description?: string;
|
|
38
|
+
schema: SpecSchema;
|
|
39
|
+
};
|
|
40
|
+
type SpecSignatureReturn = {
|
|
41
|
+
schema: SpecSchema;
|
|
42
|
+
description?: string;
|
|
43
|
+
tsType?: string;
|
|
44
|
+
};
|
|
45
|
+
type SpecSignature = {
|
|
46
|
+
parameters?: SpecSignatureParameter[];
|
|
47
|
+
returns?: SpecSignatureReturn;
|
|
48
|
+
description?: string;
|
|
49
|
+
typeParameters?: SpecTypeParameter[];
|
|
50
|
+
};
|
|
51
|
+
type SpecMember = {
|
|
52
|
+
id?: string;
|
|
53
|
+
name?: string;
|
|
54
|
+
kind?: string;
|
|
55
|
+
description?: string;
|
|
56
|
+
tags?: SpecTag[];
|
|
57
|
+
visibility?: SpecVisibility;
|
|
58
|
+
flags?: Record<string, unknown>;
|
|
59
|
+
schema?: SpecSchema;
|
|
60
|
+
signatures?: SpecSignature[];
|
|
61
|
+
};
|
|
62
|
+
type SpecExportKind = "function" | "class" | "variable" | "interface" | "type" | "enum" | "module" | "namespace" | "reference" | "external";
|
|
63
|
+
type SpecTypeKind = "class" | "interface" | "type" | "enum" | "external";
|
|
64
|
+
type SpecExport = {
|
|
65
|
+
id: string;
|
|
66
|
+
name: string;
|
|
67
|
+
slug?: string;
|
|
68
|
+
displayName?: string;
|
|
69
|
+
alias?: string;
|
|
70
|
+
category?: string;
|
|
71
|
+
importPath?: string;
|
|
72
|
+
kind: SpecExportKind;
|
|
73
|
+
signatures?: SpecSignature[];
|
|
74
|
+
typeParameters?: SpecTypeParameter[];
|
|
75
|
+
members?: SpecMember[];
|
|
76
|
+
type?: string | SpecSchema;
|
|
77
|
+
schema?: SpecSchema;
|
|
78
|
+
description?: string;
|
|
79
|
+
examples?: string[];
|
|
80
|
+
docs?: SpecDocsMetadata;
|
|
81
|
+
source?: SpecSource;
|
|
82
|
+
deprecated?: boolean;
|
|
83
|
+
flags?: Record<string, unknown>;
|
|
84
|
+
tags?: SpecTag[];
|
|
85
|
+
extends?: string;
|
|
86
|
+
implements?: string[];
|
|
87
|
+
};
|
|
88
|
+
type SpecType = {
|
|
89
|
+
id: string;
|
|
90
|
+
name: string;
|
|
91
|
+
slug?: string;
|
|
92
|
+
displayName?: string;
|
|
93
|
+
alias?: string;
|
|
94
|
+
category?: string;
|
|
95
|
+
importPath?: string;
|
|
96
|
+
kind: SpecTypeKind;
|
|
97
|
+
description?: string;
|
|
98
|
+
schema?: SpecSchema;
|
|
99
|
+
type?: string | SpecSchema;
|
|
100
|
+
members?: SpecMember[];
|
|
101
|
+
source?: SpecSource;
|
|
102
|
+
tags?: SpecTag[];
|
|
103
|
+
rawComments?: string;
|
|
104
|
+
extends?: string;
|
|
105
|
+
implements?: string[];
|
|
106
|
+
};
|
|
107
|
+
type OpenPkgMeta = {
|
|
108
|
+
name: string;
|
|
109
|
+
version?: string;
|
|
110
|
+
description?: string;
|
|
111
|
+
license?: string;
|
|
112
|
+
repository?: string;
|
|
113
|
+
ecosystem?: string;
|
|
114
|
+
};
|
|
115
|
+
type OpenPkg = {
|
|
116
|
+
$schema?: string;
|
|
117
|
+
openpkg: "0.2.0";
|
|
118
|
+
meta: OpenPkgMeta;
|
|
119
|
+
exports: SpecExport[];
|
|
120
|
+
types?: SpecType[];
|
|
121
|
+
examples?: SpecExample[];
|
|
122
|
+
docs?: SpecDocsMetadata;
|
|
123
|
+
extensions?: SpecExtension;
|
|
124
|
+
};
|
|
125
|
+
declare function dereference(spec: OpenPkg): OpenPkg;
|
|
126
|
+
type BreakingSeverity = "high" | "medium" | "low";
|
|
127
|
+
interface CategorizedBreaking {
|
|
128
|
+
id: string;
|
|
129
|
+
name: string;
|
|
130
|
+
kind: SpecExportKind;
|
|
131
|
+
severity: BreakingSeverity;
|
|
132
|
+
reason: string;
|
|
133
|
+
}
|
|
134
|
+
/** Minimal member change info for categorization (avoids circular dep with SDK) */
|
|
135
|
+
interface MemberChangeInfo {
|
|
136
|
+
className: string;
|
|
137
|
+
memberName: string;
|
|
138
|
+
memberKind: "method" | "property" | "accessor" | "constructor";
|
|
139
|
+
changeType: "added" | "removed" | "signature-changed";
|
|
140
|
+
}
|
|
141
|
+
type SpecDiff = {
|
|
142
|
+
breaking: string[];
|
|
143
|
+
nonBreaking: string[];
|
|
144
|
+
docsOnly: string[];
|
|
145
|
+
coverageDelta: number;
|
|
146
|
+
oldCoverage: number;
|
|
147
|
+
newCoverage: number;
|
|
148
|
+
newUndocumented: string[];
|
|
149
|
+
improvedExports: string[];
|
|
150
|
+
regressedExports: string[];
|
|
151
|
+
driftIntroduced: number;
|
|
152
|
+
driftResolved: number;
|
|
153
|
+
};
|
|
154
|
+
declare function diffSpec(oldSpec: OpenPkg, newSpec: OpenPkg): SpecDiff;
|
|
155
|
+
/**
|
|
156
|
+
* Categorize breaking changes by severity
|
|
157
|
+
*
|
|
158
|
+
* @param breaking - Array of breaking change IDs
|
|
159
|
+
* @param oldSpec - Previous spec version
|
|
160
|
+
* @param newSpec - Current spec version
|
|
161
|
+
* @param memberChanges - Optional member-level changes for classes
|
|
162
|
+
* @returns Categorized breaking changes sorted by severity (high first)
|
|
163
|
+
*/
|
|
164
|
+
declare function categorizeBreakingChanges(breaking: string[], oldSpec: OpenPkg, newSpec: OpenPkg, memberChanges?: MemberChangeInfo[]): CategorizedBreaking[];
|
|
165
|
+
declare function normalize(spec: OpenPkg): OpenPkg;
|
|
166
|
+
type SpecError = {
|
|
167
|
+
instancePath: string;
|
|
168
|
+
message: string;
|
|
169
|
+
keyword: string;
|
|
170
|
+
};
|
|
171
|
+
declare function validateSpec(spec: unknown): {
|
|
172
|
+
ok: true;
|
|
173
|
+
} | {
|
|
174
|
+
ok: false;
|
|
175
|
+
errors: SpecError[];
|
|
176
|
+
};
|
|
177
|
+
declare function assertSpec(spec: unknown): asserts spec is OpenPkg;
|
|
178
|
+
declare function getValidationErrors(spec: unknown): SpecError[];
|
|
179
|
+
export { validateSpec, normalize, getValidationErrors, diffSpec, dereference, categorizeBreakingChanges, assertSpec, SpecVisibility, SpecTypeParameter, SpecTypeKind, SpecType, SpecTag, SpecSource, SpecSignatureReturn, SpecSignatureParameter, SpecSignature, SpecSchema, SpecMember, SpecExtension, SpecExportKind, SpecExport, SpecExample, SpecDocsMetadata, SpecDocSignal, SpecDocDrift, SpecDiff, SCHEMA_VERSION, SCHEMA_URL, OpenPkgMeta, OpenPkg, MemberChangeInfo, JSON_SCHEMA_DRAFT, CategorizedBreaking, BreakingSeverity };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,776 @@
|
|
|
1
|
+
// src/constants.ts
|
|
2
|
+
var SCHEMA_VERSION = "0.2.0";
|
|
3
|
+
var SCHEMA_URL = "https://unpkg.com/@openpkg-ts/spec/schemas/v0.2.0/openpkg.schema.json";
|
|
4
|
+
var JSON_SCHEMA_DRAFT = "https://json-schema.org/draft/2020-12/schema";
|
|
5
|
+
// src/deref.ts
|
|
6
|
+
function dereference(spec) {
|
|
7
|
+
const clone = JSON.parse(JSON.stringify(spec));
|
|
8
|
+
const typeLookup = buildTypeLookup(clone.types);
|
|
9
|
+
const visit = (value, seen) => {
|
|
10
|
+
if (Array.isArray(value)) {
|
|
11
|
+
return value.map((item) => visit(item, new Set(seen)));
|
|
12
|
+
}
|
|
13
|
+
if (value && typeof value === "object") {
|
|
14
|
+
const record = value;
|
|
15
|
+
const ref = readTypeRef(record);
|
|
16
|
+
if (ref) {
|
|
17
|
+
return resolveTypeRef(ref, typeLookup, seen);
|
|
18
|
+
}
|
|
19
|
+
const next = {};
|
|
20
|
+
for (const [key, nested] of Object.entries(record)) {
|
|
21
|
+
next[key] = visit(nested, seen);
|
|
22
|
+
}
|
|
23
|
+
return next;
|
|
24
|
+
}
|
|
25
|
+
return value;
|
|
26
|
+
};
|
|
27
|
+
clone.exports = clone.exports.map((item) => visit(item, new Set));
|
|
28
|
+
if (clone.types) {
|
|
29
|
+
clone.types = clone.types.map((item) => visit(item, new Set));
|
|
30
|
+
}
|
|
31
|
+
return clone;
|
|
32
|
+
}
|
|
33
|
+
function buildTypeLookup(types) {
|
|
34
|
+
const map = new Map;
|
|
35
|
+
if (!Array.isArray(types)) {
|
|
36
|
+
return map;
|
|
37
|
+
}
|
|
38
|
+
for (const type of types) {
|
|
39
|
+
if (type && typeof type.id === "string") {
|
|
40
|
+
map.set(type.id, type);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return map;
|
|
44
|
+
}
|
|
45
|
+
function readTypeRef(value) {
|
|
46
|
+
const ref = value.$ref;
|
|
47
|
+
if (typeof ref !== "string") {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
const prefix = "#/types/";
|
|
51
|
+
if (!ref.startsWith(prefix)) {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
return ref.slice(prefix.length);
|
|
55
|
+
}
|
|
56
|
+
function resolveTypeRef(id, lookup, seen) {
|
|
57
|
+
if (seen.has(id)) {
|
|
58
|
+
return { $ref: `#/types/${id}` };
|
|
59
|
+
}
|
|
60
|
+
const target = lookup.get(id);
|
|
61
|
+
if (!target) {
|
|
62
|
+
return { $ref: `#/types/${id}` };
|
|
63
|
+
}
|
|
64
|
+
seen.add(id);
|
|
65
|
+
if (target.schema) {
|
|
66
|
+
return JSON.parse(JSON.stringify(target.schema));
|
|
67
|
+
}
|
|
68
|
+
return JSON.parse(JSON.stringify(target));
|
|
69
|
+
}
|
|
70
|
+
// src/diff.ts
|
|
71
|
+
function diffSpec(oldSpec, newSpec) {
|
|
72
|
+
const result = {
|
|
73
|
+
breaking: [],
|
|
74
|
+
nonBreaking: [],
|
|
75
|
+
docsOnly: [],
|
|
76
|
+
coverageDelta: 0,
|
|
77
|
+
oldCoverage: 0,
|
|
78
|
+
newCoverage: 0,
|
|
79
|
+
newUndocumented: [],
|
|
80
|
+
improvedExports: [],
|
|
81
|
+
regressedExports: [],
|
|
82
|
+
driftIntroduced: 0,
|
|
83
|
+
driftResolved: 0
|
|
84
|
+
};
|
|
85
|
+
diffCollections(result, oldSpec.exports, newSpec.exports);
|
|
86
|
+
diffCollections(result, oldSpec.types ?? [], newSpec.types ?? []);
|
|
87
|
+
result.oldCoverage = oldSpec.docs?.coverageScore ?? 0;
|
|
88
|
+
result.newCoverage = newSpec.docs?.coverageScore ?? 0;
|
|
89
|
+
result.coverageDelta = Math.round((result.newCoverage - result.oldCoverage) * 10) / 10;
|
|
90
|
+
const oldExportMap = toExportMap(oldSpec.exports);
|
|
91
|
+
const newExportMap = toExportMap(newSpec.exports);
|
|
92
|
+
for (const [id, newExport] of newExportMap.entries()) {
|
|
93
|
+
const oldExport = oldExportMap.get(id);
|
|
94
|
+
const newScore = newExport.docs?.coverageScore ?? 0;
|
|
95
|
+
const newDriftCount = newExport.docs?.drift?.length ?? 0;
|
|
96
|
+
if (!oldExport) {
|
|
97
|
+
if (newScore < 100 || (newExport.docs?.missing?.length ?? 0) > 0) {
|
|
98
|
+
result.newUndocumented.push(id);
|
|
99
|
+
}
|
|
100
|
+
result.driftIntroduced += newDriftCount;
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
const oldScore = oldExport.docs?.coverageScore ?? 0;
|
|
104
|
+
const oldDriftCount = oldExport.docs?.drift?.length ?? 0;
|
|
105
|
+
if (newScore > oldScore) {
|
|
106
|
+
result.improvedExports.push(id);
|
|
107
|
+
} else if (newScore < oldScore) {
|
|
108
|
+
result.regressedExports.push(id);
|
|
109
|
+
}
|
|
110
|
+
if (newDriftCount > oldDriftCount) {
|
|
111
|
+
result.driftIntroduced += newDriftCount - oldDriftCount;
|
|
112
|
+
} else if (oldDriftCount > newDriftCount) {
|
|
113
|
+
result.driftResolved += oldDriftCount - newDriftCount;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return result;
|
|
117
|
+
}
|
|
118
|
+
function toExportMap(exports) {
|
|
119
|
+
const map = new Map;
|
|
120
|
+
for (const exp of exports) {
|
|
121
|
+
if (exp && typeof exp.id === "string") {
|
|
122
|
+
map.set(exp.id, exp);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return map;
|
|
126
|
+
}
|
|
127
|
+
function diffCollections(result, oldItems, newItems) {
|
|
128
|
+
const oldMap = toMap(oldItems);
|
|
129
|
+
const newMap = toMap(newItems);
|
|
130
|
+
for (const [id, oldItem] of oldMap.entries()) {
|
|
131
|
+
const newItem = newMap.get(id);
|
|
132
|
+
if (!newItem) {
|
|
133
|
+
result.breaking.push(id);
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
const docOnly = isDocOnlyChange(oldItem, newItem);
|
|
137
|
+
const identical = isDeepEqual(oldItem, newItem);
|
|
138
|
+
if (identical) {
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
if (docOnly) {
|
|
142
|
+
result.docsOnly.push(id);
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
result.breaking.push(id);
|
|
146
|
+
}
|
|
147
|
+
for (const id of newMap.keys()) {
|
|
148
|
+
if (!oldMap.has(id)) {
|
|
149
|
+
result.nonBreaking.push(id);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
function toMap(items) {
|
|
154
|
+
const map = new Map;
|
|
155
|
+
for (const item of items) {
|
|
156
|
+
if (item && typeof item.id === "string") {
|
|
157
|
+
map.set(item.id, item);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return map;
|
|
161
|
+
}
|
|
162
|
+
var DOC_KEYS = new Set(["description", "examples", "tags", "source", "rawComments"]);
|
|
163
|
+
function isDocOnlyChange(a, b) {
|
|
164
|
+
const structuralA = normalizeForComparison(removeDocFields(a));
|
|
165
|
+
const structuralB = normalizeForComparison(removeDocFields(b));
|
|
166
|
+
if (structuralA !== structuralB) {
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
const fullA = normalizeForComparison(a);
|
|
170
|
+
const fullB = normalizeForComparison(b);
|
|
171
|
+
return fullA !== fullB;
|
|
172
|
+
}
|
|
173
|
+
function isDeepEqual(a, b) {
|
|
174
|
+
return normalizeForComparison(a) === normalizeForComparison(b);
|
|
175
|
+
}
|
|
176
|
+
function removeDocFields(value) {
|
|
177
|
+
if (Array.isArray(value)) {
|
|
178
|
+
return value.map((item) => removeDocFields(item));
|
|
179
|
+
}
|
|
180
|
+
if (!value || typeof value !== "object") {
|
|
181
|
+
return value;
|
|
182
|
+
}
|
|
183
|
+
const entries = Object.entries(value).filter(([key]) => !DOC_KEYS.has(key));
|
|
184
|
+
const cleaned = {};
|
|
185
|
+
for (const [key, val] of entries) {
|
|
186
|
+
cleaned[key] = removeDocFields(val);
|
|
187
|
+
}
|
|
188
|
+
return cleaned;
|
|
189
|
+
}
|
|
190
|
+
function normalizeForComparison(value) {
|
|
191
|
+
return JSON.stringify(sortKeys(value));
|
|
192
|
+
}
|
|
193
|
+
function sortKeys(value) {
|
|
194
|
+
if (Array.isArray(value)) {
|
|
195
|
+
return value.map((item) => sortKeys(item));
|
|
196
|
+
}
|
|
197
|
+
if (!value || typeof value !== "object") {
|
|
198
|
+
return value;
|
|
199
|
+
}
|
|
200
|
+
const entries = Object.entries(value).sort(([a], [b]) => a.localeCompare(b));
|
|
201
|
+
const result = {};
|
|
202
|
+
for (const [key, val] of entries) {
|
|
203
|
+
result[key] = sortKeys(val);
|
|
204
|
+
}
|
|
205
|
+
return result;
|
|
206
|
+
}
|
|
207
|
+
function categorizeBreakingChanges(breaking, oldSpec, newSpec, memberChanges) {
|
|
208
|
+
const oldExportMap = toExportMap(oldSpec.exports);
|
|
209
|
+
const newExportMap = toExportMap(newSpec.exports);
|
|
210
|
+
const categorized = [];
|
|
211
|
+
for (const id of breaking) {
|
|
212
|
+
const oldExport = oldExportMap.get(id);
|
|
213
|
+
const newExport = newExportMap.get(id);
|
|
214
|
+
if (!newExport) {
|
|
215
|
+
const kind = oldExport?.kind ?? "variable";
|
|
216
|
+
categorized.push({
|
|
217
|
+
id,
|
|
218
|
+
name: oldExport?.name ?? id,
|
|
219
|
+
kind,
|
|
220
|
+
severity: kind === "function" || kind === "class" ? "high" : "medium",
|
|
221
|
+
reason: "removed"
|
|
222
|
+
});
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
if (oldExport?.kind === "class" && memberChanges?.length) {
|
|
226
|
+
const classChanges = memberChanges.filter((mc) => mc.className === id);
|
|
227
|
+
if (classChanges.length > 0) {
|
|
228
|
+
const hasConstructorChange = classChanges.some((mc) => mc.memberKind === "constructor");
|
|
229
|
+
const hasMethodRemoval = classChanges.some((mc) => mc.changeType === "removed" && mc.memberKind === "method");
|
|
230
|
+
categorized.push({
|
|
231
|
+
id,
|
|
232
|
+
name: oldExport.name,
|
|
233
|
+
kind: "class",
|
|
234
|
+
severity: hasConstructorChange || hasMethodRemoval ? "high" : "medium",
|
|
235
|
+
reason: hasConstructorChange ? "constructor changed" : hasMethodRemoval ? "methods removed" : "methods changed"
|
|
236
|
+
});
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
if (oldExport?.kind === "interface" || oldExport?.kind === "type") {
|
|
241
|
+
categorized.push({
|
|
242
|
+
id,
|
|
243
|
+
name: oldExport.name,
|
|
244
|
+
kind: oldExport.kind,
|
|
245
|
+
severity: "medium",
|
|
246
|
+
reason: "type definition changed"
|
|
247
|
+
});
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
if (oldExport?.kind === "function") {
|
|
251
|
+
categorized.push({
|
|
252
|
+
id,
|
|
253
|
+
name: oldExport.name,
|
|
254
|
+
kind: "function",
|
|
255
|
+
severity: "high",
|
|
256
|
+
reason: "signature changed"
|
|
257
|
+
});
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
categorized.push({
|
|
261
|
+
id,
|
|
262
|
+
name: oldExport?.name ?? id,
|
|
263
|
+
kind: oldExport?.kind ?? "variable",
|
|
264
|
+
severity: "low",
|
|
265
|
+
reason: "changed"
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
const severityOrder = { high: 0, medium: 1, low: 2 };
|
|
269
|
+
return categorized.sort((a, b) => severityOrder[a.severity] - severityOrder[b.severity]);
|
|
270
|
+
}
|
|
271
|
+
// src/normalize.ts
|
|
272
|
+
var DEFAULT_ECOSYSTEM = "js/ts";
|
|
273
|
+
var arrayFieldsByExport = ["signatures", "members", "examples", "tags"];
|
|
274
|
+
var arrayFieldsByType = ["members", "tags"];
|
|
275
|
+
function normalize(spec) {
|
|
276
|
+
const normalized = JSON.parse(JSON.stringify(spec));
|
|
277
|
+
normalized.meta = {
|
|
278
|
+
ecosystem: normalized.meta?.ecosystem ?? DEFAULT_ECOSYSTEM,
|
|
279
|
+
...normalized.meta
|
|
280
|
+
};
|
|
281
|
+
normalized.exports = Array.isArray(normalized.exports) ? [...normalized.exports] : [];
|
|
282
|
+
normalized.exports.sort((a, b) => (a.name || "").localeCompare(b.name || ""));
|
|
283
|
+
normalized.exports = normalized.exports.map((item) => normalizeExport(item));
|
|
284
|
+
const types = Array.isArray(normalized.types) ? [...normalized.types] : [];
|
|
285
|
+
types.sort((a, b) => (a.name || "").localeCompare(b.name || ""));
|
|
286
|
+
normalized.types = types.map((item) => normalizeType(item));
|
|
287
|
+
return normalized;
|
|
288
|
+
}
|
|
289
|
+
function normalizeExport(item) {
|
|
290
|
+
const clone = JSON.parse(JSON.stringify(item));
|
|
291
|
+
for (const field of arrayFieldsByExport) {
|
|
292
|
+
if (!Array.isArray(clone[field])) {
|
|
293
|
+
clone[field] = [];
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
return clone;
|
|
297
|
+
}
|
|
298
|
+
function normalizeType(item) {
|
|
299
|
+
const clone = JSON.parse(JSON.stringify(item));
|
|
300
|
+
for (const field of arrayFieldsByType) {
|
|
301
|
+
if (!Array.isArray(clone[field])) {
|
|
302
|
+
clone[field] = [];
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
return clone;
|
|
306
|
+
}
|
|
307
|
+
// src/validate.ts
|
|
308
|
+
import Ajv from "ajv/dist/2020.js";
|
|
309
|
+
import addFormats from "ajv-formats";
|
|
310
|
+
// schemas/v0.2.0/openpkg.schema.json
|
|
311
|
+
var openpkg_schema_default = {
|
|
312
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
313
|
+
$id: "https://unpkg.com/@openpkg-ts/spec/schemas/v0.2.0/openpkg.schema.json",
|
|
314
|
+
title: "OpenPkg Specification",
|
|
315
|
+
description: "Schema for OpenPkg specification files",
|
|
316
|
+
type: "object",
|
|
317
|
+
required: ["openpkg", "meta", "exports"],
|
|
318
|
+
properties: {
|
|
319
|
+
$schema: {
|
|
320
|
+
type: "string",
|
|
321
|
+
description: "Reference to the OpenPkg schema version",
|
|
322
|
+
pattern: "^(https://raw\\.githubusercontent\\.com/ryanwaits/openpkg/main/schemas/v[0-9]+\\.[0-9]+\\.[0-9]+/openpkg\\.schema\\.json|https://unpkg\\.com/@openpkg-ts/spec/schemas/v[0-9]+\\.[0-9]+\\.[0-9]+/openpkg\\.schema\\.json)$"
|
|
323
|
+
},
|
|
324
|
+
openpkg: {
|
|
325
|
+
type: "string",
|
|
326
|
+
description: "OpenPkg specification version",
|
|
327
|
+
pattern: "^[0-9]+\\.[0-9]+\\.[0-9]+$",
|
|
328
|
+
const: "0.2.0"
|
|
329
|
+
},
|
|
330
|
+
meta: {
|
|
331
|
+
type: "object",
|
|
332
|
+
description: "Package metadata",
|
|
333
|
+
required: ["name"],
|
|
334
|
+
properties: {
|
|
335
|
+
name: {
|
|
336
|
+
type: "string",
|
|
337
|
+
description: "Package name"
|
|
338
|
+
},
|
|
339
|
+
version: {
|
|
340
|
+
type: "string",
|
|
341
|
+
description: "Package version"
|
|
342
|
+
},
|
|
343
|
+
description: {
|
|
344
|
+
type: "string",
|
|
345
|
+
description: "Package description"
|
|
346
|
+
},
|
|
347
|
+
license: {
|
|
348
|
+
type: "string",
|
|
349
|
+
description: "Package license"
|
|
350
|
+
},
|
|
351
|
+
repository: {
|
|
352
|
+
type: "string",
|
|
353
|
+
description: "Repository URL"
|
|
354
|
+
},
|
|
355
|
+
ecosystem: {
|
|
356
|
+
type: "string",
|
|
357
|
+
description: "Package ecosystem"
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
},
|
|
361
|
+
exports: {
|
|
362
|
+
type: "array",
|
|
363
|
+
description: "List of exported items",
|
|
364
|
+
items: {
|
|
365
|
+
$ref: "#/$defs/export"
|
|
366
|
+
}
|
|
367
|
+
},
|
|
368
|
+
types: {
|
|
369
|
+
type: "array",
|
|
370
|
+
description: "List of type definitions",
|
|
371
|
+
items: {
|
|
372
|
+
$ref: "#/$defs/typeDef"
|
|
373
|
+
}
|
|
374
|
+
},
|
|
375
|
+
docs: {
|
|
376
|
+
$ref: "#/$defs/docsMetadata",
|
|
377
|
+
description: "Aggregate documentation coverage metadata"
|
|
378
|
+
}
|
|
379
|
+
},
|
|
380
|
+
$defs: {
|
|
381
|
+
docSignal: {
|
|
382
|
+
type: "string",
|
|
383
|
+
enum: ["description", "params", "returns", "examples"]
|
|
384
|
+
},
|
|
385
|
+
docDrift: {
|
|
386
|
+
type: "object",
|
|
387
|
+
required: ["type", "issue"],
|
|
388
|
+
properties: {
|
|
389
|
+
type: {
|
|
390
|
+
type: "string",
|
|
391
|
+
enum: [
|
|
392
|
+
"param-mismatch",
|
|
393
|
+
"param-type-mismatch",
|
|
394
|
+
"return-type-mismatch",
|
|
395
|
+
"generic-constraint-mismatch",
|
|
396
|
+
"optionality-mismatch",
|
|
397
|
+
"deprecated-mismatch",
|
|
398
|
+
"visibility-mismatch",
|
|
399
|
+
"async-mismatch",
|
|
400
|
+
"property-type-drift",
|
|
401
|
+
"example-drift",
|
|
402
|
+
"example-syntax-error",
|
|
403
|
+
"example-runtime-error",
|
|
404
|
+
"example-assertion-failed",
|
|
405
|
+
"broken-link"
|
|
406
|
+
]
|
|
407
|
+
},
|
|
408
|
+
target: {
|
|
409
|
+
type: "string",
|
|
410
|
+
description: "Relevant identifier (e.g., parameter name)"
|
|
411
|
+
},
|
|
412
|
+
issue: {
|
|
413
|
+
type: "string",
|
|
414
|
+
description: "Human-friendly drift explanation"
|
|
415
|
+
},
|
|
416
|
+
suggestion: {
|
|
417
|
+
type: "string",
|
|
418
|
+
description: "Optional remediation hint"
|
|
419
|
+
}
|
|
420
|
+
},
|
|
421
|
+
additionalProperties: false
|
|
422
|
+
},
|
|
423
|
+
docsMetadata: {
|
|
424
|
+
type: "object",
|
|
425
|
+
description: "Documentation coverage metadata",
|
|
426
|
+
additionalProperties: false,
|
|
427
|
+
properties: {
|
|
428
|
+
coverageScore: {
|
|
429
|
+
type: "number",
|
|
430
|
+
minimum: 0,
|
|
431
|
+
maximum: 100,
|
|
432
|
+
description: "Documentation coverage value from 0-100."
|
|
433
|
+
},
|
|
434
|
+
missing: {
|
|
435
|
+
type: "array",
|
|
436
|
+
description: "Doc components missing for this entity",
|
|
437
|
+
items: {
|
|
438
|
+
$ref: "#/$defs/docSignal"
|
|
439
|
+
},
|
|
440
|
+
uniqueItems: true
|
|
441
|
+
},
|
|
442
|
+
drift: {
|
|
443
|
+
type: "array",
|
|
444
|
+
description: "Detected documentation drift signals",
|
|
445
|
+
items: {
|
|
446
|
+
$ref: "#/$defs/docDrift"
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
},
|
|
451
|
+
export: {
|
|
452
|
+
type: "object",
|
|
453
|
+
required: ["id", "name", "kind"],
|
|
454
|
+
properties: {
|
|
455
|
+
id: {
|
|
456
|
+
type: "string",
|
|
457
|
+
description: "Unique identifier for the export"
|
|
458
|
+
},
|
|
459
|
+
name: {
|
|
460
|
+
type: "string",
|
|
461
|
+
description: "Export name"
|
|
462
|
+
},
|
|
463
|
+
slug: {
|
|
464
|
+
type: "string",
|
|
465
|
+
description: "Stable slug for linking"
|
|
466
|
+
},
|
|
467
|
+
displayName: {
|
|
468
|
+
type: "string",
|
|
469
|
+
description: "UI-friendly label"
|
|
470
|
+
},
|
|
471
|
+
alias: {
|
|
472
|
+
type: "string",
|
|
473
|
+
description: "Export alias if re-exported with a different name (id uses alias, name uses original)"
|
|
474
|
+
},
|
|
475
|
+
category: {
|
|
476
|
+
type: "string",
|
|
477
|
+
description: "Grouping hint for navigation"
|
|
478
|
+
},
|
|
479
|
+
importPath: {
|
|
480
|
+
type: "string",
|
|
481
|
+
description: "Recommended import path"
|
|
482
|
+
},
|
|
483
|
+
kind: {
|
|
484
|
+
type: "string",
|
|
485
|
+
description: "Kind of export",
|
|
486
|
+
enum: ["function", "class", "variable", "interface", "type", "enum", "namespace", "external"]
|
|
487
|
+
},
|
|
488
|
+
description: {
|
|
489
|
+
type: "string",
|
|
490
|
+
description: "JSDoc/TSDoc description"
|
|
491
|
+
},
|
|
492
|
+
examples: {
|
|
493
|
+
type: "array",
|
|
494
|
+
description: "Usage examples from documentation",
|
|
495
|
+
items: {
|
|
496
|
+
type: "string"
|
|
497
|
+
}
|
|
498
|
+
},
|
|
499
|
+
signatures: {
|
|
500
|
+
type: "array",
|
|
501
|
+
description: "Function/method signatures",
|
|
502
|
+
items: {
|
|
503
|
+
$ref: "#/$defs/signature"
|
|
504
|
+
}
|
|
505
|
+
},
|
|
506
|
+
type: {
|
|
507
|
+
description: "Type reference or inline schema for variables",
|
|
508
|
+
oneOf: [{ type: "string" }, { $ref: "#/$defs/schema" }]
|
|
509
|
+
},
|
|
510
|
+
members: {
|
|
511
|
+
type: "array",
|
|
512
|
+
description: "Class/interface/enum members",
|
|
513
|
+
items: { type: "object" }
|
|
514
|
+
},
|
|
515
|
+
extends: {
|
|
516
|
+
type: "string",
|
|
517
|
+
description: "Base class or interface that this class/interface extends"
|
|
518
|
+
},
|
|
519
|
+
implements: {
|
|
520
|
+
type: "array",
|
|
521
|
+
description: "Interfaces implemented by this class",
|
|
522
|
+
items: { type: "string" }
|
|
523
|
+
},
|
|
524
|
+
tags: {
|
|
525
|
+
type: "array",
|
|
526
|
+
description: "JSDoc/TSDoc tags",
|
|
527
|
+
items: {
|
|
528
|
+
type: "object",
|
|
529
|
+
required: ["name", "text"],
|
|
530
|
+
properties: {
|
|
531
|
+
name: { type: "string" },
|
|
532
|
+
text: { type: "string" }
|
|
533
|
+
},
|
|
534
|
+
additionalProperties: false
|
|
535
|
+
}
|
|
536
|
+
},
|
|
537
|
+
source: {
|
|
538
|
+
$ref: "#/$defs/sourceLocation"
|
|
539
|
+
},
|
|
540
|
+
docs: {
|
|
541
|
+
$ref: "#/$defs/docsMetadata",
|
|
542
|
+
description: "Documentation coverage metadata for this export"
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
},
|
|
546
|
+
typeDef: {
|
|
547
|
+
type: "object",
|
|
548
|
+
required: ["id", "name", "kind"],
|
|
549
|
+
properties: {
|
|
550
|
+
id: {
|
|
551
|
+
type: "string",
|
|
552
|
+
description: "Unique identifier for the type"
|
|
553
|
+
},
|
|
554
|
+
name: {
|
|
555
|
+
type: "string",
|
|
556
|
+
description: "Type name"
|
|
557
|
+
},
|
|
558
|
+
slug: {
|
|
559
|
+
type: "string",
|
|
560
|
+
description: "Stable slug for linking"
|
|
561
|
+
},
|
|
562
|
+
displayName: {
|
|
563
|
+
type: "string",
|
|
564
|
+
description: "UI-friendly label"
|
|
565
|
+
},
|
|
566
|
+
alias: {
|
|
567
|
+
type: "string",
|
|
568
|
+
description: "Export alias if re-exported with a different name (id uses alias, name uses original)"
|
|
569
|
+
},
|
|
570
|
+
category: {
|
|
571
|
+
type: "string",
|
|
572
|
+
description: "Grouping hint for navigation"
|
|
573
|
+
},
|
|
574
|
+
importPath: {
|
|
575
|
+
type: "string",
|
|
576
|
+
description: "Recommended import path"
|
|
577
|
+
},
|
|
578
|
+
kind: {
|
|
579
|
+
type: "string",
|
|
580
|
+
description: "Kind of type definition",
|
|
581
|
+
enum: ["interface", "type", "enum", "class", "external"]
|
|
582
|
+
},
|
|
583
|
+
description: {
|
|
584
|
+
type: "string",
|
|
585
|
+
description: "JSDoc/TSDoc description"
|
|
586
|
+
},
|
|
587
|
+
schema: {
|
|
588
|
+
$ref: "#/$defs/schema"
|
|
589
|
+
},
|
|
590
|
+
type: {
|
|
591
|
+
type: "string",
|
|
592
|
+
description: "Type expression for type aliases"
|
|
593
|
+
},
|
|
594
|
+
members: {
|
|
595
|
+
type: "array",
|
|
596
|
+
description: "Members for classes/interfaces/enums",
|
|
597
|
+
items: { type: "object" }
|
|
598
|
+
},
|
|
599
|
+
extends: {
|
|
600
|
+
type: "string",
|
|
601
|
+
description: "Base class or interface that this class/interface extends"
|
|
602
|
+
},
|
|
603
|
+
implements: {
|
|
604
|
+
type: "array",
|
|
605
|
+
description: "Interfaces implemented by this class",
|
|
606
|
+
items: { type: "string" }
|
|
607
|
+
},
|
|
608
|
+
tags: {
|
|
609
|
+
type: "array",
|
|
610
|
+
description: "JSDoc/TSDoc tags",
|
|
611
|
+
items: {
|
|
612
|
+
type: "object",
|
|
613
|
+
required: ["name", "text"],
|
|
614
|
+
properties: {
|
|
615
|
+
name: { type: "string" },
|
|
616
|
+
text: { type: "string" }
|
|
617
|
+
},
|
|
618
|
+
additionalProperties: false
|
|
619
|
+
}
|
|
620
|
+
},
|
|
621
|
+
source: {
|
|
622
|
+
$ref: "#/$defs/sourceLocation"
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
},
|
|
626
|
+
signature: {
|
|
627
|
+
type: "object",
|
|
628
|
+
properties: {
|
|
629
|
+
parameters: {
|
|
630
|
+
type: "array",
|
|
631
|
+
items: {
|
|
632
|
+
$ref: "#/$defs/parameter"
|
|
633
|
+
}
|
|
634
|
+
},
|
|
635
|
+
returns: {
|
|
636
|
+
$ref: "#/$defs/returns"
|
|
637
|
+
},
|
|
638
|
+
description: {
|
|
639
|
+
type: "string",
|
|
640
|
+
description: "Signature-level description"
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
},
|
|
644
|
+
parameter: {
|
|
645
|
+
type: "object",
|
|
646
|
+
required: ["name", "required"],
|
|
647
|
+
properties: {
|
|
648
|
+
name: {
|
|
649
|
+
type: "string",
|
|
650
|
+
description: "Parameter name"
|
|
651
|
+
},
|
|
652
|
+
required: {
|
|
653
|
+
type: "boolean",
|
|
654
|
+
description: "Whether the parameter is required"
|
|
655
|
+
},
|
|
656
|
+
schema: {
|
|
657
|
+
$ref: "#/$defs/schema"
|
|
658
|
+
},
|
|
659
|
+
description: {
|
|
660
|
+
type: "string",
|
|
661
|
+
description: "Parameter description"
|
|
662
|
+
},
|
|
663
|
+
default: {
|
|
664
|
+
description: "Default value for the parameter"
|
|
665
|
+
},
|
|
666
|
+
rest: {
|
|
667
|
+
type: "boolean",
|
|
668
|
+
description: "Whether this is a rest parameter (...args)"
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
},
|
|
672
|
+
returns: {
|
|
673
|
+
type: "object",
|
|
674
|
+
properties: {
|
|
675
|
+
schema: {
|
|
676
|
+
$ref: "#/$defs/schema"
|
|
677
|
+
},
|
|
678
|
+
description: {
|
|
679
|
+
type: "string",
|
|
680
|
+
description: "Return value description"
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
},
|
|
684
|
+
schema: {
|
|
685
|
+
anyOf: [
|
|
686
|
+
{
|
|
687
|
+
type: "boolean"
|
|
688
|
+
},
|
|
689
|
+
{
|
|
690
|
+
type: "object",
|
|
691
|
+
properties: {
|
|
692
|
+
$ref: {
|
|
693
|
+
type: "string",
|
|
694
|
+
description: "Reference to another type",
|
|
695
|
+
pattern: "^#/types/[A-Za-z0-9_.-]+$"
|
|
696
|
+
}
|
|
697
|
+
},
|
|
698
|
+
required: ["$ref"],
|
|
699
|
+
additionalProperties: false
|
|
700
|
+
},
|
|
701
|
+
{
|
|
702
|
+
type: "object",
|
|
703
|
+
not: {
|
|
704
|
+
required: ["$ref"]
|
|
705
|
+
},
|
|
706
|
+
additionalProperties: true
|
|
707
|
+
}
|
|
708
|
+
]
|
|
709
|
+
},
|
|
710
|
+
sourceLocation: {
|
|
711
|
+
type: "object",
|
|
712
|
+
required: ["file", "line"],
|
|
713
|
+
properties: {
|
|
714
|
+
file: {
|
|
715
|
+
type: "string",
|
|
716
|
+
description: "Source file path"
|
|
717
|
+
},
|
|
718
|
+
line: {
|
|
719
|
+
type: "integer",
|
|
720
|
+
description: "Line number in source file",
|
|
721
|
+
minimum: 1
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
};
|
|
727
|
+
|
|
728
|
+
// src/validate.ts
|
|
729
|
+
var ajv = new Ajv({
|
|
730
|
+
strict: false,
|
|
731
|
+
allErrors: true,
|
|
732
|
+
allowUnionTypes: true,
|
|
733
|
+
$data: true
|
|
734
|
+
});
|
|
735
|
+
addFormats(ajv);
|
|
736
|
+
var validate = ajv.compile(openpkg_schema_default);
|
|
737
|
+
function validateSpec(spec) {
|
|
738
|
+
const ok = validate(spec);
|
|
739
|
+
if (ok) {
|
|
740
|
+
return { ok: true };
|
|
741
|
+
}
|
|
742
|
+
const errors = (validate.errors ?? []).map((error) => ({
|
|
743
|
+
instancePath: error.instancePath ?? "",
|
|
744
|
+
message: error.message ?? "invalid",
|
|
745
|
+
keyword: error.keyword ?? "unknown"
|
|
746
|
+
}));
|
|
747
|
+
return {
|
|
748
|
+
ok: false,
|
|
749
|
+
errors
|
|
750
|
+
};
|
|
751
|
+
}
|
|
752
|
+
function assertSpec(spec) {
|
|
753
|
+
const result = validateSpec(spec);
|
|
754
|
+
if (!result.ok) {
|
|
755
|
+
const details = result.errors.map((error) => `- ${error.instancePath || "/"} ${error.message}`).join(`
|
|
756
|
+
`);
|
|
757
|
+
throw new Error(`Invalid OpenPkg spec:
|
|
758
|
+
${details}`);
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
function getValidationErrors(spec) {
|
|
762
|
+
const result = validateSpec(spec);
|
|
763
|
+
return result.ok ? [] : result.errors;
|
|
764
|
+
}
|
|
765
|
+
export {
|
|
766
|
+
validateSpec,
|
|
767
|
+
normalize,
|
|
768
|
+
getValidationErrors,
|
|
769
|
+
diffSpec,
|
|
770
|
+
dereference,
|
|
771
|
+
categorizeBreakingChanges,
|
|
772
|
+
assertSpec,
|
|
773
|
+
SCHEMA_VERSION,
|
|
774
|
+
SCHEMA_URL,
|
|
775
|
+
JSON_SCHEMA_DRAFT
|
|
776
|
+
};
|