Crate swc_common

source ·
Expand description

Utilities for the swc project

§Cargo features


Adds default implementation of Emitter. Enabling this feature will add tty-related dependencies.


Adds methods to generate web sourcemap.


Base mode for plugins, which can be enabled by plugin-mode or plugin-rt.

This mode creates a trait which can be used to override swc_common itself.


Creates an implementation for the plugin trait. This implements simply invokes thread-locals declared in swc_common.


Allows replacing operations related to thread-local variables with a trait.


Use ahash instead of rustc_hash for AHashMap and AHashSet.



  • Machinery for hygienic macros, inspired by the MTWT[1] paper.
  • This module reexports items from swc_visit with some swc-specific traits.
  • serializerDeprecated
  • The SourceMap tracks all the source code used within a single crate, mapping from integer byte positions to the original source code location. Each bit of source parsed during crate parsing (typically files, in-memory strings, or various bits of macro expansion) cover a continuous range of bytes in the SourceMap and are represented by SourceFiles. Byte positions are stored in spans and used pervasively in the compiler. They are absolute positions within the SourceMap, which upon request can be converted to line and column information, source code snippets, etc.
  • This module defines types which are thread safe if cfg!(feature = "concurrent") is true.



  • A byte offset. Keep this small (currently 32-bits), as AST contains a lot of them.
  • A character offset. Because of multibyte utf8 characters, a byte offset is not equivalent to a character offset. The SourceMap will convert BytePos values to CharPos values as necessary.
  • Used to create a .map file.
  • A source code location used for error reporting Note: This struct intentionally does not implement rkyv’s archieve to avoid redundant data copy ( source_map_proxy constructs plugin-side Loc instead with shared SourceFile instance.
  • A source code location used as the result of lookup_char_pos_adj
  • A mark is a unique id associated with a macro expansion.
  • A collection of spans. Spans have two orthogonal attributes:
  • A single source in the SourceMap.
  • Spans represent a region of code, used for error reporting. Positions in spans are absolute positions from the beginning of the source_map, not positions relative to SourceFiles. Methods on the SourceMap can be used to relate spans back to the original source. You must be careful if the span crosses more than one file - you will not be able to use many of the functions on spans in source_map and you cannot assume that the length of the span = hi - lo; there may be space in the BytePos range between files.
  • A SyntaxContext represents a chain of macro expansions (represented by marks).




  • Storage for span hygiene data.


Attribute Macros§

  • Alias for #[derive(Spanned, Fold, Clone, Debug, PartialEq)] for a struct and #[derive(Spanned, Fold, Clone, Debug, PartialEq, FromVariant)] for an enum.
  • Derives serde::Serialize and serde::Deserialize.

Derive Macros§

  • Derives serde::Deserialize which is aware of tag based deserialization.
  • Derives swc_common::EqIgnoreSpan.
  • Derives [From] for all variants. This only supports an enum where every variant has a single field.
  • Derives [swc_common::Spanned]. See [swc_common::Spanned] for documentation.
  • Derives swc_common::TypeEq.