mirror of
https://github.com/documentationjs/documentation.git
synced 2026-01-18 14:17:30 +00:00
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
57 lines
1.3 KiB
JavaScript
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;
|
|
});
|
|
};
|