Skip to content

Commit

Permalink
feat: add annotateUndefined: 'ignore' option (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards1211 authored and gajus committed May 2, 2019
1 parent 6bfb378 commit 714a995
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 24 deletions.
35 changes: 19 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ npm install eslint --save-dev
npm install babel-eslint --save-dev
npm install eslint-plugin-flowtype --save-dev

# Or all at once:
# Or all at once:
npm install eslint babel-eslint eslint-plugin-flowtype --save-dev
```

Expand Down Expand Up @@ -2500,6 +2500,12 @@ const f: fn = (a, b) => { return 42; }
// Options: ["always",{"annotateUndefined":"never"}]
(foo) => { return; }

// Options: ["always",{"annotateUndefined":"ignore"}]
(foo): void => { return; }

// Options: ["always",{"annotateUndefined":"ignore"}]
(foo) => { return; }

// Options: ["always",{"annotateUndefined":"never"}]
(foo) => { return undefined; }

Expand Down Expand Up @@ -3053,7 +3059,7 @@ type FooType = { a: number, c: number, b: string }
c: number,
b: string,
}
// Message: Expected type annotations to be in ascending order. "b" should be before "c".
Expand All @@ -3062,7 +3068,7 @@ type FooType = { a: number, c: number, b: string }
c: number,
b: string,
}
// Message: Expected type annotations to be in ascending order. "b" should be before "c".
Expand All @@ -3071,7 +3077,7 @@ type FooType = { a: number, c: number, b: string }
c: number,
b: string,
}
// Message: Expected type annotations to be in ascending order. "b" should be before "c".
Expand All @@ -3080,7 +3086,7 @@ type FooType = { a: number, c: number, b: string }
c: ?number,
b: string,
}
// Message: Expected type annotations to be in ascending order. "b" should be before "c".
Expand All @@ -3089,7 +3095,7 @@ type FooType = { a: number, c: number, b: string }
c: number,
b: (param: string) => number,
}
// Message: Expected type annotations to be in ascending order. "b" should be before "c".
Expand All @@ -3098,7 +3104,7 @@ type FooType = { a: number, c: number, b: string }
c: number,
b: (param: string) => number,
}
// Message: Expected type annotations to be in ascending order. "b" should be before "c".
Expand All @@ -3107,7 +3113,7 @@ type FooType = { a: number, c: number, b: string }
a: number | string | boolean,
b: (param: string) => number,
}
// Message: Expected type annotations to be in ascending order. "a" should be before "c".
Expand All @@ -3120,7 +3126,7 @@ type FooType = { a: number, c: number, b: string }
a: number | string | boolean,
b: (param: string) => number,
}
// Message: Expected type annotations to be in ascending order. "x" should be before "z".
// Message: Expected type annotations to be in ascending order. "a" should be before "c".
Expand All @@ -3138,7 +3144,7 @@ type FooType = { a: number, c: number, b: string }
a: number | string | boolean,
b: (param: string) => number,
}
// Message: Expected type annotations to be in ascending order. "k" should be before "l".
// Message: Expected type annotations to be in ascending order. "x" should be before "z".
// Message: Expected type annotations to be in ascending order. "a" should be before "c".
Expand All @@ -3149,7 +3155,7 @@ type FooType = { a: number, c: number, b: string }
-b: number,
a: number,
}
// Message: Expected type annotations to be in ascending order. "b" should be before "c".
// Message: Expected type annotations to be in ascending order. "a" should be before "b".
Expand All @@ -3159,7 +3165,7 @@ type FooType = { a: number, c: number, b: string }
-b: number,
a: number,
|}
// Message: Expected type annotations to be in ascending order. "b" should be before "c".
// Message: Expected type annotations to be in ascending order. "a" should be before "b".
```
Expand Down Expand Up @@ -3284,7 +3290,7 @@ The following patterns are considered problems:
{ a: string, b: number }) => {}
// Message: There must not be a line break after "foo" parameter type annotation colon.
(foo:
(foo:
{ a: string, b: number }) => {}
// Message: There must not be a line break after "foo" parameter type annotation colon.
Expand Down Expand Up @@ -4889,6 +4895,3 @@ function x(foo: string = "1") {}
function x(foo: Type = bar()) {}
```
8 changes: 4 additions & 4 deletions src/rules/requireReturnType.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const schema = [
additionalProperties: false,
properties: {
annotateUndefined: {
enum: ['always', 'never'],
enum: ['always', 'never', 'ignore'],
type: 'string'
},
excludeArrowFunctions: {
Expand All @@ -34,7 +34,7 @@ const schema = [

const create = (context) => {
const annotateReturn = (_.get(context, 'options[0]') || 'always') === 'always';
const annotateUndefined = (_.get(context, 'options[1].annotateUndefined') || 'never') === 'always';
const annotateUndefined = _.get(context, 'options[1].annotateUndefined') || 'never';
const skipArrows = _.get(context, 'options[1].excludeArrowFunctions') || false;

const makeRegExp = (str) => {
Expand Down Expand Up @@ -119,9 +119,9 @@ const create = (context) => {

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

if (isFunctionReturnUndefined && isReturnTypeAnnotationUndefined && !annotateUndefined) {
if (isFunctionReturnUndefined && isReturnTypeAnnotationUndefined && annotateUndefined === 'never') {
context.report(functionNode, 'Must not annotate undefined return type.');
} else if (isFunctionReturnUndefined && !isReturnTypeAnnotationUndefined && annotateUndefined) {
} else if (isFunctionReturnUndefined && !isReturnTypeAnnotationUndefined && annotateUndefined === 'always') {
context.report(functionNode, 'Must annotate undefined return type.');
} else if (!isFunctionReturnUndefined && !isReturnTypeAnnotationUndefined && annotateReturn && !returnType && !shouldFilterNode(functionNode)) {
context.report(functionNode, 'Missing return type annotation.');
Expand Down
30 changes: 26 additions & 4 deletions tests/rules/assertions/requireReturnType.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,8 @@ export default {
annotateUndefined: {
enum: [
'always',
'never'
'never',
'ignore'
],
type: 'string'
},
Expand Down Expand Up @@ -513,19 +514,22 @@ export default {
params: {
allowedValues: [
'always',
'never'
'never',
'ignore'
]
},
parentSchema: {
enum: [
'always',
'never'
'never',
'ignore'
],
type: 'string'
},
schema: [
'always',
'never'
'never',
'ignore'
],
schemaPath: '#/items/1/properties/annotateUndefined/enum'
}
Expand Down Expand Up @@ -733,6 +737,24 @@ export default {
}
]
},
{
code: 'async function doThing(): Promise<void> {}',
options: [
'always',
{
annotateUndefined: 'ignore'
}
]
},
{
code: 'async function doThing() {}',
options: [
'always',
{
annotateUndefined: 'ignore'
}
]
},
{
code: 'function* doThing(): Generator<number, void, void> { yield 2; }',
options: [
Expand Down

0 comments on commit 714a995

Please sign in to comment.