Skip to content

Commit

Permalink
feat: support arrow function type shorthand (#182)
Browse files Browse the repository at this point in the history
* feat: support arrow function type shorthand

* fix: remove arrow param check

* chore: simplify arrow shorthand check, tests
  • Loading branch information
aaron-harvey authored and gajus committed Feb 12, 2019
1 parent 5c61606 commit 58365a0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
22 changes: 17 additions & 5 deletions src/rules/requireParameterType.js
Expand Up @@ -25,20 +25,32 @@ const create = iterateFunctionNodes((context) => {
const excludeParameterMatch = new RegExp(_.get(context, 'options[0].excludeParameterMatch', 'a^'));

return (functionNode) => {
// It is save to ignore FunctionTypeAnnotation nodes in this rule.
if (functionNode.type === 'FunctionTypeAnnotation') {
return;
}

const isArrow = functionNode.type === 'ArrowFunctionExpression';
const isArrowFunctionExpression = functionNode.expression;
const functionAnnotation = isArrow && _.get(functionNode, 'parent.id.typeAnnotation');

if (skipArrows === 'expressionsOnly' && isArrowFunctionExpression || skipArrows === true && isArrow) {
return;
}

_.forEach(functionNode.params, (identifierNode) => {
const parameterName = getParameterName(identifierNode, context);

if (excludeParameterMatch.test(parameterName)) {
return;
}

const typeAnnotation = _.get(identifierNode, 'typeAnnotation') || _.get(identifierNode, 'left.typeAnnotation');
let typeAnnotation;

const isArrow = functionNode.type === 'ArrowFunctionExpression';
const isArrowFunctionExpression = functionNode.expression;
typeAnnotation = _.get(identifierNode, 'typeAnnotation') || _.get(identifierNode, 'left.typeAnnotation');

if (skipArrows === 'expressionsOnly' && isArrowFunctionExpression || skipArrows === true && isArrow) {
return;
if (isArrow && functionAnnotation) {
typeAnnotation = true;
}

if (!typeAnnotation) {
Expand Down
9 changes: 3 additions & 6 deletions src/rules/requireReturnType.js
Expand Up @@ -116,16 +116,13 @@ const create = (context) => {
return;
}

const returnType = functionNode.returnType || isArrow && _.get(functionNode, 'parent.id.typeAnnotation');

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
) {
} else if (!isFunctionReturnUndefined && !isReturnTypeAnnotationUndefined && annotateReturn && !returnType && !shouldFilterNode(functionNode)) {
context.report(functionNode, 'Missing return type annotation.');
}
};
Expand Down
6 changes: 6 additions & 0 deletions tests/rules/assertions/requireParameterType.js
Expand Up @@ -231,12 +231,18 @@ export default {
{
code: '(...foo: string) => {}'
},
{
code: 'const f: Foo = (a, b) => 42;'
},
{
code: '({foo}: {foo: string}) => {}'
},
{
code: '([foo]: Array) => {}'
},
{
code: 'type fn = (a: string, b: number) => number;\nconst f: fn = (a, b) => {}'
},
{
code: '(foo) => {}',
settings: {
Expand Down
6 changes: 6 additions & 0 deletions tests/rules/assertions/requireReturnType.js
Expand Up @@ -549,12 +549,18 @@ export default {
{
code: '(foo): string => {}'
},
{
code: 'const f: Foo = (a, b) => 42;'
},
{
code: '(foo): string => {}',
options: [
'always'
]
},
{
code: 'type fn = (a: string, b: number) => number;\nconst f: fn = (a, b) => { return 42; }'
},
{
code: '(foo) => { return; }'
},
Expand Down

0 comments on commit 58365a0

Please sign in to comment.