Skip to content

Commit

Permalink
fix(eval): fix for SyntaxError with no location
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jun 13, 2021
1 parent b78cf73 commit ac7e67b
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/plugin-eval/src/loaders/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,20 @@ import { Script } from 'vm'

export const name = 'default'

let noLocation: boolean

try {
Reflect.construct(Script, ['}'])
} catch (e) {
noLocation = e.stack.startsWith('SyntaxError')
}

export function extractScript(expr: string) {
if (noLocation) {
const segments = expr.split('}')
return segments.length > 1 ? segments[0] : null
}

try {
Reflect.construct(Script, [expr])
} catch (e) {
Expand All @@ -23,7 +36,7 @@ export function transformScript(expr: string) {
return expr
} catch (e) {
if (!(e instanceof SyntaxError)) throw new Error('unknown error encounted')
if (e.stack.startsWith('SyntaxError')) return e.stack.split('\n', 1)[0]
if (noLocation) return e.stack.split('\n', 1)[0]
const lines = e.stack.split('\n', 5)
throw new Error(`${lines[4]}\n at ${lines[0]}:${lines[2].length}`)
}
Expand Down

0 comments on commit ac7e67b

Please sign in to comment.