Skip to content

Commit 3e8d7ba

Browse files
committed
--wip-- [skip ci]
1 parent 7b2c86e commit 3e8d7ba

File tree

7 files changed

+38
-24
lines changed

7 files changed

+38
-24
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"rust-analyzer.check.command": "clippy",
2+
// "rust-analyzer.check.command": "clippy",
33
// rust-analyzer hack:
44
// manually set --workspace in extraArgs so that it's always set only one time
55
"rust-analyzer.check.workspace": false,

Cargo.lock

Lines changed: 1 addition & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/clarinet-cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ clarinet-format = { path = "../clarinet-format" }
3434
clarinet-deployments = { path = "../clarinet-deployments" }
3535
hiro-system-kit = { path = "../hiro-system-kit" }
3636
stacks-network = { path = "../stacks-network" }
37+
ts-to-clar = { path = "../ts-to-clar" }
3738

3839
[target.'cfg(unix)'.dependencies]
3940
nix = "0.29.0"

components/clarinet-cli/src/frontend/cli.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ enum Command {
9393
/// Subcommands for Devnet usage
9494
#[clap(subcommand, name = "devnet")]
9595
Devnet(Devnet),
96+
/// Subcommands ts-to-clar transpiler usage
97+
#[clap(name = "build", aliases = &["transpile"], bin_name = "build")]
98+
Build(Build),
9699
/// Get Clarity autocompletion and inline errors from your code editor (VSCode, vim, emacs, etc)
97100
#[clap(name = "lsp", bin_name = "lsp")]
98101
LSP,
@@ -104,6 +107,12 @@ enum Command {
104107
DAP,
105108
}
106109

110+
#[derive(Parser, PartialEq, Clone, Debug)]
111+
struct Build {
112+
#[clap(long = "path", short = 'p')]
113+
pub path: Option<String>,
114+
}
115+
107116
#[derive(Parser, PartialEq, Clone, Debug)]
108117
struct Formatter {
109118
#[clap(long = "manifest-path", short = 'm')]
@@ -1325,6 +1334,24 @@ pub fn main() {
13251334
}
13261335
Devnet::DevnetStart(cmd) => devnet_start(cmd, clarinetrc),
13271336
},
1337+
Command::Build(Build { path }) => {
1338+
let file_name = path.unwrap_or_else(|| {
1339+
eprintln!("No file path provided. Please specify a .clar.ts file.");
1340+
process::exit(1);
1341+
});
1342+
let file_path = std::path::Path::new(&file_name);
1343+
let extension = file_path.extension().unwrap_or_default();
1344+
assert_eq!(extension, "ts");
1345+
assert!(file_path.is_file());
1346+
assert!(file_name.ends_with(".clar.ts"));
1347+
1348+
let output_path = file_name.strip_suffix(".ts").unwrap();
1349+
1350+
let src = std::fs::read_to_string(file_path).unwrap();
1351+
let clarity_code = ts_to_clar::transpile(&file_name, &src).unwrap();
1352+
std::fs::write(output_path, clarity_code).unwrap();
1353+
println!("Transpiled {file_name} to {output_path}");
1354+
}
13281355
};
13291356
}
13301357

components/ts-to-clar/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ license = "GPL-3.0"
99
anyhow = "1.0.98"
1010

1111
oxc_allocator = "0.75"
12-
oxc_ast = { version = "0.75", features = ["serialize"] }
12+
oxc_ast = { version = "0.75" }
1313
oxc_ast_visit = "0.75"
1414
oxc_parser = "0.75"
1515
oxc_span = "0.75"
1616
oxc_syntax = "0.75"
17-
oxc_traverse = "0.75"
17+
oxc_traverse = { version = "0.75" }
1818
oxc_semantic = "0.75"
1919

2020
clarity = { workspace = true }

components/ts-to-clar/src/expression_converter.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ fn atom(name: &str) -> PreSymbolicExpression {
3333

3434
fn get_clarity_binary_operator(operator: &ast::BinaryOperator) -> &str {
3535
use ast::BinaryOperator::*;
36+
3637
match operator {
3738
Addition => "+",
3839
Subtraction => "-",

components/ts-to-clar/src/parser.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
use std::marker::PhantomData;
1111

12-
use anyhow::{anyhow, Result};
12+
use anyhow::{Result, anyhow};
1313
use oxc_allocator::{Allocator, CloneIn};
1414
use oxc_ast::ast::{
1515
self, Expression, Function, ObjectPropertyKind, Program, PropertyKey, Statement,
@@ -18,7 +18,7 @@ use oxc_ast::ast::{
1818
use oxc_parser::Parser;
1919
use oxc_semantic::SemanticBuilder;
2020
use oxc_span::SourceType;
21-
use oxc_traverse::{traverse_mut, Traverse};
21+
use oxc_traverse::{Traverse, traverse_mut};
2222

2323
use clarity::vm::types::TypeSignature;
2424

@@ -345,25 +345,25 @@ pub fn get_ir<'a>(allocator: &'a Allocator, file_name: &str, source: &'a str) ->
345345
mod test {
346346
use crate::{
347347
clarity_std::STD_PKG_NAME,
348-
parser::{get_ir, IRConstant, IRDataMap, IRDataVar, IR},
348+
parser::{IR, IRConstant, IRDataMap, IRDataVar, get_ir},
349349
types::{get_ascii_type, get_utf8_type},
350350
};
351351

352352
use clarity::vm::{
353+
ClarityName,
353354
types::{
354355
TupleTypeSignature,
355356
TypeSignature::{self, *},
356357
},
357-
ClarityName,
358358
};
359359
use indoc::{formatdoc, indoc};
360360
use oxc_allocator::{Allocator, Box, FromIn};
361361
use oxc_ast::{
362+
AstBuilder,
362363
ast::{
363364
BinaryOperator, Expression, NumberBase, ObjectPropertyKind, PropertyKey, PropertyKind,
364365
Statement,
365366
},
366-
AstBuilder,
367367
};
368368
use oxc_span::{Atom, Span};
369369

0 commit comments

Comments
 (0)