Skip to content

Commit

Permalink
Merge pull request #187 from gajus/aaron-harvey-feature/require-retur…
Browse files Browse the repository at this point in the history
…n-type-filtering

merge
  • Loading branch information
danharper authored Jan 15, 2017
2 parents 95e9513 + edb2113 commit 3b1c895
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 1 deletion.
37 changes: 37 additions & 0 deletions .README/rules/require-return-type.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,41 @@ Alternatively, you can want to exclude only concise arrow function (e.g. `() =>
}
```

You can exclude or include specific tests with the `includeOnlyMatching` and `excludeMatching` rules.

```js
{
"rules": {
"flowtype/require-return-type": [
2,
"always",
{
"includeOnlyMatching": [
"^F.*",
"Ba(r|z)"
]
}
]
}
}

{
"rules": {
"flowtype/require-return-type": [
2,
"always",
{
"excludeMatching": [
"^F.*",
"Ba(r|z)"
]
}
]
}
}

```

Both rules take an array that can contain either strings or valid RegExp statements.

<!-- assertions requireReturnType -->
28 changes: 27 additions & 1 deletion src/rules/requireReturnType.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ export default (context) => {
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) => {
Expand All @@ -24,6 +31,25 @@ export default (context) => {
return isReturnTypeAnnotationLiteralUndefined || isReturnTypeAnnotationVoid;
};

const shouldFilterNode = (functionNode) => {
const isArrow = functionNode.type === 'ArrowFunctionExpression';
const identiferName = _.get(functionNode, isArrow ? 'parent.id.name' : 'id.name');

const checkRegExp = (regex) => {
return regex.test(identiferName);
};

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();

Expand All @@ -46,7 +72,7 @@ export default (context) => {
} else if (isFunctionReturnUndefined && !isReturnTypeAnnotationUndefined && annotateUndefined) {
context.report(functionNode, 'Must annotate undefined return type.');
} else if (!isFunctionReturnUndefined && !isReturnTypeAnnotationUndefined) {
if (annotateReturn && !functionNode.returnType) {
if (annotateReturn && !functionNode.returnType && !shouldFilterNode(functionNode)) {
context.report(functionNode, 'Missing return type annotation.');
}
}
Expand Down
100 changes: 100 additions & 0 deletions tests/rules/assertions/requireReturnType.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,38 @@ export default {
excludeArrowFunctions: 'expressionsOnly'
}
]
},
{
code: 'function foo() { return 42; }\nfunction bar() { return 42; }',
errors: [
{
message: 'Missing return type annotation.'
}
],
options: [
'always',
{
includeOnlyMatching: [
'bar'
]
}
]
},
{
code: 'const foo = () => { return 42; };\nconst bar = () => { return 42; }',
errors: [
{
message: 'Missing return type annotation.'
}
],
options: [
'always',
{
includeOnlyMatching: [
'bar'
]
}
]
}
],
valid: [
Expand Down Expand Up @@ -474,6 +506,74 @@ export default {
excludeArrowFunctions: 'expressionsOnly'
}
]
},
{
code: 'function foo() { return 42; }',
options: [
'always',
{
excludeMatching: [
'foo'
]
}
]
},
{
code: 'function foo() { return 42; }',
options: [
'always',
{
includeOnlyMatching: [
'bar'
]
}
]
},
{
code: 'function foo(): number { return 42; }\nfunction bar() { return 42; }',
options: [
'always',
{
excludeMatching: [
'bar'
]
}
]
},
{
code: 'function foo(): number { return 42; }\nfunction bar() { return 42; }',
options: [
'always',
{
includeOnlyMatching: [
'foo',
'baz'
]
}
]
},
{
code: 'function foo(): number { return 42; }\nfunction bar() { return 42; }',
options: [
'always',
{
excludeMatching: [
'^b.*',
'qux'
]
}
]
},
{
code: 'function foo(): number { return 42; }\nfunction bar() { return 42; }',
options: [
'always',
{
includeOnlyMatching: [
'^f.*'
]
}
]
}
]
};

0 comments on commit 3b1c895

Please sign in to comment.