Skip to content

feat: Add UnusedLiveViewAssign rule#9

Open
ftes wants to merge 2 commits into
Jump-App:mainfrom
ftes:feat/unused-liveview-assign
Open

feat: Add UnusedLiveViewAssign rule#9
ftes wants to merge 2 commits into
Jump-App:mainfrom
ftes:feat/unused-liveview-assign

Conversation

@ftes
Copy link
Copy Markdown

@ftes ftes commented May 22, 2026

Detect unused LiveView/LiveComponent assigns.

Conventions and limitations

  • Helper functions should read the assigns map through a variable namedassigns or through socket.assigns. Reads like opts.foo are not inferred to mean LiveView assigns.
  • Only literal writes via assign, assign_new, assign_async, allow_upload, stream, stream_async, and update are checked.
  • Dynamic assign keys are not tracked. Examples include assign(socket, field, value), socket.assigns[field], and Map.get(assigns, field).
  • Phoenix.Component.assigns_to_attributes/2 is not treated as a read. Prefer explicit attr declarations and attr :rest, :global for forwarded attributes.
  • Some assigns are intended to be consumed by framework or layout code outside the current LiveView module. Use ignored_assigns to whitelist those keys.

Copy link
Copy Markdown
Collaborator

@s3cur3 s3cur3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, this is really exciting! It surfaces 268 issues on our codebase at Jump. 👀 😄 I'd like to go through those and make sure there aren't any false positives (by the terms of the contract in the docs), but me and my team are out of the office until Tuesday next week. I'll plan to get back to you next week, though. 👍

Comment on lines +23 to +25
- Helper functions should read the assigns map through a variable named
`assigns` or through `socket.assigns`. Reads like `opts.foo` are not
inferred to mean LiveView assigns.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It took me a second to understand what this was saying. Might be clearer as something like:

Suggested change
- Helper functions should read the assigns map through a variable named
`assigns` or through `socket.assigns`. Reads like `opts.foo` are not
inferred to mean LiveView assigns.
- Helper functions should read the assigns map through a variable named
`assigns` or through `socket.assigns`. If you bind assigns to another
name (like `%{assigns: opts} = socket`, followed by using
`opts.my_attr`), the reads will not be recognized as being related
to LiveView assigns.

Comment on lines +28 to +30
- Dynamic assign keys are not tracked. Examples include
`assign(socket, field, value)`, `socket.assigns[field]`, and
`Map.get(assigns, field)`.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Dynamic assign keys are not tracked. Examples include
`assign(socket, field, value)`, `socket.assigns[field]`, and
`Map.get(assigns, field)`.
- Writes to dynamic assign keys are not tracked. Examples include
`assign(socket, field, value)`, `socket.assigns[field]`, and
`Map.get(assigns, field)`.

Prefer explicit `attr` declarations and `attr :rest, :global` for
forwarded attributes.
- Some assigns are intended to be consumed by framework or layout code
outside the current LiveView module. Use `ignored_assigns` to whitelist
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
outside the current LiveView module. Use `ignored_assigns` to whitelist
outside the current LiveView module. Use `ignored_assigns` to allowlist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants