@@ -18,10 +18,30 @@ import type {
18
18
} from "../scopes.d.ts" ;
19
19
import { TokenIterator } from "../vlq.ts" ;
20
20
21
- export function decode ( sourceMap : SourceMapJson ) : ScopeInfo {
21
+ /**
22
+ * The mode decides how well-formed the encoded scopes have to be, to be accepted by the decoder.
23
+ *
24
+ * LOOSE is the default and is much more lenient. It's still best effort though and the decoder doesn't
25
+ * implement any error recovery: e.g. superfluous "start" items can lead to whole trees being omitted.
26
+ *
27
+ * STRICT mode will throw in the following situations:
28
+ *
29
+ * - Encountering ORIGINAL_SCOPE_END, or GENERATED_RANGE_END items that don't have matching *_START items.
30
+ * - Miss-matches between the number of variables in a scope vs the number of value expressions in the ranges.
31
+ * - Out-of-bound indices into the "names" array.
32
+ */
33
+ export const enum DecodeMode {
34
+ STRICT = 1 ,
35
+ LOOSE = 2 ,
36
+ }
37
+
38
+ export function decode (
39
+ sourceMap : SourceMapJson ,
40
+ options ?: { mode : DecodeMode } ,
41
+ ) : ScopeInfo {
22
42
if ( ! sourceMap . scopes || ! sourceMap . names ) return { scopes : [ ] , ranges : [ ] } ;
23
43
24
- return new Decoder ( sourceMap . scopes , sourceMap . names ) . decode ( ) ;
44
+ return new Decoder ( sourceMap . scopes , sourceMap . names , options ) . decode ( ) ;
25
45
}
26
46
27
47
const DEFAULT_SCOPE_STATE = {
@@ -43,6 +63,7 @@ const DEFAULT_RANGE_STATE = {
43
63
class Decoder {
44
64
readonly #encodedScopes: string ;
45
65
readonly #names: string [ ] ;
66
+ readonly #mode: DecodeMode ;
46
67
47
68
#scopes: ( OriginalScope | null ) [ ] = [ ] ;
48
69
#ranges: GeneratedRange [ ] = [ ] ;
@@ -56,9 +77,10 @@ class Decoder {
56
77
readonly #countToScope = new Map < number , OriginalScope > ( ) ;
57
78
#scopeCounter = 0 ;
58
79
59
- constructor ( scopes : string , names : string [ ] ) {
80
+ constructor ( scopes : string , names : string [ ] , options ?: { mode : DecodeMode } ) {
60
81
this . #encodedScopes = scopes ;
61
82
this . #names = names ;
83
+ this . #mode = options ?. mode ?? DecodeMode . LOOSE ;
62
84
}
63
85
64
86
decode ( ) : ScopeInfo {
0 commit comments