wip: generate codeframe for compiler deprecations

This commit is contained in:
Evan You 2021-04-16 11:51:47 -04:00
parent d974adb327
commit 79cbf21c3e
4 changed files with 40 additions and 35 deletions

View File

@ -124,13 +124,8 @@ export function warnDeprecation(
typeof message === 'function' ? message(...args) : message typeof message === 'function' ? message(...args) : message
}${link ? `\n Details: ${link}` : ``}` }${link ? `\n Details: ${link}` : ``}`
if (loc) {
const err = new SyntaxError(msg) as CompilerError const err = new SyntaxError(msg) as CompilerError
err.code = key err.code = key
err.loc = loc if (loc) err.loc = loc
context.onWarn(err) context.onWarn(err)
return
}
context.onWarn(msg)
} }

View File

@ -10,7 +10,7 @@ import { CompilerCompatOptions } from './compat/compatConfig'
import { ParserPlugin } from '@babel/parser' import { ParserPlugin } from '@babel/parser'
export interface ErrorHandlingOptions { export interface ErrorHandlingOptions {
onWarn?: (msg: string | CompilerError) => void onWarn?: (warning: CompilerError) => void
onError?: (error: CompilerError) => void onError?: (error: CompilerError) => void
} }

View File

@ -1,7 +1,7 @@
// This entry is the "full-build" that includes both the runtime // This entry is the "full-build" that includes both the runtime
// and the compiler, and supports on-the-fly compilation of the template option. // and the compiler, and supports on-the-fly compilation of the template option.
import { initDev } from './dev' import { initDev } from './dev'
import { compile, CompilerOptions, CompilerError } from '@vue/compiler-dom' import { compile, CompilerError, CompilerOptions } from '@vue/compiler-dom'
import { import {
registerRuntimeCompiler, registerRuntimeCompiler,
RenderFunction, RenderFunction,
@ -55,8 +55,21 @@ function compileToFunction(
extend( extend(
{ {
hoistStatic: true, hoistStatic: true,
onError(err: CompilerError) { onError(err) {
if (__DEV__) { if (__DEV__) {
onError(err)
} else {
/* istanbul ignore next */
throw err
}
},
onWarn: __DEV__ ? onError : NOOP
} as CompilerOptions,
options
)
)
function onError(err: CompilerError) {
const message = `Template compilation error: ${err.message}` const message = `Template compilation error: ${err.message}`
const codeFrame = const codeFrame =
err.loc && err.loc &&
@ -66,15 +79,7 @@ function compileToFunction(
err.loc.end.offset err.loc.end.offset
) )
warn(codeFrame ? `${message}\n${codeFrame}` : message) warn(codeFrame ? `${message}\n${codeFrame}` : message)
} else {
/* istanbul ignore next */
throw err
} }
}
},
options
)
)
// The wildcard import results in a huge object with every export // The wildcard import results in a huge object with every export
// with keys that cannot be mangled, and can be quite heavy size-wise. // with keys that cannot be mangled, and can be quite heavy size-wise.

View File

@ -49,8 +49,21 @@ function compileToFunction(
extend( extend(
{ {
hoistStatic: true, hoistStatic: true,
onError(err: CompilerError) { onError(err) {
if (__DEV__) { if (__DEV__) {
onError(err)
} else {
/* istanbul ignore next */
throw err
}
},
onWarn: __DEV__ ? onError : NOOP
} as CompilerOptions,
options
)
)
function onError(err: CompilerError) {
const message = `Template compilation error: ${err.message}` const message = `Template compilation error: ${err.message}`
const codeFrame = const codeFrame =
err.loc && err.loc &&
@ -60,15 +73,7 @@ function compileToFunction(
err.loc.end.offset err.loc.end.offset
) )
warn(codeFrame ? `${message}\n${codeFrame}` : message) warn(codeFrame ? `${message}\n${codeFrame}` : message)
} else {
/* istanbul ignore next */
throw err
} }
}
},
options
)
)
// The wildcard import results in a huge object with every export // The wildcard import results in a huge object with every export
// with keys that cannot be mangled, and can be quite heavy size-wise. // with keys that cannot be mangled, and can be quite heavy size-wise.