-
-
Notifications
You must be signed in to change notification settings - Fork 32
Fix no-leaked-event-listener false positive when using React NativeBackHandler, closes #1323
#1336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,26 @@ | ||
| [@eslint-react/core](../README.md) / isInitializedFromReact | ||
|
|
||
| # Function: isInitializedFromReact() | ||
|
|
||
| ```ts | ||
| function isInitializedFromReact( | ||
| name: string, | ||
| importSource: string, | ||
| initialScope: Scope): boolean; | ||
| ``` | ||
|
|
||
| Check if an identifier name is initialized from react | ||
|
|
||
| ## Parameters | ||
|
|
||
| | Parameter | Type | Description | | ||
| | ------ | ------ | ------ | | ||
| | `name` | `string` | The top-level identifier's name | | ||
| | `importSource` | `string` | The import source to check against | | ||
| | `initialScope` | `Scope` | Initial scope to search for the identifier | | ||
|
|
||
| ## Returns | ||
|
|
||
| `boolean` | ||
|
|
||
| Whether the identifier name is initialized from react |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| export * from "./get-instance-id"; | ||
| export * from "./is-from-react"; | ||
| export * from "./is-from-source"; | ||
| export * from "./is-instance-id-equal"; | ||
| export * from "./is-react-api"; |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,61 +1,18 @@ | ||
| import * as AST from "@eslint-react/ast"; | ||
| import { identity } from "@eslint-react/eff"; | ||
| import { findVariable } from "@eslint-react/var"; | ||
| import type { Scope } from "@typescript-eslint/scope-manager"; | ||
| import type { TSESTree } from "@typescript-eslint/types"; | ||
| import { AST_NODE_TYPES as T } from "@typescript-eslint/types"; | ||
| import { P, match } from "ts-pattern"; | ||
|
|
||
| /** | ||
| * Get the arguments of a require expression | ||
| * @param node The node to match | ||
| * @returns The require expression arguments or undefined if the node is not a require expression | ||
| */ | ||
| function getRequireExpressionArguments(node: TSESTree.Node) { | ||
| return match<typeof node, TSESTree.CallExpressionArgument[] | null>(node) | ||
| // require("source") | ||
| .with({ type: T.CallExpression, arguments: P.select(), callee: { type: T.Identifier, name: "require" } }, identity) | ||
| // require("source").variable | ||
| .with({ type: T.MemberExpression, object: P.select() }, getRequireExpressionArguments) | ||
| .otherwise(() => null); | ||
| } | ||
| import { isInitializedFromSource } from "./is-from-source"; | ||
|
|
||
| /** | ||
| * Check if an identifier name is initialized from react | ||
| * @param name The top-level identifier's name | ||
| * @param importSource The import source to check against | ||
| * @param initialScope Initial scope to search for the identifier | ||
| * @returns Whether the identifier name is initialized from react | ||
| * @internal | ||
| */ | ||
| export function isInitializedFromReact( | ||
| name: string, | ||
| importSource: string, | ||
| initialScope: Scope, | ||
| ): boolean { | ||
| if (name.toLowerCase() === "react") return true; | ||
| const latestDef = findVariable(name, initialScope)?.defs.at(-1); | ||
| if (latestDef == null) return false; | ||
| const { node, parent } = latestDef; | ||
| if (node.type === T.VariableDeclarator && node.init != null) { | ||
| const { init } = node; | ||
| // check for: `variable = React.variable` | ||
| if (init.type === T.MemberExpression && init.object.type === T.Identifier) { | ||
| return isInitializedFromReact(init.object.name, importSource, initialScope); | ||
| } | ||
| // check for: `{ variable } = React` | ||
| if (init.type === T.Identifier) { | ||
| return isInitializedFromReact(init.name, importSource, initialScope); | ||
| } | ||
| // check for: `variable = require('react')` or `variable = require('react').variable` | ||
| const args = getRequireExpressionArguments(init); | ||
| const arg0 = args?.[0]; | ||
| if (arg0 == null || !AST.isLiteral(arg0, "string")) { | ||
| return false; | ||
| } | ||
| // check for: `require('react')` or `require('react/...')` | ||
| return arg0.value === importSource || arg0.value.startsWith(`${importSource}/`); | ||
| } | ||
| // latest definition is an import declaration: import { variable } from 'react' | ||
| return parent?.type === T.ImportDeclaration && parent.source.value === importSource; | ||
| ) { | ||
| return name.toLowerCase() === "react" || isInitializedFromSource(name, importSource, initialScope); | ||
| } |
This file contains hidden or 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,60 @@ | ||
| import * as AST from "@eslint-react/ast"; | ||
| import { identity } from "@eslint-react/eff"; | ||
| import { findVariable } from "@eslint-react/var"; | ||
| import type { Scope } from "@typescript-eslint/scope-manager"; | ||
| import type { TSESTree } from "@typescript-eslint/types"; | ||
| import { AST_NODE_TYPES as T } from "@typescript-eslint/types"; | ||
| import { P, match } from "ts-pattern"; | ||
|
|
||
| /** | ||
| * Get the arguments of a require expression | ||
| * @param node The node to match | ||
| * @returns The require expression arguments or undefined if the node is not a require expression | ||
| */ | ||
| function getRequireExpressionArguments(node: TSESTree.Node) { | ||
| return match<typeof node, TSESTree.CallExpressionArgument[] | null>(node) | ||
| // require("source") | ||
| .with({ type: T.CallExpression, arguments: P.select(), callee: { type: T.Identifier, name: "require" } }, identity) | ||
| // require("source").variable | ||
| .with({ type: T.MemberExpression, object: P.select() }, getRequireExpressionArguments) | ||
| .otherwise(() => null); | ||
| } | ||
|
|
||
| /** | ||
| * Check if an identifier name is initialized from source | ||
| * @param name The top-level identifier's name | ||
| * @param source The import source to check against | ||
| * @param initialScope Initial scope to search for the identifier | ||
| * @returns Whether the identifier name is initialized from source | ||
| * @internal | ||
| */ | ||
| export function isInitializedFromSource( | ||
| name: string, | ||
| source: string, | ||
| initialScope: Scope, | ||
| ) { | ||
| const latestDef = findVariable(name, initialScope)?.defs.at(-1); | ||
| if (latestDef == null) return false; | ||
| const { node, parent } = latestDef; | ||
| if (node.type === T.VariableDeclarator && node.init != null) { | ||
| const { init } = node; | ||
| // check for: `variable = React.variable` | ||
| if (init.type === T.MemberExpression && init.object.type === T.Identifier) { | ||
| return isInitializedFromSource(init.object.name, source, initialScope); | ||
| } | ||
| // check for: `{ variable } = React` | ||
| if (init.type === T.Identifier) { | ||
| return isInitializedFromSource(init.name, source, initialScope); | ||
| } | ||
| // check for: `variable = require('react')` or `variable = require('react').variable` | ||
| const args = getRequireExpressionArguments(init); | ||
| const arg0 = args?.[0]; | ||
| if (arg0 == null || !AST.isLiteral(arg0, "string")) { | ||
| return false; | ||
| } | ||
| // check for: `require('react')` or `require('react/...')` | ||
| return arg0.value === source || arg0.value.startsWith(`${source}/`); | ||
| } | ||
| // latest definition is an import declaration: import { variable } from 'react' | ||
| return parent?.type === T.ImportDeclaration && parent.source.value === source; | ||
| } | ||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.