SourceMap
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:
- Read a source map, make minor modifications, write it back
encode(...). - Create a new source map
init(version:), fill in fields, and write itencode(...). - Read and unpack a source map
UnpackedSourceMap.init(...)and query itUnpackedSourceMap.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
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
public struct Segment: Hashable, Sendable
extension Segment: CustomStringConvertible
struct Source
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
struct SourcePos
struct SourcePos
A position in an original source file. Only has meaning in the context of a SourceMap.
Declaration
public struct SourcePos: Hashable, Sendable
extension SourcePos: CustomStringConvertible
Initializers
init(Data)
init(Data)
init(String)
init(String)
Decode a source map from a JSON string.
See init(_:)
Declaration
public init(_ string: String) throws
init(version: Int)
init(version: Int)
Methods
func encode() -> Data
func encode() -> Data
Encode the source map as JSON
Declaration
public func encode() throws -> Data
Throws
If JSON encoding fails for some JSON reason - does not perform any source-map validation.
func encodeString() -> String
func encodeString() -> String
func getSourceURL(source: Int, sourceMapURL: URL) -> URL
func getSourceURL(source: Int, sourceMapURL: URL) -> URL
Get the URL of a source, incorporating the sourceRoot if set.
Declaration
Parameters
source |
The index into |
sourceMapURL |
The absolute URL of this source map – relative source URLs are interpreted relative to this base. |
func set(segments: [[Segment]], validate: Bool)
func set(segments: [[Segment]], validate: Bool)
Update the mappings.
Declaration
Parameters
segments |
The segments to replace the current source map’s mappings |
validate |
Whether to check The default is |
Throws
Only if validate is set and there is a mismatch.
Properties
var description: String
var description: String
var file: String?
var file: String?
The name of the generated code file with which the source map is associated.
Declaration
public var file: String?
var mappings: String
var mappings: String
var names: [String]
var names: [String]
Names that can be associated with segments of the generated code.
Declaration
public var names: [String]
var segments: [[Segment]]
var segments: [[Segment]]
One list of Segments for every line in the generated code file.
Use set(segments:validate:) to write this field.
Declaration
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?
var sourceRoot: String?
var sources: [Source]
var sources: [Source]
The original sources referred to from the source map.
Declaration
public var sources: [Source]
var version: Int
var version: Int
Static Properties
static var VERSION: Int
static var VERSION: Int