Skip to content

Commit 90c5390

Browse files
committed
fix: show correct column and line on lint error
1 parent 0ab086f commit 90c5390

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/parser.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// eslint-disable-next-line @typescript-eslint/no-triple-slash-reference
2-
/// <reference path="../types.d.ts" />
2+
/// <reference path="./types.d.ts" />
33

44
import { parse as esParse } from 'espree'
55
import remarkMdx from 'remark-mdx'
@@ -50,12 +50,23 @@ export const parseForESLint = (
5050

5151
const node = transNodePos(position)
5252

53-
const { tokens: esTokens, ...AST } = esParse(
54-
rawText,
55-
options,
56-
) as AST.Program
53+
let program: AST.Program
5754

58-
const offset = node.start - AST.range[0]
55+
try {
56+
program = esParse(rawText, options) as AST.Program
57+
} catch (e) {
58+
if (e instanceof SyntaxError) {
59+
e.index += node.start
60+
e.column += node.loc.start.column - 1
61+
e.lineNumber += node.loc.start.line - 1
62+
}
63+
64+
throw e
65+
}
66+
67+
const { tokens: esTokens, range } = program
68+
69+
const offset = node.start - range[0]
5970

6071
tokens.push(
6172
...esTokens.map(token => {

types.d.ts renamed to src/types.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
declare interface SyntaxError {
2+
column?: number
3+
index?: number
4+
lineNumber?: number
5+
}
6+
17
declare module 'espree' {
28
import * as estree from 'estree'
39

0 commit comments

Comments
 (0)