8000 [no-misused-signals] restrict incorrect usage of signals - proposal · Issue #1891 · angular-eslint/angular-eslint · GitHub
[go: up one dir, main page]

Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

[no-misused-signals] restrict incorrect usage of signals - proposal #1891

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

Closed
MillerSvt opened this issue Jun 20, 2024 · 3 comments
Closed

[no-misused-signals] restrict incorrect usage of signals - proposal #1891

MillerSvt opened this issue Jun 20, 2024 · 3 comments
Labels
package: eslint-plugin Angular-specific TypeScript rules PRs Welcome If a high-quality PR is created for this it will be accepted

Comments

@MillerSvt
Copy link
MillerSvt commented Jun 20, 2024

Description of the proposal

This rule forbids providing Signals to logical locations such as if statements in places where the TypeScript compiler allows them but they are not handled properly. It could be about using signals in a nullish check or comparing them using operators like ===, ==, >, <, >=, or <=. These situations can often arise due to a missing call or just a misunderstanding of the way signals are handled.

In some rare cases === or == operators can be valid. So need to add option ignoreNullishCheck to disable higlighting them.

Reference: https://typescript-eslint.io/rules/no-misused-promises/

Incorrect code

const signal = signal(0);
const otherSignal = signal(0);

if (signal) {
  // Do something
}

if (signal === otherSignal) {
  // Do something
}

if (signal > otherSignal) {
  // Do something
}

if (signal < otherSignal) {
  // Do something
}

const val = signal ? 123 : 456;

while (signal) {
  // Do something
}
@if (signal) {
   something  
}

@if (signal === otherSignal) {
   something  
}

@if (signal > otherSignal) {
   something  
}

@if (signal < otherSignal) {
   something  
}

@switch (signal) {
   something
}

{{ signal }}

Correct code

const signal = signal(0);
const otherSignal = signal(0);

if (signal()) {
  // Do something
}

if (signal() === otherSignal()) {
  // Do something
}

if (signal() > otherSignal()) {
  // Do something
}

if (signal() < otherSignal()) {
  // Do something
}

const val = signal() ? 123 : 456;

while (signal()) {
  // Do something
}
@if (signal()) {
   something  
}

@if (signal() === otherSignal()) {
   something  
}

@if (signal() > otherSignal()) {
   something  
}

@if (signal() < otherSignal()) {
   something  
}

@switch (signal()) {
   something
}

{{ signal() }}
@MillerSvt MillerSvt added package: eslint-plugin Angular-specific TypeScript rules triage This issue needs to be looked at and categorized by a maintainer labels Jun 20, 2024
@PowerKiKi
Copy link

This seems to be a duplicate of #1753 and should probably be merged into a single issue.

@marcorohn
Copy link

Since signals are functions, this is also valid typescript code, but will never run, as Function.length always returns 0 for signals:

const mySignal = signal(['hello', 'world']);
if (mySignal.length > 0) {
  ...
}

Just happened to me recently when converting a component to use signals, and I'm recently sure that is not that uncommon

@JamesHenry JamesHenry added PRs Welcome If a high-quality PR is created for this it will be accepted and removed triage This issue needs to be looked at and categorized by a maintainer labels Mar 8, 2025
@JamesHenry
Copy link
Member

Thanks folks, it makes sense to me and I'll happily accept a PR for this rule

@angular-eslint angular-eslint locked and limited conversation to collaborators Mar 8, 2025
@JamesHenry JamesHenry converted this issue into discussion #2302 Mar 8, 2025

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

443C
Labels
package: eslint-plugin Angular-specific TypeScript rules PRs Welcome If a high-quality PR is created for this it will be accepted
Projects
None yet
Development

No branches or pull requests

4 participants
0