Skip to content

Commit 79cbf21

Browse files
committed
wip: generate codeframe for compiler deprecations
1 parent d974adb commit 79cbf21

File tree

4 files changed

+40
-35
lines changed

4 files changed

+40
-35
lines changed

packages/compiler-core/src/compat/compatConfig.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,8 @@ export function warnDeprecation(
124124
typeof message === 'function' ? message(...args) : message
125125
}${link ? `\n Details: ${link}` : ``}`
126126

127-
if (loc) {
128-
const err = new SyntaxError(msg) as CompilerError
129-
err.code = key
130-
err.loc = loc
131-
context.onWarn(err)
132-
return
133-
}
134-
135-
context.onWarn(msg)
127+
const err = new SyntaxError(msg) as CompilerError
128+
err.code = key
129+
if (loc) err.loc = loc
130+
context.onWarn(err)
136131
}

packages/compiler-core/src/options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { CompilerCompatOptions } from './compat/compatConfig'
1010
import { ParserPlugin } from '@babel/parser'
1111

1212
export interface ErrorHandlingOptions {
13-
onWarn?: (msg: string | CompilerError) => void
13+
onWarn?: (warning: CompilerError) => void
1414
onError?: (error: CompilerError) => void
1515
}
1616

packages/vue-compat/src/index.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// This entry is the "full-build" that includes both the runtime
22
// and the compiler, and supports on-the-fly compilation of the template option.
33
import { initDev } from './dev'
4-
import { compile, CompilerOptions, CompilerError } from '@vue/compiler-dom'
4+
import { compile, CompilerError, CompilerOptions } from '@vue/compiler-dom'
55
import {
66
registerRuntimeCompiler,
77
RenderFunction,
@@ -55,27 +55,32 @@ function compileToFunction(
5555
extend(
5656
{
5757
hoistStatic: true,
58-
onError(err: CompilerError) {
58+
onError(err) {
5959
if (__DEV__) {
60-
const message = `Template compilation error: ${err.message}`
61-
const codeFrame =
62-
err.loc &&
63-
generateCodeFrame(
64-
template as string,
65-
err.loc.start.offset,
66-
err.loc.end.offset
67-
)
68-
warn(codeFrame ? `${message}\n${codeFrame}` : message)
60+
onError(err)
6961
} else {
7062
/* istanbul ignore next */
7163
throw err
7264
}
73-
}
74-
},
65+
},
66+
onWarn: __DEV__ ? onError : NOOP
67+
} as CompilerOptions,
7568
options
7669
)
7770
)
7871

72+
function onError(err: CompilerError) {
73+
const message = `Template compilation error: ${err.message}`
74+
const codeFrame =
75+
err.loc &&
76+
generateCodeFrame(
77+
template as string,
78+
err.loc.start.offset,
79+
err.loc.end.offset
80+
)
81+
warn(codeFrame ? `${message}\n${codeFrame}` : message)
82+
}
83+
7984
// The wildcard import results in a huge object with every export
8085
// with keys that cannot be mangled, and can be quite heavy size-wise.
8186
// In the global build we know `Vue` is available globally so we can avoid

packages/vue/src/index.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,27 +49,32 @@ function compileToFunction(
4949
extend(
5050
{
5151
hoistStatic: true,
52-
onError(err: CompilerError) {
52+
onError(err) {
5353
if (__DEV__) {
54-
const message = `Template compilation error: ${err.message}`
55-
const codeFrame =
56-
err.loc &&
57-
generateCodeFrame(
58-
template as string,
59-
err.loc.start.offset,
60-
err.loc.end.offset
61-
)
62-
warn(codeFrame ? `${message}\n${codeFrame}` : message)
54+
onError(err)
6355
} else {
6456
/* istanbul ignore next */
6557
throw err
6658
}
67-
}
68-
},
59+
},
60+
onWarn: __DEV__ ? onError : NOOP
61+
} as CompilerOptions,
6962
options
7063
)
7164
)
7265

66+
function onError(err: CompilerError) {
67+
const message = `Template compilation error: ${err.message}`
68+
const codeFrame =
69+
err.loc &&
70+
generateCodeFrame(
71+
template as string,
72+
err.loc.start.offset,
73+
err.loc.end.offset
74+
)
75+
warn(codeFrame ? `${message}\n${codeFrame}` : message)
76+
}
77+
7378
// The wildcard import results in a huge object with every export
7479
// with keys that cannot be mangled, and can be quite heavy size-wise.
7580
// In the global build we know `Vue` is available globally so we can avoid

0 commit comments

Comments
 (0)