From 921bdd04cb32dd9564865d32668eb7422d6eea2a Mon Sep 17 00:00:00 2001 From: Paul Loyd Date: Tue, 6 Nov 2018 20:03:36 +0300 Subject: [PATCH] Support string keys --- src/collector/definitions.js | 9 ++++++--- tests/samples/stringKeys/schema.json | 20 ++++++++++++++++++++ tests/samples/stringKeys/source.js | 13 +++++++++++++ tests/samples/stringKeys/types.yaml | 18 ++++++++++++++++++ 4 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 tests/samples/stringKeys/schema.json create mode 100644 tests/samples/stringKeys/source.js create mode 100644 tests/samples/stringKeys/types.yaml diff --git a/src/collector/definitions.js b/src/collector/definitions.js index 7e5eb0b..c4dd86a 100644 --- a/src/collector/definitions.js +++ b/src/collector/definitions.js @@ -12,7 +12,8 @@ import type { } from '@babel/types'; import { - isIdentifier, isObjectTypeProperty, isStringLiteralTypeAnnotation, isClassProperty, + isIdentifier, isStringLiteral, isObjectTypeProperty, + isStringLiteralTypeAnnotation, isClassProperty, } from '@babel/types'; import Context from './context'; @@ -215,10 +216,12 @@ function makeField(ctx: Context, node: ObjectTypeProperty | ClassProperty): ?Fie // TODO: warning about computed properties. invariant(isObjectTypeProperty(node) || !node.computed); - invariant(isIdentifier(node.key)); + invariant(isIdentifier(node.key) || isStringLiteral(node.key)); + + const name = isIdentifier(node.key) ? node.key.name : node.key.value; return { - name: node.key.name, + name, value: type, required: node.optional == null || !node.optional, }; diff --git a/tests/samples/stringKeys/schema.json b/tests/samples/stringKeys/schema.json new file mode 100644 index 0000000..346f76a --- /dev/null +++ b/tests/samples/stringKeys/schema.json @@ -0,0 +1,20 @@ +{ + "$schema": "http://json-schema.org/draft-06/schema#", + "definitions": { + "stringKeys::X": { + "type": "object", + "properties": {"a b": {"type": "string"}}, + "required": ["a b"] + }, + "stringKeys::Y": { + "type": "object", + "properties": {"a b": {"type": "string"}}, + "required": ["a b"] + }, + "stringKeys::Z": { + "type": "object", + "properties": {"a b": {"type": "string"}}, + "required": ["a b"] + } + } +} diff --git a/tests/samples/stringKeys/source.js b/tests/samples/stringKeys/source.js new file mode 100644 index 0000000..ab89ecc --- /dev/null +++ b/tests/samples/stringKeys/source.js @@ -0,0 +1,13 @@ +type X = { + 'a b': string; +}; + +interface Y { + 'a b': string; +} + +class Z { + 'a b': string; +} + +export {X, Y, Z}; diff --git a/tests/samples/stringKeys/types.yaml b/tests/samples/stringKeys/types.yaml new file mode 100644 index 0000000..5b7505f --- /dev/null +++ b/tests/samples/stringKeys/types.yaml @@ -0,0 +1,18 @@ +- kind: record + fields: + - name: "a b" + value: {kind: string} + required: true + id: [stringKeys, X] +- kind: record + fields: + - name: "a b" + value: {kind: string} + required: true + id: [stringKeys, Y] +- kind: record + fields: + - name: "a b" + value: {kind: string} + required: true + id: [stringKeys, Z]