@amritk/helpers 0.13.4 → 0.14.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/README.md +2 -1
- package/dist/fold-nullable.d.ts +19 -0
- package/dist/fold-nullable.js +42 -0
- package/dist/generate-type-definition.d.ts +8 -0
- package/dist/generate-type-definition.js +251 -184
- package/dist/walk-ref-graph.js +9 -1
- package/package.json +5 -1
- package/src/fold-nullable.ts +60 -0
- package/src/generate-type-definition.ts +416 -231
- package/src/walk-ref-graph.ts +18 -1
package/src/walk-ref-graph.ts
CHANGED
|
@@ -167,11 +167,14 @@ export const walkRefGraph = (
|
|
|
167
167
|
|
|
168
168
|
const processedRefs = new Set<string>()
|
|
169
169
|
const processedFilenames = new Set<string>()
|
|
170
|
+
/** Which ref claimed each filename, so a later collision can name both sides. */
|
|
171
|
+
const filenameOwners = new Map<string, string>()
|
|
170
172
|
|
|
171
173
|
// Root node first — its filename reserves a slot so a later ref that maps to
|
|
172
174
|
// the same name does not emit a duplicate file.
|
|
173
175
|
const rootFilename = rootTypeName.toLowerCase()
|
|
174
176
|
processedFilenames.add(rootFilename)
|
|
177
|
+
filenameOwners.set(rootFilename, `the root type ${rootTypeName}`)
|
|
175
178
|
|
|
176
179
|
// An alias root (a document that is just `$ref: '#/$defs/x'`) whose derived
|
|
177
180
|
// filename equals its target's would reserve the filename for a wrapper that
|
|
@@ -218,8 +221,22 @@ export const walkRefGraph = (
|
|
|
218
221
|
}
|
|
219
222
|
|
|
220
223
|
const filename = refToFilename(ref)
|
|
221
|
-
if (
|
|
224
|
+
if (processedFilenames.has(filename)) {
|
|
225
|
+
// Two definitions that reduce to one filename (`Pet`/`pet`, `foo-bar`/`foo.bar`)
|
|
226
|
+
// silently collapse: the first one generated wins and the second is dropped,
|
|
227
|
+
// so every reference to it gets the wrong type with no other signal. Warn
|
|
228
|
+
// loudly — renaming the definition is the only fix, and it has to be the
|
|
229
|
+
// caller's, since the name is what the emitted imports are keyed on.
|
|
230
|
+
const owner = filenameOwners.get(filename)
|
|
231
|
+
if (owner !== undefined && owner !== ref) {
|
|
232
|
+
console.warn(
|
|
233
|
+
`Warning: ${ref} and ${owner} both map to "${filename}.ts" — only ${owner} was generated, so ` +
|
|
234
|
+
`every reference to the other resolves to the wrong type. Rename one so each gets its own file.`,
|
|
235
|
+
)
|
|
236
|
+
}
|
|
237
|
+
} else {
|
|
222
238
|
processedFilenames.add(filename)
|
|
239
|
+
filenameOwners.set(filename, ref)
|
|
223
240
|
visit({
|
|
224
241
|
ref,
|
|
225
242
|
typeName: refToName(ref, typeSuffix),
|