Skip to content

Commit 14be75f

Browse files
authored
fix: json error with position (#15225)
1 parent 0348137 commit 14be75f

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

packages/vite/src/node/__tests__/plugins/importGlob/__snapshots__/fixture.spec.ts.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const customQueryObject = /* #__PURE__ */ Object.assign({"./sibling.ts":
3939
export const parent = /* #__PURE__ */ Object.assign({
4040
4141
});
42-
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"])
42+
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"])
4343
4444
4545
});
@@ -93,7 +93,7 @@ export const customQueryObject = /* #__PURE__ */ Object.assign({"./sibling.ts":
9393
export const parent = /* #__PURE__ */ Object.assign({
9494
9595
});
96-
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"])
96+
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"])
9797
9898
9999
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { expect, test } from 'vitest'
2+
import { extractJsonErrorPosition } from '../../plugins/json'
3+
4+
const getErrorMessage = (input: string) => {
5+
try {
6+
JSON.parse(input)
7+
throw new Error('No error happened')
8+
} catch (e) {
9+
return e.message
10+
}
11+
}
12+
13+
test('can extract json error position', () => {
14+
const cases = [
15+
{ input: '{', expectedPosition: 0 },
16+
{ input: '{},', expectedPosition: 1 },
17+
{ input: '"f', expectedPosition: 1 },
18+
{ input: '[', expectedPosition: 0 },
19+
]
20+
21+
for (const { input, expectedPosition } of cases) {
22+
expect(extractJsonErrorPosition(getErrorMessage(input), input.length)).toBe(
23+
expectedPosition,
24+
)
25+
}
26+
})

packages/vite/src/node/plugins/json.ts

+17-4
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,26 @@ export function jsonPlugin(
7171
map: { mappings: '' },
7272
}
7373
} catch (e) {
74-
const errorMessageList = /\d+/.exec(e.message)
75-
const position = errorMessageList && parseInt(errorMessageList[0], 10)
74+
const position = extractJsonErrorPosition(e.message, json.length)
7675
const msg = position
77-
? `, invalid JSON syntax found at line ${position}`
76+
? `, invalid JSON syntax found at position ${position}`
7877
: `.`
79-
this.error(`Failed to parse JSON file` + msg, e.idx)
78+
this.error(`Failed to parse JSON file` + msg, position)
8079
}
8180
},
8281
}
8382
}
83+
84+
export function extractJsonErrorPosition(
85+
errorMessage: string,
86+
inputLength: number,
87+
): number | undefined {
88+
if (errorMessage.startsWith('Unexpected end of JSON input')) {
89+
return inputLength - 1
90+
}
91+
92+
const errorMessageList = /at position (\d+)/.exec(errorMessage)
93+
return errorMessageList
94+
? Math.max(parseInt(errorMessageList[1], 10) - 1, 0)
95+
: undefined
96+
}

0 commit comments

Comments
 (0)