@amritk/helpers 0.7.1 → 0.9.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.
Files changed (83) hide show
  1. package/dist/build-dynamic-ref-map.d.ts +0 -1
  2. package/dist/derive-root-type-name.d.ts +0 -1
  3. package/dist/escape-regex-pattern.d.ts +16 -0
  4. package/dist/escape-regex-pattern.js +20 -0
  5. package/dist/extract-dynamic-anchor-defs.d.ts +0 -1
  6. package/dist/extract-refs.d.ts +0 -1
  7. package/dist/generate-index-barrel.d.ts +0 -1
  8. package/dist/generate-type-definition.d.ts +0 -1
  9. package/dist/has-ref.d.ts +0 -1
  10. package/dist/is-object.d.ts +0 -1
  11. package/dist/mjst-extension.d.ts +0 -1
  12. package/dist/parse-documentation.d.ts +0 -1
  13. package/dist/ref-to-filename.d.ts +0 -1
  14. package/dist/ref-to-name.d.ts +0 -1
  15. package/dist/resolve-dynamic-refs.d.ts +4 -4
  16. package/dist/resolve-dynamic-refs.js +10 -4
  17. package/dist/resolve-ref.d.ts +0 -1
  18. package/dist/safe-accessor.d.ts +0 -1
  19. package/dist/schema-guards.d.ts +16 -1
  20. package/dist/schema-guards.js +29 -0
  21. package/dist/upgrade-draft07-schema.d.ts +0 -1
  22. package/dist/validate-array.d.ts +0 -1
  23. package/dist/validate-record.d.ts +0 -1
  24. package/dist/walk-ref-graph.d.ts +0 -1
  25. package/package.json +2 -4
  26. package/dist/build-dynamic-ref-map.d.ts.map +0 -1
  27. package/dist/derive-root-type-name.d.ts.map +0 -1
  28. package/dist/extract-dynamic-anchor-defs.d.ts.map +0 -1
  29. package/dist/extract-refs.d.ts.map +0 -1
  30. package/dist/generate-index-barrel.d.ts.map +0 -1
  31. package/dist/generate-type-definition.d.ts.map +0 -1
  32. package/dist/has-ref.d.ts.map +0 -1
  33. package/dist/is-object.d.ts.map +0 -1
  34. package/dist/mjst-extension.d.ts.map +0 -1
  35. package/dist/parse-documentation.d.ts.map +0 -1
  36. package/dist/ref-to-filename.d.ts.map +0 -1
  37. package/dist/ref-to-name.d.ts.map +0 -1
  38. package/dist/resolve-dynamic-refs.d.ts.map +0 -1
  39. package/dist/resolve-ref.d.ts.map +0 -1
  40. package/dist/safe-accessor.d.ts.map +0 -1
  41. package/dist/schema-guards.d.ts.map +0 -1
  42. package/dist/upgrade-draft07-schema.d.ts.map +0 -1
  43. package/dist/validate-array.d.ts.map +0 -1
  44. package/dist/validate-record.d.ts.map +0 -1
  45. package/dist/walk-ref-graph.d.ts.map +0 -1
  46. package/src/build-dynamic-ref-map.test.ts +0 -65
  47. package/src/build-dynamic-ref-map.ts +0 -42
  48. package/src/derive-root-type-name.test.ts +0 -42
  49. package/src/derive-root-type-name.ts +0 -42
  50. package/src/extract-dynamic-anchor-defs.test.ts +0 -29
  51. package/src/extract-dynamic-anchor-defs.ts +0 -33
  52. package/src/extract-refs.test.ts +0 -183
  53. package/src/extract-refs.ts +0 -73
  54. package/src/generate-index-barrel.test.ts +0 -41
  55. package/src/generate-index-barrel.ts +0 -62
  56. package/src/generate-type-definition.test.ts +0 -1386
  57. package/src/generate-type-definition.ts +0 -580
  58. package/src/has-ref.ts +0 -10
  59. package/src/is-object.test.ts +0 -65
  60. package/src/is-object.ts +0 -15
  61. package/src/mjst-extension.test.ts +0 -59
  62. package/src/mjst-extension.ts +0 -81
  63. package/src/parse-documentation.test.ts +0 -313
  64. package/src/parse-documentation.ts +0 -173
  65. package/src/ref-to-filename.test.ts +0 -64
  66. package/src/ref-to-filename.ts +0 -116
  67. package/src/ref-to-name.test.ts +0 -57
  68. package/src/ref-to-name.ts +0 -42
  69. package/src/resolve-dynamic-refs.test.ts +0 -94
  70. package/src/resolve-dynamic-refs.ts +0 -48
  71. package/src/resolve-ref.test.ts +0 -186
  72. package/src/resolve-ref.ts +0 -82
  73. package/src/safe-accessor.test.ts +0 -41
  74. package/src/safe-accessor.ts +0 -48
  75. package/src/schema-guards.test.ts +0 -406
  76. package/src/schema-guards.ts +0 -160
  77. package/src/upgrade-draft07-schema.ts +0 -179
  78. package/src/validate-array.test.ts +0 -52
  79. package/src/validate-array.ts +0 -15
  80. package/src/validate-record.test.ts +0 -56
  81. package/src/validate-record.ts +0 -19
  82. package/src/walk-ref-graph.test.ts +0 -118
  83. package/src/walk-ref-graph.ts +0 -184
