@amritk/helpers 0.10.2 → 0.10.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.
|
@@ -35,10 +35,10 @@ export const escapeRegexPattern = (pattern) => {
|
|
|
35
35
|
// Line terminators, keyed by code point, and the backslash escape each maps to.
|
|
36
36
|
// These are the four characters disallowed *raw* inside a regex literal.
|
|
37
37
|
const lineTerminatorEscapes = {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
38
|
+
10: '\\n', // LF
|
|
39
|
+
13: '\\r', // CR
|
|
40
|
+
8232: '\\u2028', // LINE SEPARATOR
|
|
41
|
+
8233: '\\u2029', // PARAGRAPH SEPARATOR
|
|
42
42
|
};
|
|
43
43
|
// Match either an escape sequence (`\` + any char, kept verbatim) or a single
|
|
44
44
|
// character that would corrupt the literal (a bare `/` or a line terminator).
|
package/dist/walk-ref-graph.d.ts
CHANGED
|
@@ -6,7 +6,12 @@ import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
|
|
|
6
6
|
* being copy-pasted into every generator.
|
|
7
7
|
*/
|
|
8
8
|
export type RefNode = {
|
|
9
|
-
/**
|
|
9
|
+
/**
|
|
10
|
+
* The `$ref` string this node was reached through, or `undefined` for the
|
|
11
|
+
* root schema. Exception: an alias root (a bare `$ref` document) whose
|
|
12
|
+
* derived filename collides with its target's carries the target's ref, so
|
|
13
|
+
* generators can exclude self-imports from the merged file.
|
|
14
|
+
*/
|
|
10
15
|
ref: string | undefined;
|
|
11
16
|
/** PascalCase type name — the root type name verbatim, or `refToName(ref, typeSuffix)`. */
|
|
12
17
|
typeName: string;
|
package/dist/walk-ref-graph.js
CHANGED
|
@@ -7,6 +7,38 @@ import { resolveDynamicRefs } from './resolve-dynamic-refs.js';
|
|
|
7
7
|
import { resolveRef } from './resolve-ref.js';
|
|
8
8
|
import { upgradeDraft07Schema } from './upgrade-draft07-schema.js';
|
|
9
9
|
const rootCaches = new WeakMap();
|
|
10
|
+
/**
|
|
11
|
+
* Keywords that give a schema a shape of its own. A root carrying any of these
|
|
12
|
+
* next to its `$ref` is a real composition, not a pure alias, and keeps the
|
|
13
|
+
* default root handling.
|
|
14
|
+
*/
|
|
15
|
+
const SHAPE_KEYWORDS = [
|
|
16
|
+
'properties',
|
|
17
|
+
'type',
|
|
18
|
+
'oneOf',
|
|
19
|
+
'anyOf',
|
|
20
|
+
'allOf',
|
|
21
|
+
'patternProperties',
|
|
22
|
+
'additionalProperties',
|
|
23
|
+
'enum',
|
|
24
|
+
'const',
|
|
25
|
+
'items',
|
|
26
|
+
'prefixItems',
|
|
27
|
+
'required',
|
|
28
|
+
'if',
|
|
29
|
+
'then',
|
|
30
|
+
'else',
|
|
31
|
+
'not',
|
|
32
|
+
];
|
|
33
|
+
/** The `$ref` of a pure-alias root document (only `$ref` + metadata keys), or null. */
|
|
34
|
+
const getAliasRootRef = (schema) => {
|
|
35
|
+
if (typeof schema !== 'object' || schema === null)
|
|
36
|
+
return null;
|
|
37
|
+
const record = schema;
|
|
38
|
+
if (typeof record['$ref'] !== 'string')
|
|
39
|
+
return null;
|
|
40
|
+
return SHAPE_KEYWORDS.some((key) => key in record) ? null : record['$ref'];
|
|
41
|
+
};
|
|
10
42
|
const getRootCache = (rootSchema) => {
|
|
11
43
|
// Only object roots can key a WeakMap. A boolean root has no refs to walk and
|
|
12
44
|
// the draft-07 upgrade is a no-op for it, so a throwaway cache is fine.
|
|
@@ -78,11 +110,27 @@ export const walkRefGraph = (rootSchema, rootTypeName, options, visit) => {
|
|
|
78
110
|
// the same name does not emit a duplicate file.
|
|
79
111
|
const rootFilename = rootTypeName.toLowerCase();
|
|
80
112
|
processedFilenames.add(rootFilename);
|
|
113
|
+
// An alias root (a document that is just `$ref: '#/$defs/x'`) whose derived
|
|
114
|
+
// filename equals its target's would reserve the filename for a wrapper that
|
|
115
|
+
// re-exports the target — and the target, mapping to the same file, would
|
|
116
|
+
// then never be generated, leaving a file that imports itself. Generate the
|
|
117
|
+
// *target's* schema under the root's name instead, carrying the ref so
|
|
118
|
+
// generators can exclude the self-import.
|
|
119
|
+
let rootNodeSchema = upgraded;
|
|
120
|
+
let rootNodeRef;
|
|
121
|
+
const aliasRef = getAliasRootRef(upgraded);
|
|
122
|
+
if (aliasRef && refToFilename(aliasRef) === rootFilename) {
|
|
123
|
+
const resolved = cachedResolveRef(cache, aliasRef);
|
|
124
|
+
if (resolved) {
|
|
125
|
+
rootNodeSchema = resolved;
|
|
126
|
+
rootNodeRef = aliasRef;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
81
129
|
visit({
|
|
82
|
-
ref:
|
|
130
|
+
ref: rootNodeRef,
|
|
83
131
|
typeName: rootTypeName,
|
|
84
132
|
filename: rootFilename,
|
|
85
|
-
schema: resolveDynamicRefs(
|
|
133
|
+
schema: resolveDynamicRefs(rootNodeSchema, dynamicRefMap),
|
|
86
134
|
rootSchema: upgraded,
|
|
87
135
|
isRoot: true,
|
|
88
136
|
});
|
package/package.json
CHANGED
|
@@ -37,10 +37,10 @@ export const escapeRegexPattern = (pattern: string): string => {
|
|
|
37
37
|
// Line terminators, keyed by code point, and the backslash escape each maps to.
|
|
38
38
|
// These are the four characters disallowed *raw* inside a regex literal.
|
|
39
39
|
const lineTerminatorEscapes: Record<number, string> = {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
40
|
+
10: '\\n', // LF
|
|
41
|
+
13: '\\r', // CR
|
|
42
|
+
8232: '\\u2028', // LINE SEPARATOR
|
|
43
|
+
8233: '\\u2029', // PARAGRAPH SEPARATOR
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
// Match either an escape sequence (`\` + any char, kept verbatim) or a single
|
package/src/walk-ref-graph.ts
CHANGED
|
@@ -16,7 +16,12 @@ import { upgradeDraft07Schema } from './upgrade-draft07-schema'
|
|
|
16
16
|
* being copy-pasted into every generator.
|
|
17
17
|
*/
|
|
18
18
|
export type RefNode = {
|
|
19
|
-
/**
|
|
19
|
+
/**
|
|
20
|
+
* The `$ref` string this node was reached through, or `undefined` for the
|
|
21
|
+
* root schema. Exception: an alias root (a bare `$ref` document) whose
|
|
22
|
+
* derived filename collides with its target's carries the target's ref, so
|
|
23
|
+
* generators can exclude self-imports from the merged file.
|
|
24
|
+
*/
|
|
20
25
|
ref: string | undefined
|
|
21
26
|
/** PascalCase type name — the root type name verbatim, or `refToName(ref, typeSuffix)`. */
|
|
22
27
|
typeName: string
|
|
@@ -57,6 +62,38 @@ type RootCache = {
|
|
|
57
62
|
|
|
58
63
|
const rootCaches = new WeakMap<object, RootCache>()
|
|
59
64
|
|
|
65
|
+
/**
|
|
66
|
+
* Keywords that give a schema a shape of its own. A root carrying any of these
|
|
67
|
+
* next to its `$ref` is a real composition, not a pure alias, and keeps the
|
|
68
|
+
* default root handling.
|
|
69
|
+
*/
|
|
70
|
+
const SHAPE_KEYWORDS = [
|
|
71
|
+
'properties',
|
|
72
|
+
'type',
|
|
73
|
+
'oneOf',
|
|
74
|
+
'anyOf',
|
|
75
|
+
'allOf',
|
|
76
|
+
'patternProperties',
|
|
77
|
+
'additionalProperties',
|
|
78
|
+
'enum',
|
|
79
|
+
'const',
|
|
80
|
+
'items',
|
|
81
|
+
'prefixItems',
|
|
82
|
+
'required',
|
|
83
|
+
'if',
|
|
84
|
+
'then',
|
|
85
|
+
'else',
|
|
86
|
+
'not',
|
|
87
|
+
] as const
|
|
88
|
+
|
|
89
|
+
/** The `$ref` of a pure-alias root document (only `$ref` + metadata keys), or null. */
|
|
90
|
+
const getAliasRootRef = (schema: JSONSchema): string | null => {
|
|
91
|
+
if (typeof schema !== 'object' || schema === null) return null
|
|
92
|
+
const record = schema as Record<string, unknown>
|
|
93
|
+
if (typeof record['$ref'] !== 'string') return null
|
|
94
|
+
return SHAPE_KEYWORDS.some((key) => key in record) ? null : record['$ref']
|
|
95
|
+
}
|
|
96
|
+
|
|
60
97
|
const getRootCache = (rootSchema: JSONSchema): RootCache => {
|
|
61
98
|
// Only object roots can key a WeakMap. A boolean root has no refs to walk and
|
|
62
99
|
// the draft-07 upgrade is a no-op for it, so a throwaway cache is fine.
|
|
@@ -136,11 +173,29 @@ export const walkRefGraph = (
|
|
|
136
173
|
// the same name does not emit a duplicate file.
|
|
137
174
|
const rootFilename = rootTypeName.toLowerCase()
|
|
138
175
|
processedFilenames.add(rootFilename)
|
|
176
|
+
|
|
177
|
+
// An alias root (a document that is just `$ref: '#/$defs/x'`) whose derived
|
|
178
|
+
// filename equals its target's would reserve the filename for a wrapper that
|
|
179
|
+
// re-exports the target — and the target, mapping to the same file, would
|
|
180
|
+
// then never be generated, leaving a file that imports itself. Generate the
|
|
181
|
+
// *target's* schema under the root's name instead, carrying the ref so
|
|
182
|
+
// generators can exclude the self-import.
|
|
183
|
+
let rootNodeSchema = upgraded as JSONSchema
|
|
184
|
+
let rootNodeRef: string | undefined
|
|
185
|
+
const aliasRef = getAliasRootRef(upgraded as JSONSchema)
|
|
186
|
+
if (aliasRef && refToFilename(aliasRef) === rootFilename) {
|
|
187
|
+
const resolved = cachedResolveRef(cache, aliasRef)
|
|
188
|
+
if (resolved) {
|
|
189
|
+
rootNodeSchema = resolved as JSONSchema
|
|
190
|
+
rootNodeRef = aliasRef
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
139
194
|
visit({
|
|
140
|
-
ref:
|
|
195
|
+
ref: rootNodeRef,
|
|
141
196
|
typeName: rootTypeName,
|
|
142
197
|
filename: rootFilename,
|
|
143
|
-
schema: resolveDynamicRefs(
|
|
198
|
+
schema: resolveDynamicRefs(rootNodeSchema, dynamicRefMap),
|
|
144
199
|
rootSchema: upgraded,
|
|
145
200
|
isRoot: true,
|
|
146
201
|
})
|