Browse Source

types: update typing for vue-template-compiler

Evan You 7 năm trước cách đây
mục cha
commit
142cf6c489

+ 32 - 5
packages/vue-template-compiler/types/index.d.ts

@@ -8,14 +8,25 @@ interface CompilerOptions {
   directives?: Record<string, DirectiveFunction>;
   preserveWhitespace?: boolean;
   whitespace?: 'preserve' | 'condense';
+  outputSourceRange?: any
 }
 
-interface CompiledResult {
+interface CompilerOptionsWithSourceRange extends CompilerOptions {
+  outputSourceRange: true
+}
+
+interface ErrorWithRange {
+  msg: string;
+  start: number;
+  end: number;
+}
+
+interface CompiledResult<ErrorType> {
   ast: ASTElement | undefined;
   render: string;
   staticRenderFns: string[];
-  errors: string[];
-  tips: string[];
+  errors: ErrorType[];
+  tips: ErrorType[];
 }
 
 interface CompiledResultFunctions {
@@ -202,17 +213,27 @@ export interface SFCDescriptor {
 /*
  * Exposed functions
  */
+export function compile(
+  template: string,
+  options: CompilerOptionsWithSourceRange
+): CompiledResult<ErrorWithRange>
+
 export function compile(
   template: string,
   options?: CompilerOptions
-): CompiledResult;
+): CompiledResult<string>;
 
 export function compileToFunctions(template: string): CompiledResultFunctions;
 
+export function ssrCompile(
+  template: string,
+  options: CompilerOptionsWithSourceRange
+): CompiledResult<ErrorWithRange>;
+
 export function ssrCompile(
   template: string,
   options?: CompilerOptions
-): CompiledResult;
+): CompiledResult<string>;
 
 export function ssrCompileToFunctions(template: string): CompiledResultFunctions;
 
@@ -220,3 +241,9 @@ export function parseComponent(
   file: string,
   options?: SFCParserOptions
 ): SFCDescriptor;
+
+export function generateCodeFrame(
+  template: string,
+  start: number,
+  end: number
+): string;

+ 25 - 0
packages/vue-template-compiler/types/test.ts

@@ -9,6 +9,7 @@ import {
 
 // check compile options
 const compiled = compile("<div>hi</div>", {
+  outputSourceRange: true,
   preserveWhitespace: false,
   whitespace: 'condense',
   modules: [
@@ -35,6 +36,30 @@ const compiled = compile("<div>hi</div>", {
 new Function(compiled.render);
 compiled.staticRenderFns.map(fn => new Function(fn));
 
+// with outputSourceRange: true
+// errors should be objects with range
+compiled.errors.forEach(e => {
+  console.log(e.msg)
+})
+
+// without option or without outputSourceRange: true, should be strings
+const { errors } = compile(`foo`)
+errors.forEach(e => {
+  console.log(e.length)
+})
+
+const { errors: errors2 } = compile(`foo`, {})
+errors2.forEach(e => {
+  console.log(e.length)
+})
+
+const { errors: errors3 } = compile(`foo`, {
+  outputSourceRange: false
+})
+errors3.forEach(e => {
+  console.log(e.length)
+})
+
 const compiledFns = compileToFunctions("<div>hi</div>");
 
 // can be passed to component render / staticRenderFns options