@@ -1,33 +0,0 @@
1
- import type { JSONSchema } from 'json-schema-typed/draft-2020-12'
2
-
3
- /**
4
- * Collects `#/$defs/<key>` refs for every root definition that carries a
5
- * `$dynamicAnchor`.
6
- *
7
- * These definitions are reachable only through `$dynamicRef`, which the ref
8
- * walker does not follow directly (a `$dynamicRef` is rewritten to a concrete
9
- * `$ref` by `resolveDynamicRefs` *after* the schema is extracted, so plain
10
- * `extractRefs` never sees it). Seeding them explicitly guarantees a file is
11
- * generated for each dynamic-anchor target — without this, a generator would
12
- * emit code that imports a type whose file was never produced.
13
- *
14
- * @example
15
- * ```ts
16
- * // $defs.schema has $dynamicAnchor: "meta"
17
- * extractDynamicAnchorDefs(rootSchema) // ['#/$defs/schema']
18
- * ```
19
- */
20
- export const extractDynamicAnchorDefs = (schema: JSONSchema): string[] => {
21
- const refs: string[] = []
22
-
23
- if (typeof schema !== 'object' || schema === null) return refs
24
- if (!('$defs' in schema) || typeof schema['$defs'] !== 'object' || schema['$defs'] === null) return refs
25
-
26
- for (const [key, value] of Object.entries(schema['$defs'])) {
27
- if (typeof value === 'object' && value !== null && '$dynamicAnchor' in value) {
28
- refs.push(`#/$defs/${key}`)
29
- }
30
- }
31
-
32
- return refs
33
- }
@@ -1,183 +0,0 @@
1
- import { describe, expect, it } from 'vitest'
2
-
3
- import { extractRefs } from './extract-refs'
4
-
5
- describe('extract-refs', () => {
6
- it('extracts refs from a simple schema', () => {
7
- const schema = {
8
- type: 'object',
9
- properties: {
10
- contact: { $ref: '#/$defs/contact' },
11
- server: { $ref: '#/$defs/server' },
12
- },
13
- }
14
-
15
- const refs = extractRefs(schema)
16
-
17
- expect(refs).toEqual(new Set(['#/$defs/contact', '#/$defs/server']))
18
- })
19
-
20
- it('extracts refs from nested objects', () => {
21
- const schema = {
22
- type: 'object',
23
- properties: {
24
- info: {
25
- type: 'object',
26
- properties: {
27
- contact: { $ref: '#/$defs/contact' },
28
- },
29
- },
30
- },
31
- }
32
-
33
- const refs = extractRefs(schema)
34
-
35
- expect(refs).toEqual(new Set(['#/$defs/contact']))
36
- })
37
-
38
- it('extracts refs from arrays', () => {
39
- const schema = {
40
- type: 'object',
41
- properties: {
42
- servers: {
43
- type: 'array',
44
- items: { $ref: '#/$defs/server' },
45
- },
46
- },
47
- }
48
-
49
- const refs = extractRefs(schema)
50
-
51
- expect(refs).toEqual(new Set(['#/$defs/server']))
52
- })
53
-
54
- it('extracts refs from allOf, anyOf, oneOf', () => {
55
- const schema = {
56
- allOf: [{ $ref: '#/$defs/base' }],
57
- anyOf: [{ $ref: '#/$defs/option1' }, { $ref: '#/$defs/option2' }],
58
- oneOf: [{ $ref: '#/$defs/choice1' }],
59
- }
60
-
61
- const refs = extractRefs(schema)
62
-
63
- expect(refs).toEqual(new Set(['#/$defs/base', '#/$defs/option1', '#/$defs/option2', '#/$defs/choice1']))
64
- })
65
-
66
- it('extracts refs from additionalProperties', () => {
67
- const schema = {
68
- type: 'object',
69
- additionalProperties: { $ref: '#/$defs/value' },
70
- }
71
-
72
- const refs = extractRefs(schema)
73
-
74
- expect(refs).toEqual(new Set(['#/$defs/value']))
75
- })
76
-
77
- it('includes URI refs (http/https)', () => {
78
- const schema = {
79
- type: 'object',
80
- properties: {
81
- internal: { $ref: '#/$defs/internal' },
82
- external: { $ref: 'https://example.com/schema.json' },
83
- },
84
- }
85
-
86
- const refs = extractRefs(schema)
87
-
88
- expect(refs).toEqual(new Set(['#/$defs/internal', 'https://example.com/schema.json']))
89
- })
90
-
91
- it('includes URI refs with fragments', () => {
92
- const schema = {
93
- type: 'object',
94
- properties: {
95
- queue: { $ref: 'http://example.com/channel.json#/definitions/queue' },
96
- },
97
- }
98
-
99
- const refs = extractRefs(schema)
100
-
101
- expect(refs).toEqual(new Set(['http://example.com/channel.json#/definitions/queue']))
102
- })
103
-
104
- it('ignores bare # self-references', () => {
105
- const schema = {
106
- type: 'object',
107
- properties: {
108
- self: { $ref: '#' },
109
- contact: { $ref: '#/$defs/contact' },
110
- },
111
- }
112
-
113
- const refs = extractRefs(schema)
114
-
115
- expect(refs).toEqual(new Set(['#/$defs/contact']))
116
- })
117
-
118
- it('ignores relative path refs', () => {
119
- const schema = {
120
- type: 'object',
121
- properties: {
122
- msg: { $ref: '/components/messages/userSignedUp' },
123
- contact: { $ref: '#/$defs/contact' },
124
- },
125
- }
126
-
127
- const refs = extractRefs(schema)
128
-
129
- expect(refs).toEqual(new Set(['#/$defs/contact']))
130
- })
131
-
132
- it('returns empty set when no refs exist', () => {
133
- const schema = {
134
- type: 'object',
135
- properties: {
136
- name: { type: 'string' },
137
- age: { type: 'number' },
138
- },
139
- }
140
-
141
- const refs = extractRefs(schema)
142
-
143
- expect(refs).toEqual(new Set())
144
- })
145
-
146
- it('handles deeply nested refs', () => {
147
- const schema = {
148
- type: 'object',
149
- properties: {
150
- level1: {
151
- type: 'object',
152
- properties: {
153
- level2: {
154
- type: 'object',
155
- properties: {
156
- level3: { $ref: '#/$defs/deep' },
157
- },
158
- },
159
- },
160
- },
161
- },
162
- }
163
-
164
- const refs = extractRefs(schema)
165
-
166
- expect(refs).toEqual(new Set(['#/$defs/deep']))
167
- })
168
-
169
- it('deduplicates refs', () => {
170
- const schema = {
171
- type: 'object',
172
- properties: {
173
- contact1: { $ref: '#/$defs/contact' },
174
- contact2: { $ref: '#/$defs/contact' },
175
- contact3: { $ref: '#/$defs/contact' },
176
- },
177
- }
178
-
179
- const refs = extractRefs(schema)
180
-
181
- expect(refs).toEqual(new Set(['#/$defs/contact']))
182
- })
183
- })
@@ -1,73 +0,0 @@
1
- import type { JSONSchema } from 'json-schema-typed/draft-2020-12'
2
-
3
- /**
4
- * Returns true if a $ref value should be queued for processing.
5
- *
6
- * Accepted forms:
7
- * - Internal: `#/$defs/foo` or `#/definitions/foo`
8
- * - URI key: `http://example.com/foo.json` (no fragment, or empty fragment)
9
- * - URI with fragment: `http://example.com/foo.json#/definitions/bar`
10
- *
11
- * Excluded:
12
- * - `#` alone (self-reference, not a standalone definition)
13
- * - Relative path refs (e.g. `/components/messages/foo`) — these point into
14
- * example data in the schema document, not into type definitions
15
- */
16
- const isResolvableRef = (ref: string): boolean => {
17
- if (ref === '#') return false
18
- if (ref.startsWith('#')) return true
19
- if (ref.startsWith('http://') || ref.startsWith('https://')) return true
20
- return false
21
- }
22
-
23
- /**
24
- * Extracts all $ref values from a JSON Schema recursively.
25
- * Returns both internal (`#`-prefixed) and URI refs so the build pipeline
26
- * can resolve and generate files for all referenced definitions.
27
- *
28
- * @param schema - The JSON Schema to extract refs from
29
- * @returns A Set of unique ref strings found in the schema
30
- *
31
- * @example
32
- * ```ts
33
- * const schema = {
34
- * type: 'object',
35
- * properties: {
36
- * contact: { $ref: '#/$defs/contact' },
37
- * channel: { $ref: 'http://example.com/channel.json' },
38
- * }
39
- * }
40
- * const refs = extractRefs(schema)
41
- * // refs = Set(['#/$defs/contact', 'http://example.com/channel.json'])
42
- * ```
43
- */
44
- export const extractRefs = (schema: JSONSchema): Set<string> => {
45
- const refs = new Set<string>()
46
-
47
- const traverse = (obj: unknown): void => {
48
- if (typeof obj !== 'object' || obj === null) {
49
- return
50
- }
51
-
52
- if (Array.isArray(obj)) {
53
- for (const item of obj) {
54
- traverse(item)
55
- }
56
- return
57
- }
58
-
59
- const record = obj as Record<string, unknown>
60
-
61
- if ('$ref' in record && typeof record['$ref'] === 'string' && isResolvableRef(record['$ref'] as string)) {
62
- refs.add(record['$ref'] as string)
63
- }
64
-
65
- // Recursively traverse all properties using for...in to avoid intermediate array allocation
66
- for (const key in record) {
67
- traverse(record[key])
68
- }
69
- }
70
-
71
- traverse(schema)
72
- return refs
73
- }
@@ -1,41 +0,0 @@
1
- import { describe, expect, it } from 'vitest'
2
-
3
- import { generateIndexBarrel } from './generate-index-barrel'
4
-
5
- describe('generate-index-barrel', () => {
6
- it('re-exports types and consts, sorted by filename', () => {
7
- const files = [
8
- { filename: 'contact.ts', content: 'export type Contact = {}\nexport const parseContact = () => {}\n' },
9
- { filename: 'address.ts', content: 'export type Address = {}\nexport const parseAddress = () => {}\n' },
10
- ]
11
-
12
- expect(generateIndexBarrel(files)).toBe(
13
- "export { type Address, parseAddress } from './address';\n" +
14
- "export { type Contact, parseContact } from './contact';\n",
15
- )
16
- })
17
-
18
- it('emits type-only re-exports when typesOnly is set', () => {
19
- const files = [{ filename: 'contact.ts', content: 'export type Contact = {}\n' }]
20
-
21
- expect(generateIndexBarrel(files, { typesOnly: true })).toBe("export type { Contact } from './contact';\n")
22
- })
23
-
24
- it('never re-exports internal _helpers modules', () => {
25
- const files = [
26
- { filename: 'document.ts', content: 'export type Document = {}\n' },
27
- { filename: '_helpers/is-object.ts', content: 'export const isObject = () => {}\n' },
28
- ]
29
-
30
- expect(generateIndexBarrel(files)).toBe("export { type Document } from './document';\n")
31
- })
32
-
33
- it('skips files that export nothing', () => {
34
- const files = [
35
- { filename: 'document.ts', content: 'export type Document = {}\n' },
36
- { filename: 'empty.ts', content: '// nothing here\n' },
37
- ]
38
-
39
- expect(generateIndexBarrel(files)).toBe("export { type Document } from './document';\n")
40
- })
41
- })
@@ -1,62 +0,0 @@
1
- /** A generated file: its name (with extension) and TypeScript source. */
2
- export type IndexBarrelFile = {
3
- filename: string
4
- content: string
5
- }
6
-
7
- /** Options controlling how the barrel re-exports each module. */
8
- export type GenerateIndexBarrelOptions = {
9
- /**
10
- * When true, every re-export is type-only (`export type { ... }`). Used by the
11
- * types-only parser output, where no runtime values exist to re-export.
12
- * Defaults to `false`.
13
- */
14
- readonly typesOnly?: boolean
15
- }
16
-
17
- // Generated files declare their public surface with these two forms, so we can
18
- // recover the export names from the source text without parsing it.
19
- const TYPE_EXPORT_RE = /^export type (\w+)/gm
20
- const CONST_EXPORT_RE = /^export const (\w+)/gm
21
-
22
- /**
23
- * Builds the `index.ts` barrel that re-exports every generated module. This is
24
- * the shared version of the near-identical barrel each generator used to build
25
- * inline: it scans each file's source for `export type` / `export const`
26
- * declarations and emits one re-export line per module, sorted by filename.
27
- *
28
- * Files under `_helpers/` are internal runtime helpers (embedded-mode output)
29
- * and are never re-exported. Modules that expose nothing are skipped.
30
- *
31
- * @param files - The generated files to barrel (the `index.ts` itself excluded).
32
- * @param options - See {@link GenerateIndexBarrelOptions}.
33
- * @returns The `index.ts` file content.
34
- */
35
- export const generateIndexBarrel = (files: IndexBarrelFile[], options: GenerateIndexBarrelOptions = {}): string => {
36
- const typesOnly = options.typesOnly ?? false
37
-
38
- const sortedFiles = files
39
- .filter((file) => !file.filename.startsWith('_helpers/'))
40
- .sort((a, b) => a.filename.localeCompare(b.filename))
41
-
42
- let indexContent = ''
43
- for (const file of sortedFiles) {
44
- const moduleName = file.filename.replace(/\.ts$/, '')
45
- const typeNames: string[] = []
46
- const constNames: string[] = []
47
-
48
- for (const match of file.content.matchAll(TYPE_EXPORT_RE)) typeNames.push(match[1] as string)
49
- for (const match of file.content.matchAll(CONST_EXPORT_RE)) constNames.push(match[1] as string)
50
-
51
- if (typeNames.length === 0 && constNames.length === 0) continue
52
-
53
- if (typesOnly) {
54
- indexContent += `export type { ${typeNames.join(', ')} } from './${moduleName}';\n`
55
- } else {
56
- const typeExports = typeNames.map((name) => `type ${name}`)
57
- indexContent += `export { ${[...typeExports, ...constNames].join(', ')} } from './${moduleName}';\n`
58
- }
59
- }
60
-
61
- return indexContent
62
- }