Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

ematipico
Copy link
Member

@ematipico ematipico commented Aug 1, 2025

Summary

Implements the rule noUnnecessaryConditions using our inference engine.

Part of #6611

Test Plan

I used the tests from typescript-eslint

Docs

Copy link

changeset-bot bot commented Aug 1, 2025

🦋 Changeset detected

Latest commit: 721a2ed

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 13 packages
Name Type
@biomejs/biome Patch
@biomejs/cli-win32-x64 Patch
@biomejs/cli-win32-arm64 Patch
@biomejs/cli-darwin-x64 Patch
@biomejs/cli-darwin-arm64 Patch
@biomejs/cli-linux-x64 Patch
@biomejs/cli-linux-arm64 Patch
@biomejs/cli-linux-x64-musl Patch
@biomejs/cli-linux-arm64-musl Patch
@biomejs/wasm-web Patch
@biomejs/wasm-bundler Patch
@biomejs/wasm-nodejs Patch
@biomejs/backend-jsonrpc Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@ematipico ematipico requested review from a team August 1, 2025 09:30
@github-actions github-actions bot added A-CLI Area: CLI A-Project Area: project A-Linter Area: linter L-JavaScript Language: JavaScript and super languages L-CSS Language: CSS L-JSON Language: JSON and super languages A-Diagnostic Area: diagnostocis labels Aug 1, 2025
@ematipico ematipico force-pushed the feat/no-unnecessary-contidtions branch from f7006a8 to 0beda5b Compare August 1, 2025 10:23
@ematipico
Copy link
Member Author

cc @arendjr for some reason the type resolver doesn't work in this rule when running the rules_check (the docs part)

Copy link

codspeed-hq bot commented Aug 1, 2025

CodSpeed Performance Report

Merging #7084 will not alter performance

Comparing feat/no-unnecessary-contidtions (721a2ed) with main (d3118c6)

Summary

✅ 128 untouched benchmarks

@ematipico ematipico force-pushed the feat/no-unnecessary-contidtions branch from 0beda5b to 7295f3f Compare August 1, 2025 10:36
Copy link
Contributor

@dyc3 dyc3 left a comment

Choose a reason for hiding this comment

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

Just a couple minor notes

Copy link
Contributor

Choose a reason for hiding this comment

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

nit: revert quick test changes

Comment on lines 331 to 334
let text = literal.text_trimmed();
// Remove quotes to get actual string content
if text.len() >= 2 {
let content = &text[1..text.len() - 1]; // Remove surrounding quotes
Copy link
Contributor

Choose a reason for hiding this comment

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

You probably want str_expr.inner_string_text.

@arendjr
Copy link
Contributor

arendjr commented Aug 3, 2025

cc @arendjr for some reason the type resolver doesn't work in this rule when running the rules_check (the docs part)

Is this still an issue? If so, how can I see the issue?

@ematipico
Copy link
Member Author

Still an issue. Remove the ignore from the code blocks and run

just lint-rules

@@ -0,0 +1,141 @@
// should generate diagnostics

Copy link
Contributor

@vladimir-ivanov vladimir-ivanov Aug 3, 2025

Choose a reason for hiding this comment

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

we need some tests to cover:

declare const b1: false;
declare const b2: boolean;
if (b1 && b2) {
}
const b1 = 22;
declare const b2: boolean;
if (b1 && b2) {
}

and the more funky ones:

((string & { __brandA: string }) | (number & { __brandB: string })) & ""
((string & { __brandA: string }) | (number & { __brandB: string })) & ("foo" | 123)
(123 | true) & { __brand: string }
("" | false) & { __brand: string }
("foo" | "bar") & { __brand: string }
"" & {}
"" & { __brand: string }

for instance:

declare const b1: ((string & { __brandA: string }) | (number & { __brandB: string })) & "";
declare const b2: boolean;
if (b1 && b2) {
}

etc.

@@ -0,0 +1,183 @@
// should not generate diagnostics

Copy link
Contributor

Choose a reason for hiding this comment

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

some more funky tests can be added around declare const here too:

declare const arr2: Array<{ x: { y: { z: object } } }>;
arr2[42]?.x.y?.z;

declare const arr: string[];

if (arr[0]) {
arr[0] ?? 'foo';
}

declare const arr: object[];

if (arr[42] && arr[42]) {
}

declare function isString(x: unknown): x is string;
declare const a: string;
isString('fa' + 'lafel');

declare function isString(x: unknown): x is string;
declare const a: string;
isString(a);

declare function assertsString(x: unknown): asserts x is string;
declare const a: string;
assertsString(a);

declare function assert(x: unknown): asserts x;
assert({});

declare function assert(x: unknown, y: unknown): asserts x;

assert(true, Math.random() > 0.5);

type Foo = { [key: string]: () => number } | null;
declare const foo: Foo;
foo?.['bar']?.()?.toExponential();

also these operators:

declare let foo: null;
foo ||= null;
foo &&= null;
foo ??= null;
etc...

@vladimir-ivanov
Copy link
Contributor

vladimir-ivanov commented Aug 3, 2025

conditional.is_truthy and conditional.is_falsy I found may need some more tweaking for more complex ts cases - see my other comments on tests.
one of the reasons I gave up doing the pr :-0.
Probably this can be merged as is and extra tests/ tweaks added later.

@ematipico
Copy link
Member Author

one of the reasons I gave up doing the pr

We have the nursery category for these reasons. The rule doesn't need to be perfect on the first try

@ematipico
Copy link
Member Author

@vladimir-ivanov can you please add those use cases in the main issue so we can track them?

@ematipico ematipico force-pushed the feat/no-unnecessary-contidtions branch from d4d06e7 to bdc74ce Compare August 5, 2025 12:17
@arendjr
Copy link
Contributor

arendjr commented Aug 5, 2025

@ematipico Re: just lint-rules, indeed we don't initialise the module graph for those snippets. With noFloatingPromises, I simply omitted the expect_diagnostic annotations.

I pushed a commit doing the same here, and also another small fix I just noticed. edit: you were too fast 😆

@ematipico ematipico merged commit 50ca155 into main Aug 5, 2025
31 checks passed
@ematipico ematipico deleted the feat/no-unnecessary-contidtions branch August 5, 2025 13:50
@github-actions github-actions bot mentioned this pull request Aug 5, 2025
@radiosilence
Copy link

radiosilence commented Aug 12, 2025

Just trying it out and it's making a lot of false positives, flagging switch statements, etc!

Screenshot 2025-08-12 at 09 27 12 Screenshot 2025-08-12 at 09 26 56

@ematipico
Copy link
Member Author

@radiosilence please follow up here #6611

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-CLI Area: CLI A-Diagnostic Area: diagnostocis A-Linter Area: linter A-Project Area: project L-CSS Language: CSS L-JavaScript Language: JavaScript and super languages L-JSON Language: JSON and super languages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants