Skip to content

Commit

Permalink
add new converter for typecastExpression (atlassian#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
Noviny authored Apr 4, 2019
1 parent 6769531 commit e401ba8
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .changeset/9ba74d74/changes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"releases": [
{ "name": "extract-react-types", "type": "patch" },
{ "name": "kind2string", "type": "patch" }
],
"dependents": []
}
1 change: 1 addition & 0 deletions .changeset/9ba74d74/changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Add converter for typeCastExpression
39 changes: 39 additions & 0 deletions packages/extract-react-types/__snapshots__/test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions packages/extract-react-types/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
4 changes: 4 additions & 0 deletions packages/extract-react-types/src/kinds.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 13 additions & 0 deletions packages/extract-react-types/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1545,6 +1545,19 @@ const TESTS = [
}))
`
},
{
name: 'Flow TypeCastExpression',
typeSystem: 'flow',
code: `
type Props = { bar: string }
class Component extends React.Component<Props> {
static defaultProps = {
bar: (ascii: string),
}
}
`
}
];

Expand Down
4 changes: 3 additions & 1 deletion packages/kind2string/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`;
},
Expand Down
14 changes: 14 additions & 0 deletions packages/kind2string/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<Props> {
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', () => {});
});
Expand Down

0 comments on commit e401ba8

Please sign in to comment.