SourceMap

Swift
public struct SourceMap: Hashable, Sendable

extension SourceMap: CustomStringConvertible

A source map describing how each segment of a generated file corresponds to some original source file.

The main use cases imagined are:

  1. Read a source map, make minor modifications, write it back encode(...).
  2. Create a new source map init(version:), fill in fields, and write it encode(...).
  3. Read and unpack a source map UnpackedSourceMap.init(...) and query it UnpackedSourceMap.map(...).

There are two representations of the actual mappings. The mappings property holds the compacted mapping string that looks like AAAA;EACA. This can be decoded into or written via arrays of Segments. These arrays can be very large and time-consuming to create. The separate UnpackedSourceMap type works to cache them and satisfy queries.


Topics

Types

struct Segment

A decoded segment of the source map.

This maps a region from a particular generated line (not explicit in this type) and column range (really just start column) to a source region and optionally a name, or asserts that range is not related to a source.

All indices are 0-based.

Declaration
Swift
public struct Segment: Hashable, Sendable

extension Segment: CustomStringConvertible

struct Source

The location and content of an original source referred to from the source map.

Use getSourceURL(...) to interpret source URLs incorporating sourceRoot.

Declaration
Swift
public struct Source: Hashable, Sendable

struct SourcePos

A position in an original source file. Only has meaning in the context of a SourceMap.

Declaration
Swift
public struct SourcePos: Hashable, Sendable

extension SourcePos: CustomStringConvertible

Initializers

init(Data)

Decode a source map from JSON DataData.

Declaration
Swift
public init(_ data: Data) throws
Parameters
data

The source map JSON.

Throws

If the JSON is bad, the version is bad, or if mandatory fields are missing.

init(String)

Decode a source map from a JSON string.

See init(_:)

Declaration
Swift
public init(_ string: String) throws

init(version: Int)

Create an empty source map.

Declaration
Swift
public init(version: Int = SourceMap.VERSION)

Methods

func encode() -> Data

Encode the source map as JSON

Declaration
Swift
public func encode() throws -> Data
Throws

If JSON encoding fails for some JSON reason - does not perform any source-map validation.

func encodeString() -> String

Encode the source map as JSON in a string

See encode().

Declaration
Swift
public func encodeString() throws -> String

func getSourceURL(source: Int, sourceMapURL: URL) -> URL

Get the URL of a source, incorporating the sourceRoot if set.

Declaration
Swift
public func getSourceURL(source: Int, sourceMapURL: URL) -> URL
Parameters
source

The index into sources to look up.

sourceMapURL

The absolute URL of this source map – relative source URLs are interpreted relative to this base.

func set(segments: [[Segment]], validate: Bool)

Update the mappings.

Declaration
Swift
public mutating func set(segments: [[Segment]], validate: Bool = true) throws
Parameters
segments

The segments to replace the current source map’s mappings

validate

Whether to check segments against sources or names. If this is false then any inconsistencies are passed through unchanged to mappings.

The default is true to avoid accidental mistakes, but if you are working with existing source maps it may be better to disable validation to preserve any existing errors.

Throws

Only if validate is set and there is a mismatch.

Properties

var description: String

A short description of the source map.

Declaration
Swift
public var description: String { get }

var file: String?

The name of the generated code file with which the source map is associated.

Declaration
Swift
public var file: String?

var mappings: String

The source map’s mappings in their compacted format.

Writing to segments updates this field.

Declaration
Swift
public var mappings: String

var names: [String]

Names that can be associated with segments of the generated code.

Declaration
Swift
public var names: [String]

var segments: [[Segment]]

One list of Segments for every line in the generated code file.

Use set(segments:validate:) to write this field.

Declaration
Swift
public var segments: [[Segment]] { get throws }
Throws

If the mappings are seriously undecodable in some way indicating a corrupt source map. Invalid indices do not cause errors.

var sourceRoot: String?

Value to prepend to each sources URL before attempting their resolution.

Declaration
Swift
public var sourceRoot: String?

var sources: [Source]

The original sources referred to from the source map.

Declaration
Swift
public var sources: [Source]

var version: Int

The spec version that this source map follows.

Declaration
Swift
public let version: Int

Static Properties

static var VERSION: Int

The expected version - 3 - of source maps.

Declaration
Swift
public static let VERSION: Int