-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy patherrors.ts
More file actions
75 lines (65 loc) · 2.26 KB
/
errors.ts
File metadata and controls
75 lines (65 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import lineColumn from 'line-column';
import { MatchResult, FailedMatchResult } from '@ohm-js/wasm';
import { NodeTypes, Position } from './types';
interface LineColPosition {
line: number;
column: number;
}
export class LiquidHTMLCSTParsingError extends SyntaxError {
loc?: { start: LineColPosition; end: LineColPosition };
constructor(ohm: MatchResult) {
super((ohm as FailedMatchResult).shortMessage);
this.name = 'LiquidHTMLParsingError';
// In @ohm-js/wasm v0.6.17, input is accessed via _ctx.input instead of directly on the match
const input = (ohm as any)._ctx?.input ?? (ohm as any).input;
const errorPos = (ohm as any)._rightmostFailurePosition;
const lineCol = lineColumn(input).fromIndex(Math.min(errorPos, input.length - 1));
// Plugging ourselves into @babel/code-frame since this is how
// the babel parser can print where the parsing error occured.
// https://github.com/prettier/prettier/blob/cd4a57b113177c105a7ceb94e71f3a5a53535b81/src/main/parser.js
if (lineCol) {
this.loc = {
start: {
line: lineCol.line,
column: lineCol.col,
},
end: {
line: lineCol.line,
column: lineCol.col,
},
};
}
}
}
export type UnclosedNode = { type: NodeTypes; name: string; blockStartPosition: Position };
export class LiquidHTMLASTParsingError extends SyntaxError {
loc?: { start: LineColPosition; end: LineColPosition };
unclosed: UnclosedNode | null;
constructor(
message: string,
source: string,
startIndex: number,
endIndex: number,
unclosed?: UnclosedNode,
) {
super(message);
this.name = 'LiquidHTMLParsingError';
this.unclosed = unclosed ?? null;
const lc = lineColumn(source);
const start = lc.fromIndex(startIndex);
const end = lc.fromIndex(Math.min(endIndex, source.length - 1));
// Plugging ourselves into @babel/code-frame since this is how
// the babel parser can print where the parsing error occured.
// https://github.com/prettier/prettier/blob/cd4a57b113177c105a7ceb94e71f3a5a53535b81/src/main/parser.js
this.loc = {
start: {
line: start!.line,
column: start!.col,
},
end: {
line: end!.line,
column: end!.col,
},
};
}
}