mirror of
https://github.com/documentationjs/documentation.git
synced 2026-01-25 14:26:29 +00:00
* style(prettier): Use prettier for code formatting This saves us style issues. Also adds husky and lint-staged for pre-commit testing Refs https://github.com/documentationjs/documentation/issues/709
57 lines
1.2 KiB
JavaScript
57 lines
1.2 KiB
JavaScript
'use strict';
|
|
/* @flow */
|
|
|
|
var findTarget = require('./finders').findTarget,
|
|
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
|
|
*/
|
|
function inferType(comment /*: 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;
|
|
}
|
|
|
|
module.exports = inferType;
|