Skip to content

feat(lint): implement noImplicitCoercion rule#6395

Merged
ematipico merged 7 commits intobiomejs:mainfrom
mdevils:feat/no-implicit-coercion
Jun 25, 2025
Merged

feat(lint): implement noImplicitCoercion rule#6395
ematipico merged 7 commits intobiomejs:mainfrom
mdevils:feat/no-implicit-coercion

Conversation

@mdevils
Copy link
Copy Markdown
Contributor

@mdevils mdevils commented Jun 18, 2025

Summary

Added the new rule noImplicitCoercion, which disallows shorthand type conversions in favor of explicit type conversion functions.

Example (Invalid): Boolean conversion using double negation:

!!foo;
!!(foo + bar);

Example (Invalid): Number conversion using unary operators:

+foo;
-(-foo);
foo - 0;
foo * 1;
foo / 1;

Example (Invalid): String conversion using concatenation:

"" + foo;
foo + "";
`` + foo;
foo += "";

Example (Invalid): Index checking using bitwise NOT:

~foo.indexOf(1);
~foo.bar.indexOf(2);

Example (Valid): Using explicit type conversion functions:

Boolean(foo);
Number(foo);
String(foo);
foo.indexOf(1) !== -1;

Test Plan

Tests are included.

Closes #3935.

@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 A-Diagnostic Area: diagnostocis labels Jun 18, 2025
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Jun 18, 2025

CodSpeed Performance Report

Merging #6395 will degrade performances by 17.01%

Comparing mdevils:feat/no-implicit-coercion (37f852d) with main (e128ea9)

Summary

❌ 1 regressions
✅ 114 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark BASE HEAD Change
eucjp_1600564308684076393.json[cached] 760.1 µs 915.9 µs -17.01%

Copy link
Copy Markdown
Contributor

@arendjr arendjr left a comment

Choose a reason for hiding this comment

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

Nice work and solid implementation!

trait ExpressionExt {
/// Returns the actual expression, i.e. in case of parenthesized expressions
/// it returns the inner expression.
fn actual_expression(&self) -> Option<AnyJsExpression>;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggestion:

Suggested change
fn actual_expression(&self) -> Option<AnyJsExpression>;
fn inner_expression(&self) -> Option<AnyJsExpression>;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sure, changed.

@arendjr arendjr added this to the Biome 2.1 milestone Jun 23, 2025
Comment thread .changeset/six-hornets-do.md Outdated
1 * a / 2;
(a * 1) / 2;
a * 1 / (b * 1);
/* test */ a * 1 + 2;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If possible, we could add another test with a trailing comment

/* test */ a /*comment*/ * 1 + 2;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good case, added.

Comment thread .changeset/six-hornets-do.md Outdated
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Jun 23, 2025

🦋 Changeset detected

Latest commit: 37f852d

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
Copy link
Copy Markdown
Member

@mdevils I changed the changeset to patch, because nursery rules can go out in patch releases https://biomejs.dev/linter/#nursery

@mdevils mdevils force-pushed the feat/no-implicit-coercion branch from c32b034 to 38476da Compare June 24, 2025 21:16
@mdevils
Copy link
Copy Markdown
Contributor Author

mdevils commented Jun 24, 2025

Rebased.

@ematipico ematipico merged commit f62e748 into biomejs:main Jun 25, 2025
28 checks passed
@ematipico ematipico removed this from the Biome 2.1 milestone Jun 25, 2025
@github-actions github-actions Bot mentioned this pull request Jun 25, 2025
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-JavaScript Language: JavaScript and super languages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

📎 Implement noImplicitCoercion - eslint/no-implicit-coercion

3 participants