swc_common/
lib.rs

1//! Utilities for the swc project
2//!
3//!
4//! # Cargo features
5//!
6//! ## `tty-emitter`
7//!
8//! Adds default implementation of Emitter.
9//! Enabling this feature will add tty-related dependencies.
10//!
11//! ## `sourcemap`
12//!
13//! Adds methods to generate web sourcemap.
14//!
15//! ## `plugin-base`
16//!
17//! Base mode for plugins, which can be enabled by `plugin-mode` or `plugin-rt`.
18//!
19//! This mode creates a trait which can be used to override `swc_common` itself.
20//!
21//! ## `plugin-rt`
22//!
23//! Creates an implementation for the plugin trait. This implements simply
24//! invokes thread-locals declared in `swc_common`.
25//!
26//! ## `plugin-mode`
27//!
28//! Allows replacing operations related to thread-local variables with a trait.
29#![deny(clippy::all)]
30#![cfg_attr(docsrs, feature(doc_cfg))]
31#![cfg_attr(not(test), allow(unused))]
32
33use std::fmt::Debug;
34
35pub use ast_node::{ast_node, ast_serde, Decode, DeserializeEnum, Encode, Spanned};
36pub use from_variant::FromVariant;
37pub use swc_eq_ignore_macros::{EqIgnoreSpan, TypeEq};
38
39pub use self::{
40    eq::{EqIgnoreSpan, TypeEq},
41    errors::{SourceMapper, SourceMapperDyn},
42    pos::{
43        hygiene, BytePos, CharPos, FileName, Globals, Loc, LocWithOpt, Mark, MultiSpan, SourceFile,
44        SourceFileAndBytePos, SourceFileAndLine, Span, SpanLinesError, Spanned, SyntaxContext,
45        DUMMY_SP, GLOBALS, NO_EXPANSION,
46    },
47    source_map::{FileLines, FileLoader, FilePathMapping, SourceMap, SpanSnippetError},
48    syntax_pos::LineCol,
49};
50
51/// A trait for ast nodes.
52pub trait AstNode: Debug + PartialEq + Clone + Spanned {
53    const TYPE: &'static str;
54}
55
56pub mod cache;
57pub mod comments;
58mod eq;
59pub mod errors;
60pub mod input;
61pub mod iter;
62pub mod pass;
63pub mod plugin;
64mod pos;
65#[doc(hidden)]
66pub mod private;
67mod rustc_data_structures;
68pub mod serializer;
69pub mod source_map;
70pub mod sync;
71mod syntax_pos;
72#[cfg(all(swc_ast_unknown, feature = "encoding-impl"))]
73pub mod unknown;
74pub mod util;
75
76#[cfg(all(not(debug_assertions), feature = "plugin-rt", feature = "plugin-mode"))]
77compile_error!("You can't enable `plugin-rt` and `plugin-mode` at the same time");
78
79/// Warning: The particular implementation of serialization and deserialization
80/// of the ast nodes may change in the future, and so these types would be
81/// removed. It's safe to say they will be serializable in some form or another,
82/// but not necessarily with these specific types underlying the implementation.
83/// As such, *use these types at your own risk*.
84#[cfg(feature = "rkyv-impl")]
85#[doc(hidden)]
86pub use self::syntax_pos::{
87    ArchivedBytePos, ArchivedCharPos, ArchivedFileName, ArchivedMultiSpan, ArchivedSourceFile,
88    ArchivedSourceFileAndBytePos, ArchivedSpan, ArchivedSpanLinesError, ArchivedSpanSnippetError,
89};
90
91#[cfg(swc_ast_unknown)]
92#[track_caller]
93pub fn unknown_impl() -> std::convert::Infallible {
94    panic!("unknown node")
95}
96
97#[cfg(swc_ast_unknown)]
98#[macro_export]
99macro_rules! unknown {
100    () => {
101        match $crate::unknown_impl() {}
102    };
103}