Skip to content

Commit 6b7652a

Browse files
Add frontend module (#69)
1 parent 1002587 commit 6b7652a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2471
-979
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## Unreleased
44

5+
* refactor darklua frontend ([#69](https://github.com/seaofvoices/darklua/pull/69)):
6+
* the `--config-path` argument of the `minify` command was removed
7+
* the configuration file does not accept the `column_span` field anymore (use the [`generator` field](https://darklua.com/docs/generators/) instead)
8+
* darklua can now also read `.luau` files
9+
510
## 0.8.0
611

712
* update configuration file ([!60](https://gitlab.com/seaofvoices/darklua/-/merge_requests/60))

Cargo.lock

+127-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ structopt = "0.3.25"
3030
full_moon = { version = "0.15.1", features = ["roblox"] }
3131
serde = { version = "1.0", features = ["derive"] }
3232
json5 = "0.4"
33+
elsa = "1.7.0"
3334

3435
[dev-dependencies]
3536
assert_cmd = "2.0.4"
@@ -41,6 +42,10 @@ rand_distr = "0.2.2"
4142
regex = "1.5.5"
4243
tempfile = "3.3.0"
4344

45+
[target.'cfg(target_arch = "wasm32")'.dependencies]
46+
node-sys = "0.4.2"
47+
web-sys = { version = "0.3.60", features = ["Window", "Performance"] }
48+
4449
# This is needed because when runnin `cargo test`, the library and its
4550
# dependencies are build with the `dev` profile. To make sure full_moon
4651
# does not stack overflow when parsing complex code, it needs to be compiled

site/darklua-wasm/src/lib.rs

+23-41
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
mod utils;
22

3-
use darklua_core::{
4-
generator::{LuaGenerator, TokenBasedLuaGenerator},
5-
nodes::Block,
6-
rules::{self, get_default_rules, Context, Rule},
7-
Parser,
8-
};
9-
use serde::{Deserialize, Serialize};
3+
use darklua_core::{Configuration, Options, Resources};
104
use utils::set_panic_hook;
115
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
126

@@ -16,26 +10,12 @@ use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
1610
#[global_allocator]
1711
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
1812

19-
#[derive(Serialize, Deserialize)]
20-
pub struct Config {
21-
#[serde(default = "get_default_rules")]
22-
pub rules: Vec<Box<dyn Rule>>,
23-
}
24-
25-
fn generate_code(original_code: &str, block: &Block) -> String {
26-
let mut generator = TokenBasedLuaGenerator::new(original_code);
27-
generator.write_block(block);
28-
generator.into_string()
29-
}
30-
3113
#[wasm_bindgen]
3214
pub fn process_code(code: &str, opt_config: JsValue) -> Result<String, JsValue> {
3315
set_panic_hook();
3416

3517
let config = if opt_config.is_undefined() {
36-
Config {
37-
rules: get_default_rules(),
38-
}
18+
Configuration::default()
3919
} else if opt_config.is_object() {
4020
let config_string = String::from(js_sys::JSON::stringify(&opt_config)?);
4121
json5::from_str(&config_string)
@@ -50,32 +30,34 @@ pub fn process_code(code: &str, opt_config: JsValue) -> Result<String, JsValue>
5030
})?
5131
};
5232

53-
let parser = Parser::default().preserve_tokens();
33+
let resources = Resources::from_memory();
34+
const LOCATION: &str = "file.lua";
35+
resources.write(LOCATION, code).unwrap();
5436

55-
let mut block = parser.parse(code).map_err(|error| error.to_string())?;
37+
let result = darklua_core::process(
38+
&resources,
39+
Options::new(LOCATION).with_configuration(config),
40+
);
5641

57-
for (index, rule) in config.rules.iter().enumerate() {
58-
let mut context = Context::default();
59-
rule.process(&mut block, &mut context)
60-
.map_err(|rule_errors| {
61-
let errors: Vec<_> = rule_errors.iter().map(ToString::to_string).collect();
62-
format!(
63-
"error with rule {} ({}):\n -> {}",
64-
rule.get_name().to_owned(),
65-
index,
66-
errors.join("\n -> "),
67-
)
68-
})?;
69-
}
70-
71-
let lua_code = generate_code(&code, &block);
42+
match result.result() {
43+
Ok(()) => {
44+
let lua_code = resources.get(LOCATION).unwrap();
7245

73-
Ok(lua_code)
46+
Ok(lua_code)
47+
}
48+
Err(errors) => {
49+
let errors: Vec<_> = errors
50+
.into_iter()
51+
.map(|error| format!("-> {}", error))
52+
.collect();
53+
Err(format!("unable to process code:\n{}", errors.join("\n")).into())
54+
}
55+
}
7456
}
7557

7658
#[wasm_bindgen]
7759
pub fn get_all_rule_names() -> Box<[JsValue]> {
78-
rules::get_all_rule_names()
60+
darklua_core::rules::get_all_rule_names()
7961
.into_iter()
8062
.map(JsValue::from_str)
8163
.collect::<Vec<_>>()

src/bin.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ fn main() {
2020
match darklua.run() {
2121
Ok(()) => {}
2222
Err(err) => {
23-
eprintln!("{}", err);
24-
process::exit(1);
23+
process::exit(err.exit_code());
2524
}
2625
}
2726
}

0 commit comments

Comments
 (0)