Skip to content

Commit

Permalink
fix: json error with position (#15225)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Dec 4, 2023
1 parent 0348137 commit 14be75f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const customQueryObject = /* #__PURE__ */ Object.assign({"./sibling.ts":
export const parent = /* #__PURE__ */ Object.assign({
});
export const rootMixedRelative = /* #__PURE__ */ Object.assign({"/css.spec.ts": () => import("../../css.spec.ts?url").then(m => m["default"]),"/define.spec.ts": () => import("../../define.spec.ts?url").then(m => m["default"]),"/esbuild.spec.ts": () => import("../../esbuild.spec.ts?url").then(m => m["default"]),"/import.spec.ts": () => import("../../import.spec.ts?url").then(m => m["default"]),"/importGlob/fixture-b/a.ts": () => import("../fixture-b/a.ts?url").then(m => m["default"]),"/importGlob/fixture-b/b.ts": () => import("../fixture-b/b.ts?url").then(m => m["default"]),"/importGlob/fixture-b/index.ts": () => import("../fixture-b/index.ts?url").then(m => m["default"])
export const rootMixedRelative = /* #__PURE__ */ Object.assign({"/css.spec.ts": () => import("../../css.spec.ts?url").then(m => m["default"]),"/define.spec.ts": () => import("../../define.spec.ts?url").then(m => m["default"]),"/esbuild.spec.ts": () => import("../../esbuild.spec.ts?url").then(m => m["default"]),"/import.spec.ts": () => import("../../import.spec.ts?url").then(m => m["default"]),"/importGlob/fixture-b/a.ts": () => import("../fixture-b/a.ts?url").then(m => m["default"]),"/importGlob/fixture-b/b.ts": () => import("../fixture-b/b.ts?url").then(m => m["default"]),"/importGlob/fixture-b/index.ts": () => import("../fixture-b/index.ts?url").then(m => m["default"]),"/json.spec.ts": () => import("../../json.spec.ts?url").then(m => m["default"])
});
Expand Down Expand Up @@ -93,7 +93,7 @@ export const customQueryObject = /* #__PURE__ */ Object.assign({"./sibling.ts":
export const parent = /* #__PURE__ */ Object.assign({
});
export const rootMixedRelative = /* #__PURE__ */ Object.assign({"/css.spec.ts": () => import("../../css.spec.ts?url&lang.ts").then(m => m["default"]),"/define.spec.ts": () => import("../../define.spec.ts?url&lang.ts").then(m => m["default"]),"/esbuild.spec.ts": () => import("../../esbuild.spec.ts?url&lang.ts").then(m => m["default"]),"/import.spec.ts": () => import("../../import.spec.ts?url&lang.ts").then(m => m["default"]),"/importGlob/fixture-b/a.ts": () => import("../fixture-b/a.ts?url&lang.ts").then(m => m["default"]),"/importGlob/fixture-b/b.ts": () => import("../fixture-b/b.ts?url&lang.ts").then(m => m["default"]),"/importGlob/fixture-b/index.ts": () => import("../fixture-b/index.ts?url&lang.ts").then(m => m["default"])
export const rootMixedRelative = /* #__PURE__ */ Object.assign({"/css.spec.ts": () => import("../../css.spec.ts?url&lang.ts").then(m => m["default"]),"/define.spec.ts": () => import("../../define.spec.ts?url&lang.ts").then(m => m["default"]),"/esbuild.spec.ts": () => import("../../esbuild.spec.ts?url&lang.ts").then(m => m["default"]),"/import.spec.ts": () => import("../../import.spec.ts?url&lang.ts").then(m => m["default"]),"/importGlob/fixture-b/a.ts": () => import("../fixture-b/a.ts?url&lang.ts").then(m => m["default"]),"/importGlob/fixture-b/b.ts": () => import("../fixture-b/b.ts?url&lang.ts").then(m => m["default"]),"/importGlob/fixture-b/index.ts": () => import("../fixture-b/index.ts?url&lang.ts").then(m => m["default"]),"/json.spec.ts": () => import("../../json.spec.ts?url&lang.ts").then(m => m["default"])
});
Expand Down
26 changes: 26 additions & 0 deletions packages/vite/src/node/__tests__/plugins/json.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { expect, test } from 'vitest'
import { extractJsonErrorPosition } from '../../plugins/json'

const getErrorMessage = (input: string) => {
try {
JSON.parse(input)
throw new Error('No error happened')
} catch (e) {
return e.message
}
}

test('can extract json error position', () => {
const cases = [
{ input: '{', expectedPosition: 0 },
{ input: '{},', expectedPosition: 1 },
{ input: '"f', expectedPosition: 1 },
{ input: '[', expectedPosition: 0 },
]

for (const { input, expectedPosition } of cases) {
expect(extractJsonErrorPosition(getErrorMessage(input), input.length)).toBe(
expectedPosition,
)
}
})
21 changes: 17 additions & 4 deletions packages/vite/src/node/plugins/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,26 @@ export function jsonPlugin(
map: { mappings: '' },
}
} catch (e) {
const errorMessageList = /\d+/.exec(e.message)
const position = errorMessageList && parseInt(errorMessageList[0], 10)
const position = extractJsonErrorPosition(e.message, json.length)
const msg = position
? `, invalid JSON syntax found at line ${position}`
? `, invalid JSON syntax found at position ${position}`
: `.`
this.error(`Failed to parse JSON file` + msg, e.idx)
this.error(`Failed to parse JSON file` + msg, position)
}
},
}
}

export function extractJsonErrorPosition(
errorMessage: string,
inputLength: number,
): number | undefined {
if (errorMessage.startsWith('Unexpected end of JSON input')) {
return inputLength - 1
}

const errorMessageList = /at position (\d+)/.exec(errorMessage)
return errorMessageList
? Math.max(parseInt(errorMessageList[1], 10) - 1, 0)
: undefined
}

0 comments on commit 14be75f

Please sign in to comment.