Skip to content

Commit c87763c

Browse files
authored
fix: handle error in numberToPos and formatError (#4782)
1 parent 30d756e commit c87763c

File tree

5 files changed

+18
-20
lines changed

5 files changed

+18
-20
lines changed

packages/vite/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@
7979
"@vue/compiler-dom": "^3.1.5",
8080
"acorn": "^8.4.1",
8181
"acorn-class-fields": "^1.0.0",
82-
"acorn-numeric-separator": "^0.3.6",
8382
"acorn-static-class-features": "^1.0.0",
8483
"brotli-size": "^4.0.0",
8584
"builtin-modules": "^3.2.0",

packages/vite/src/node/server/pluginContainer.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ import {
5151
} from 'rollup'
5252
import * as acorn from 'acorn'
5353
import acornClassFields from 'acorn-class-fields'
54-
import acornNumericSeparator from 'acorn-numeric-separator'
5554
import acornStaticClassFeatures from 'acorn-static-class-features'
5655
import { RawSourceMap } from '@ampproject/remapping/dist/types/types'
5756
import { combineSourcemaps } from '../utils'
@@ -116,8 +115,7 @@ type PluginContext = Omit<
116115

117116
export let parser = acorn.Parser.extend(
118117
acornClassFields,
119-
acornStaticClassFeatures,
120-
acornNumericSeparator
118+
acornStaticClassFeatures
121119
)
122120

123121
export async function createPluginContainer(
@@ -285,10 +283,22 @@ export async function createPluginContainer(
285283
: // some rollup plugins, e.g. json, sets position instead of pos
286284
(err as any).position
287285
if (pos != null) {
288-
err.loc = err.loc || {
289-
file: err.id,
290-
...numberToPos(ctx._activeCode, pos)
286+
let errLocation;
287+
try {
288+
errLocation = numberToPos(ctx._activeCode, pos);
291289
}
290+
catch (err2) {
291+
logger.error(
292+
chalk.red(`Error in error handler:\n${err2.stack || err2.message}\n`),
293+
// print extra newline to separate the two errors
294+
{ error: err2 }
295+
)
296+
throw err;
297+
}
298+
err.loc = err.loc || {
299+
file: err.id,
300+
...errLocation
301+
};
292302
err.frame = err.frame || generateCodeFrame(ctx._activeCode, pos)
293303
} else if (err.loc) {
294304
// css preprocessors may report errors in an included file
@@ -387,8 +397,7 @@ export async function createPluginContainer(
387397
parser = acorn.Parser.extend(
388398
...[
389399
acornClassFields,
390-
acornStaticClassFeatures,
391-
acornNumericSeparator
400+
acornStaticClassFeatures
392401
].concat(options.acornInjectPlugins)
393402
)
394403
}

packages/vite/src/node/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ export function numberToPos(
291291
): { line: number; column: number } {
292292
if (typeof offset !== 'number') return offset
293293
if (offset > source.length) {
294-
throw new Error('offset is longer than source length!')
294+
throw new Error(`offset is longer than source length! offset ${offset} > length ${source.length}`);
295295
}
296296
const lines = source.split(splitRE)
297297
let counted = 0

packages/vite/types/shims.d.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ declare module 'acorn-static-class-features' {
2727
export default plugin
2828
}
2929

30-
declare module 'acorn-numeric-separator' {
31-
const plugin: any
32-
export default plugin
33-
}
34-
3530
declare module 'connect-history-api-fallback' {
3631
const plugin: any
3732
export = plugin

yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,11 +1654,6 @@ acorn-node@^1.6.1:
16541654
acorn-walk "^7.0.0"
16551655
xtend "^4.0.2"
16561656

1657-
acorn-numeric-separator@^0.3.6:
1658-
version "0.3.6"
1659-
resolved "https://registry.yarnpkg.com/acorn-numeric-separator/-/acorn-numeric-separator-0.3.6.tgz#af7f0abaf8e74bd9ca1117602954d0a3b75804f3"
1660-
integrity sha512-jUr5esgChu4k7VzesH/Nww3EysuyGJJcTEEiXqILUFKpO96PNyEXmK21M6nE0TSqGA1PeEg1MzgqJaoFsn9JMw==
1661-
16621657
acorn-private-class-elements@^1.0.0:
16631658
version "1.0.0"
16641659
resolved "https://registry.yarnpkg.com/acorn-private-class-elements/-/acorn-private-class-elements-1.0.0.tgz#c5805bf8a46cd065dc9b3513bfebb504c88cd706"

0 commit comments

Comments
 (0)