This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 657
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #433 from VictorHom/noDidUpdateSetState
add rule for no setState in componentDidUpdate and tests
- Loading branch information
Showing
7 changed files
with
275 additions
and
1 deletion.
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
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
Binary file not shown.
165 changes: 165 additions & 0 deletions
165
packages/@romejs/js-compiler/lint/rules/react/noDidUpdateSetState.test.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,165 @@ | ||
# `noDidUpdateSetState.test.ts` | ||
|
||
**DO NOT MODIFY**. This file has been autogenerated. Run `rome test packages/@romejs/js-compiler/lint/rules/react/noDidUpdateSetState.test.ts --update-snapshots` to update. | ||
|
||
## `no this.setState in componentDidUpdate` | ||
|
||
### `0` | ||
|
||
``` | ||
unknown:4:12 lint/noDidUpdateSetState ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
✖ Avoid this.setState in componentDidUpdate | ||
2 │ class Hello extends React.Component { | ||
3 │ componentDidUpdate() { | ||
> 4 │ this.setState({ | ||
│ ^^^^^^^^^^^^^ | ||
5 │ name: 'John' | ||
6 │ }); | ||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
✖ Found 1 problem | ||
``` | ||
|
||
### `0: formatted` | ||
|
||
``` | ||
class Hello extends React.Component { | ||
componentDidUpdate() { | ||
this.setState({ | ||
name: "John", | ||
}); | ||
} | ||
} | ||
``` | ||
|
||
### `1` | ||
|
||
``` | ||
unknown:5:12 lint/noDidUpdateSetState ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
✖ Avoid this.setState in componentDidUpdate | ||
3 │ componentDidUpdate() { | ||
4 │ foo(); | ||
> 5 │ this.setState({ | ||
│ ^^^^^^^^^^^^^ | ||
6 │ name: 'John' | ||
7 │ }); | ||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
✖ Found 1 problem | ||
``` | ||
|
||
### `1: formatted` | ||
|
||
``` | ||
class Hello extends React.Component { | ||
componentDidUpdate() { | ||
foo(); | ||
this.setState({ | ||
name: "John", | ||
}); | ||
} | ||
} | ||
``` | ||
|
||
### `2` | ||
|
||
``` | ||
unknown:4:12 lint/noDidUpdateSetState ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
✖ Avoid this.setState in componentDidUpdate | ||
2 │ class Hello extends Component { | ||
3 │ componentDidUpdate() { | ||
> 4 │ this.setState({ | ||
│ ^^^^^^^^^^^^^ | ||
5 │ name: 'John' | ||
6 │ }); | ||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
✖ Found 1 problem | ||
``` | ||
|
||
### `2: formatted` | ||
|
||
``` | ||
class Hello extends Component { | ||
componentDidUpdate() { | ||
this.setState({ | ||
name: "John", | ||
}); | ||
} | ||
} | ||
``` | ||
|
||
### `3` | ||
|
||
``` | ||
unknown:5:12 lint/noDidUpdateSetState ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
✖ Avoid this.setState in componentDidUpdate | ||
3 │ componentDidUpdate() { | ||
4 │ foo(); | ||
> 5 │ this.setState({ | ||
│ ^^^^^^^^^^^^^ | ||
6 │ name: 'John' | ||
7 │ }); | ||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
✖ Found 1 problem | ||
``` | ||
|
||
### `3: formatted` | ||
|
||
``` | ||
class Hello extends Component { | ||
componentDidUpdate() { | ||
foo(); | ||
this.setState({ | ||
name: "John", | ||
}); | ||
} | ||
} | ||
``` | ||
|
||
### `4` | ||
|
||
``` | ||
✔ No known problems! | ||
``` | ||
|
||
### `4: formatted` | ||
|
||
``` | ||
class Hello extends React.Component { | ||
componentDidUpdate() { | ||
if (condition) { | ||
this.setState({ | ||
name: "John", | ||
}); | ||
} | ||
} | ||
} | ||
``` |
72 changes: 72 additions & 0 deletions
72
packages/@romejs/js-compiler/lint/rules/react/noDidUpdateSetState.test.ts
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,72 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {test} from "rome"; | ||
import {testLintMultiple} from "../testHelpers"; | ||
|
||
test( | ||
"no this.setState in componentDidUpdate", | ||
async (t) => { | ||
await testLintMultiple( | ||
t, | ||
[ | ||
// INVALID | ||
` | ||
class Hello extends React.Component { | ||
componentDidUpdate() { | ||
this.setState({ | ||
name: 'John' | ||
}); | ||
} | ||
} | ||
`, | ||
` | ||
class Hello extends React.Component { | ||
componentDidUpdate() { | ||
foo(); | ||
this.setState({ | ||
name: 'John' | ||
}); | ||
} | ||
} | ||
`, | ||
` | ||
class Hello extends Component { | ||
componentDidUpdate() { | ||
this.setState({ | ||
name: 'John' | ||
}); | ||
} | ||
} | ||
`, | ||
` | ||
class Hello extends Component { | ||
componentDidUpdate() { | ||
foo(); | ||
this.setState({ | ||
name: 'John' | ||
}); | ||
} | ||
} | ||
`, | ||
// VALID | ||
` | ||
class Hello extends React.Component { | ||
componentDidUpdate() { | ||
if (condition) { | ||
this.setState({ | ||
name: 'John' | ||
}); | ||
} | ||
} | ||
} | ||
`, | ||
], | ||
{category: "lint/noDidUpdateSetState"}, | ||
); | ||
}, | ||
); |
31 changes: 31 additions & 0 deletions
31
packages/@romejs/js-compiler/lint/rules/react/noDidUpdateSetState.ts
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,31 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {Path, TransformExitResult} from "@romejs/js-compiler"; | ||
import {descriptions} from "@romejs/diagnostics"; | ||
import {doesNodeMatchPattern, isConditional} from "@romejs/js-ast-utils"; | ||
|
||
function inComponentDidUpdate(path: Path): boolean { | ||
const func = path.findAncestry(({node}) => isConditional(node)) !== undefined | ||
return !func && path.findAncestry(({node}) => node.type === "ClassMethod" && node.key.type === "StaticPropertyKey" && node.key.value.type === "Identifier" && node.key.value.name === "componentDidUpdate") !== undefined; | ||
} | ||
|
||
export default { | ||
name: "noDidUpdateSetState", | ||
enter(path: Path): TransformExitResult { | ||
const {node} = path; | ||
|
||
if (doesNodeMatchPattern(node, "this.setState") && inComponentDidUpdate(path)) { | ||
path.context.addNodeDiagnostic( | ||
node, | ||
descriptions.LINT.NO_DID_UPDATE_SET_STATE | ||
); | ||
} | ||
|
||
return node; | ||
}, | ||
}; |