-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from kwonoj/feat-impl
feat(visit): initial impl
- Loading branch information
Showing
8 changed files
with
467 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
name: CI | ||
|
||
on: | ||
pull_request: | ||
types: ['opened', 'reopened', 'synchronize'] | ||
push: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
name: Run test | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: buildjet/cache@v3 | ||
with: | ||
path: | | ||
~/.cargo/bin/ | ||
~/.cargo/registry/index/ | ||
~/.cargo/registry/cache/ | ||
~/.cargo/git/db/ | ||
target/ | ||
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} | ||
- uses: actions/setup-node@v2 | ||
with: | ||
node-version: "16" | ||
cache: "npm" | ||
- uses: actions-rs/toolchain@v1 | ||
with: | ||
profile: minimal | ||
components: llvm-tools-preview | ||
- name: Install cargo-llvm-cov | ||
uses: taiki-e/install-action@cargo-llvm-cov | ||
- name: install | ||
run: | | ||
npm install -g npm@latest | ||
npm ci | ||
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh | ||
rustup target add wasm32-wasi | ||
rustup target add wasm32-unknown-unknown | ||
- name: test | ||
run: | | ||
npm run build | ||
- name: build | ||
run: | | ||
cargo check |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
/node_modules | ||
/test/parser/**/_actual.json | ||
*.log | ||
/test/**/_actual.* | ||
/test/**/_actual.* | ||
/test.js |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 OJ Kwon | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
### Woodpile | ||
|
||
Woodpile is a utility library to traverse [SWC](https://github.com/swc-project/swc) ASTs in Javascript. It is a thin interop layer to the SWC's rust implementation of its visitor macro, attempt to provide consistent, synchronized mechanism to traverse ASTs in Javascript as well. This package could be useful to duck typing, prototyping SWC plugin in Javascript as its interface aims to provide similar experience as SWC's visitor macro. | ||
|
||
For those reason, this package aims correctness over performance. There are inevitable costs to exchange data between Javascript to Rust, and vice versa. If you want to achieve peak performace, you should use SWC's visitor macro directly. | ||
|
||
|
||
### Usage | ||
|
||
`visit` is a main interface to traverse AST. | ||
|
||
Currently, `visit` accepts an object with `visit` property have corresponding callbacks to traverse. | ||
|
||
```ts | ||
const { visit } = require('woodpile'); | ||
const { parseSync } = require('@swc/core'); | ||
|
||
const ast = parseSync('console.log("Hello, World!")'); | ||
|
||
visit(ast, { | ||
visit: { | ||
// Callbacks with visit${NodeName} will be called recursively for the node | ||
visitProgram: (node, self) => { | ||
console.log('visitProgram', node); | ||
}, | ||
visitExpr: (node) => { | ||
console.log('visitExpr', node); | ||
} | ||
}, | ||
}); | ||
``` | ||
|
||
It is possible to return node in each callback which attempts to replace given node. | ||
|
||
```ts | ||
visitProgram: (node) => { | ||
node.Span = ...; | ||
return node | ||
} | ||
``` | ||
|
||
However, it doesn't check if the returned node is valid or not but will hard fail if the returned node is not valid. Callback also passes `self` as a second parameter. This is a context to the visitor object itself. | ||
|
||
There are also another utility function `compat`, attempts to provide conversion to the estree-compatble AST from SWC. Note this is the _closest attempt_ to generate compatible AST, likely will have some differences. | ||
|
||
```ts | ||
const { compat } = require('woodpile'); | ||
const { parseSync } = require('@swc/core'); | ||
|
||
const ast = parseSync('console.log("Hello, World!")'); | ||
|
||
const compat_ast = compat(ast, { | ||
source: "" // optional, original source code to the input ast | ||
flavor: "babel" | "acorn" // optional, default to babel | ||
}) | ||
``` |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
nightly-2023-07-03 |
Oops, something went wrong.