Skip to content

Commit

Permalink
Add logical expression operator (atlassian#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
Noviny authored Apr 12, 2019
1 parent a972d10 commit 4b3b4a4
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .changeset/4298a628/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/4298a628/changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Add logicalExpression converter
133 changes: 133 additions & 0 deletions packages/extract-react-types/__snapshots__/test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,139 @@ Object {
}
`;

exports[`LogicalExpression and 1`] = `
Object {
"component": Object {
"kind": "object",
"members": Array [
Object {
"default": Object {
"kind": "logicalExpression",
"left": Object {
"kind": "boolean",
"value": true,
},
"operator": "&&",
"right": Object {
"kind": "string",
"value": "something",
},
},
"key": Object {
"kind": "id",
"name": "and",
},
"kind": "property",
"optional": false,
"value": Object {
"kind": "string",
},
},
],
"name": Object {
"kind": "id",
"name": "Button",
"type": null,
},
},
"kind": "program",
}
`;

exports[`LogicalExpression or 1`] = `
Object {
"component": Object {
"kind": "object",
"members": Array [
Object {
"default": Object {
"kind": "logicalExpression",
"left": Object {
"kind": "string",
"value": "me",
},
"operator": "||",
"right": Object {
"kind": "string",
"value": "you",
},
},
"key": Object {
"kind": "id",
"name": "or",
},
"kind": "property",
"optional": false,
"value": Object {
"kind": "string",
},
},
],
"name": Object {
"kind": "id",
"name": "Button",
"type": null,
},
},
"kind": "program",
}
`;

exports[`LogicalExpression or complicated 1`] = `
Object {
"component": Object {
"kind": "object",
"members": Array [
Object {
"default": Object {
"kind": "logicalExpression",
"left": Object {
"kind": "logicalExpression",
"left": Object {
"kind": "string",
"value": "me",
},
"operator": "||",
"right": Object {
"kind": "string",
"value": "you",
},
},
"operator": "||",
"right": Object {
"kind": "logicalExpression",
"left": Object {
"kind": "string",
"value": "someone else",
},
"operator": "&&",
"right": Object {
"kind": "string",
"value": "impossible state",
},
},
},
"key": Object {
"kind": "id",
"name": "or",
},
"kind": "property",
"optional": false,
"value": Object {
"kind": "string",
},
},
],
"name": Object {
"kind": "id",
"name": "Button",
"type": null,
},
},
"kind": "program",
}
`;

exports[`NullLiteralTypeAnnotation 1`] = `
Object {
"component": Object {
Expand Down
9 changes: 9 additions & 0 deletions packages/extract-react-types/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,15 @@ converters.TemplateLiteral = (path, context) /*: K.TemplateLiteral*/ => {
};
};

converters.LogicalExpression = (path, context) => {
return {
kind: 'logicalExpression',
operator: path.node.operator,
left: convert(path.get('left'), context),
right: convert(path.get('right'), context),
};
};

converters.RestElement = (path, context) /*: K.Rest */ => {
return {
kind: 'rest',
Expand Down
11 changes: 10 additions & 1 deletion packages/extract-react-types/src/kinds.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ export type ArrayType = {
type: AnyTypeKind,
}
export type LogicalExpression = {
kind: "logicalExpression",
operator: string,
left: AnyValueKind,
right: AnyValueKind
};
export type Obj = { kind: "object", members: Array<Property> };
export type Property = {
kind: "property",
Expand Down Expand Up @@ -211,6 +218,8 @@ export type AnyValueKind =
| String
| TemplateExpression
| TemplateLiteral
| Variable;
| Variable
| LogicalExpression;
export type AnyKind = AnyTypeKind | AnyValueKind | Program;
*/
35 changes: 35 additions & 0 deletions packages/extract-react-types/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,41 @@ const TESTS = [
}
`
},
{
name: 'LogicalExpression and',
typeSystem: 'flow',
code: `
class Button extends React.Component<{ and: string }> {
static defaultProps = {
and: true && 'something',
}
}
`
},
{
name: 'LogicalExpression or',
typeSystem: 'flow',
code: `
class Button extends React.Component<{ or: string }> {
static defaultProps = {
or: 'me' || 'you',
}
}
`
},
{
name: 'LogicalExpression or complicated',
typeSystem: 'flow',
code: `
class Button extends React.Component<{ or: string }> {
static defaultProps = {
or: 'me' || 'you' || 'someone else' && 'impossible state',
}
}
`
},
{
name: 'flow string',
typeSystem: 'flow',
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 @@ -67,7 +67,9 @@ const converters = {
literal: (type /*: any */, mode /*: string */) /*: string*/ => `${type.kind}`,
mixed: (type /*: K.Mixed*/, mode /*: string */) /*:string*/ => type.kind,
null: (type /*: K.Null */, mode /*: string */) /*: 'null' */ => 'null',

logicalExpression: (type, mode /*: string */) /*:string*/ => {
return `${convert(type.left)} ${type.operator} ${convert(type.right)}`;
},
unary: (type /*: K.Unary*/, mode /*: string */) /*:string*/ => {
let space = unaryWhiteList.includes(type.operator) ? '' : ' ';
return `${type.operator}${space}${convert(type.argument)}`;
Expand Down
13 changes: 13 additions & 0 deletions packages/kind2string/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,19 @@ describe('kind 2 string tests', () => {
let converted = convert(res);
expect(converted).toBe('ascii');
});
describe('logicalExpression', () => {
it('should work', () => {
let file = `class Button extends React.Component<{ or: string }> {
static defaultProps = {
or: 'me' || 'you' || 'someone else' && 'impossible state',
}
}`;

let res = extractReactTypes(file, 'flow').component.members[0].default;
let converted = convert(res);
expect(converted).toBe('"me" || "you" || "someone else" && "impossible state"');
});
});
describe('utilities', () => {
describe('resolveLast', () => {});
});
Expand Down

0 comments on commit 4b3b4a4

Please sign in to comment.