Skip to content

Commit 3701f6d

Browse files
committed
feat(lint): integrate eslint-plugin-react-hooks with rules-of-hooks and exhaustive-deps as errors
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
1 parent 8d17c78 commit 3701f6d

20 files changed

Lines changed: 191 additions & 116 deletions

File tree

.eslintrc.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module.exports = {
2121
"plugin:react/recommended",
2222
"plugin:prettier/recommended",
2323
],
24-
plugins: ["react", "import", "jest", "prettier"],
24+
plugins: ["react", "react-hooks", "import", "jest", "prettier"],
2525
settings: {
2626
react: {
2727
pragma: "React",
@@ -74,5 +74,7 @@ module.exports = {
7474
"react/display-name": 0,
7575
"import/no-extraneous-dependencies": 2,
7676
"react/jsx-filename-extension": 2,
77+
"react-hooks/rules-of-hooks": 2,
78+
"react-hooks/exhaustive-deps": 2,
7779
},
7880
}

flavors/swagger-ui-react/index.jsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ const SwaggerUI = ({
9393
})
9494

9595
setSystem(systemInstance)
96+
// eslint-disable-next-line react-hooks/exhaustive-deps -- intentionally mount-only: creates the initial SwaggerUI system instance once; prop changes after mount are handled by the dedicated effects below
9697
}, [])
9798

9899
useEffect(() => {
@@ -106,6 +107,7 @@ const SwaggerUI = ({
106107
}
107108
}
108109
}
110+
// eslint-disable-next-line react-hooks/exhaustive-deps -- prevUrl is intentionally excluded: it is a usePrevious ref value and adding it would cause the effect to double-fire when the URL changes
109111
}, [system, url])
110112

111113
useEffect(() => {
@@ -121,6 +123,7 @@ const SwaggerUI = ({
121123
system.specActions.updateSpec(updatedSpec)
122124
}
123125
}
126+
// eslint-disable-next-line react-hooks/exhaustive-deps -- prevSpec is intentionally excluded: it is a usePrevious ref value and adding it would cause the effect to double-fire when the spec changes
124127
}, [system, spec])
125128

126129
return SwaggerUIComponent ? <SwaggerUIComponent /> : null

package-lock.json

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@
147147
"eslint-plugin-jest": "^28.12.0",
148148
"eslint-plugin-prettier": "^5.2.3",
149149
"eslint-plugin-react": "^7.37.4",
150+
"eslint-plugin-react-hooks": "=7.0.1",
150151
"esm": "=3.2.25",
151152
"expect": "=29.7.0",
152153
"express": "^4.22.1",

src/core/plugins/json-schema-2020-12/components/JSONSchema/JSONSchema.jsx

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ const JSONSchema = forwardRef(
4545
const renderedSchemas = useRenderedSchemas(schema)
4646
const constraints = fn.stringifyConstraints(schema)
4747
const Accordion = useComponent("Accordion")
48-
const Keyword$schema = useComponent("Keyword$schema")
49-
const Keyword$vocabulary = useComponent("Keyword$vocabulary")
50-
const Keyword$id = useComponent("Keyword$id")
51-
const Keyword$anchor = useComponent("Keyword$anchor")
52-
const Keyword$dynamicAnchor = useComponent("Keyword$dynamicAnchor")
53-
const Keyword$ref = useComponent("Keyword$ref")
54-
const Keyword$dynamicRef = useComponent("Keyword$dynamicRef")
55-
const Keyword$defs = useComponent("Keyword$defs")
56-
const Keyword$comment = useComponent("Keyword$comment")
48+
const KeywordSchema = useComponent("Keyword$schema")
49+
const KeywordVocabulary = useComponent("Keyword$vocabulary")
50+
const KeywordId = useComponent("Keyword$id")
51+
const KeywordAnchor = useComponent("Keyword$anchor")
52+
const KeywordDynamicAnchor = useComponent("Keyword$dynamicAnchor")
53+
const KeywordRef = useComponent("Keyword$ref")
54+
const KeywordDynamicRef = useComponent("Keyword$dynamicRef")
55+
const KeywordDefs = useComponent("Keyword$defs")
56+
const KeywordComment = useComponent("Keyword$comment")
5757
const KeywordAllOf = useComponent("KeywordAllOf")
5858
const KeywordAnyOf = useComponent("KeywordAnyOf")
5959
const KeywordOneOf = useComponent("KeywordOneOf")
@@ -193,17 +193,17 @@ const JSONSchema = forwardRef(
193193
/>
194194
<KeywordDefault schema={schema} />
195195
<KeywordExamples schema={schema} />
196-
<Keyword$schema schema={schema} />
197-
<Keyword$vocabulary schema={schema} />
198-
<Keyword$id schema={schema} />
199-
<Keyword$anchor schema={schema} />
200-
<Keyword$dynamicAnchor schema={schema} />
201-
<Keyword$ref schema={schema} />
196+
<KeywordSchema schema={schema} />
197+
<KeywordVocabulary schema={schema} />
198+
<KeywordId schema={schema} />
199+
<KeywordAnchor schema={schema} />
200+
<KeywordDynamicAnchor schema={schema} />
201+
<KeywordRef schema={schema} />
202202
{!isCircular && isExpandable && (
203-
<Keyword$defs schema={schema} />
203+
<KeywordDefs schema={schema} />
204204
)}
205-
<Keyword$dynamicRef schema={schema} />
206-
<Keyword$comment schema={schema} />
205+
<KeywordDynamicRef schema={schema} />
206+
<KeywordComment schema={schema} />
207207
<ExtensionKeywords schema={schema} />
208208
</>
209209
)}

