Support string keys

master
Paul Loyd 2018-11-06 20:03:36 +03:00
parent cdc9304304
commit 921bdd04cb
4 changed files with 57 additions and 3 deletions

View File

@ -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,
};

View File

@ -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"]
}
}
}

View File

@ -0,0 +1,13 @@
type X = {
'a b': string;
};
interface Y {
'a b': string;
}
class Z {
'a b': string;
}
export {X, Y, Z};

View File

@ -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]