UNPKG

6.9 kBTypeScriptView Raw
1import type { SourcePosition } from '../location';
2import type { Source } from '../source';
3import type { BrokenKind, InternalSyntheticKind, NonExistentKind, OffsetKind } from './kinds';
4import type { SourceSpan } from './span';
5/**
6 * All positions have these details in common. Most notably, all three kinds of positions can
7 * must be able to attempt to convert themselves into {@see CharPosition}.
8 */
9export interface PositionData {
10 readonly kind: OffsetKind;
11 toCharPos(): CharPosition | null;
12 toJSON(): SourcePosition;
13}
14/**
15 * Used to indicate that an attempt to convert a `SourcePosition` to a character offset failed. It
16 * is separate from `null` so that `null` can be used to indicate that the computation wasn't yet
17 * attempted (and therefore to cache the failure)
18 */
19export declare const BROKEN = "BROKEN";
20export type BROKEN = 'BROKEN';
21export type AnyPosition = HbsPosition | CharPosition | InvisiblePosition;
22/**
23 * A `SourceOffset` represents a single position in the source.
24 *
25 * There are three kinds of backing data for `SourceOffset` objects:
26 *
27 * - `CharPosition`, which contains a character offset into the raw source string
28 * - `HbsPosition`, which contains a `SourcePosition` from the Handlebars AST, which can be
29 * converted to a `CharPosition` on demand.
30 * - `InvisiblePosition`, which represents a position not in source (@see {InvisiblePosition})
31 */
32export declare class SourceOffset {
33 readonly data: PositionData & AnyPosition;
34 /**
35 * Create a `SourceOffset` from a Handlebars `SourcePosition`. It's stored as-is, and converted
36 * into a character offset on demand, which avoids unnecessarily computing the offset of every
37 * `SourceLocation`, but also means that broken `SourcePosition`s are not always detected.
38 */
39 static forHbsPos(source: Source, pos: SourcePosition): SourceOffset;
40 /**
41 * Create a `SourceOffset` that corresponds to a broken `SourcePosition`. This means that the
42 * calling code determined (or knows) that the `SourceLocation` doesn't correspond correctly to
43 * any part of the source.
44 */
45 static broken(pos?: SourcePosition): SourceOffset;
46 constructor(data: PositionData & AnyPosition);
47 /**
48 * Get the character offset for this `SourceOffset`, if possible.
49 */
50 get offset(): number | null;
51 /**
52 * Compare this offset with another one.
53 *
54 * If both offsets are `HbsPosition`s, they're equivalent as long as their lines and columns are
55 * the same. This avoids computing offsets unnecessarily.
56 *
57 * Otherwise, two `SourceOffset`s are equivalent if their successfully computed character offsets
58 * are the same.
59 */
60 eql(right: SourceOffset): boolean;
61 /**
62 * Create a span that starts from this source offset and ends with another source offset. Avoid
63 * computing character offsets if both `SourceOffset`s are still lazy.
64 */
65 until(other: SourceOffset): SourceSpan;
66 /**
67 * Create a `SourceOffset` by moving the character position represented by this source offset
68 * forward or backward (if `by` is negative), if possible.
69 *
70 * If this `SourceOffset` can't compute a valid character offset, `move` returns a broken offset.
71 *
72 * If the resulting character offset is less than 0 or greater than the size of the source, `move`
73 * returns a broken offset.
74 */
75 move(by: number): SourceOffset;
76 /**
77 * Create a new `SourceSpan` that represents a collapsed range at this source offset. Avoid
78 * computing the character offset if it has not already been computed.
79 */
80 collapsed(): SourceSpan;
81 /**
82 * Convert this `SourceOffset` into a Handlebars {@see SourcePosition} for compatibility with
83 * existing plugins.
84 */
85 toJSON(): SourcePosition;
86}
87export declare class CharPosition implements PositionData {
88 readonly source: Source;
89 readonly charPos: number;
90 readonly kind: "CharPosition";
91 /** Computed from char offset */
92 _locPos: HbsPosition | BROKEN | null;
93 constructor(source: Source, charPos: number);
94 /**
95 * This is already a `CharPosition`.
96 *
97 * {@see HbsPosition} for the alternative.
98 */
99 toCharPos(): this;
100 /**
101 * Produce a Handlebars {@see SourcePosition} for this `CharPosition`. If this `CharPosition` was
102 * computed using {@see SourceOffset#move}, this will compute the `SourcePosition` for the offset.
103 */
104 toJSON(): SourcePosition;
105 wrap(): SourceOffset;
106 /**
107 * A `CharPosition` always has an offset it can produce without any additional computation.
108 */
109 get offset(): number;
110 /**
111 * Convert the current character offset to an `HbsPosition`, if it was not already computed. Once
112 * a `CharPosition` has computed its `HbsPosition`, it will not need to do compute it again, and
113 * the same `CharPosition` is retained when used as one of the ends of a `SourceSpan`, so
114 * computing the `HbsPosition` should be a one-time operation.
115 */
116 toHbsPos(): HbsPosition | null;
117}
118export declare class HbsPosition implements PositionData {
119 readonly source: Source;
120 readonly hbsPos: SourcePosition;
121 readonly kind: "HbsPosition";
122 _charPos: CharPosition | BROKEN | null;
123 constructor(source: Source, hbsPos: SourcePosition, charPos?: number | null);
124 /**
125 * Lazily compute the character offset from the {@see SourcePosition}. Once an `HbsPosition` has
126 * computed its `CharPosition`, it will not need to do compute it again, and the same
127 * `HbsPosition` is retained when used as one of the ends of a `SourceSpan`, so computing the
128 * `CharPosition` should be a one-time operation.
129 */
130 toCharPos(): CharPosition | null;
131 /**
132 * Return the {@see SourcePosition} that this `HbsPosition` was instantiated with. This operation
133 * does not need to compute anything.
134 */
135 toJSON(): SourcePosition;
136 wrap(): SourceOffset;
137 /**
138 * This is already an `HbsPosition`.
139 *
140 * {@see CharPosition} for the alternative.
141 */
142 toHbsPos(): this;
143}
144export declare class InvisiblePosition implements PositionData {
145 readonly kind: BrokenKind | InternalSyntheticKind | NonExistentKind;
146 readonly pos: SourcePosition;
147 constructor(kind: BrokenKind | InternalSyntheticKind | NonExistentKind, pos: SourcePosition);
148 /**
149 * A broken position cannot be turned into a {@see CharacterPosition}.
150 */
151 toCharPos(): null;
152 /**
153 * The serialization of an `InvisiblePosition is whatever Handlebars {@see SourcePosition} was
154 * originally identified as broken, non-existent or synthetic.
155 *
156 * If an `InvisiblePosition` never had an source offset at all, this method returns
157 * {@see UNKNOWN_POSITION} for compatibility.
158 */
159 toJSON(): SourcePosition;
160 wrap(): SourceOffset;
161 get offset(): null;
162}