Skip to content

Commit

Permalink
fix: handle error in numberToPos and formatError (#4782)
Browse files Browse the repository at this point in the history
  • Loading branch information
milahu committed Sep 1, 2021
1 parent 30d756e commit c87763c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 20 deletions.
1 change: 0 additions & 1 deletion packages/vite/package.json
Expand Up @@ -79,7 +79,6 @@
"@vue/compiler-dom": "^3.1.5",
"acorn": "^8.4.1",
"acorn-class-fields": "^1.0.0",
"acorn-numeric-separator": "^0.3.6",
"acorn-static-class-features": "^1.0.0",
"brotli-size": "^4.0.0",
"builtin-modules": "^3.2.0",
Expand Down
25 changes: 17 additions & 8 deletions packages/vite/src/node/server/pluginContainer.ts
Expand Up @@ -51,7 +51,6 @@ import {
} from 'rollup'
import * as acorn from 'acorn'
import acornClassFields from 'acorn-class-fields'
import acornNumericSeparator from 'acorn-numeric-separator'
import acornStaticClassFeatures from 'acorn-static-class-features'
import { RawSourceMap } from '@ampproject/remapping/dist/types/types'
import { combineSourcemaps } from '../utils'
Expand Down Expand Up @@ -116,8 +115,7 @@ type PluginContext = Omit<

export let parser = acorn.Parser.extend(
acornClassFields,
acornStaticClassFeatures,
acornNumericSeparator
acornStaticClassFeatures
)

export async function createPluginContainer(
Expand Down Expand Up @@ -285,10 +283,22 @@ export async function createPluginContainer(
: // some rollup plugins, e.g. json, sets position instead of pos
(err as any).position
if (pos != null) {
err.loc = err.loc || {
file: err.id,
...numberToPos(ctx._activeCode, pos)
let errLocation;
try {
errLocation = numberToPos(ctx._activeCode, pos);
}
catch (err2) {
logger.error(
chalk.red(`Error in error handler:\n${err2.stack || err2.message}\n`),
// print extra newline to separate the two errors
{ error: err2 }
)
throw err;
}
err.loc = err.loc || {
file: err.id,
...errLocation
};
err.frame = err.frame || generateCodeFrame(ctx._activeCode, pos)
} else if (err.loc) {
// css preprocessors may report errors in an included file
Expand Down Expand Up @@ -387,8 +397,7 @@ export async function createPluginContainer(
parser = acorn.Parser.extend(
...[
acornClassFields,
acornStaticClassFeatures,
acornNumericSeparator
acornStaticClassFeatures
].concat(options.acornInjectPlugins)
)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/utils.ts
Expand Up @@ -291,7 +291,7 @@ export function numberToPos(
): { line: number; column: number } {
if (typeof offset !== 'number') return offset
if (offset > source.length) {
throw new Error('offset is longer than source length!')
throw new Error(`offset is longer than source length! offset ${offset} > length ${source.length}`);
}
const lines = source.split(splitRE)
let counted = 0
Expand Down
5 changes: 0 additions & 5 deletions packages/vite/types/shims.d.ts
Expand Up @@ -27,11 +27,6 @@ declare module 'acorn-static-class-features' {
export default plugin
}

declare module 'acorn-numeric-separator' {
const plugin: any
export default plugin
}

declare module 'connect-history-api-fallback' {
const plugin: any
export = plugin
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Expand Up @@ -1654,11 +1654,6 @@ acorn-node@^1.6.1:
acorn-walk "^7.0.0"
xtend "^4.0.2"

acorn-numeric-separator@^0.3.6:
version "0.3.6"
resolved "https://registry.yarnpkg.com/acorn-numeric-separator/-/acorn-numeric-separator-0.3.6.tgz#af7f0abaf8e74bd9ca1117602954d0a3b75804f3"
integrity sha512-jUr5esgChu4k7VzesH/Nww3EysuyGJJcTEEiXqILUFKpO96PNyEXmK21M6nE0TSqGA1PeEg1MzgqJaoFsn9JMw==

acorn-private-class-elements@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/acorn-private-class-elements/-/acorn-private-class-elements-1.0.0.tgz#c5805bf8a46cd065dc9b3513bfebb504c88cd706"
Expand Down

0 comments on commit c87763c

Please sign in to comment.