tsync/to_typescript/consts.rs
1use syn::__private::ToTokens;
2
3use crate::{utils, BuildState};
4
5impl super::ToTypescript for syn::ItemConst {
6 fn convert_to_ts(self, state: &mut BuildState, config: &crate::BuildSettings) {
7 // ignore if we aren't in a type interface
8 if config.uses_type_interface {
9 return;
10 }
11
12 // this currently only supports literals
13 // e.g. const NAME: [type_ignored] = 0
14 // e.g. const NAME: [type_ignored] = "some_string"
15 // e.g. const NAME: [type_ignored] = serde_json::json!({ "I am valid": "json with no free variables" })
16 // however doesn't enforce that the json! macro contains no variables.
17 // if your lucky you might have also tsynced them but otherwise you will get a typescript error.
18
19 let name = self.ident.to_string();
20 let body = match self.expr.as_ref() {
21 syn::Expr::Lit(literal) => {
22 // convert it directly to a string to put in TS.
23 Some(literal.to_token_stream().to_string())
24 }
25 syn::Expr::Macro(mcr) => {
26 if mcr
27 .mac
28 .path
29 .segments
30 .iter()
31 .any(|x| x.to_token_stream().to_string() == "json")
32 {
33 Some(mcr.mac.tokens.to_string())
34 } else {
35 None
36 }
37 }
38 _ => None,
39 };
40 match body {
41 Some(body) => {
42 state.types.push('\n');
43 let comments = utils::get_comments(self.attrs);
44 state.write_comments(&comments, 0);
45 state
46 .types
47 .push_str(&format!("export const {} = {};", name, body));
48 state.types.push('\n');
49 }
50 _ => {
51 if crate::DEBUG.try_get().is_some_and(|d| *d) {
52 println!("#[tsync] failed for const {}", self.to_token_stream());
53 }
54 }
55 }
56 }
57}