diff --git a/.changeset/9ba74d74/changes.json b/.changeset/9ba74d74/changes.json new file mode 100644 index 00000000..42a4dcd5 --- /dev/null +++ b/.changeset/9ba74d74/changes.json @@ -0,0 +1,7 @@ +{ + "releases": [ + { "name": "extract-react-types", "type": "patch" }, + { "name": "kind2string", "type": "patch" } + ], + "dependents": [] +} diff --git a/.changeset/9ba74d74/changes.md b/.changeset/9ba74d74/changes.md new file mode 100644 index 00000000..90b5e363 --- /dev/null +++ b/.changeset/9ba74d74/changes.md @@ -0,0 +1 @@ +- Add converter for typeCastExpression \ No newline at end of file diff --git a/packages/extract-react-types/__snapshots__/test.js.snap b/packages/extract-react-types/__snapshots__/test.js.snap index 88868c2b..9ea1e2d9 100644 --- a/packages/extract-react-types/__snapshots__/test.js.snap +++ b/packages/extract-react-types/__snapshots__/test.js.snap @@ -169,6 +169,45 @@ Object { } `; +exports[`Flow TypeCastExpression 1`] = ` +Object { + "component": Object { + "kind": "generic", + "name": Object { + "kind": "id", + "name": "Component", + "type": null, + }, + "value": Object { + "kind": "object", + "members": Array [ + Object { + "default": Object { + "expression": Object { + "kind": "id", + "name": "ascii", + "type": null, + }, + "kind": "typeCastExpression", + }, + "key": Object { + "kind": "id", + "name": "bar", + }, + "kind": "property", + "optional": false, + "value": Object { + "kind": "string", + }, + }, + ], + "referenceIdName": "Props", + }, + }, + "kind": "program", +} +`; + exports[`NullLiteralTypeAnnotation 1`] = ` Object { "component": Object { diff --git a/packages/extract-react-types/src/index.js b/packages/extract-react-types/src/index.js index 693b6611..edcd99f4 100644 --- a/packages/extract-react-types/src/index.js +++ b/packages/extract-react-types/src/index.js @@ -417,6 +417,10 @@ converters.StringLiteral = (path, context) /*: K.String*/ => { return { kind: 'string', value: path.node.value }; }; +converters.TypeCastExpression = (path, context) /*: K.TypeCastExpression*/ => { + return { kind: 'typeCastExpression', expression: convert(path.get('expression'), context) }; +}; + /* eslint-disable-next-line no-unused-vars */ converters.NumericLiteral = (path, context) /*: K.Number*/ => { return { kind: 'number', value: path.node.value }; diff --git a/packages/extract-react-types/src/kinds.js b/packages/extract-react-types/src/kinds.js index 800698bc..49c50ce9 100644 --- a/packages/extract-react-types/src/kinds.js +++ b/packages/extract-react-types/src/kinds.js @@ -22,6 +22,10 @@ export type Rest = { kind: "rest", argument: Id }; +export type TypeCastExpression = { + kind: "typeCastExpression", + expression: Id +}; export type TemplateExpression = { kind: "templateExpression", tag: Id }; export type AssignmentPattern = { kind: "assignmentPattern", diff --git a/packages/extract-react-types/test.js b/packages/extract-react-types/test.js index 4ff730ba..285bd9f1 100644 --- a/packages/extract-react-types/test.js +++ b/packages/extract-react-types/test.js @@ -1545,6 +1545,19 @@ const TESTS = [ })) ` + }, + { + name: 'Flow TypeCastExpression', + typeSystem: 'flow', + code: ` + type Props = { bar: string } + + class Component extends React.Component { + static defaultProps = { + bar: (ascii: string), + } + } + ` } ]; diff --git a/packages/kind2string/src/index.js b/packages/kind2string/src/index.js index 2be21ab8..51e6ca32 100644 --- a/packages/kind2string/src/index.js +++ b/packages/kind2string/src/index.js @@ -76,7 +76,9 @@ const converters = { id: (type /*: K.Id*/, mode /*: string */) /*:string*/ => { return type.name; }, - + typeCastExpression: (type /*: K.TypeCastExpression*/, mode /*: string */) /*:string*/ => { + return convert(type.expression); + }, JSXMemberExpression: (type /*:any*/, mode /*: string */) /*:string*/ => { return `${convert(type.object)}.${convert(type.property)}`; }, diff --git a/packages/kind2string/test.js b/packages/kind2string/test.js index e98a8f29..8b4005d1 100644 --- a/packages/kind2string/test.js +++ b/packages/kind2string/test.js @@ -336,6 +336,20 @@ describe('kind 2 string tests', () => { expect(converted).toBe('foo'); }); }); + describe('typecastExpression', () => { + let file = ` + type Props = { bar: string } + + class Component extends React.Component { + static defaultProps = { + bar: (ascii: string), + } + } + `; + let res = extractReactTypes(file, 'flow').component.value.members[0].default; + let converted = convert(res); + expect(converted).toBe('ascii'); + }); describe('utilities', () => { describe('resolveLast', () => {}); });