# uast-javascript
JavaScript read with [swc](https://swc.rs/)'s parser into swc's own module
AST. `JavaScriptParser::parse` reads the source as an ECMAScript module with
JSX enabled and returns a `JavaScriptAst` holding the `swc_ecma_ast::Module`,
or every error the parser reported. The AST is the language's own shape, not
the uast spine: it keeps swc's spans but not comments or whitespace, so it
does not reconstruct the source.
For JavaScript in the spine (a lossless `Tree` with the vocabulary the
TypeScript dialect shares), use `parse_javascript` from
[uast-treesitter](https://crates.io/crates/uast-treesitter). The TypeScript
counterpart of this crate is
[uast-typescript](https://crates.io/crates/uast-typescript).
## What it reads
- swc_ecma_parser 45 with `EsSyntax { jsx: true }` and the parser's other
defaults: the latest ECMAScript syntax swc reads, plus JSX.
- Always a module (`parse_module`), so `import` and `export` are accepted
and the body is read in strict mode.
- Any error is an `Err(String)`: a fatal one alone, or the recoverable
errors swc collected, one per line, each in its `Debug` form.
- `JavaScriptAst` implements serde's `Serialize` and `Deserialize` (swc's
`serde-impl`), and round-trips through JSON.
## Usage
```toml
[dependencies]
uast-javascript = "0.1"
```
```rust
use uast_javascript::JavaScriptParser;
# fn main() -> Result<(), Box<dyn std::error::Error>> {
let source = "import { h } from './h.js';\nexport const view = <p>{h(1)}</p>;\n";
let ast = JavaScriptParser::parse(source)?;
assert_eq!(ast.module.body.len(), 2);
assert!(JavaScriptParser::parse("const = 1;").is_err());
// A module is strict mode code.
assert!(JavaScriptParser::parse("with (o) {}").is_err());
# Ok(())
# }
```
To match on the AST, depend on the same `swc_ecma_ast` (29) the crate does.
## Through `uast-grammar`
The `javascript` feature of
[uast-grammar](https://crates.io/crates/uast-grammar), off by default,
re-exports `uast_grammar::{JavaScriptAst, JavaScriptParser}`. The default
`treesitter` feature gives the spine reader, `uast_grammar::parse_javascript`.
## License
MIT. See [LICENSE](LICENSE).