genco/lang/js.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
//! Specialization for JavaScript code generation.
//!
//! # Examples
//!
//! Basic example:
//!
//! ```rust
//! use genco::prelude::*;
//!
//! # fn main() -> genco::fmt::Result {
//! let toks: js::Tokens = quote! {
//! function foo(v) {
//! return v + ", World";
//! }
//!
//! foo("Hello");
//! };
//!
//! assert_eq!(
//! vec![
//! "function foo(v) {",
//! " return v + \", World\";",
//! "}",
//! "",
//! "foo(\"Hello\");",
//! ],
//! toks.to_file_vec()?
//! );
//! # Ok(())
//! # }
//! ```
//!
//! # String Quoting in JavaScript
//!
//! JavaScript uses c-style string quoting, with indefinitely long unicode
//! escape sequences. But any non-control character can be embedded directly
//! into the string literal (like `"π"`).
//!
//! ```rust
//! use genco::prelude::*;
//!
//! # fn main() -> genco::fmt::Result {
//! let toks: js::Tokens = quote!("start Ο π \n \x7f ΓΏ $ \\ end");
//! assert_eq!("\"start Ο π \\n \\x7f ΓΏ $ \\\\ end\"", toks.to_string()?);
//!
//! let toks: js::Tokens = quote!($(quoted("start Ο π \n \x7f ΓΏ $ \\ end")));
//! assert_eq!("\"start Ο π \\n \\x7f ΓΏ $ \\\\ end\"", toks.to_string()?);
//! # Ok(())
//! # }
//! ```
use core::fmt::Write as _;
use alloc::collections::{BTreeMap, BTreeSet};
use alloc::string::String;
use crate::fmt;
use crate::tokens::ItemStr;
use relative_path::{RelativePath, RelativePathBuf};
/// Tokens container specialization for Rust.
pub type Tokens = crate::Tokens<JavaScript>;
impl crate::lang::LangSupportsEval for JavaScript {}
impl_lang! {
/// JavaScript language specialization.
pub JavaScript {
type Config = Config;
type Format = Format;
type Item = Import;
/// Start a string quote.
fn open_quote(
out: &mut fmt::Formatter<'_>,
_config: &Self::Config,
_format: &Self::Format,
has_eval: bool,
) -> fmt::Result {
if has_eval {
out.write_char('`')?;
} else {
out.write_char('"')?;
}
Ok(())
}
/// End a string quote.
fn close_quote(
out: &mut fmt::Formatter<'_>,
_config: &Self::Config,
_format: &Self::Format,
has_eval: bool,
) -> fmt::Result {
if has_eval {
out.write_char('`')?;
} else {
out.write_char('"')?;
}
Ok(())
}
fn start_string_eval(
out: &mut fmt::Formatter<'_>,
_config: &Self::Config,
_format: &Self::Format,
) -> fmt::Result {
out.write_str("${")?;
Ok(())
}
fn end_string_eval(
out: &mut fmt::Formatter<'_>,
_config: &Self::Config,
_format: &Self::Format,
) -> fmt::Result {
out.write_char('}')?;
Ok(())
}
fn write_quoted(out: &mut fmt::Formatter<'_>, input: &str) -> fmt::Result {
// Reference: https://mathiasbynens.be/notes/javascript-escapes
for c in input.chars() {
match c {
// backspace
'\u{0008}' => out.write_str("\\b")?,
// form feed
'\u{0012}' => out.write_str("\\f")?,
// new line
'\n' => out.write_str("\\n")?,
// carriage return
'\r' => out.write_str("\\r")?,
// horizontal tab
'\t' => out.write_str("\\t")?,
// vertical tab
'\u{0011}' => out.write_str("\\v")?,
// null character.
'\0' => out.write_str("\\0")?,
// Note: only relevant if we were to use single-quoted strings.
// '\'' => out.write_str("\\'")?,
'"' => out.write_str("\\\"")?,
'\\' => out.write_str("\\\\")?,
c if !c.is_control() => out.write_char(c)?,
c if (c as u32) < 0x100 => {
write!(out, "\\x{:02x}", c as u32)?;
}
c => {
write!(out, "\\u{{{:x}}}", c as u32)?;
}
};
}
Ok(())
}
fn format_file(
tokens: &Tokens,
out: &mut fmt::Formatter<'_>,
config: &Self::Config,
) -> fmt::Result {
let mut imports = Tokens::new();
Self::imports(&mut imports, tokens, config);
let format = Format::default();
imports.format(out, config, &format)?;
tokens.format(out, config, &format)?;
Ok(())
}
}
Import {
fn format(&self, out: &mut fmt::Formatter<'_>, _: &Config, _: &Format) -> fmt::Result {
let name = match self.kind {
ImportKind::Named => self.alias.as_ref().unwrap_or(&self.name),
_ => &self.name,
};
out.write_str(name)
}
}
}
/// Format state for JavaScript.
#[derive(Debug, Default)]
pub struct Format {}
/// Configuration for JavaScript.
#[derive(Debug, Default)]
pub struct Config {
module_path: Option<RelativePathBuf>,
}
impl Config {
/// Configure the path to the current module being renderer.
///
/// This setting will determine what path imports are renderer relative
/// towards. So importing a module from `"foo/bar.js"`, and setting this to
/// `"foo/baz.js"` will cause the import to be rendered relatively as
/// `"../bar.js"`.
///
/// # Examples
///
/// ```
/// use genco::prelude::*;
/// use genco::fmt;
///
/// let foo1 = js::import(js::Module::Path("foo/bar.js".into()), "Foo1");
/// let foo2 = js::import(js::Module::Path("foo/bar.js".into()), "Foo2");
/// let react = js::import("react", "React").into_default();
///
/// let toks: js::Tokens = quote! {
/// $foo1
/// $foo2
/// $react
/// };
///
/// let mut w = fmt::VecWriter::new();
///
/// let config = js::Config::default().with_module_path("foo/baz.js");
/// let fmt = fmt::Config::from_lang::<JavaScript>();
///
/// toks.format_file(&mut w.as_formatter(&fmt), &config)?;
///
/// assert_eq!(
/// vec![
/// "import {Foo1, Foo2} from \"../bar.js\";",
/// "import React from \"react\";",
/// "",
/// "Foo1",
/// "Foo2",
/// "React"
/// ],
/// w.into_vec()
/// );
/// # Ok::<_, genco::fmt::Error>(())
/// ```
pub fn with_module_path<M>(self, module_path: M) -> Self
where
M: Into<RelativePathBuf>,
{
Self {
module_path: Some(module_path.into()),
}
}
}
/// Internal type to determine the kind of import used.
#[derive(Debug, Clone, Copy, Hash, PartialOrd, Ord, PartialEq, Eq)]
enum ImportKind {
Named,
Default,
Wildcard,
}
/// The import of a JavaScript type `import {foo} from "module.js"`.
///
/// Created through the [import()] function.
#[derive(Debug, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)]
pub struct Import {
/// The kind of the import.
kind: ImportKind,
/// Module of the imported name.
module: Module,
/// Name imported.
name: ItemStr,
/// Alias of an imported item.
///
/// If this is set, you'll get an import like:
///
/// ```text
/// import {<name> as <alias>} from <module>
/// ```
alias: Option<ItemStr>,
}
impl Import {
/// Change alias of imported item.
///
/// This implies that the import is a named import.
///
/// If this is set, you'll get an import like:
///
/// ```text
/// import {<name> as <alias>} from <module>
/// ```
///
/// # Examples
///
/// ```
/// use genco::prelude::*;
///
/// let a = js::import("collections", "vec");
/// let b = js::import("collections", "vec").with_alias("list");
///
/// let toks = quote! {
/// $a
/// $b
/// };
///
/// assert_eq!(
/// vec![
/// "import {vec, vec as list} from \"collections\";",
/// "",
/// "vec",
/// "list",
/// ],
/// toks.to_file_vec()?
/// );
/// # Ok::<_, genco::fmt::Error>(())
/// ```
pub fn with_alias<N>(self, alias: N) -> Self
where
N: Into<ItemStr>,
{
Self {
kind: ImportKind::Named,
alias: Some(alias.into()),
..self
}
}
/// Convert into a default import.
///
/// # Examples
///
/// ```
/// use genco::prelude::*;
///
/// let default_vec = js::import("collections", "defaultVec").into_default();
///
/// let toks = quote!($default_vec);
///
/// assert_eq!(
/// vec![
/// "import defaultVec from \"collections\";",
/// "",
/// "defaultVec",
/// ],
/// toks.to_file_vec()?
/// );
/// # Ok::<_, genco::fmt::Error>(())
/// ```
pub fn into_default(self) -> Self {
Self {
kind: ImportKind::Default,
alias: None,
..self
}
}
/// Convert into a wildcard import.
///
/// # Examples
///
/// ```
/// use genco::prelude::*;
///
/// let all = js::import("collections", "all").into_wildcard();
///
/// let toks = quote!($all);
///
/// assert_eq!(
/// vec![
/// "import * as all from \"collections\";",
/// "",
/// "all",
/// ],
/// toks.to_file_vec()?
/// );
/// # Ok::<_, genco::fmt::Error>(())
/// ```
pub fn into_wildcard(self) -> Self {
Self {
kind: ImportKind::Wildcard,
alias: None,
..self
}
}
}
/// A module being imported.
#[derive(Debug, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)]
pub enum Module {
/// A module imported from a specific path.
///
/// The path will be relativized according to the module specified in the
/// [Config::with_module_path].
Path(RelativePathBuf),
/// A globally imported module.
Global(ItemStr),
}
impl<'a> From<&'a str> for Module {
fn from(value: &'a str) -> Self {
Self::Global(value.into())
}
}
impl From<String> for Module {
fn from(value: String) -> Self {
Self::Global(value.into())
}
}
impl From<ItemStr> for Module {
fn from(value: ItemStr) -> Self {
Self::Global(value)
}
}
impl JavaScript {
/// Translate imports into the necessary tokens.
fn imports(out: &mut Tokens, tokens: &Tokens, config: &Config) {
use crate as genco;
use crate::prelude::*;
let mut modules = BTreeMap::<&Module, ResolvedModule<'_>>::new();
let mut wildcards = BTreeSet::new();
for import in tokens.walk_imports() {
match import.kind {
ImportKind::Named => {
let module = modules.entry(&import.module).or_default();
module.set.insert(match &import.alias {
None => ImportedElement::Plain(&import.name),
Some(alias) => ImportedElement::Aliased(&import.name, alias),
});
}
ImportKind::Default => {
let module = modules.entry(&import.module).or_default();
module.default_import = Some(&import.name);
}
ImportKind::Wildcard => {
wildcards.insert((&import.module, &import.name));
}
}
}
if modules.is_empty() && wildcards.is_empty() {
return;
}
for (module, name) in wildcards {
out.push();
quote_in! { *out =>
import * as $name from $(ref t => render_from(t, config.module_path.as_deref(), module));
}
}
for (name, module) in modules {
out.push();
quote_in! { *out =>
import $(ref tokens => {
if let Some(default) = module.default_import {
tokens.append(ItemStr::from(default));
if !module.set.is_empty() {
tokens.append(",");
tokens.space();
}
}
if !module.set.is_empty() {
tokens.append("{");
let mut it = module.set.iter().peekable();
while let Some(el) = it.next() {
match *el {
ImportedElement::Plain(name) => {
tokens.append(name);
},
ImportedElement::Aliased(name, alias) => {
quote_in!(*tokens => $name as $alias);
}
}
if it.peek().is_some() {
tokens.append(",");
tokens.space();
}
}
tokens.append("}");
}
}) from $(ref t => render_from(t, config.module_path.as_deref(), name));
};
}
out.line();
#[derive(Default)]
struct ResolvedModule<'a> {
default_import: Option<&'a ItemStr>,
set: BTreeSet<ImportedElement<'a>>,
}
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
enum ImportedElement<'a> {
Plain(&'a ItemStr),
Aliased(&'a ItemStr, &'a ItemStr),
}
fn render_from(t: &mut js::Tokens, module_path: Option<&RelativePath>, module: &Module) {
quote_in! { *t =>
$(match (module_path, module) {
(_, Module::Global(from)) => $(quoted(from)),
(None, Module::Path(path)) => $(quoted(path.as_str())),
(Some(module_path), Module::Path(path)) => $(quoted(module_path.relative(path).as_str())),
})
}
}
}
}
/// The import of a JavaScript type `import {foo} from "module.js"`.
///
/// # Examples
///
/// ```
/// use genco::prelude::*;
///
/// let default_vec = js::import("collections", "defaultVec").into_default();
/// let all = js::import("collections", "all").into_wildcard();
/// let vec = js::import("collections", "vec");
/// let vec_as_list = js::import("collections", "vec").with_alias("list");
///
/// let toks = quote! {
/// $default_vec
/// $all
/// $vec
/// $vec_as_list
/// };
///
/// assert_eq!(
/// vec![
/// "import * as all from \"collections\";",
/// "import defaultVec, {vec, vec as list} from \"collections\";",
/// "",
/// "defaultVec",
/// "all",
/// "vec",
/// "list",
/// ],
/// toks.to_file_vec()?
/// );
/// # Ok::<_, genco::fmt::Error>(())
/// ```
pub fn import<M, N>(module: M, name: N) -> Import
where
M: Into<Module>,
N: Into<ItemStr>,
{
Import {
kind: ImportKind::Named,
module: module.into(),
name: name.into(),
alias: None,
}
}