Skip to content

Commit 1ee2a21

Browse files
feat: a new unassertCode function that returns the modified code and sourcemap
1 parent 8733dc3 commit 1ee2a21

File tree

6 files changed

+521
-40
lines changed

6 files changed

+521
-40
lines changed

README.md

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,17 @@ function add(a, b) {
9191
API
9292
---------------------------------------
9393
94-
unassert package exports three functions. [`unassertAst`](https://github.com/unassert-js/unassert#const-modifiedast--unassertastast-options) is the main function. [`createVisitor`](https://github.com/unassert-js/unassert#const-visitor--createvisitoroptions) and [`defaultOptions`](https://github.com/unassert-js/unassert#const-options--defaultoptions) are for customization.
94+
unassert package exports four functions.
9595
96+
Main functions:
97+
98+
* [`unassertAst`](https://github.com/unassert-js/unassert#const-modifiedast--unassertastast-options)
99+
* [`unassertCode`](https://github.com/unassert-js/unassert#const-unasserted--unassertcodecodeast-options)
100+
101+
For customization:
102+
103+
* [`createVisitor`](https://github.com/unassert-js/unassert#const-visitor--createvisitoroptions)
104+
* [`defaultOptions`](https://github.com/unassert-js/unassert#const-options--defaultoptions)
96105
97106
### const modifiedAst = unassertAst(ast, options)
98107
@@ -128,6 +137,7 @@ For example, the default target modules are as follows.
128137
'node:assert',
129138
'node:assert/strict'
130139
]
140+
}
131141
```
132142
133143
In this case, unassert will remove assert variable declarations such as,
@@ -201,6 +211,41 @@ unassert removes all `strictAssert`, `ok`, `eq` calls.
201211
202212
Please see [customization example](https://github.com/unassert-js/unassert#example-1) for more details.
203213
214+
### const unasserted = unassertCode(code, ast, options)
215+
216+
```javascript
217+
const { unassertCode } = require('unassert')
218+
```
219+
220+
```javascript
221+
import { unassertCode } from 'unassert'
222+
```
223+
224+
| return type |
225+
|:--------------------------------------------------------------|
226+
| `{ code: string; map: SourceMap | null; }` |
227+
228+
Remove assertion calls from the code. Default behaviour can be customized by `options`.
229+
Note that the `ast` is manipulated directly.
230+
231+
#### MagicString
232+
233+
If a [MagicString](https://www.npmjs.com/package/magic-string) is passed instead of a normal string,
234+
this function will simply modify that string and return it.
235+
236+
#### options
237+
238+
Object for configuration options. passed `options` is `Object.assign`ed with default options. If not passed, default options will be used.
239+
240+
##### options.modules
241+
242+
The same as for [`unassertAst`](#optionsmodules).
243+
244+
##### options.sourceMap
245+
246+
If `true`, a sourcemap of the changes will be generated in addition to the code.
247+
248+
You can alternatively specify [sourcemap options](https://github.com/rich-harris/magic-string#sgeneratemap-options-) if you which to customize how the sourcemap is generated.
204249
205250
### const visitor = createVisitor(options)
206251
@@ -218,6 +263,18 @@ import { createVisitor } from 'unassert'
218263
219264
Create visitor object to be used with `estraverse.replace`. Visitor can be customized by `options`.
220265
266+
#### options
267+
268+
Object for configuration options. passed `options` is `Object.assign`ed with default options. If not passed, default options will be used.
269+
270+
##### options.modules
271+
272+
The same as for [`unassertAst`](#optionsmodules).
273+
274+
##### options.code
275+
276+
A [MagicString](https://www.npmjs.com/package/magic-string) of the code the ast represents.
277+
If given, this code will be updated with the changes made to the ast.
221278
222279
### const options = defaultOptions()
223280

package-lock.json

Lines changed: 31 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
}
1616
],
1717
"dependencies": {
18-
"estraverse": "^5.0.0"
18+
"estraverse": "^5.0.0",
19+
"magic-string": "^0.30.0"
1920
},
2021
"devDependencies": {
2122
"@twada/benchmark-commits": "^0.1.0",

src/index.d.mts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import type MagicString from "magic-string";
2+
import type { SourceMap, SourceMapOptions } from "magic-string";
3+
import type { Node } from "acorn";
4+
import type { Visitor } from "estraverse";
5+
6+
export type UnassertAstOptions = Partial<{
7+
modules: string[];
8+
variables: string[];
9+
}>;
10+
11+
export type UnassertCodeOptions = UnassertAstOptions &
12+
Partial<{
13+
sourceMap: boolean | SourceMapOptions;
14+
}>;
15+
16+
export type CreateVisitorOptions = UnassertAstOptions &
17+
Partial<{
18+
code: MagicString;
19+
}>;
20+
21+
export type UnassertCodeResult = {
22+
code: string;
23+
map: SourceMap | null;
24+
};
25+
26+
export function unassertAst(ast: Node, options?: UnassertAstOptions): Node;
27+
28+
export function unassertCode(
29+
code: string,
30+
ast: Node,
31+
options?: UnassertCodeOptions
32+
): UnassertCodeResult;
33+
export function unassertCode(
34+
code: MagicString,
35+
ast: Node,
36+
options?: UnassertCodeOptions
37+
): MagicString;
38+
export function unassertCode(
39+
code: string | MagicString,
40+
ast: Node,
41+
options?: UnassertCodeOptions
42+
): UnassertCodeResult | MagicString;
43+
44+
export function defaultOptions(): UnassertAstOptions;
45+
46+
export function createVisitor(options?: CreateVisitorOptions): Visitor;

0 commit comments

Comments
 (0)