Skip to content

Commit 142cf6c

Browse files
committed
types: update typing for vue-template-compiler
1 parent a4ed58c commit 142cf6c

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

Diff for: packages/vue-template-compiler/types/index.d.ts

+32-5
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,25 @@ interface CompilerOptions {
88
directives?: Record<string, DirectiveFunction>;
99
preserveWhitespace?: boolean;
1010
whitespace?: 'preserve' | 'condense';
11+
outputSourceRange?: any
1112
}
1213

13-
interface CompiledResult {
14+
interface CompilerOptionsWithSourceRange extends CompilerOptions {
15+
outputSourceRange: true
16+
}
17+
18+
interface ErrorWithRange {
19+
msg: string;
20+
start: number;
21+
end: number;
22+
}
23+
24+
interface CompiledResult<ErrorType> {
1425
ast: ASTElement | undefined;
1526
render: string;
1627
staticRenderFns: string[];
17-
errors: string[];
18-
tips: string[];
28+
errors: ErrorType[];
29+
tips: ErrorType[];
1930
}
2031

2132
interface CompiledResultFunctions {
@@ -202,21 +213,37 @@ export interface SFCDescriptor {
202213
/*
203214
* Exposed functions
204215
*/
216+
export function compile(
217+
template: string,
218+
options: CompilerOptionsWithSourceRange
219+
): CompiledResult<ErrorWithRange>
220+
205221
export function compile(
206222
template: string,
207223
options?: CompilerOptions
208-
): CompiledResult;
224+
): CompiledResult<string>;
209225

210226
export function compileToFunctions(template: string): CompiledResultFunctions;
211227

228+
export function ssrCompile(
229+
template: string,
230+
options: CompilerOptionsWithSourceRange
231+
): CompiledResult<ErrorWithRange>;
232+
212233
export function ssrCompile(
213234
template: string,
214235
options?: CompilerOptions
215-
): CompiledResult;
236+
): CompiledResult<string>;
216237

217238
export function ssrCompileToFunctions(template: string): CompiledResultFunctions;
218239

219240
export function parseComponent(
220241
file: string,
221242
options?: SFCParserOptions
222243
): SFCDescriptor;
244+
245+
export function generateCodeFrame(
246+
template: string,
247+
start: number,
248+
end: number
249+
): string;

Diff for: packages/vue-template-compiler/types/test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99

1010
// check compile options
1111
const compiled = compile("<div>hi</div>", {
12+
outputSourceRange: true,
1213
preserveWhitespace: false,
1314
whitespace: 'condense',
1415
modules: [
@@ -35,6 +36,30 @@ const compiled = compile("<div>hi</div>", {
3536
new Function(compiled.render);
3637
compiled.staticRenderFns.map(fn => new Function(fn));
3738

39+
// with outputSourceRange: true
40+
// errors should be objects with range
41+
compiled.errors.forEach(e => {
42+
console.log(e.msg)
43+
})
44+
45+
// without option or without outputSourceRange: true, should be strings
46+
const { errors } = compile(`foo`)
47+
errors.forEach(e => {
48+
console.log(e.length)
49+
})
50+
51+
const { errors: errors2 } = compile(`foo`, {})
52+
errors2.forEach(e => {
53+
console.log(e.length)
54+
})
55+
56+
const { errors: errors3 } = compile(`foo`, {
57+
outputSourceRange: false
58+
})
59+
errors3.forEach(e => {
60+
console.log(e.length)
61+
})
62+
3863
const compiledFns = compileToFunctions("<div>hi</div>");
3964

4065
// can be passed to component render / staticRenderFns options

0 commit comments

Comments
 (0)