This library hosts a production ready implementation of the source map “Scopes” proposal.
The library contains:
This library doesn't implement mappings encoding/decoding, but it does support encoding the scopes information into an already existing source map with “mappings” and “names”.
With NPM:
npx jsr add @chrome-devtools/source-map-scopes-codec
Using the library is straight-forward:
import { encode } from "@chrome-devtools/source-map-scopes-codec"; const scopeInformation = ...; const map = encode(scopeInformation); // Or with a pre-existing source map. const map = encode(scopeInformation, preExistingSourceMap);
To decode:
import { decode } from "@chrome-devtools/source-map-scopes-codec"; const scopeInformation = decode(sourceMap);
The library also contains a builder that makes creating structured scope information easier:
import { ScopeInfoBuilder } from "@chrome-devtools/source-map-scopes-codec"; const scopeInformation = new ScopeInfoBuilder() .startSource() .startScope(0, 0, { kind: "Global" }) .startScope(5, 10) .setScopeKind("Function") // Same as passing 'kind' to 'startScope'. .setScopeName("foo") // Same as passing 'name' to 'startScope'. .endScope(10, 5) .endScope(11, 1) .endSource() .startRange(0, 0, { scope: 0 }) .startRange(0, 10) .setRangeScopeDefinition(1) // Same as passing 'scope' to 'startRange'. .endRange(0, 15) .endRange(1, 1) .build();
There is also a SafeScopeInfoBuilder that checks that scopes and ranges are well nested and some other integrity constraints (e.g. definition scopes are known).