-
-
Notifications
You must be signed in to change notification settings - Fork 90
feat: Add remove-assert plugin for SWC #575
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
base: main
Are you sure you want to change the base?
feat: Add remove-assert plugin for SWC #575
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
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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>
| @@ -0,0 +1,22 @@ | |||
| # remove-console | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please update this document
…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>
| match specifier { | ||
| // import assert from 'assert' | ||
| ImportSpecifier::Default(default) => { | ||
| self.assert_import_ctxts.push(default.local.ctxt); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is wrong
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should collect default.local.to_id() instead
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