diff --git a/.changeset/chilled-ways-worry.md b/.changeset/chilled-ways-worry.md new file mode 100644 index 0000000000..bb52a2b38c --- /dev/null +++ b/.changeset/chilled-ways-worry.md @@ -0,0 +1,5 @@ +--- +"changelog": minor +--- + +provide API to list supported language versions diff --git a/crates/codegen/syntax/src/rust_lib_code_generator.rs b/crates/codegen/syntax/src/rust_lib_code_generator.rs index fc9a5a24d6..96553caa16 100644 --- a/crates/codegen/syntax/src/rust_lib_code_generator.rs +++ b/crates/codegen/syntax/src/rust_lib_code_generator.rs @@ -69,9 +69,10 @@ impl CodeGenerator { InvalidProductionVersion(ProductionKind), }} + {versions_array} + impl Language {{ pub fn new(version: Version) -> Result {{ - {versions_array} if VERSIONS.contains(&version.to_string().as_str()) {{ Ok(Self {{ {version_flag_initializers}, @@ -86,6 +87,10 @@ impl CodeGenerator { &self.version }} + pub fn supported_versions() -> Vec {{ + return VERSIONS.iter().map(|v| Version::parse(v).unwrap()).collect(); + }} + pub fn parse(&self, production_kind: ProductionKind, input: &str) -> Result {{ let output = match production_kind {{ {scanner_invocations}, @@ -107,7 +112,7 @@ impl CodeGenerator { language_title = &language.title, versions_array = { let versions = language.versions.iter().map(|v| v.to_string()); - quote! { static VERSIONS: &'static [&'static str] = &[ #(#versions),* ]; } + quote! { const VERSIONS: &'static [&'static str] = &[ #(#versions),* ]; } }, version_flag_initializers = self.version_flag_initializers(), scanner_invocations = self.scanner_invocations(), diff --git a/crates/codegen/syntax/src/typescript_lib_code_generator.rs b/crates/codegen/syntax/src/typescript_lib_code_generator.rs index f6d1754c79..2e32430bcd 100644 --- a/crates/codegen/syntax/src/typescript_lib_code_generator.rs +++ b/crates/codegen/syntax/src/typescript_lib_code_generator.rs @@ -80,11 +80,12 @@ impl CodeGenerator { }} }} + {versions_array} + #[napi] impl Language {{ #[napi(constructor)] pub fn new(version: String) -> Result {{ - {versions_array} let version = Version::parse(&version).map_err(|_| Error::InvalidSemanticVersion(version))?; if VERSIONS.contains(&version.to_string().as_str()) {{ Ok(Self {{ @@ -101,6 +102,11 @@ impl CodeGenerator { self.version.to_string() }} + #[napi] + pub fn supported_versions() -> Vec {{ + return VERSIONS.iter().map(|v| v.to_string()).collect(); + }} + #[napi] pub fn parse(&self, production_kind: ProductionKind, input: String) -> Result {{ let input = input.as_str(); @@ -121,7 +127,7 @@ impl CodeGenerator { version_flag_declarations = self.version_flag_declarations(), versions_array = { let versions = language.versions.iter().map(|v| v.to_string()); - quote! { static VERSIONS: &'static [&'static str] = &[ #(#versions),* ]; } + quote! { const VERSIONS: &'static [&'static str] = &[ #(#versions),* ]; } }, version_flag_initializers = self.version_flag_initializers(), language_title = &language.title, diff --git a/crates/solidity/outputs/cargo/crate/src/generated/language.rs b/crates/solidity/outputs/cargo/crate/src/generated/language.rs index f1239e90a5..c04ad84af0 100644 --- a/crates/solidity/outputs/cargo/crate/src/generated/language.rs +++ b/crates/solidity/outputs/cargo/crate/src/generated/language.rs @@ -349,19 +349,19 @@ pub enum Error { InvalidProductionVersion(ProductionKind), } +const VERSIONS: &'static [&'static str] = &[ + "0.4.11", "0.4.12", "0.4.13", "0.4.14", "0.4.15", "0.4.16", "0.4.17", "0.4.18", "0.4.19", + "0.4.20", "0.4.21", "0.4.22", "0.4.23", "0.4.24", "0.4.25", "0.4.26", "0.5.0", "0.5.1", + "0.5.2", "0.5.3", "0.5.4", "0.5.5", "0.5.6", "0.5.7", "0.5.8", "0.5.9", "0.5.10", "0.5.11", + "0.5.12", "0.5.13", "0.5.14", "0.5.15", "0.5.16", "0.5.17", "0.6.0", "0.6.1", "0.6.2", "0.6.3", + "0.6.4", "0.6.5", "0.6.6", "0.6.7", "0.6.8", "0.6.9", "0.6.10", "0.6.11", "0.6.12", "0.7.0", + "0.7.1", "0.7.2", "0.7.3", "0.7.4", "0.7.5", "0.7.6", "0.8.0", "0.8.1", "0.8.2", "0.8.3", + "0.8.4", "0.8.5", "0.8.6", "0.8.7", "0.8.8", "0.8.9", "0.8.10", "0.8.11", "0.8.12", "0.8.13", + "0.8.14", "0.8.15", "0.8.16", "0.8.17", "0.8.18", "0.8.19", +]; + impl Language { pub fn new(version: Version) -> Result { - static VERSIONS: &'static [&'static str] = &[ - "0.4.11", "0.4.12", "0.4.13", "0.4.14", "0.4.15", "0.4.16", "0.4.17", "0.4.18", - "0.4.19", "0.4.20", "0.4.21", "0.4.22", "0.4.23", "0.4.24", "0.4.25", "0.4.26", - "0.5.0", "0.5.1", "0.5.2", "0.5.3", "0.5.4", "0.5.5", "0.5.6", "0.5.7", "0.5.8", - "0.5.9", "0.5.10", "0.5.11", "0.5.12", "0.5.13", "0.5.14", "0.5.15", "0.5.16", - "0.5.17", "0.6.0", "0.6.1", "0.6.2", "0.6.3", "0.6.4", "0.6.5", "0.6.6", "0.6.7", - "0.6.8", "0.6.9", "0.6.10", "0.6.11", "0.6.12", "0.7.0", "0.7.1", "0.7.2", "0.7.3", - "0.7.4", "0.7.5", "0.7.6", "0.8.0", "0.8.1", "0.8.2", "0.8.3", "0.8.4", "0.8.5", - "0.8.6", "0.8.7", "0.8.8", "0.8.9", "0.8.10", "0.8.11", "0.8.12", "0.8.13", "0.8.14", - "0.8.15", "0.8.16", "0.8.17", "0.8.18", "0.8.19", - ]; if VERSIONS.contains(&version.to_string().as_str()) { Ok(Self { version_is_equal_to_or_greater_than_0_4_21: Version::parse("0.4.21").unwrap() @@ -401,6 +401,13 @@ impl Language { &self.version } + pub fn supported_versions() -> Vec { + return VERSIONS + .iter() + .map(|v| Version::parse(v).unwrap()) + .collect(); + } + pub fn parse( &self, production_kind: ProductionKind, diff --git a/crates/solidity/outputs/cargo/tests/src/lib.rs b/crates/solidity/outputs/cargo/tests/src/lib.rs index 36a82f73e2..b1e5e569ef 100644 --- a/crates/solidity/outputs/cargo/tests/src/lib.rs +++ b/crates/solidity/outputs/cargo/tests/src/lib.rs @@ -2,3 +2,5 @@ mod cst_output; #[cfg(test)] mod errors; +#[cfg(test)] +mod versions; diff --git a/crates/solidity/outputs/cargo/tests/src/versions/mod.rs b/crates/solidity/outputs/cargo/tests/src/versions/mod.rs new file mode 100644 index 0000000000..a8a74e1ef9 --- /dev/null +++ b/crates/solidity/outputs/cargo/tests/src/versions/mod.rs @@ -0,0 +1,11 @@ +use semver::Version; +use slang_solidity::language::Language; + +#[test] +fn list_supported_versions() { + let versions = Language::supported_versions(); + + assert_eq!(false, versions.is_empty()); + assert_eq!(false, versions.contains(&Version::parse("0.0.0").unwrap())); + assert_eq!(true, versions.contains(&Version::parse("0.4.11").unwrap())); +} diff --git a/crates/solidity/outputs/npm/crate/src/generated/language.rs b/crates/solidity/outputs/npm/crate/src/generated/language.rs index ccd9137a89..e82761c8bd 100644 --- a/crates/solidity/outputs/npm/crate/src/generated/language.rs +++ b/crates/solidity/outputs/npm/crate/src/generated/language.rs @@ -360,21 +360,21 @@ impl From for napi::Error { } } +const VERSIONS: &'static [&'static str] = &[ + "0.4.11", "0.4.12", "0.4.13", "0.4.14", "0.4.15", "0.4.16", "0.4.17", "0.4.18", "0.4.19", + "0.4.20", "0.4.21", "0.4.22", "0.4.23", "0.4.24", "0.4.25", "0.4.26", "0.5.0", "0.5.1", + "0.5.2", "0.5.3", "0.5.4", "0.5.5", "0.5.6", "0.5.7", "0.5.8", "0.5.9", "0.5.10", "0.5.11", + "0.5.12", "0.5.13", "0.5.14", "0.5.15", "0.5.16", "0.5.17", "0.6.0", "0.6.1", "0.6.2", "0.6.3", + "0.6.4", "0.6.5", "0.6.6", "0.6.7", "0.6.8", "0.6.9", "0.6.10", "0.6.11", "0.6.12", "0.7.0", + "0.7.1", "0.7.2", "0.7.3", "0.7.4", "0.7.5", "0.7.6", "0.8.0", "0.8.1", "0.8.2", "0.8.3", + "0.8.4", "0.8.5", "0.8.6", "0.8.7", "0.8.8", "0.8.9", "0.8.10", "0.8.11", "0.8.12", "0.8.13", + "0.8.14", "0.8.15", "0.8.16", "0.8.17", "0.8.18", "0.8.19", +]; + #[napi] impl Language { #[napi(constructor)] pub fn new(version: String) -> Result { - static VERSIONS: &'static [&'static str] = &[ - "0.4.11", "0.4.12", "0.4.13", "0.4.14", "0.4.15", "0.4.16", "0.4.17", "0.4.18", - "0.4.19", "0.4.20", "0.4.21", "0.4.22", "0.4.23", "0.4.24", "0.4.25", "0.4.26", - "0.5.0", "0.5.1", "0.5.2", "0.5.3", "0.5.4", "0.5.5", "0.5.6", "0.5.7", "0.5.8", - "0.5.9", "0.5.10", "0.5.11", "0.5.12", "0.5.13", "0.5.14", "0.5.15", "0.5.16", - "0.5.17", "0.6.0", "0.6.1", "0.6.2", "0.6.3", "0.6.4", "0.6.5", "0.6.6", "0.6.7", - "0.6.8", "0.6.9", "0.6.10", "0.6.11", "0.6.12", "0.7.0", "0.7.1", "0.7.2", "0.7.3", - "0.7.4", "0.7.5", "0.7.6", "0.8.0", "0.8.1", "0.8.2", "0.8.3", "0.8.4", "0.8.5", - "0.8.6", "0.8.7", "0.8.8", "0.8.9", "0.8.10", "0.8.11", "0.8.12", "0.8.13", "0.8.14", - "0.8.15", "0.8.16", "0.8.17", "0.8.18", "0.8.19", - ]; let version = Version::parse(&version).map_err(|_| Error::InvalidSemanticVersion(version))?; if VERSIONS.contains(&version.to_string().as_str()) { @@ -417,6 +417,11 @@ impl Language { self.version.to_string() } + #[napi] + pub fn supported_versions() -> Vec { + return VERSIONS.iter().map(|v| v.to_string()).collect(); + } + #[napi] pub fn parse( &self, diff --git a/crates/solidity/outputs/npm/package/src/generated/index.d.ts b/crates/solidity/outputs/npm/package/src/generated/index.d.ts index 3842a0c43c..a36f6d10a9 100644 --- a/crates/solidity/outputs/npm/package/src/generated/index.d.ts +++ b/crates/solidity/outputs/npm/package/src/generated/index.d.ts @@ -676,6 +676,7 @@ export class TokenNode { export class Language { constructor(version: string); get version(): string; + static supportedVersions(): Array; parse(productionKind: ProductionKind, input: string): ParseOutput; } export class ParseOutput { diff --git a/crates/solidity/outputs/npm/tests/src/versions.ts b/crates/solidity/outputs/npm/tests/src/versions.ts new file mode 100644 index 0000000000..fa6f642fb0 --- /dev/null +++ b/crates/solidity/outputs/npm/tests/src/versions.ts @@ -0,0 +1,10 @@ +import test from "ava"; +import { Language } from "@nomicfoundation/slang/language"; + +test("list supported versions", (t) => { + const versions = Language.supportedVersions(); + + t.true(versions.length > 0); + t.true(versions.includes("0.4.11")); + t.false(versions.includes("0.0.0")); +});