Skip to content

Commit b050da5

Browse files
authored
feature: add better support for json schema refs (#78)
1 parent 6305a28 commit b050da5

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

packages/json-worker/src/parts/JsonCompletionProperty/JsonCompletionProperty.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@ import * as PropertyKeyToCompletionOption from '../PropertyKeyToCompletionOption
44
import * as ResolveSchemaRef from '../ResolveSchemaRef/ResolveSchemaRef.ts'
55
import type { JsonSchema } from '../JsonSchema/JsonSchema.ts'
66

7-
const getSchemaProperties = (schema: JsonSchema): JsonSchema['properties'] => {
7+
const getSchemaProperties = (
8+
rootSchema: JsonSchema,
9+
schema: JsonSchema,
10+
): JsonSchema['properties'] => {
811
if (schema.properties) {
912
return schema.properties
1013
}
1114

1215
if (schema.$ref) {
13-
const resolved = ResolveSchemaRef.resolveSchemaRef(schema, schema.$ref)
14-
return resolved.properties || {}
16+
const resolved = ResolveSchemaRef.resolveSchemaRef(rootSchema, schema.$ref)
17+
return getSchemaProperties(rootSchema, resolved)
1518
}
1619

1720
if (schema.allOf) {
1821
const properties: { [key: string]: JsonSchema } = {}
1922
for (const subSchema of schema.allOf) {
20-
const subProperties = getSchemaProperties(subSchema)
23+
const subProperties = getSchemaProperties(rootSchema, subSchema)
2124
Object.assign(properties, subProperties)
2225
}
2326
return properties
@@ -26,7 +29,7 @@ const getSchemaProperties = (schema: JsonSchema): JsonSchema['properties'] => {
2629
if (schema.anyOf) {
2730
const properties: { [key: string]: JsonSchema } = {}
2831
for (const subSchema of schema.anyOf) {
29-
const subProperties = getSchemaProperties(subSchema)
32+
const subProperties = getSchemaProperties(rootSchema, subSchema)
3033
Object.assign(properties, subProperties)
3134
}
3235
return properties
@@ -39,7 +42,7 @@ export const jsonCompletionProperty = (
3942
schema: JsonSchema,
4043
node: AstNode,
4144
): readonly CompletionItem[] => {
42-
const properties = getSchemaProperties(schema)
45+
const properties = getSchemaProperties(schema, schema)
4346
const keys = Object.keys(properties || {})
4447
return keys.map(PropertyKeyToCompletionOption.propertyKeyToCompletionOption)
4548
}

packages/json-worker/test/JsonCompletionProperty.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { jest, test, expect } from '@jest/globals'
22
import * as JsonCompletionProperty from '../src/parts/JsonCompletionProperty/JsonCompletionProperty.ts'
3+
import * as CompletionType from '../src/parts/CompletionType/CompletionType.ts'
34

4-
test.skip('handles schema references', () => {
5+
test('handles schema references', () => {
56
const schema = {
67
allOf: [
78
{
@@ -34,7 +35,7 @@ test.skip('handles schema references', () => {
3435
const result = JsonCompletionProperty.jsonCompletionProperty(schema, node)
3536
expect(result).toEqual([
3637
{
37-
kind: 10,
38+
kind: CompletionType.Property,
3839
label: 'compilerOptions',
3940
},
4041
])

0 commit comments

Comments
 (0)