src/core/plugins/json-schema-2020-12/components/keywords/$anchor.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import React from "react"
55

66
import { schema } from "../../prop-types"
77

8-
const $anchor = ({ schema }) => {
8+
const Anchor = ({ schema }) => {
99
if (!schema?.$anchor) return null
1010

1111
return (
@@ -20,8 +20,8 @@ const $anchor = ({ schema }) => {
2020
)
2121
}
2222

23-
$anchor.propTypes = {
23+
Anchor.propTypes = {
2424
schema: schema.isRequired,
2525
}
2626

27-
export default $anchor
27+
export default Anchor

src/core/plugins/json-schema-2020-12/components/keywords/$comment.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import React from "react"
55

66
import { schema } from "../../prop-types"
77

8-
const $comment = ({ schema }) => {
8+
const Comment = ({ schema }) => {
99
if (!schema?.$comment) return null
1010

1111
return (
@@ -20,8 +20,8 @@ const $comment = ({ schema }) => {
2020
)
2121
}
2222

23-
$comment.propTypes = {
23+
Comment.propTypes = {
2424
schema: schema.isRequired,
2525
}
2626

27-
export default $comment
27+
export default Comment

src/core/plugins/json-schema-2020-12/components/keywords/$defs.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { schema } from "../../prop-types"
88
import { useComponent, useIsExpanded, usePath, useLevel } from "../../hooks"
99
import { JSONSchemaLevelContext, JSONSchemaPathContext } from "../../context"
1010

11-
const $defs = ({ schema }) => {
11+
const Defs = ({ schema }) => {
1212
const $defs = schema?.$defs || {}
1313
const pathToken = "$defs"
1414
const { path } = usePath(pathToken)
@@ -86,8 +86,8 @@ const $defs = ({ schema }) => {
8686
)
8787
}
8888

89-
$defs.propTypes = {
89+
Defs.propTypes = {
9090
schema: schema.isRequired,
9191
}
9292

93-
export default $defs
93+
export default Defs

src/core/plugins/json-schema-2020-12/components/keywords/$dynamicAnchor.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import React from "react"
55

66
import { schema } from "../../prop-types"
77

8-
const $dynamicAnchor = ({ schema }) => {
8+
const DynamicAnchor = ({ schema }) => {
99
if (!schema?.$dynamicAnchor) return null
1010

1111
return (
@@ -20,8 +20,8 @@ const $dynamicAnchor = ({ schema }) => {
2020
)
2121
}
2222

23-
$dynamicAnchor.propTypes = {
23+
DynamicAnchor.propTypes = {
2424
schema: schema.isRequired,
2525
}
2626

27-
export default $dynamicAnchor
27+
export default DynamicAnchor

src/core/plugins/json-schema-2020-12/components/keywords/$dynamicRef.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import React from "react"
55

66
import { schema } from "../../prop-types"
77

8-
const $dynamicRef = ({ schema }) => {
8+
const DynamicRef = ({ schema }) => {
99
if (!schema?.$dynamicRef) return null
1010

1111
return (
@@ -20,8 +20,8 @@ const $dynamicRef = ({ schema }) => {
2020
)
2121
}
2222

23-
$dynamicRef.propTypes = {
23+
DynamicRef.propTypes = {
2424
schema: schema.isRequired,
2525
}
2626

27-
export default $dynamicRef
27+
export default DynamicRef

0 commit comments

Comments
 (0)