Skip to content

Commit

Permalink
Add support for null and void types (#519)
Browse files Browse the repository at this point in the history
This adds support for `void` and `null` in type annotations:

```js
function f(): void {}
function g(): null {
  return null;
}
```

Towards #512
  • Loading branch information
arv authored and tmcw committed Sep 1, 2016
1 parent 6b981e0 commit d2b93ae
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 41 deletions.
12 changes: 6 additions & 6 deletions lib/flow_doctrine.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ var namedTypes = {
};

var oneToOne = {
'AnyTypeAnnotation': {
type: 'AllLiteral'
}
'AnyTypeAnnotation': 'AllLiteral',
'NullLiteralTypeAnnotation': 'NullLiteral',
'VoidTypeAnnotation': 'VoidLiteral'
};

var literalTypes = {
'StringLiteralTypeAnnotation': 'StringLiteral',
'BooleanLiteralTypeAnnotation': 'BooleanLiteral',
'NumericLiteralTypeAnnotation': 'NumberLiteral',
'BooleanLiteralTypeAnnotation': 'BooleanLiteral'
'StringLiteralTypeAnnotation': 'StringLiteral'
};

/**
Expand All @@ -37,7 +37,7 @@ function flowDoctrine(type) {
}

if (type.type in oneToOne) {
return oneToOne[type.type];
return {type: oneToOne[type.type]};
}

switch (type.type) {
Expand Down
76 changes: 41 additions & 35 deletions test/lib/flow_doctrine.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ function toDoctrineType(flowType) {
).context.ast.node.params[0].typeAnnotation.typeAnnotation);
}

/* eslint-disable */
test('flowDoctrine', function (t) {

t.deepEqual(toDoctrineType('number'),
Expand Down Expand Up @@ -99,38 +98,37 @@ test('flowDoctrine', function (t) {
}]
}, 'number[]');

t.deepEqual(toDoctrineType('[]'),
{
type: 'ArrayType',
elements: []
}, '[]');

t.deepEqual(toDoctrineType('[number]'),
{
type: 'ArrayType',
elements: [
{
type: 'NameExpression',
name: 'number'
}
]
}, '[number]');

t.deepEqual(toDoctrineType('[string, boolean]'),
{
type: 'ArrayType',
elements: [
{
type: 'NameExpression',
name: 'string'
},
{
type: 'NameExpression',
name: 'boolean'
}
]
}, '[string, boolean]');
t.deepEqual(toDoctrineType('[]'),
{
type: 'ArrayType',
elements: []
}, '[]');

t.deepEqual(toDoctrineType('[number]'),
{
type: 'ArrayType',
elements: [
{
type: 'NameExpression',
name: 'number'
}
]
}, '[number]');

t.deepEqual(toDoctrineType('[string, boolean]'),
{
type: 'ArrayType',
elements: [
{
type: 'NameExpression',
name: 'string'
},
{
type: 'NameExpression',
name: 'boolean'
}
]
}, '[string, boolean]');

t.deepEqual(toDoctrineType('boolean'),
{
Expand Down Expand Up @@ -162,13 +160,21 @@ test('flowDoctrine', function (t) {
name: true
}, 'BooleanLiteral');

t.deepEqual(toDoctrineType('true'),
t.deepEqual(toDoctrineType('false'),
{
type: 'BooleanLiteral',
name: true
name: false
}, 'BooleanLiteral');

t.deepEqual(toDoctrineType('null'),
{
type: 'NullLiteral',
}, 'NullLiteral');

t.deepEqual(toDoctrineType('void'),
{
type: 'VoidLiteral',
}, 'VoidLiteral');

t.end();
});
/* eslint-enable */

0 comments on commit d2b93ae

Please sign in to comment.