Skip to content

Commit

Permalink
feat: a new unassertCode function that returns the modified code and …
Browse files Browse the repository at this point in the history
…sourcemap
  • Loading branch information
RebeccaStevens committed Mar 29, 2023
1 parent 8733dc3 commit 410ccf5
Show file tree
Hide file tree
Showing 6 changed files with 522 additions and 40 deletions.
59 changes: 58 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,17 @@ function add(a, b) {
API
---------------------------------------
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.
unassert package exports four functions.
Main functions:
* [`unassertAst`](https://github.com/unassert-js/unassert#const-modifiedast--unassertastast-options)
* [`unassertCode`](https://github.com/unassert-js/unassert#const-unasserted--unassertcodecodeast-options)
For customization:
* [`createVisitor`](https://github.com/unassert-js/unassert#const-visitor--createvisitoroptions)
* [`defaultOptions`](https://github.com/unassert-js/unassert#const-options--defaultoptions)
### const modifiedAst = unassertAst(ast, options)
Expand Down Expand Up @@ -128,6 +137,7 @@ For example, the default target modules are as follows.
'node:assert',
'node:assert/strict'
]
}
```
In this case, unassert will remove assert variable declarations such as,
Expand Down Expand Up @@ -201,6 +211,41 @@ unassert removes all `strictAssert`, `ok`, `eq` calls.
Please see [customization example](https://github.com/unassert-js/unassert#example-1) for more details.
### const unasserted = unassertCode(code, ast, options)
```javascript
const { unassertCode } = require('unassert')
```
```javascript
import { unassertCode } from 'unassert'
```
| return type |
|:--------------------------------------------------------------|
| `{ code: string; map: SourceMap | null; }` |
Remove assertion calls from the code. Default behaviour can be customized by `options`.
Note that the `ast` is manipulated directly.
#### MagicString
If a [MagicString](https://www.npmjs.com/package/magic-string) is passed instead of a normal string,
this function will simply modify that string and return it.
#### options
Object for configuration options. passed `options` is `Object.assign`ed with default options. If not passed, default options will be used.
##### options.modules
The same as for [`unassertAst`](#optionsmodules).
##### options.sourceMap
If `true`, a sourcemap of the changes will be generated in addition to the code.
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.
### const visitor = createVisitor(options)
Expand All @@ -218,6 +263,18 @@ import { createVisitor } from 'unassert'
Create visitor object to be used with `estraverse.replace`. Visitor can be customized by `options`.
#### options
Object for configuration options. passed `options` is `Object.assign`ed with default options. If not passed, default options will be used.
##### options.modules
The same as for [`unassertAst`](#optionsmodules).
##### options.code
A [MagicString](https://www.npmjs.com/package/magic-string) of the code the ast represents.
If given, this code will be updated with the changes made to the ast.
### const options = defaultOptions()
Expand Down
32 changes: 31 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
}
],
"dependencies": {
"estraverse": "^5.0.0"
"estraverse": "^5.0.0",
"magic-string": "^0.30.0"
},
"devDependencies": {
"@twada/benchmark-commits": "^0.1.0",
Expand Down
46 changes: 46 additions & 0 deletions src/index.d.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import type MagicString from "magic-string";
import type { SourceMap, SourceMapOptions } from "magic-string";
import type { Node } from "acorn";
import type { Visitor } from "estraverse";

export type UnassertAstOptions = Partial<{
modules: string[];
variables: string[];
}>;

export type UnassertCodeOptions = UnassertAstOptions &
Partial<{
sourceMap: boolean | SourceMapOptions;
}>;

export type CreateVisitorOptions = UnassertAstOptions &
Partial<{
code: MagicString;
}>;

export type UnassertCodeResult = {
code: string;
map: SourceMap | null;
};

export function unassertAst(ast: Node, options?: UnassertAstOptions): Node;

export function unassertCode(
code: string,
ast: Node,
options?: UnassertCodeOptions
): UnassertCodeResult;
export function unassertCode(
code: MagicString,
ast: Node,
options?: UnassertCodeOptions
): MagicString;
export function unassertCode(
code: string | MagicString,
ast: Node,
options?: UnassertCodeOptions
): UnassertCodeResult | MagicString;

export function defaultOptions(): UnassertAstOptions;

export function createVisitor(options?: CreateVisitorOptions): Visitor;
Loading

0 comments on commit 410ccf5

Please sign in to comment.