feat: Add remove-assert plugin for SWC#575
Closed
chunghyunleeme wants to merge 14 commits intoswc-project:mainfrom
Closed
feat: Add remove-assert plugin for SWC#575chunghyunleeme wants to merge 14 commits intoswc-project:mainfrom
chunghyunleeme wants to merge 14 commits intoswc-project:mainfrom
Conversation
Implement a new Wasm plugin that removes assert() statements during compilation, reducing bundle size and runtime overhead in production builds. This is similar to how Python's -O flag and C's NDEBUG macro handle assertions. - Supports automatic removal of global assert() calls - Preserves locally-defined assert functions - Fully tested with comprehensive test cases - Works as a production build optimization tool Addresses GitHub issue: swc-project/swc#11449
Implement a new Wasm plugin that removes assert() statements during compilation, reducing bundle size and runtime overhead in production builds. This is similar to how Python's -O flag and C's NDEBUG macro handle assertions. - Supports automatic removal of global assert() calls - Preserves locally-defined assert functions - Fully tested with comprehensive test cases - Works as a production build optimization tool Addresses GitHub issue: swc-project/swc#11449
🦋 Changeset detectedLatest commit: 1cc1c43 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
kdy1
reviewed
Jan 19, 2026
Member
kdy1
left a comment
There was a problem hiding this comment.
Can you add some more tests?
The test suite should include test for:
- import * as assert from 'assert'
- from
node:assert - import { assert, fail } from 'assert';
- import { assert } from 'assert'; && import { fail } from 'assert'; (in same file)
- import { fail } from 'assert'; && import * as assert from 'assert'; (in same file)
Add fixture tests covering various import patterns:
- import * as assert from 'assert'
- import assert from 'node:assert'
- import { assert, fail } from 'assert'
- separate import { assert } and import { fail }
- mixed import { fail } and import * as assert
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add WASM-based tests covering various import patterns:
- import * as assert from 'assert'
- import assert from 'node:assert'
- import { assert, fail } from 'assert'
- separate import { assert } and import { fail }
- mixed import { fail } and import * as assert
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Imported assert identifiers are bound (not unresolved), so assert calls should NOT be removed when assert is imported. Updated the expected output files to reflect the actual behavior. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
The plugin now removes assert calls from modules imported from:
- 'assert'
- 'node:assert'
- 'assert/strict'
- 'node:assert/strict'
Supports all import styles:
- import assert from 'assert'
- import * as assert from 'assert'
- import { assert, fail, ok } from 'assert'
Also handles method calls like assert.ok(), assert.strictEqual(), etc.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Update fixture outputs to expect assert calls to be removed - Add test for assert method calls (assert.ok, assert.strictEqual, etc.) - Update vitest snapshots Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
The plugin now removes the entire import declaration when importing
from assert modules ('assert', 'node:assert', 'assert/strict',
'node:assert/strict'), not just the assert calls.
Before:
```js
import assert from 'assert';
;
```
After:
```js
;
```
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
kdy1
requested changes
Jan 21, 2026
| @@ -0,0 +1,22 @@ | |||
| # remove-console | |||
…ntation Replace generic remove-console template with remove-assert specific documentation, including proper description, configuration examples, and input/output examples. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
kdy1
reviewed
Jan 21, 2026
| match specifier { | ||
| // import assert from 'assert' | ||
| ImportSpecifier::Default(default) => { | ||
| self.assert_import_ctxts.push(default.local.ctxt); |
Member
There was a problem hiding this comment.
We should collect default.local.to_id() instead
Member
|
Closing as stale and I'm going to do it by myself |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Implements a new Wasm plugin for SWC that automatically removes
assert()statements during compilation, reducing bundle size and runtime overhead in production builds. This is similar to how Python's-Oflag and C'sNDEBUGmacro eliminate assertions.Features
assert()callsassertcalls (fromassert,node:assert,assert/strict,node:assert/strict)assert.ok(),assert.strictEqual(),assert.deepEqual(), etc.assertfunctions (scoping aware)Supported Import Patterns
Test Plan
Example Usage
{ "jsc": { "experimental": { "plugins": [ ["@swc/plugin-remove-assert", {}] ] } } }Input
Output (production build)
Related Issue
Addresses: https://github.com/swc-project/swc/issues/11449