Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add helper getPositionAt to fix incorrect loc #293

Merged
merged 5 commits into from
Mar 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions packages/eslint-mdx/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export const getLinesFromCode = (code: string) => code.split('\n')
// fix #292
export const getLocFromRange = (
lines: string[],
[start, end]: [number, number],
[start, end]: readonly [number, number],
): SourceLocation => {
let offset = 0

Expand Down Expand Up @@ -146,16 +146,17 @@ export const restoreNodeLocation = <T>(
return node
}

const { range } = node
let {
range: [start, end],
} = node

const start = range[0] + offset
const end = range[1] + offset
const range = [(start += offset), (end += +offset)] as const

return Object.assign(node, {
start,
end,
range: [start, end],
loc: getLocFromRange(lines, [start, end]),
range,
loc: getLocFromRange(lines, range),
})
}

Expand Down
4 changes: 1 addition & 3 deletions packages/eslint-mdx/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,11 @@ export class Parser {
throw e
}

const offset = start - program.range[0]

for (const prop of AST_PROPS)
this._ast[prop].push(
// ts doesn't understand the mixed type
...program[prop].map((item: never) =>
restoreNodeLocation(lines, item, offset),
restoreNodeLocation(lines, item, start),
),
)
}
Expand Down