-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy patherrors.ts
More file actions
74 lines (64 loc) · 2.09 KB
/
Copy patherrors.ts
File metadata and controls
74 lines (64 loc) · 2.09 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
import lineColumn from 'line-column';
import { FailedMatchResult } from 'ohm-js';
import { NodeTypes, Position } from './types';
interface LineColPosition {
line: number;
column: number;
}
export class LiquidHTMLCSTParsingError extends SyntaxError {
loc?: { start: LineColPosition; end: LineColPosition };
constructor(ohm: FailedMatchResult) {
super(ohm.shortMessage);
this.name = 'LiquidHTMLParsingError';
const input = ohm.input;
const errorPos = ohm.getRightmostFailurePosition();
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,
},
};
}
}