Skip to content

Commit

Permalink
Merge pull request #1 from kwonoj/feat-impl
Browse files Browse the repository at this point in the history
feat(visit): initial impl
  • Loading branch information
kwonoj committed Jul 27, 2023
2 parents 3dabd28 + 15874c0 commit 7f9852b
Show file tree
Hide file tree
Showing 8 changed files with 467 additions and 24 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/ci_main.yml
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
/node_modules
/test/parser/**/_actual.json
*.log
/test/**/_actual.*
/test/**/_actual.*
/test.js
21 changes: 14 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
swc_core = { version = "0.79.28", features = ["ecma_visit", "ecma_visit_path", "ecma_ast_serde"] }
swc_core = { version = "0.79.28", features = [
"common",
"ecma_visit",
"ecma_visit_path",
"ecma_ast_serde",
] }
swc_estree_compat = { version = "0.187.26" }
swc_estree_ast = { version = "0.21.18" }

wasm-bindgen = {version = "0.2.87", features = [] }
serde = {version = "1.0.175", features = ["derive"]}
serde-wasm-bindgen = {version = "0.5.0"}
js-sys = {version = "0.3.64" }
paste = {version = "1.0.14" }
wasm-bindgen = { version = "0.2.87" }
serde = { version = "1.0.175", features = ["derive"] }
serde-wasm-bindgen = { version = "0.5.0" }
js-sys = { version = "0.3.64" }
paste = { version = "1.0.14" }

getrandom = {version = "0.2.10", features = ["js"] }
getrandom = { version = "0.2.10", features = ["js"] }
21 changes: 21 additions & 0 deletions LICENSE
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.
56 changes: 56 additions & 0 deletions README.md
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
})
```
26 changes: 21 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
{
"name": "woodpile",
"version": "0.0.1",
"description": "",
"main": "test.js",
"description": "SWC AST walker for Javascript",
"main": "pkg/woodpile.js",
"files": [
"pkg/*.wasm",
"pkg/*.js",
"pkg/*.d.ts",
"README.md",
"LICENSE"
],
"scripts": {
"build": "wasm-pack build --dev --target nodejs",
"prepublishOnly": "npm run build",
"build": "wasm-pack build --target nodejs",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/kwonoj/woodpile.git"
},
"keywords": [],
"author": "",
"keywords": [
"SWC",
"AST",
"estree",
"Babel",
"walker",
"visitor",
"swc_ecma_visit"
],
"author": "OJ Kwon <kwon.ohjoong@gmail.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/kwonoj/woodpile/issues"
Expand Down
1 change: 1 addition & 0 deletions rust-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nightly-2023-07-03

0 comments on commit 7f9852b

Please sign in to comment.