2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-07-31 13:16:03 -04:00
|
|
|
use crate::compilers::CompiledModule;
|
|
|
|
use crate::file_fetcher::SourceFile;
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::ErrBox;
|
2019-10-26 21:04:34 -04:00
|
|
|
use regex::Regex;
|
2019-07-31 13:16:03 -04:00
|
|
|
|
2019-10-26 21:04:34 -04:00
|
|
|
// From https://github.com/mathiasbynens/mothereff.in/blob/master/js-variables/eff.js
|
|
|
|
static JS_RESERVED_WORDS: &str = r"^(?:do|if|in|for|let|new|try|var|case|else|enum|eval|false|null|this|true|void|with|await|break|catch|class|const|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|instanceof)$";
|
|
|
|
|
2019-07-31 13:16:03 -04:00
|
|
|
pub struct JsonCompiler {}
|
|
|
|
|
|
|
|
impl JsonCompiler {
|
2020-02-25 14:42:00 -05:00
|
|
|
pub async fn compile(
|
2020-01-04 05:20:52 -05:00
|
|
|
&self,
|
2019-07-31 13:16:03 -04:00
|
|
|
source_file: &SourceFile,
|
2020-02-06 21:24:51 -05:00
|
|
|
) -> Result<CompiledModule, ErrBox> {
|
|
|
|
let maybe_json_value = serde_json::from_slice(&source_file.source_code);
|
2019-10-26 21:04:34 -04:00
|
|
|
if let Err(err) = maybe_json_value {
|
2020-02-06 21:24:51 -05:00
|
|
|
return Err(ErrBox::from(err));
|
2019-10-26 21:04:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut code = format!(
|
|
|
|
"export default {};\n",
|
2020-02-06 21:24:51 -05:00
|
|
|
std::str::from_utf8(&source_file.source_code).unwrap()
|
2019-10-26 21:04:34 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
if let serde_json::Value::Object(m) = maybe_json_value.unwrap() {
|
|
|
|
// Best effort variable name exports
|
|
|
|
// Actual all allowed JS variable names are way tricker.
|
|
|
|
// We only handle a subset of alphanumeric names.
|
|
|
|
let js_var_regex = Regex::new(r"^[a-zA-Z_$][0-9a-zA-Z_$]*$").unwrap();
|
|
|
|
// Also avoid collision with reserved words.
|
|
|
|
let reserved_words = Regex::new(JS_RESERVED_WORDS).unwrap();
|
|
|
|
for (key, value) in m.iter() {
|
|
|
|
if js_var_regex.is_match(&key) && !reserved_words.is_match(&key) {
|
|
|
|
code.push_str(&format!(
|
|
|
|
"export const {} = {};\n",
|
|
|
|
key,
|
|
|
|
value.to_string()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-06 21:24:51 -05:00
|
|
|
Ok(CompiledModule {
|
2019-10-26 21:04:34 -04:00
|
|
|
code,
|
2019-07-31 13:16:03 -04:00
|
|
|
name: source_file.url.to_string(),
|
2020-02-06 21:24:51 -05:00
|
|
|
})
|
2019-07-31 13:16:03 -04:00
|
|
|
}
|
|
|
|
}
|