/
requireReturnType.js
160 lines (138 loc) · 5.42 KB
/
requireReturnType.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import _ from 'lodash';
const schema = [
{
enum: ['always'],
type: 'string'
},
{
additionalProperties: false,
properties: {
annotateUndefined: {
enum: ['always', 'never'],
type: 'string'
},
excludeArrowFunctions: {
enum: [false, true, 'expressionsOnly']
},
excludeMatching: {
items: {
type: 'string'
},
type: 'array'
},
includeOnlyMatching: {
items: {
type: 'string'
},
type: 'array'
}
},
type: 'object'
}
];
const create = (context) => {
const annotateReturn = (_.get(context, 'options[0]') || 'always') === 'always';
const annotateUndefined = (_.get(context, 'options[1].annotateUndefined') || 'never') === 'always';
const skipArrows = _.get(context, 'options[1].excludeArrowFunctions') || false;
const makeRegExp = (str) => {
return new RegExp(str);
};
const excludeMatching = _.get(context, 'options[1].excludeMatching', []).map(makeRegExp);
const includeOnlyMatching = _.get(context, 'options[1].includeOnlyMatching', []).map(makeRegExp);
const targetNodes = [];
const registerFunction = (functionNode) => {
targetNodes.push({
functionNode
});
};
const isUndefinedReturnType = (returnNode) => {
return returnNode.argument === null || returnNode.argument.name === 'undefined' || returnNode.argument.operator === 'void';
};
const getIsReturnTypeAnnotationUndefined = (targetNode) => {
const isReturnTypeAnnotationLiteralUndefined = _.get(targetNode, 'functionNode.returnType.typeAnnotation.id.name') === 'undefined' && _.get(targetNode, 'functionNode.returnType.typeAnnotation.type') === 'GenericTypeAnnotation';
const isReturnTypeAnnotationVoid = _.get(targetNode, 'functionNode.returnType.typeAnnotation.type') === 'VoidTypeAnnotation';
const isAsyncReturnTypeAnnotationVoid = _.get(targetNode, 'functionNode.async') &&
_.get(targetNode, 'functionNode.returnType.typeAnnotation.id.name') === 'Promise' && (
_.get(targetNode, 'functionNode.returnType.typeAnnotation.typeParameters.params[0].type') === 'VoidTypeAnnotation' ||
_.get(targetNode, 'functionNode.returnType.typeAnnotation.typeParameters.params[0].id.name') === 'undefined' &&
_.get(targetNode, 'functionNode.returnType.typeAnnotation.typeParameters.params[0].type') === 'GenericTypeAnnotation'
);
return isReturnTypeAnnotationLiteralUndefined || isReturnTypeAnnotationVoid || isAsyncReturnTypeAnnotationVoid;
};
const shouldFilterNode = (functionNode) => {
const isArrow = functionNode.type === 'ArrowFunctionExpression';
const isMethod = functionNode.parent && functionNode.parent.type === 'MethodDefinition';
const isProperty = functionNode.parent && functionNode.parent.type === 'ClassProperty';
let selector;
if (isMethod || isProperty) {
selector = 'parent.key.name';
} else if (isArrow) {
selector = 'parent.id.name';
} else {
selector = 'id.name';
}
const identifierName = _.get(functionNode, selector);
const checkRegExp = (regex) => {
return regex.test(identifierName);
};
if (excludeMatching.length && _.some(excludeMatching, checkRegExp)) {
return true;
}
if (includeOnlyMatching.length && !_.some(includeOnlyMatching, checkRegExp)) {
return true;
}
return false;
};
const evaluateFunction = (functionNode) => {
const targetNode = targetNodes.pop();
if (functionNode !== targetNode.functionNode) {
throw new Error('Mismatch.');
}
const isArrow = functionNode.type === 'ArrowFunctionExpression';
const isArrowFunctionExpression = functionNode.expression;
const isFunctionReturnUndefined = !isArrowFunctionExpression && !(functionNode.generator && !functionNode.async) && (!targetNode.returnStatementNode || isUndefinedReturnType(targetNode.returnStatementNode));
const isReturnTypeAnnotationUndefined = getIsReturnTypeAnnotationUndefined(targetNode);
if (skipArrows === 'expressionsOnly' && isArrowFunctionExpression || skipArrows === true && isArrow) {
return;
}
if (shouldFilterNode(functionNode)) {
return;
}
if (isFunctionReturnUndefined && isReturnTypeAnnotationUndefined && !annotateUndefined) {
context.report(functionNode, 'Must not annotate undefined return type.');
} else if (isFunctionReturnUndefined && !isReturnTypeAnnotationUndefined && annotateUndefined) {
context.report(functionNode, 'Must annotate undefined return type.');
} else if (
!isFunctionReturnUndefined &&
!isReturnTypeAnnotationUndefined &&
annotateReturn &&
!functionNode.returnType
) {
context.report(functionNode, 'Missing return type annotation.');
}
};
const evaluateNoise = () => {
targetNodes.pop();
};
return {
ArrowFunctionExpression: registerFunction,
'ArrowFunctionExpression:exit': evaluateFunction,
ClassDeclaration: registerFunction,
'ClassDeclaration:exit': evaluateNoise,
ClassExpression: registerFunction,
'ClassExpression:exit': evaluateNoise,
FunctionDeclaration: registerFunction,
'FunctionDeclaration:exit': evaluateFunction,
FunctionExpression: registerFunction,
'FunctionExpression:exit': evaluateFunction,
ReturnStatement: (node) => {
if (targetNodes.length) {
targetNodes[targetNodes.length - 1].returnStatementNode = node;
}
}
};
};
export default {
create,
schema
};