Skip to content

Commit

Permalink
fix: show correct column and line on lint error
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Jul 29, 2019
1 parent 0ab086f commit 90c5390
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// eslint-disable-next-line @typescript-eslint/no-triple-slash-reference
/// <reference path="../types.d.ts" />
/// <reference path="./types.d.ts" />

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

const node = transNodePos(position)

const { tokens: esTokens, ...AST } = esParse(
rawText,
options,
) as AST.Program
let program: AST.Program

const offset = node.start - AST.range[0]
try {
program = esParse(rawText, options) as AST.Program
} catch (e) {
if (e instanceof SyntaxError) {
e.index += node.start
e.column += node.loc.start.column - 1
e.lineNumber += node.loc.start.line - 1
}

throw e
}

const { tokens: esTokens, range } = program

const offset = node.start - range[0]

tokens.push(
...esTokens.map(token => {
Expand Down
6 changes: 6 additions & 0 deletions types.d.ts → src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
declare interface SyntaxError {
column?: number
index?: number
lineNumber?: number
}

declare module 'espree' {
import * as estree from 'estree'

Expand Down

0 comments on commit 90c5390

Please sign in to comment.