-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move eslint-plugin-khan repo into wonder-stuff (#638)
## Summary: This code could use some TLC to update it to use jest and TS, but we can do that later. The important thing is that it's co-located with eslint-config-khan and will be using changeset for publishing instead of having to publish manually. Issue: None ## Test plan: - yarn --cwd packages/eslint-plugin-khan test Author: kevinbarabash Reviewers: github-code-scanning[bot], jeresig Required Reviewers: Approved By: jeresig Checks: ✅ codecov/project, ✅ Test (macos-latest, 16.x), ✅ CodeQL, ✅ Lint, typecheck, and coverage check (ubuntu-latest, 16.x), ✅ gerald, ✅ Prime node_modules cache for primary configuration (ubuntu-latest, 16.x), ✅ Analyze (javascript), ⏭ dependabot Pull Request URL: #638
- Loading branch information
1 parent
d2fbdcc
commit ad92272
Showing
48 changed files
with
4,208 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@khanacademy/eslint-plugin": patch | ||
--- | ||
|
||
Update 'imports-require-flow' rule to support modern parsers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# @khanacademy/eslint-plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2017 Khan Academy | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# eslint-plugin-khan | ||
|
||
[![Build Status](https://travis-ci.org/Khan/eslint-plugin-khan.svg?branch=master)](https://travis-ci.org/Khan/eslint-plugin-khan) | ||
|
||
eslint plugin with our set of custom rules for various things | ||
|
||
## Rules | ||
|
||
- [khan/flow-array-type-style](docs/flow-array-type-style.md) | ||
- [khan/flow-exact-props](docs/flow-exact-props.md) | ||
- [khan/flow-exact-state](docs/flow-exact-state.md) | ||
- [khan/flow-no-one-tuple](docs/flow-no-one-tuple.md) | ||
- [khan/imports-requiring-flow](docs/imports-requiring-flow.md) | ||
- [khan/jest-async-use-real-timers](docs/jest-async-use-real-timers.md) | ||
- [khan/jest-enzyme-matchers](docs/jest-enzyme-matchers.md) | ||
- [khan/react-no-method-jsx-attribute](docs/react-no-method-jsx-attribute.md) | ||
- [khan/react-no-subscriptions-before-mount](docs/react-no-subscriptions-before-mount.md) | ||
- [khan/react-svg-path-precision](docs/react-svg-path-precision.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Prefer generic style array types in flow (flow-array-type-style) | ||
|
||
In flow, array types can be written as `T[]` or `Array<T>`. When the type is | ||
nullable, the former can be consfusing, e.g. | ||
|
||
``` | ||
?T[] = ?Array<T> | ||
(?T)[] = Array<?T> | ||
``` | ||
|
||
This rule prefers the `Array<T>` for array types to avoid the confusion. | ||
|
||
## Rule Details | ||
|
||
The following are considered warnings: | ||
|
||
```js | ||
type foo = number[] | ||
type foo = ?number[] | ||
type foo = (?number)[] | ||
``` | ||
The following are not considered warnings: | ||
```js | ||
type foo = Array<number> | ||
type foo = ?Array<number> | ||
type foo = Array<?number> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Require exact object types for Props (flow-exact-props) | ||
|
||
Using exact object types for Props can help identify extra props and in | ||
the case of optional props, typos. | ||
|
||
This rule supports autofixing. Please note, this may result in new flow | ||
errors. | ||
|
||
## Rule Details | ||
|
||
The following are considered warnings: | ||
|
||
```js | ||
type Props = { x: number }; | ||
class Foo extends React.Component<Props> {} | ||
``` | ||
|
||
```js | ||
type FooProps = { x: number }; | ||
class Foo extends React.Component<FooProps> {} | ||
``` | ||
|
||
```js | ||
type Props = { x: number }; | ||
const Foo = (props: Props) => {} | ||
``` | ||
|
||
The following are not considered warnings: | ||
|
||
```js | ||
type Props = {| x: number |}; | ||
class Foo extends React.Component<Props> {} | ||
``` | ||
|
||
```js | ||
type BarProps = { x: number }; | ||
type FooProps = {| x: number |}; | ||
class Foo extends React.Component<FooProps> {} | ||
``` | ||
|
||
```js | ||
type Props = {| x: number |}; | ||
const Foo = (props: Props) => {} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Require exact object types for State (flow-exact-state) | ||
|
||
Using exact object types for State can help types when there are optional | ||
state properties. | ||
|
||
This rule supports autofixing. Please note, this may result in new flow | ||
errors. | ||
|
||
## Rule Details | ||
|
||
The following are considered warnings: | ||
|
||
```js | ||
type Props = {| x: number |}; | ||
type State = { x: number }; | ||
class Foo extends React.Component<Props, State> {} | ||
``` | ||
|
||
```js | ||
type FooProps = {| x: number |}; | ||
type FooState = { x: number }; | ||
class Foo extends React.Component<FooProps, FooState> {} | ||
``` | ||
|
||
The following are not considered warnings: | ||
|
||
```js | ||
type Props = { x: number }; | ||
type State = {| x: number |}; | ||
class Foo extends React.Component<Props, State> {} | ||
``` | ||
|
||
```js | ||
type FooProps = { x: number }; | ||
type BarState = { x: number }; | ||
type FooState = {| x: number |}; | ||
class Foo extends React.Component<FooProps, FooState> { | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Disallow one-tuples in flow (flow-no-one-tuple) | ||
|
||
A common mistake people make, when starting to use Flow, is to assume that | ||
arrays of a type can be expressed by wrapping the type in brackets (eg. | ||
`[number]`). | ||
|
||
This expression _actually_ refers to a tuple, which is an array that holds a | ||
finite number of items, each with their type specified. The correct expression | ||
would be `Array<number>`. | ||
|
||
To help avoid this mistake, we warn against the uncommon pattern of a | ||
single-value tuple. | ||
|
||
## Rule Details | ||
|
||
The following are considered warnings: | ||
|
||
```js | ||
type foo = [number] | ||
``` | ||
|
||
The following are not considered warnings: | ||
|
||
```js | ||
type foo = Array<number> | ||
type foo = [number, number] | ||
``` | ||
|
||
If you actually need a one-tuple, the rule can be disabled, e.g. | ||
|
||
```js | ||
type foo = [number] // eslint-disable-line flow-no-one-tuple | ||
``` |
95 changes: 95 additions & 0 deletions
95
packages/eslint-plugin-khan/docs/imports-requiring-flow.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# Specify imports that require flow (imports-requiring-flow) | ||
|
||
We'd like to require the use of flow with certain modules so that we can | ||
rely on flow to catch issues when refactoring those modules. This rule | ||
requires that any files importing one of the specified modules be using | ||
flow as indicated by a `// @flow` comment in the file. | ||
|
||
This rule also works with directories, so that any modules under that directory | ||
or its subdirectories are required to be using flow types. | ||
|
||
Notes: | ||
- All paths in `modules` that aren't npm packages (i.e. are directories or files | ||
within your codebase) are considered to be relative to `rootDir`. | ||
- `rootDir` is required and should usually be set to `__dirname`. This | ||
requires the the configuration of `@khanacademy/imports-requiring-flow` to be | ||
done in a `.js` file. | ||
|
||
## Rule Details | ||
|
||
Give the following rule config: | ||
|
||
```js | ||
"@khanacademy/imports-requiring-flow": [ | ||
"error", { | ||
rootDir: __dirname, | ||
modules: ["foo", "src/bar.js"], | ||
}, | ||
] | ||
``` | ||
|
||
The following are considered warnings: | ||
|
||
```js | ||
import foo from "foo"; | ||
``` | ||
|
||
```js | ||
import foo from "foo/bar"; | ||
``` | ||
|
||
```js | ||
import bar from "./bar"; | ||
``` | ||
|
||
```js | ||
const foo = require("foo"); | ||
``` | ||
|
||
```js | ||
const foo = require("foo/bar"); | ||
``` | ||
|
||
```js | ||
const bar = require("./bar.js"); | ||
``` | ||
|
||
The following are not considered warnings: | ||
|
||
```js | ||
// @flow | ||
import foo from "foo"; | ||
``` | ||
|
||
```js | ||
// @flow | ||
import foo from "foo/bar"; | ||
``` | ||
|
||
```js | ||
// @flow | ||
import bar from "./bar"; | ||
``` | ||
|
||
```js | ||
// @flow | ||
const foo = require("foo"); | ||
``` | ||
|
||
```js | ||
// @flow | ||
const foo = require("foo/bar"); | ||
``` | ||
|
||
```js | ||
// @flow | ||
const bar = require("./bar.js"); | ||
``` | ||
|
||
```js | ||
import baz from "./baz.js"; | ||
``` | ||
|
||
```js | ||
const qux = require("qux"); | ||
``` |
56 changes: 56 additions & 0 deletions
56
packages/eslint-plugin-khan/docs/jest-async-use-real-timers.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Require async tests to call jest.useRealTimers() (jest-async-use-real-timers) | ||
|
||
By default jest uses real timers. Some projects opt to use fake timers | ||
as a default in order to speed up test execution. This requires any | ||
async tests to explicitly call `jest.useRealTimers()`. This rule can | ||
be used in this situation to help developers remember to make this call. | ||
|
||
## Rule Details | ||
|
||
The following are considered warnings: | ||
|
||
```js | ||
describe("foo", () => { | ||
it("requires real timers", async () => {}); | ||
}) | ||
``` | ||
|
||
```js | ||
describe("foo", () => { | ||
it("requires real timers", async function() {}); | ||
}) | ||
``` | ||
|
||
The following are not considered warnings: | ||
|
||
```js | ||
describe("foo", () => { | ||
it("doesn't require real timers", () => {}); | ||
}) | ||
``` | ||
|
||
```js | ||
describe("foo", () => { | ||
it("requires real timers", async () => { | ||
jest.useRealTimers(); | ||
}); | ||
}) | ||
``` | ||
|
||
```js | ||
describe("foo", () => { | ||
it("requires real timers", async function() { | ||
jest.useRealTimers(); | ||
}); | ||
}) | ||
``` | ||
|
||
```js | ||
describe("foo", () => { | ||
beforeEach(() => { | ||
jest.useRealTimers(); | ||
}); | ||
|
||
it("requires real timers", async () => {}); | ||
}) | ||
``` |
Oops, something went wrong.