Skip to content

Commit

Permalink
Merge branch 'master' into fix-interface-delimiters
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus authored Jul 28, 2019
2 parents 8898e9a + 2981c01 commit 8e140f5
Show file tree
Hide file tree
Showing 107 changed files with 4,237 additions and 9,748 deletions.
7 changes: 6 additions & 1 deletion .README/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# eslint-plugin-flowtype

[![GitSpo Mentions](https://gitspo.com/badges/mentions/gajus/eslint-plugin-flowtype?style=flat-square)](https://gitspo.com/mentions/gajus/eslint-plugin-flowtype)
[![NPM version](http://img.shields.io/npm/v/eslint-plugin-flowtype.svg?style=flat-square)](https://www.npmjs.org/package/eslint-plugin-flowtype)
[![Travis build status](http://img.shields.io/travis/gajus/eslint-plugin-flowtype/master.svg?style=flat-square)](https://travis-ci.org/gajus/eslint-plugin-flowtype)
[![js-canonical-style](https://img.shields.io/badge/code%20style-canonical-blue.svg?style=flat-square)](https://github.com/gajus/canonical)
Expand All @@ -21,7 +22,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 @@ -62,6 +63,7 @@ npm install eslint babel-eslint eslint-plugin-flowtype --save-dev
"comma"
],
"flowtype/require-parameter-type": 2,
"flowtype/require-readonly-react-props": 0,
"flowtype/require-return-type": [
2,
"always",
Expand Down Expand Up @@ -154,6 +156,7 @@ When `true`, only checks files with a [`@flow` annotation](http://flowtype.org/d

{"gitdown": "include", "file": "./rules/array-style-complex-type.md"}
{"gitdown": "include", "file": "./rules/array-style-simple-type.md"}
{"gitdown": "include", "file": "./rules/arrow-parens.md"}
{"gitdown": "include", "file": "./rules/boolean-style.md"}
{"gitdown": "include", "file": "./rules/define-flow-type.md"}
{"gitdown": "include", "file": "./rules/delimiter-dangle.md"}
Expand All @@ -171,7 +174,9 @@ When `true`, only checks files with a [`@flow` annotation](http://flowtype.org/d
{"gitdown": "include", "file": "./rules/object-type-delimiter.md"}
{"gitdown": "include", "file": "./rules/require-compound-type-alias.md"}
{"gitdown": "include", "file": "./rules/require-exact-type.md"}
{"gitdown": "include", "file": "./rules/require-inexact-type.md"}
{"gitdown": "include", "file": "./rules/require-parameter-type.md"}
{"gitdown": "include", "file": "./rules/require-readonly-react-props.md"}
{"gitdown": "include", "file": "./rules/require-return-type.md"}
{"gitdown": "include", "file": "./rules/require-types-at-top.md"}
{"gitdown": "include", "file": "./rules/require-valid-file-annotation.md"}
Expand Down
18 changes: 18 additions & 0 deletions .README/rules/arrow-parens.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
### `arrow-parens`

_The `--fix` option on the command line automatically fixes problems reported by this rule._

Enforces the consistent use of parentheses in arrow functions.

This rule has a string option and an object one.

String options are:

- `"always"` (default) requires parens around arguments in all cases.
- `"as-needed"` enforces no braces where they can be omitted.

Object properties for variants of the `"as-needed"` option:

- `"requireForBlockBody": true` modifies the as-needed rule in order to require parens if the function body is in an instructions block (surrounded by braces).

<!-- assertions arrowParens -->
32 changes: 32 additions & 0 deletions .README/rules/require-inexact-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
### `require-inexact-type`

This rule enforces explicit inexact object types.

#### Options

The rule has one string option:

- `"always"` (default): Report all object type definitions that aren't explicit inexact, but ignore exact objects.
- `"never"`: Report all object type definitions that are explicit inexact.

```js
{
"rules": {
"flowtype/require-inexact-type": [
2,
"always"
]
}
}

{
"rules": {
"flowtype/require-inexact-type": [
2,
"never"
]
}
}
```

<!-- assertions requireInexactType -->
82 changes: 82 additions & 0 deletions .README/rules/require-readonly-react-props.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
### `require-readonly-react-props`

This rule validates that React props are marked as $ReadOnly. React props are immutable and modifying them could lead to unexpected results. Marking prop shapes as $ReadOnly avoids these issues.

The rule tries its best to work with both class and functional components. For class components, it does a fuzzy check for one of "Component", "PureComponent", "React.Component" and "React.PureComponent". It doesn't actually infer that those identifiers resolve to a proper `React.Component` object.

For example, this will NOT be checked:

```js
import MyReact from 'react';
class Foo extends MyReact.Component { }
```

As a result, you can safely use other classes without getting warnings from this rule:

```js
class MyClass extends MySuperClass { }
```

React's functional components are hard to detect statically. The way it's done here is by searching for JSX within a function. When present, a function is considered a React component:

```js
// this gets checked
type Props = { };
function MyComponent(props: Props) {
return <p />;
}

// this doesn't get checked since no JSX is present in a function
type Options = { };
function SomeHelper(options: Options) {
// ...
}

// this doesn't get checked since no JSX is present directly in a function
function helper() { return <p /> }
function MyComponent(props: Props) {
return helper();
}
```

The rule only works for locally defined props that are marked with a `$ReadOnly` or using covariant notation. It doesn't work with imported props:

```js
// the rule has no way of knowing whether ImportedProps are read-only
import { type ImportedProps } from './somewhere';
class Foo extends React.Component<ImportedProps> { }


// the rule also checks for covariant properties
type Props = {|
+foo: string
|};
class Bar extends React.Component<Props> { }

// this fails because the object is not fully read-only
type Props = {|
+foo: string,
bar: number,
|};
class Bar extends React.Component<Props> { }

// this fails because spreading makes object mutable (as of Flow 0.98)
// https://github.com/gajus/eslint-plugin-flowtype/pull/400#issuecomment-489813899
type Props = {|
+foo: string,
...bar,
|};
class Bar extends React.Component<Props> { }
```


```js
{
"rules": {
"flowtype/require-readonly-react-props": 2
}
}
```


<!-- assertions requireReadonlyReactProps -->
5 changes: 5 additions & 0 deletions .README/rules/require-valid-file-annotation.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ This rule has an object option:
* `"line"`: Require single line annotations (i.e. `// @flow`).
* `"block"`: Require block annotations (i.e. `/* @flow */`).

* `"strict"` - Enforce a strict flow file annotation.
* `false` (default): strict flow annotation is not required.
* `true`: Require strict flow annotation (i.e. `// @flow strict`).

```js
{
"rules": {
Expand All @@ -34,6 +38,7 @@ This rule has an object option:
2,
"always", {
"annotationStyle": "block"
"strict": true,
}
]
}
Expand Down
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "canonical"
"extends": "canonical",
"rules": {
"unicorn/prevent-abbreviations": 0
}
}
2 changes: 2 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github: gajus
patreon: gajus
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ dist
!.README
!.travis.yml
/package-lock.json
/yack.lock
Loading

0 comments on commit 8e140f5

Please sign in to comment.