UNPKG

8.86 kBTypeScriptView Raw
1import { SourceLocation, SourcePosition } from '../location';
2import { SourceSlice } from '../slice';
3import { Source } from '../source';
4import { MatchFn } from './match';
5import { AnyPosition, BROKEN, CharPosition, HbsPosition, OffsetKind, SourceOffset } from './offset';
6/**
7 * All spans have these details in common.
8 */
9interface SpanData {
10 readonly kind: OffsetKind;
11 /**
12 * Convert this span into a string. If the span is broken, return `''`.
13 */
14 asString(): string;
15 /**
16 * Gets the module the span was located in.
17 */
18 getModule(): string;
19 /**
20 * Get the starting position for this span. Try to avoid creating new position objects, as they
21 * cache computations.
22 */
23 getStart(): AnyPosition;
24 /**
25 * Get the ending position for this span. Try to avoid creating new position objects, as they
26 * cache computations.
27 */
28 getEnd(): AnyPosition;
29 /**
30 * Compute the `SourceLocation` for this span, returned as an instance of `HbsSpan`.
31 */
32 toHbsSpan(): HbsSpan | null;
33 /**
34 * For compatibility, whenever the `start` or `end` of a {@see SourceOffset} changes, spans are
35 * notified of the change so they can update themselves. This shouldn't happen outside of AST
36 * plugins.
37 */
38 locDidUpdate(changes: {
39 start?: SourcePosition;
40 end?: SourcePosition;
41 }): void;
42 /**
43 * Serialize into a {@see SerializedSourceSpan}, which is compact and designed for readability in
44 * context like AST Explorer. If you need a {@see SourceLocation}, use {@see toJSON}.
45 */
46 serialize(): SerializedSourceSpan;
47}
48/**
49 * A `SourceSpan` object represents a span of characters inside of a template source.
50 *
51 * There are three kinds of `SourceSpan` objects:
52 *
53 * - `ConcreteSourceSpan`, which contains byte offsets
54 * - `LazySourceSpan`, which contains `SourceLocation`s from the Handlebars AST, which can be
55 * converted to byte offsets on demand.
56 * - `InvisibleSourceSpan`, which represent source strings that aren't present in the source,
57 * because:
58 * - they were created synthetically
59 * - their location is nonsensical (the span is broken)
60 * - they represent nothing in the source (this currently happens only when a bug in the
61 * upstream Handlebars parser fails to assign a location to empty blocks)
62 *
63 * At a high level, all `SourceSpan` objects provide:
64 *
65 * - byte offsets
66 * - source in column and line format
67 *
68 * And you can do these operations on `SourceSpan`s:
69 *
70 * - collapse it to a `SourceSpan` representing its starting or ending position
71 * - slice out some characters, optionally skipping some characters at the beginning or end
72 * - create a new `SourceSpan` with a different starting or ending offset
73 *
74 * All SourceSpan objects implement `SourceLocation`, for compatibility. All SourceSpan
75 * objects have a `toJSON` that emits `SourceLocation`, also for compatibility.
76 *
77 * For compatibility, subclasses of `AbstractSourceSpan` must implement `locDidUpdate`, which
78 * happens when an AST plugin attempts to modify the `start` or `end` of a span directly.
79 *
80 * The goal is to avoid creating any problems for use-cases like AST Explorer.
81 */
82export declare class SourceSpan implements SourceLocation {
83 private data;
84 static get NON_EXISTENT(): SourceSpan;
85 static load(source: Source, serialized: SerializedSourceSpan): SourceSpan;
86 static forHbsLoc(source: Source, loc: SourceLocation): SourceSpan;
87 static forCharPositions(source: Source, startPos: number, endPos: number): SourceSpan;
88 static synthetic(chars: string): SourceSpan;
89 static broken(pos?: SourceLocation): SourceSpan;
90 readonly isInvisible: boolean;
91 constructor(data: SpanData & AnySpan);
92 getStart(): SourceOffset;
93 getEnd(): SourceOffset;
94 get loc(): SourceLocation;
95 get module(): string;
96 /**
97 * Get the starting `SourcePosition` for this `SourceSpan`, lazily computing it if needed.
98 */
99 get startPosition(): SourcePosition;
100 /**
101 * Get the ending `SourcePosition` for this `SourceSpan`, lazily computing it if needed.
102 */
103 get endPosition(): SourcePosition;
104 /**
105 * Support converting ASTv1 nodes into a serialized format using JSON.stringify.
106 */
107 toJSON(): SourceLocation;
108 /**
109 * Create a new span with the current span's end and a new beginning.
110 */
111 withStart(other: SourceOffset): SourceSpan;
112 /**
113 * Create a new span with the current span's beginning and a new ending.
114 */
115 withEnd(this: SourceSpan, other: SourceOffset): SourceSpan;
116 asString(): string;
117 /**
118 * Convert this `SourceSpan` into a `SourceSlice`. In debug mode, this method optionally checks
119 * that the byte offsets represented by this `SourceSpan` actually correspond to the expected
120 * string.
121 */
122 toSlice(expected?: string): SourceSlice;
123 /**
124 * For compatibility with SourceLocation in AST plugins
125 *
126 * @deprecated use startPosition instead
127 */
128 get start(): SourcePosition;
129 /**
130 * For compatibility with SourceLocation in AST plugins
131 *
132 * @deprecated use withStart instead
133 */
134 set start(position: SourcePosition);
135 /**
136 * For compatibility with SourceLocation in AST plugins
137 *
138 * @deprecated use endPosition instead
139 */
140 get end(): SourcePosition;
141 /**
142 * For compatibility with SourceLocation in AST plugins
143 *
144 * @deprecated use withEnd instead
145 */
146 set end(position: SourcePosition);
147 /**
148 * For compatibility with SourceLocation in AST plugins
149 *
150 * @deprecated use module instead
151 */
152 get source(): string;
153 collapse(where: 'start' | 'end'): SourceSpan;
154 extend(other: SourceSpan): SourceSpan;
155 serialize(): SerializedSourceSpan;
156 slice({ skipStart, skipEnd }: {
157 skipStart?: number;
158 skipEnd?: number;
159 }): SourceSpan;
160 sliceStartChars({ skipStart, chars }: {
161 skipStart?: number;
162 chars: number;
163 }): SourceSpan;
164 sliceEndChars({ skipEnd, chars }: {
165 skipEnd?: number;
166 chars: number;
167 }): SourceSpan;
168}
169declare type AnySpan = HbsSpan | CharPositionSpan | InvisibleSpan;
170declare class CharPositionSpan implements SpanData {
171 readonly source: Source;
172 readonly charPositions: {
173 start: CharPosition;
174 end: CharPosition;
175 };
176 readonly kind = OffsetKind.CharPosition;
177 _locPosSpan: HbsSpan | BROKEN | null;
178 constructor(source: Source, charPositions: {
179 start: CharPosition;
180 end: CharPosition;
181 });
182 wrap(): SourceSpan;
183 asString(): string;
184 getModule(): string;
185 getStart(): AnyPosition;
186 getEnd(): AnyPosition;
187 locDidUpdate(): void;
188 toHbsSpan(): HbsSpan | null;
189 serialize(): SerializedSourceSpan;
190 toCharPosSpan(): CharPositionSpan;
191}
192export declare class HbsSpan implements SpanData {
193 readonly source: Source;
194 readonly hbsPositions: {
195 start: HbsPosition;
196 end: HbsPosition;
197 };
198 readonly kind = OffsetKind.HbsPosition;
199 _charPosSpan: CharPositionSpan | BROKEN | null;
200 _providedHbsLoc: SourceLocation | null;
201 constructor(source: Source, hbsPositions: {
202 start: HbsPosition;
203 end: HbsPosition;
204 }, providedHbsLoc?: SourceLocation | null);
205 serialize(): SerializedConcreteSourceSpan;
206 wrap(): SourceSpan;
207 private updateProvided;
208 locDidUpdate({ start, end }: {
209 start?: SourcePosition;
210 end?: SourcePosition;
211 }): void;
212 asString(): string;
213 getModule(): string;
214 getStart(): AnyPosition;
215 getEnd(): AnyPosition;
216 toHbsLoc(): SourceLocation;
217 toHbsSpan(): HbsSpan;
218 toCharPosSpan(): CharPositionSpan | null;
219}
220declare class InvisibleSpan implements SpanData {
221 readonly kind: OffsetKind.Broken | OffsetKind.InternalsSynthetic | OffsetKind.NonExistent;
222 readonly loc: SourceLocation;
223 readonly string: string | null;
224 constructor(kind: OffsetKind.Broken | OffsetKind.InternalsSynthetic | OffsetKind.NonExistent, loc: SourceLocation, string?: string | null);
225 serialize(): SerializedConcreteSourceSpan;
226 wrap(): SourceSpan;
227 asString(): string;
228 locDidUpdate({ start, end }: {
229 start?: SourcePosition;
230 end?: SourcePosition;
231 }): void;
232 getModule(): string;
233 getStart(): AnyPosition;
234 getEnd(): AnyPosition;
235 toCharPosSpan(): InvisibleSpan;
236 toHbsSpan(): null;
237 toHbsLoc(): SourceLocation;
238}
239export declare const span: MatchFn<SourceSpan>;
240export declare type SerializedConcreteSourceSpan = /** collapsed */ number | /** normal */ [start: number, size: number] | /** synthetic */ string;
241export declare type SerializedSourceSpan = SerializedConcreteSourceSpan | OffsetKind.NonExistent | OffsetKind.Broken;
242export {};
243//# sourceMappingURL=span.d.ts.map
\No newline at end of file