Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
49fa04a
Flatten Spans on regex AST nodes
ottomated Oct 9, 2024
7fc8653
Merge branch 'main' of https://github.com/ottomated/oxc
ottomated Oct 9, 2024
0515e43
create estree derive generator
ottomated Oct 9, 2024
3084869
Merge branch 'main' into manual-serialize
ottomated Oct 10, 2024
149e86e
begin parse #[serde(...)] attributes
ottomated Oct 10, 2024
9e2266b
generate_derive on EVERYTHING
ottomated Oct 10, 2024
a478dea
Finished
ottomated Oct 10, 2024
d989448
comment on ESTree trait
ottomated Oct 10, 2024
c0c960e
camelCase fields
ottomated Oct 10, 2024
efbdae6
remove comments
ottomated Oct 10, 2024
6462e6b
Merge branch 'main' into manual-serialize
ottomated Oct 10, 2024
fb02a4c
[autofix.ci] apply automated fixes
autofix-ci[bot] Oct 10, 2024
0b6732b
regenerate
ottomated Oct 10, 2024
c151467
Merge branch 'manual-serialize' of github.com:ottomated/oxc into manu…
ottomated Oct 10, 2024
6f9857e
Merge branch 'main' of https://github.com/oxc-project/oxc
ottomated Oct 11, 2024
69884df
begin tsify work
ottomated Oct 11, 2024
c859d86
handle some macro attributes
ottomated Oct 15, 2024
6246824
flatten fields
ottomated Oct 16, 2024
08c3731
Use wasm-bindgen for now
ottomated Oct 16, 2024
6db2a1c
no type on Span
ottomated Oct 17, 2024
bc66fa9
Merge branch 'main' of https://github.com/oxc-project/oxc
ottomated Oct 17, 2024
724e87b
Merge branch 'main' into manual-serialize
ottomated Oct 17, 2024
422e4dc
remove tsify from ast features
ottomated Oct 17, 2024
284d736
remove tsify dep from oxc_syntax
ottomated Oct 17, 2024
9e4332f
[autofix.ci] apply automated fixes
autofix-ci[bot] Oct 17, 2024
544dead
add CompactStr type
ottomated Oct 17, 2024
9e22bfe
fix: restore `features = ["derive"]` for `serde` in `oxc_ast` crate
overlookmotel Oct 19, 2024
5065bf3
style: restore `Cargo.toml` formatting
overlookmotel Oct 19, 2024
6a3fb3e
style: order `#[generate_derive]` after `#[derive]`
overlookmotel Oct 19, 2024
6db7396
style: fix indentation
overlookmotel Oct 19, 2024
566eb50
refactor: remove unused `TypescriptGenerator`
overlookmotel Oct 19, 2024
5f9d398
refactor: revert `GeneratorOutput`
overlookmotel Oct 19, 2024
4d71118
fix: correct comments
overlookmotel Oct 19, 2024
b78e854
fix: `type` field in TS defs on structs with `#[estree(custom_seriali…
overlookmotel Oct 19, 2024
7639831
fix: use `| null` to match other type defs for `FormalParameterRest`
overlookmotel Oct 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
begin tsify work
  • Loading branch information
ottomated committed Oct 11, 2024
commit 69884df825e9519ba6f1b950e2b5743731fbfb8d
1 change: 1 addition & 0 deletions .github/.generated_ast_watch_list.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ src:
- 'crates/oxc_ast/src/generated/ast_builder.rs'
- 'crates/oxc_ast/src/generated/visit.rs'
- 'crates/oxc_ast/src/generated/visit_mut.rs'
- 'npm/oxc-types/src/generated/types.d.ts'
- 'tasks/ast_tools/src/**'
- '.github/.generated_ast_watch_list.yml'
4 changes: 4 additions & 0 deletions Cargo.lock

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

577 changes: 577 additions & 0 deletions npm/oxc-types/src/generated/types.d.ts

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions tasks/ast_tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ bpaf = { workspace = true, features = ["autocomplete", "bright-color", "derive"]
convert_case = { workspace = true }
itertools = { workspace = true }
lazy_static = { workspace = true }
oxc_allocator.workspace = true
oxc_parser.workspace = true
oxc_prettier.workspace = true
oxc_span.workspace = true
prettyplease = { workspace = true }
proc-macro2 = { workspace = true }
quote = { workspace = true }
Expand Down
5 changes: 4 additions & 1 deletion tasks/ast_tools/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ impl From<(PathBuf, TokenStream)> for SideEffect {

impl From<GeneratorOutput> for SideEffect {
fn from(output: GeneratorOutput) -> Self {
Self::from((output.0, output.1))
match output {
GeneratorOutput::Rust(path, stream) => Self::from((path, stream)),
GeneratorOutput::Raw(path, content) => Self(path, content.into()),
}
}
}

Expand Down
10 changes: 0 additions & 10 deletions tasks/ast_tools/src/derives/estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,4 @@ fn serialize_enum(def: &EnumDef) -> TokenStream {
}
}
}
// if def.markers.estree.untagged {
// let match_branches = def.variants.iter().map(|var| {
// let var_ident= var.ident();
// var.fields
// quote! {
// #ident::#var_ident()
// }
// })
// }
// def.markers.estree.
}
2 changes: 1 addition & 1 deletion tasks/ast_tools/src/generators/assert_layouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Generator for AssertLayouts {

let header = generated_header!();

GeneratorOutput(
GeneratorOutput::Rust(
output(crate::AST_CRATE, "assert_layouts.rs"),
quote! {
#header
Expand Down
2 changes: 1 addition & 1 deletion tasks/ast_tools/src/generators/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Generator for AstBuilderGenerator {

let header = generated_header!();

GeneratorOutput(
GeneratorOutput::Rust(
output(crate::AST_CRATE, "ast_builder.rs"),
quote! {
#header
Expand Down
2 changes: 1 addition & 1 deletion tasks/ast_tools/src/generators/ast_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ impl Generator for AstKindGenerator {

let header = generated_header!();

GeneratorOutput(
GeneratorOutput::Rust(
output(crate::AST_CRATE, "ast_kind.rs"),
quote! {
#header
Expand Down
7 changes: 6 additions & 1 deletion tasks/ast_tools/src/generators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ use crate::codegen::LateCtx;
mod assert_layouts;
mod ast_builder;
mod ast_kind;
mod typescript;
mod visit;

pub use assert_layouts::AssertLayouts;
pub use ast_builder::AstBuilderGenerator;
pub use ast_kind::AstKindGenerator;
pub use typescript::TypescriptGenerator;
pub use visit::{VisitGenerator, VisitMutGenerator};

/// Inserts a newline in the `TokenStream`.
Expand All @@ -27,7 +29,10 @@ pub trait Generator {
}

#[derive(Debug, Clone)]
pub struct GeneratorOutput(/* output path */ pub PathBuf, pub TokenStream);
pub enum GeneratorOutput {
Rust(/* output path */ PathBuf, TokenStream),
Raw(/* output path */ PathBuf, String),
}

macro_rules! define_generator {
($vis:vis struct $ident:ident $($lifetime:lifetime)? $($rest:tt)*) => {
Expand Down
94 changes: 94 additions & 0 deletions tasks/ast_tools/src/generators/typescript.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use itertools::Itertools;
use oxc_allocator::Allocator;
use oxc_parser::{ParseOptions, Parser};
use oxc_prettier::{Prettier, PrettierOptions, TrailingComma};
use oxc_span::SourceType;

use super::define_generator;
use crate::{
codegen::LateCtx,
output,
schema::{EnumDef, GetIdent, StructDef, TypeDef, TypeName},
Generator, GeneratorOutput,
};

define_generator! {
pub struct TypescriptGenerator;
}

impl Generator for TypescriptGenerator {
fn generate(&mut self, ctx: &LateCtx) -> GeneratorOutput {
let file = file!().replace('\\', "/");
let mut contents = format!(
"\
// To edit this generated file you have to edit `{file}`\n\
// Auto-generated code, DO NOT EDIT DIRECTLY!\n\n\
type bool = boolean;type f64 = number;type str = string;type Atom = string;type u32 = number;type u64 = number;\n\n"
);

for def in ctx.schema() {
let type_def = match def {
TypeDef::Struct(it) => generate_struct(it),
TypeDef::Enum(it) => generate_enum(it),
};

let ident = def.ident();
contents.push_str(&format!("export type {ident} = {type_def}\n\n",));
}

GeneratorOutput::Raw(output(crate::TYPESCRIPT_PACKAGE, "types.d.ts"), contents)
}
}

fn generate_enum(def: &EnumDef) -> String {
if def.markers.estree.untagged {
def.all_variants().map(|v| v.ident().to_string()).join(" | ")
} else {
def.all_variants().map(|v| format!("'{}'", v.ident().to_string())).join(" | ")
}
}

fn generate_struct(def: &StructDef) -> String {
let mut type_def = "{".to_string();
for field in &def.fields {
let name = field.ident().expect("expected named field!").to_string();
let name = name.strip_prefix("r#").map(ToString::to_string).unwrap_or(name);
let ty = type_to_string(field.typ.name());
type_def.push_str(&format!("{name}: {ty};"));
}
type_def.push('}');
type_def
}

fn type_to_string(ty: &TypeName) -> String {
match ty {
TypeName::Ident(ident) => ident.to_string(),
TypeName::Vec(type_name) => format!("Array<{}>", type_to_string(type_name)),
TypeName::Box(type_name) | TypeName::Ref(type_name) | TypeName::Complex(type_name) => {
type_to_string(type_name)
}
TypeName::Opt(type_name) => format!("({}) | null", type_to_string(type_name)),
}
}

/// Unusable until oxc_prettier supports comments
#[allow(dead_code)]
fn format_typescript(source_text: &str) -> String {
let allocator = Allocator::default();
let source_type = SourceType::ts();
let ret = Parser::new(&allocator, source_text, source_type)
.with_options(ParseOptions { preserve_parens: false, ..ParseOptions::default() })
.parse();
Prettier::new(
&allocator,
source_text,
ret.trivias,
PrettierOptions {
semi: true,
trailing_comma: TrailingComma::All,
single_quote: true,
..PrettierOptions::default()
},
)
.build(&ret.program)
}
4 changes: 2 additions & 2 deletions tasks/ast_tools/src/generators/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ define_generator! {

impl Generator for VisitGenerator {
fn generate(&mut self, ctx: &LateCtx) -> GeneratorOutput {
GeneratorOutput(output(crate::AST_CRATE, "visit.rs"), generate_visit::<false>(ctx))
GeneratorOutput::Rust(output(crate::AST_CRATE, "visit.rs"), generate_visit::<false>(ctx))
}
}

impl Generator for VisitMutGenerator {
fn generate(&mut self, ctx: &LateCtx) -> GeneratorOutput {
GeneratorOutput(output(crate::AST_CRATE, "visit_mut.rs"), generate_visit::<true>(ctx))
GeneratorOutput::Rust(output(crate::AST_CRATE, "visit_mut.rs"), generate_visit::<true>(ctx))
}
}

Expand Down
4 changes: 3 additions & 1 deletion tasks/ast_tools/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use derives::{
use fmt::cargo_fmt;
use generators::{
AssertLayouts, AstBuilderGenerator, AstKindGenerator, Generator, GeneratorOutput,
VisitGenerator, VisitMutGenerator,
TypescriptGenerator, VisitGenerator, VisitMutGenerator,
};
use passes::{CalcLayout, Linker};
use util::{write_all_to, NormalizeError};
Expand All @@ -42,6 +42,7 @@ static SOURCE_PATHS: &[&str] = &[
];

const AST_CRATE: &str = "crates/oxc_ast";
const TYPESCRIPT_PACKAGE: &str = "npm/oxc-types";

type Result<R> = std::result::Result<R, String>;
type TypeId = usize;
Expand Down Expand Up @@ -83,6 +84,7 @@ fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
.generate(AstBuilderGenerator)
.generate(VisitGenerator)
.generate(VisitMutGenerator)
.generate(TypescriptGenerator)
.run()?;

if !cli_options.dry_run {
Expand Down