From 4de3214de7eb1cd3fd9976d97ef1e8103a0e3d90 Mon Sep 17 00:00:00 2001 From: Aumetra Weisman Date: Tue, 12 Nov 2024 15:59:40 +0100 Subject: [PATCH] Add TypeScript compile checks --- Cargo.lock | 21 ++++--- packages/cw-schema-codegen/Cargo.toml | 3 + .../cw-schema-codegen/src/typescript/mod.rs | 1 - .../cw-schema-codegen/tests/typescript.rs | 58 +++++++++++++++++-- packages/cw-schema-derive/src/expand.rs | 2 + packages/cw-schema/src/lib.rs | 10 +++- 6 files changed, 77 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8549fb989..8ef3cf348 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -950,8 +950,10 @@ dependencies = [ "heck", "insta", "log", + "serde", "serde_json", "simple_logger", + "tempfile", ] [[package]] @@ -1282,9 +1284,9 @@ checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" [[package]] name = "fastrand" -version = "2.1.0" +version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a" +checksum = "486f806e73c5707928240ddc295403b1b93c96a02038563881c4a2fd84b81ac4" [[package]] name = "ff" @@ -1659,9 +1661,9 @@ checksum = "884e2677b40cc8c339eaefcb701c32ef1fd2493d71118dc0ca4b6a736c93bd67" [[package]] name = "libc" -version = "0.2.155" +version = "0.2.162" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" +checksum = "18d287de67fe55fd7e1581fe933d965a5a9477b38e949cfa9f8574ef01506398" [[package]] name = "libm" @@ -2328,9 +2330,9 @@ dependencies = [ [[package]] name = "rustix" -version = "0.38.34" +version = "0.38.40" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f" +checksum = "99e4ea3e1cdc4b559b8e5650f9c8e5998e3e5c1343b4eaf034565f32318d63c0" dependencies = [ "bitflags 2.5.0", "errno", @@ -2707,14 +2709,15 @@ checksum = "e1fc403891a21bcfb7c37834ba66a547a8f402146eba7265b5a6d88059c9ff2f" [[package]] name = "tempfile" -version = "3.10.1" +version = "3.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1" +checksum = "28cce251fcbc87fac86a866eeb0d6c2d536fc16d06f184bb61aeae11aa4cee0c" dependencies = [ "cfg-if", "fastrand", + "once_cell", "rustix", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] diff --git a/packages/cw-schema-codegen/Cargo.toml b/packages/cw-schema-codegen/Cargo.toml index afc6d6987..6f9e6c7d0 100644 --- a/packages/cw-schema-codegen/Cargo.toml +++ b/packages/cw-schema-codegen/Cargo.toml @@ -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" diff --git a/packages/cw-schema-codegen/src/typescript/mod.rs b/packages/cw-schema-codegen/src/typescript/mod.rs index 74f3d9e00..fba8e3e7a 100644 --- a/packages/cw-schema-codegen/src/typescript/mod.rs +++ b/packages/cw-schema-codegen/src/typescript/mod.rs @@ -1,7 +1,6 @@ use self::template::{ EnumTemplate, EnumVariantTemplate, FieldTemplate, StructTemplate, TypeTemplate, }; -use heck::ToPascalCase; use std::{borrow::Cow, io}; pub mod template; diff --git a/packages/cw-schema-codegen/tests/typescript.rs b/packages/cw-schema-codegen/tests/typescript.rs index 11cb4fff4..c19fd9d07 100644 --- a/packages/cw-schema-codegen/tests/typescript.rs +++ b/packages/cw-schema-codegen/tests/typescript.rs @@ -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), @@ -52,3 +54,49 @@ fn codegen_snap() { insta::assert_snapshot!(output); } } + +#[test] +fn assert_validity() { + let schemas = [ + cw_schema::schema_of::(), + cw_schema::schema_of::(), + cw_schema::schema_of::<Òwó>(), + cw_schema::schema_of::(), + cw_schema::schema_of::(), + ]; + + 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::(); + + 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) + ); + } +} diff --git a/packages/cw-schema-derive/src/expand.rs b/packages/cw-schema-derive/src/expand.rs index a06646138..e724797fb 100644 --- a/packages/cw-schema-derive/src/expand.rs +++ b/packages/cw-schema-derive/src/expand.rs @@ -375,6 +375,7 @@ fn expand_enum(mut meta: ContainerMeta, input: DataEnum) -> syn::Result #crate_path::DefinitionReference { let node = #crate_path::Node { @@ -461,6 +462,7 @@ fn expand_struct(mut meta: ContainerMeta, input: DataStruct) -> syn::Result #crate_path::DefinitionReference { let node = { diff --git a/packages/cw-schema/src/lib.rs b/packages/cw-schema/src/lib.rs index b0e80c2c9..0e1c2c339 100644 --- a/packages/cw-schema/src/lib.rs +++ b/packages/cw-schema/src/lib.rs @@ -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 NonStaticAny for PhantomData { - fn get_type_id(&self) -> TypeId where Self: 'static { + fn get_type_id(&self) -> TypeId + where + Self: 'static, + { TypeId::of::() } } @@ -198,7 +203,6 @@ impl SchemaVisitor { } pub trait Schemaifier { - #[doc(hidden)] fn id() -> Identifier { Identifier::of::() }