Erik Arvidsson 71922c7c8a Handle assignments in document exported (#544)
This improves detection of cases where we have:

```js
export const f = function () {};
export const o = { ... };
```

and similarly indirection using default/named.

```js
const f = function () {};
export {f};
```

Fixes #543
2016-09-19 12:53:18 -04:00

57 lines
1.3 KiB
JavaScript

'use strict';
var findTarget = require('./finders').findTarget,
shouldSkipInference = require('./should_skip_inference'),
flowDoctrine = require('../flow_doctrine'),
t = require('babel-types');
var constTypeMapping = {
'BooleanLiteral': {type: 'BooleanTypeAnnotation'},
'NumericLiteral': {type: 'NumberTypeAnnotation'},
'StringLiteral': {type: 'StringTypeAnnotation'}
};
/**
* Infers type tags by using Flow type annotations
*
* @name inferType
* @param {Object} comment parsed comment
* @returns {Object} comment with type tag inferred
*/
module.exports = function () {
return shouldSkipInference(function inferType(comment) {
if (comment.type) {
return comment;
}
var path = findTarget(comment.context.ast);
if (!path) {
return comment;
}
var n = path.node;
var type;
switch (n.type) {
case 'VariableDeclarator':
type = n.id.typeAnnotation;
if (!type && comment.kind === 'constant') {
type = constTypeMapping[n.init.type];
}
break;
case 'ClassProperty':
type = n.typeAnnotation;
break;
case 'TypeAlias':
type = n.right;
break;
}
if (type) {
if (t.isTypeAnnotation(type)) {
type = type.typeAnnotation;
}
comment.type = flowDoctrine(type);
}
return comment;
});
};