Skip to content

Commit

Permalink
Add TypeScript compile checks
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Nov 12, 2024
1 parent d91cd1b commit 4de3214
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 18 deletions.
21 changes: 12 additions & 9 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions packages/cw-schema-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ simple_logger = { version = "5.0.0", features = ["stderr"] }

[dev-dependencies]
insta = "1.40.0"
serde = { workspace = true, features = ["derive"] }
serde_json = "1.0.128"
tempfile = "3.14.0"
1 change: 0 additions & 1 deletion packages/cw-schema-codegen/src/typescript/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use self::template::{
EnumTemplate, EnumVariantTemplate, FieldTemplate, StructTemplate, TypeTemplate,
};
use heck::ToPascalCase;
use std::{borrow::Cow, io};

pub mod template;
Expand Down
58 changes: 53 additions & 5 deletions packages/cw-schema-codegen/tests/typescript.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
use cw_schema::Schemaifier;
use serde::Serialize;
use std::io::Write;

#[derive(Schemaifier)]
#[derive(Schemaifier, Serialize)]
struct Owo {
field_1: u32,
field_2: String,
}

#[derive(Schemaifier)]
#[derive(Schemaifier, Serialize)]
struct Uwu(String, u32);

#[derive(Schemaifier)]
#[derive(Schemaifier, Serialize)]
struct Òwó;

#[derive(Schemaifier)]
#[derive(Schemaifier, Serialize)]
enum Empty {}

#[derive(Schemaifier)]
#[derive(Schemaifier, Serialize)]
enum Hehehe {
A,
B(u32),
Expand Down Expand Up @@ -52,3 +54,49 @@ fn codegen_snap() {
insta::assert_snapshot!(output);
}
}

#[test]
fn assert_validity() {
let schemas = [
cw_schema::schema_of::<Owo>(),
cw_schema::schema_of::<Uwu>(),
cw_schema::schema_of::<Òwó>(),
cw_schema::schema_of::<Empty>(),
cw_schema::schema_of::<Hehehe>(),
];

for schema in schemas {
let cw_schema::Schema::V1(schema) = schema else {
unreachable!();
};

let output = schema
.definitions
.iter()
.map(|node| {
let mut buf = Vec::new();
cw_schema_codegen::typescript::process_node(&mut buf, &schema, node).unwrap();
String::from_utf8(buf).unwrap()
})
.collect::<String>();

let mut file = tempfile::NamedTempFile::with_suffix(".ts").unwrap();
file.write_all(output.as_bytes()).unwrap();
file.flush().unwrap();

let output = std::process::Command::new("npx")
.arg("--package=typescript")
.arg("--")
.arg("tsc")
.arg(file.path())
.output()
.unwrap();

assert!(
output.status.success(),
"stdout: {stdout}, stderr: {stderr}",
stdout = String::from_utf8_lossy(&output.stdout),
stderr = String::from_utf8_lossy(&output.stderr)
);
}
}
2 changes: 2 additions & 0 deletions packages/cw-schema-derive/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ fn expand_enum(mut meta: ContainerMeta, input: DataEnum) -> syn::Result<TokenStr
let (impl_generics, ty_generics, where_clause) = meta.generics.split_for_impl();

Ok(quote! {
#[automatically_derived]
impl #impl_generics #crate_path::Schemaifier for #name #ty_generics #where_clause {
fn visit_schema(visitor: &mut #crate_path::SchemaVisitor) -> #crate_path::DefinitionReference {
let node = #crate_path::Node {
Expand Down Expand Up @@ -461,6 +462,7 @@ fn expand_struct(mut meta: ContainerMeta, input: DataStruct) -> syn::Result<Toke
let (impl_generics, ty_generics, where_clause) = meta.generics.split_for_impl();

Ok(quote! {
#[automatically_derived]
impl #impl_generics #crate_path::Schemaifier for #name #ty_generics #where_clause {
fn visit_schema(visitor: &mut #crate_path::SchemaVisitor) -> #crate_path::DefinitionReference {
let node = {
Expand Down
10 changes: 7 additions & 3 deletions packages/cw-schema/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,16 @@ impl Identifier {
//
// Seems more robust than the previous implementation.
trait NonStaticAny {
fn get_type_id(&self) -> TypeId where Self: 'static;
fn get_type_id(&self) -> TypeId
where
Self: 'static;
}

impl<T: ?Sized> NonStaticAny for PhantomData<T> {
fn get_type_id(&self) -> TypeId where Self: 'static {
fn get_type_id(&self) -> TypeId
where
Self: 'static,
{
TypeId::of::<T>()
}
}
Expand Down Expand Up @@ -198,7 +203,6 @@ impl SchemaVisitor {
}

pub trait Schemaifier {
#[doc(hidden)]
fn id() -> Identifier {
Identifier::of::<Self>()
}
Expand Down

0 comments on commit 4de3214

Please sign in to comment.