-
-
Notifications
You must be signed in to change notification settings - Fork 97
Add Roact dangling connection lint #533
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
Open
chriscerie
wants to merge
28
commits into
Kampfkarren:main
Choose a base branch
from
chriscerie:roact_dangling_connection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
fd551de
Add roact dangling connection lint
chriscerie 1fd05c1
Update docs and changelog
chriscerie bfd8a04
Fix scope_manager.function_calls missing method names
chriscerie d5e2123
Use scope manager to get dangling connections
chriscerie e3ba026
Fix non-exhaustive pattern
chriscerie b5e290e
Merge branch 'main' into roact_dangling_connection
chriscerie 959d019
Change match to if let
chriscerie 96408af
Merge branch 'main' of https://github.com/Kampfkarren/selene into roa…
chriscerie 7e43d84
Require react prefix to useEffect to display useEffect error
chriscerie 809aa6d
Merge branch 'main' into roact_dangling_connection
chriscerie fd071d1
Merge branch 'main' into roact_dangling_connection
chriscerie 78ba6a7
Merge branch 'main' into roact_dangling_connection
chriscerie 4bb9701
Merge branch 'main' into roact_dangling_connection
chriscerie 23aa26f
Test
chriscerie c23ba11
Test
chriscerie 786de0b
Test
chriscerie 4d26ab3
Test
chriscerie fc5c2f5
Merge branch 'main' into roact_dangling_connection
chriscerie a0d53df
Merge branch 'main' of https://github.com/Kampfkarren/selene
chriscerie c91e195
Merge branch 'main' into roact_dangling_connection
chriscerie 98176b5
Merge branch 'main' of https://github.com/Kampfkarren/selene
chriscerie 807c4a8
Merge branch 'main' of https://github.com/chriscerie/selene into roac…
chriscerie 8f66ac0
Fix test
chriscerie b5dff3b
Remove file
chriscerie e3d680a
Remove change
chriscerie a1c0bcd
Fix clippy
chriscerie 51a7d1c
Merge branch 'main' of https://github.com/Kampfkarren/selene into roa…
chriscerie 3008199
Only warn in useEffect
chriscerie 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
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,24 @@ | ||
| # roblox_roact_dangling_connection | ||
| ## What it does | ||
| Checks for connections in Roact components that are either not assigned to a variable or passed as arguments. | ||
|
|
||
| ## Why this is bad | ||
| This indicates a memory leak and can cause unexpected behaviors. | ||
|
|
||
| ## Example | ||
| ```lua | ||
| local function MyComponent() | ||
| useEffect(function() | ||
| a:Connect() | ||
| end, {}) | ||
| end | ||
| ``` | ||
|
|
||
| ## Remarks | ||
| This lint is active if the file has a variable named `Roact` or `React` and that the connection is made within a function. | ||
|
|
||
| This checks for connections by identifying the following keywords: | ||
| * Connect | ||
| * connect | ||
| * ConnectParallel | ||
| * Once |
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
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
234 changes: 234 additions & 0 deletions
234
selene-lib/src/lints/roblox_roact_dangling_connection.rs
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,234 @@ | ||
| use super::*; | ||
| use crate::ast_util::range; | ||
| use std::{collections::HashSet, convert::Infallible}; | ||
|
|
||
| use full_moon::{ | ||
| ast::{self, Ast}, | ||
| visitors::Visitor, | ||
| }; | ||
|
|
||
| pub struct RoactDanglingConnectionLint; | ||
|
|
||
| impl Lint for RoactDanglingConnectionLint { | ||
| type Config = (); | ||
| type Error = Infallible; | ||
|
|
||
| const SEVERITY: Severity = Severity::Error; | ||
| const LINT_TYPE: LintType = LintType::Correctness; | ||
|
|
||
| fn new(_: Self::Config) -> Result<Self, Self::Error> { | ||
| Ok(Self) | ||
| } | ||
|
|
||
| fn pass( | ||
| &self, | ||
| ast: &Ast, | ||
| context: &Context, | ||
| AstContext { scope_manager, .. }: &AstContext, | ||
| ) -> Vec<Diagnostic> { | ||
| if !context.is_roblox() { | ||
| return Vec::new(); | ||
| } | ||
|
|
||
| if scope_manager.variables.iter().all(|(_, variable)| { | ||
| !["Roact", "React"].contains(&variable.name.trim_end_matches(char::is_whitespace)) | ||
| }) { | ||
| return Vec::new(); | ||
| } | ||
|
|
||
| let mut visitor = RoactDanglingConnectionVisitor { | ||
| dangling_connections: Vec::new(), | ||
| dangling_connection_start_ranges: scope_manager | ||
| .function_calls | ||
| .iter() | ||
| .filter_map(|(_, function_call_stmt)| { | ||
| function_call_stmt | ||
| .call_name_path | ||
| .last() | ||
| .and_then(|last_name| { | ||
| if ["Connect", "connect", "ConnectParallel", "Once"] | ||
| .contains(&last_name.as_str()) | ||
| { | ||
| Some(function_call_stmt.call_prefix_range.0) | ||
| } else { | ||
| None | ||
| } | ||
| }) | ||
| }) | ||
| .collect(), | ||
| function_contexts: Vec::new(), | ||
| }; | ||
|
|
||
| visitor.visit_ast(ast); | ||
|
|
||
| let mut diagnostics = Vec::new(); | ||
|
|
||
| for invalid_event in visitor.dangling_connections { | ||
| match invalid_event.function_context { | ||
| ConnectionContext::UseEffect => { | ||
| diagnostics.push(Diagnostic::new( | ||
| "roblox_roact_dangling_connection", | ||
| "disconnect the connection in the useEffect cleanup function".to_owned(), | ||
| Label::new(invalid_event.range), | ||
| )); | ||
| } | ||
| _ => { | ||
| diagnostics.push(Diagnostic::new( | ||
| "roblox_roact_dangling_connection", | ||
| "disconnect the connection where appropriate".to_owned(), | ||
| Label::new(invalid_event.range), | ||
| )); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| diagnostics | ||
| } | ||
| } | ||
|
|
||
| fn get_last_function_call_suffix(prefix: &ast::Prefix, suffixes: &[&ast::Suffix]) -> String { | ||
| let last_suffix = match suffixes.last() { | ||
| Some(ast::Suffix::Call(ast::Call::MethodCall(_))) => suffixes.last(), | ||
| Some(ast::Suffix::Call(ast::Call::AnonymousCall(_))) => { | ||
| if suffixes.len() == 1 { | ||
| // a() | ||
| return if let ast::Prefix::Name(name) = prefix { | ||
| name.token().to_string() | ||
| } else { | ||
| "".to_owned() | ||
| }; | ||
| } else { | ||
| // In a.b(), b is the suffix before the last one | ||
| Some(&suffixes[suffixes.len() - 2]) | ||
| } | ||
| } | ||
| _ => return "".to_owned(), | ||
| }; | ||
|
|
||
| last_suffix | ||
| .map(|suffix| match suffix { | ||
| ast::Suffix::Index(ast::Index::Dot { name, .. }) => name.token().to_string(), | ||
| ast::Suffix::Call(ast::Call::MethodCall(method_call)) => { | ||
| method_call.name().token().to_string() | ||
| } | ||
| ast::Suffix::Call(ast::Call::AnonymousCall(anonymous_call)) => { | ||
| anonymous_call.to_string() | ||
| } | ||
| _ => "".to_string(), | ||
| }) | ||
| .unwrap_or_default() | ||
| } | ||
|
|
||
| #[derive(Debug, PartialEq, Clone, Copy)] | ||
| enum ConnectionContext { | ||
| Unknown, | ||
| UseEffect, | ||
| } | ||
|
|
||
| #[derive(Debug, PartialEq, Clone, Copy)] | ||
| enum ConnectionContextType { | ||
| FunctionBody, | ||
| FunctionCall, | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| struct RoactDanglingConnectionVisitor { | ||
| dangling_connections: Vec<DanglingConnection>, | ||
| dangling_connection_start_ranges: HashSet<usize>, | ||
| function_contexts: Vec<(ConnectionContextType, ConnectionContext)>, | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| struct DanglingConnection { | ||
| range: (usize, usize), | ||
| function_context: ConnectionContext, | ||
| } | ||
|
|
||
| fn get_last_known_context( | ||
| function_contexts: &[(ConnectionContextType, ConnectionContext)], | ||
| ) -> ConnectionContext { | ||
| match function_contexts | ||
| .iter() | ||
| .rev() | ||
| .find(|(_, connection_type)| *connection_type != ConnectionContext::Unknown) | ||
| { | ||
| Some(context) => context.1, | ||
| None => ConnectionContext::Unknown, | ||
| } | ||
| } | ||
|
|
||
| impl Visitor for RoactDanglingConnectionVisitor { | ||
| fn visit_function_call(&mut self, call: &ast::FunctionCall) { | ||
| let last_suffix = | ||
| get_last_function_call_suffix(call.prefix(), &call.suffixes().collect::<Vec<_>>()); | ||
|
|
||
| if !self.function_contexts.is_empty() { | ||
| if let Some(call_range) = call.range() { | ||
| if self | ||
| .dangling_connection_start_ranges | ||
| .contains(&call_range.0.bytes()) | ||
| { | ||
| self.dangling_connections.push(DanglingConnection { | ||
| range: range(call), | ||
| function_context: get_last_known_context(&self.function_contexts), | ||
| }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| self.function_contexts.push(( | ||
| ConnectionContextType::FunctionCall, | ||
| match last_suffix.as_str() { | ||
| "useEffect" => ConnectionContext::UseEffect, | ||
| _ => ConnectionContext::Unknown, | ||
| }, | ||
| )); | ||
| } | ||
|
|
||
| fn visit_function_call_end(&mut self, _: &ast::FunctionCall) { | ||
| self.function_contexts.pop(); | ||
| } | ||
|
|
||
| fn visit_function_body(&mut self, _: &ast::FunctionBody) { | ||
| self.function_contexts.push(( | ||
| ConnectionContextType::FunctionBody, | ||
| ConnectionContext::Unknown, | ||
| )); | ||
| } | ||
|
|
||
| fn visit_function_body_end(&mut self, _: &ast::FunctionBody) { | ||
| self.function_contexts.pop(); | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::{super::test_util::test_lint, *}; | ||
|
|
||
| #[test] | ||
| fn test_no_roact() { | ||
| test_lint( | ||
| RoactDanglingConnectionLint::new(()).unwrap(), | ||
| "roblox_roact_dangling_connection", | ||
| "no_roact", | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_roblox_roact_dangling_connection() { | ||
| test_lint( | ||
| RoactDanglingConnectionLint::new(()).unwrap(), | ||
| "roblox_roact_dangling_connection", | ||
| "roblox_roact_dangling_connection", | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_with_roact() { | ||
| test_lint( | ||
| RoactDanglingConnectionLint::new(()).unwrap(), | ||
| "roblox_roact_dangling_connection", | ||
| "with_roact", | ||
| ); | ||
| } | ||
| } | ||
74 changes: 74 additions & 0 deletions
74
selene-lib/tests/lints/roblox_roact_dangling_connection/no_roact.lua
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,74 @@ | ||
| a:Connect() | ||
| a.Connect() | ||
| a:connect() | ||
| a.connect() | ||
|
|
||
| a.b:Connect() | ||
| a.b.Connect() | ||
| a.b:connect() | ||
| a.b.connect() | ||
|
|
||
| a.b.c:Connect() | ||
| a.b.c.Connect() | ||
| a.b.c:connect() | ||
| a.b.c.connect() | ||
|
|
||
| local foo = a:Connect() | ||
| local foo = a.Connect() | ||
| local foo = a:connect() | ||
| local foo = a.connect() | ||
|
|
||
| foo = a:Connect() | ||
| foo = a.Connect() | ||
| foo = a:connect() | ||
| foo = a.connect() | ||
|
|
||
| foo = a.b:Connect() | ||
| foo = a.b.Connect() | ||
| foo = a.b:connect() | ||
| foo = a.b.connect() | ||
|
|
||
| foo = a.b.c:Connect() | ||
| foo = a.b.c.Connect() | ||
| foo = a.b.c:connect() | ||
| foo = a.b.c.connect() | ||
|
|
||
| local function c() | ||
| a:Connect() | ||
| a:connect() | ||
|
|
||
| a.b:Connect() | ||
| a.b:connect() | ||
|
|
||
| a.b.c:Connect() | ||
| a.b.c:connect() | ||
|
|
||
| foo = a:Connect() | ||
| foo = a:connect() | ||
|
|
||
| foo = a.b:Connect() | ||
| foo = a.b:connect() | ||
|
|
||
| foo = a.b.c:Connect() | ||
| foo = a.b.c:connect() | ||
| end | ||
|
|
||
| function d:e() | ||
| a:Connect() | ||
| a:connect() | ||
|
|
||
| a.b:Connect() | ||
| a.b:connect() | ||
|
|
||
| a.b.c:Connect() | ||
| a.b.c:connect() | ||
|
|
||
| foo = a:Connect() | ||
| foo = a:connect() | ||
|
|
||
| foo = a.b:Connect() | ||
| foo = a.b:connect() | ||
|
|
||
| foo = a.b.c:Connect() | ||
| foo = a.b.c:connect() | ||
| end |
2 changes: 2 additions & 0 deletions
2
selene-lib/tests/lints/roblox_roact_dangling_connection/no_roact.std.toml
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,2 @@ | ||
| [selene] | ||
| name = "roblox" |
Empty file.
Oops, something went wrong.
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.