eslint-plugin-flowtype
Advanced tools
Comparing version 2.4.1 to 2.6.1
@@ -22,2 +22,4 @@ 'use strict'; | ||
var skipArrows = _lodash2.default.get(context, 'options[0].excludeArrowFunctions'); | ||
return function (functionNode) { | ||
@@ -28,2 +30,9 @@ _lodash2.default.forEach(functionNode.params, function (identifierNode) { | ||
var isArrow = functionNode.type === 'ArrowFunctionExpression'; | ||
var isArrowFunctionExpression = functionNode.expression; | ||
if (skipArrows === 'expressionsOnly' && isArrowFunctionExpression || skipArrows === true && isArrow) { | ||
return; | ||
} | ||
if (!typeAnnotation) { | ||
@@ -30,0 +39,0 @@ context.report(identifierNode, 'Missing "' + parameterName + '" parameter type annotation.'); |
@@ -24,2 +24,3 @@ 'use strict'; | ||
var annotateUndefined = (_lodash2.default.get(context, 'options[1].annotateUndefined') || 'never') === 'always'; | ||
var skipArrows = _lodash2.default.get(context, 'options[1].excludeArrowFunctions') || false; | ||
@@ -52,6 +53,12 @@ var targetNodes = []; | ||
var isArrow = functionNode.type === 'ArrowFunctionExpression'; | ||
var isArrowFunctionExpression = functionNode.expression; | ||
var isFunctionReturnUndefined = !isArrowFunctionExpression && (!targetNode.returnStatementNode || isUndefinedReturnType(targetNode.returnStatementNode)); | ||
var hasImplicitReturnType = functionNode.async || functionNode.generator; | ||
var isFunctionReturnUndefined = !isArrowFunctionExpression && !hasImplicitReturnType && (!targetNode.returnStatementNode || isUndefinedReturnType(targetNode.returnStatementNode)); | ||
var isReturnTypeAnnotationUndefined = getIsReturnTypeAnnotationUndefined(targetNode); | ||
if (skipArrows === 'expressionsOnly' && isArrowFunctionExpression || skipArrows === true && isArrow) { | ||
return; | ||
} | ||
if (isFunctionReturnUndefined && isReturnTypeAnnotationUndefined && !annotateUndefined) { | ||
@@ -58,0 +65,0 @@ context.report(functionNode, 'Must not annotate undefined return type.'); |
@@ -18,2 +18,4 @@ 'use strict'; | ||
var sourceCode = context.getSourceCode(); | ||
return function (functionNode) { | ||
@@ -25,3 +27,4 @@ _lodash2.default.forEach(functionNode.params, function (identifierNode) { | ||
if (typeAnnotation) { | ||
var spaceBefore = typeAnnotation.start - identifierNode.end - (identifierNode.optional ? 1 : 0); | ||
var tokenBeforeType = sourceCode.getTokenBefore(typeAnnotation, identifierNode.optional ? 1 : 0); | ||
var spaceBefore = typeAnnotation.start - tokenBeforeType.end - (identifierNode.optional ? 1 : 0); | ||
@@ -28,0 +31,0 @@ if (always && spaceBefore > 1) { |
{ | ||
"name": "eslint-plugin-flowtype", | ||
"description": "Flowtype linting rules for ESLint.", | ||
"version": "2.4.1", | ||
"version": "2.6.1", | ||
"main": "./dist/index.js", | ||
@@ -6,0 +6,0 @@ "repository": { |
253
README.md
@@ -227,2 +227,32 @@ <h1 id="eslint-plugin-flowtype">eslint-plugin-flowtype</h1> | ||
<h4 id="eslint-plugin-flowtype-rules-require-parameter-type-options">Options</h4> | ||
You can skip all arrow functions by providing the `excludeArrowFunctions` option with `true`. | ||
Alternatively, you can want to exclude only function expressions (e.g. `x => x * 2`). Provide `excludeArrowFunctions` with `expressionsOnly` for this. | ||
```js | ||
{ | ||
"rules": { | ||
"flowtype/require-parameter-type": [ | ||
2, | ||
{ | ||
"excludeArrowFunctions": true | ||
} | ||
] | ||
} | ||
} | ||
{ | ||
"rules": { | ||
"flowtype/require-parameter-type": [ | ||
2, | ||
{ | ||
"excludeArrowFunctions": "expressionsOnly" | ||
} | ||
] | ||
} | ||
} | ||
``` | ||
The following patterns are considered problems: | ||
@@ -234,2 +264,9 @@ | ||
function x(foo) {} | ||
// Message: Missing "foo" parameter type annotation. | ||
// Options: [{"excludeArrowFunctions":true}] | ||
function x(foo) {} | ||
// Message: Missing "foo" parameter type annotation. | ||
(foo = 'FOO') => {} | ||
@@ -253,2 +290,10 @@ // Message: Missing "foo" parameter type annotation. | ||
// Message: Missing "foo" parameter type annotation. | ||
// Options: [{"excludeArrowFunctions":"expressionsOnly"}] | ||
(foo) => {} | ||
// Message: Missing "foo" parameter type annotation. | ||
// Options: [{"excludeArrowFunctions":"expressionsOnly"}] | ||
function x(foo) {} | ||
// Message: Missing "foo" parameter type annotation. | ||
``` | ||
@@ -270,2 +315,8 @@ | ||
(foo) => {} | ||
// Options: [{"excludeArrowFunctions":true}] | ||
(foo) => {} | ||
// Options: [{"excludeArrowFunctions":"expressionsOnly"}] | ||
(foo) => 3 | ||
``` | ||
@@ -279,2 +330,34 @@ | ||
<h4 id="eslint-plugin-flowtype-rules-require-return-type-options">Options</h4> | ||
You can skip all arrow functions by providing the `excludeArrowFunctions` option with `true`. | ||
Alternatively, you can want to exclude only function expressions (e.g. `() => 2`). Provide `excludeArrowFunctions` with `expressionsOnly` for this. | ||
```js | ||
{ | ||
"rules": { | ||
"flowtype/require-return-type": [ | ||
2, | ||
"always", | ||
{ | ||
"excludeArrowFunctions": true | ||
} | ||
] | ||
} | ||
} | ||
{ | ||
"rules": { | ||
"flowtype/require-return-type": [ | ||
2, | ||
"always", | ||
{ | ||
"excludeArrowFunctions": "expressionsOnly" | ||
} | ||
] | ||
} | ||
} | ||
``` | ||
The following patterns are considered problems: | ||
@@ -341,2 +424,30 @@ | ||
// Message: Must annotate undefined return type. | ||
// Options: ["always"] | ||
async () => { return 2; } | ||
// Message: Missing return type annotation. | ||
// Options: ["always",{"annotateUndefined":"always"}] | ||
async () => {} | ||
// Message: Missing return type annotation. | ||
// Options: ["always",{"annotateUndefined":"always"}] | ||
async function x() {} | ||
// Message: Missing return type annotation. | ||
// Options: ["always"] | ||
async () => { return; } | ||
// Message: Missing return type annotation. | ||
// Options: ["always"] | ||
function* x() {} | ||
// Message: Missing return type annotation. | ||
// Options: ["always",{"excludeArrowFunctions":"expressionsOnly"}] | ||
() => { return 3; } | ||
// Message: Missing return type annotation. | ||
// Options: ["always",{"excludeArrowFunctions":"expressionsOnly"}] | ||
async () => { return 4; } | ||
// Message: Missing return type annotation. | ||
``` | ||
@@ -386,2 +497,31 @@ | ||
(foo) => { return undefined; } | ||
// Options: ["always",{"annotateUndefined":"always"}] | ||
async function doThing(): Promise<void> {} | ||
// Options: ["always",{"annotateUndefined":"always"}] | ||
function* doThing(): Generator<number, void, void> { yield 2; } | ||
async (foo): Promise<number> => { return 3; } | ||
// Options: ["always",{"excludeArrowFunctions":true}] | ||
() => 3 | ||
// Options: ["always",{"excludeArrowFunctions":true}] | ||
() => { return 4; } | ||
// Options: ["always",{"excludeArrowFunctions":true}] | ||
() => undefined | ||
// Options: ["always",{"excludeArrowFunctions":true,"annotateUndefined":"always"}] | ||
() => undefined | ||
// Options: ["always",{"excludeArrowFunctions":true,"annotateUndefined":"always"}] | ||
() => { return undefined; } | ||
// Options: ["always",{"excludeArrowFunctions":"expressionsOnly"}] | ||
() => 3 | ||
// Options: ["always",{"excludeArrowFunctions":"expressionsOnly"}] | ||
async () => 3 | ||
``` | ||
@@ -487,2 +627,5 @@ | ||
function foo (foo:string) {} | ||
// Message: There must be a space after "foo" parameter type annotation colon. | ||
// Options: ["always"] | ||
@@ -527,2 +670,17 @@ (foo: string) => {} | ||
// Message: There must be 1 space after return type colon. | ||
async function foo({ lorem, ipsum, dolor }:SomeType) {} | ||
// Message: There must be a space after "{ lorem, ipsum, dolor }:SomeType" parameter type annotation colon. | ||
({ lorem, ipsum, dolor } : SomeType) => {} | ||
// Message: There must be 1 space after "{ lorem, ipsum, dolor }" parameter type annotation colon. | ||
(foo:{ a: string, b: number }) => {} | ||
// Message: There must be a space after "foo" parameter type annotation colon. | ||
({ a, b } :{ a: string, b: number }) => {} | ||
// Message: There must be a space after "{ a, b }" parameter type annotation colon. | ||
([ a, b ] :string[]) => {} | ||
// Message: There must be a space after "[ a, b ]" parameter type annotation colon. | ||
``` | ||
@@ -537,2 +695,6 @@ | ||
function x(foo: string) {} | ||
class Foo { constructor(foo: string) {} } | ||
(foo: (string|number)) => {} | ||
@@ -543,2 +705,8 @@ | ||
// Options: ["never"] | ||
function x(foo:string) {} | ||
// Options: ["never"] | ||
class Foo { constructor(foo:string) {} } | ||
// Options: ["always"] | ||
@@ -582,2 +750,19 @@ (foo: string) => {} | ||
(): ( () => void ) => {} | ||
async function foo({ lorem, ipsum, dolor }: SomeType) {} | ||
({ lorem, ipsum, dolor }: SomeType) => {} | ||
(foo: { a: string, b: number }) => {} | ||
({ a, b }: ?{ a: string, b: number }) => {} | ||
function x({ a, b }: { a: string, b: number }) {} | ||
(): { a: number, b: string } => {} | ||
// Options: ["never"] | ||
() :{ a: number, b: string } => {} | ||
([ a, b ]: string[]) => {} | ||
``` | ||
@@ -619,2 +804,38 @@ | ||
// Message: There must be 1 space before "foo" parameter type annotation colon. | ||
function x(foo : string) {} | ||
// Message: There must be no space before "foo" parameter type annotation colon. | ||
// Options: ["always"] | ||
function x(foo: string) {} | ||
// Message: There must be a space before "foo" parameter type annotation colon. | ||
var x = function (foo : string) {} | ||
// Message: There must be no space before "foo" parameter type annotation colon. | ||
// Options: ["always"] | ||
var x = function (foo: string) {} | ||
// Message: There must be a space before "foo" parameter type annotation colon. | ||
class Foo { constructor(foo : string ) {} } | ||
// Message: There must be no space before "foo" parameter type annotation colon. | ||
// Options: ["always"] | ||
class Foo { constructor(foo: string ) {} } | ||
// Message: There must be a space before "foo" parameter type annotation colon. | ||
async function foo({ lorem, ipsum, dolor } : SomeType) {} | ||
// Message: There must be no space before "{ lorem, ipsum, dolor } : SomeType" parameter type annotation colon. | ||
({ lorem, ipsum, dolor } : SomeType) => {} | ||
// Message: There must be no space before "{ lorem, ipsum, dolor }" parameter type annotation colon. | ||
(foo : { a: string, b: number }) => {} | ||
// Message: There must be no space before "foo" parameter type annotation colon. | ||
({ a, b } : { a: string, b: number }) => {} | ||
// Message: There must be no space before "{ a, b }" parameter type annotation colon. | ||
([ a, b ] : string[]) => {} | ||
// Message: There must be no space before "[ a, b ]" parameter type annotation colon. | ||
``` | ||
@@ -639,2 +860,34 @@ | ||
(foo ?: string) => {} | ||
function x(foo: string) {} | ||
// Options: ["always"] | ||
function x(foo : string) {} | ||
var x = function (foo: string) {} | ||
// Options: ["always"] | ||
var x = function (foo : string) {} | ||
class Foo { constructor(foo: string ) {} } | ||
// Options: ["always"] | ||
class Foo { constructor(foo : string ) {} } | ||
async function foo({ lorem, ipsum, dolor }: SomeType) {} | ||
({ lorem, ipsum, dolor }: SomeType) => {} | ||
(foo: { a: string, b: number }) => {} | ||
({ a, b }: ?{ a: string, b: number }) => {} | ||
function x({ a, b }: { a: string, b: number }) {} | ||
(): { a: number, b: string } => {} | ||
// Options: ["always"] | ||
() : { a: number, b: string } => {} | ||
([ a, b ]: string[]) => {} | ||
``` | ||
@@ -641,0 +894,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
71908
507
980