From 49a50d3c9cf96e4628d82e726134b81b4112cd82 Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 13 Dec 2019 18:01:04 -0500 Subject: [PATCH] feat(compiler): accept line offset in codeframe --- packages/compiler-core/src/index.ts | 3 ++- packages/shared/src/codeframe.ts | 10 +++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/compiler-core/src/index.ts b/packages/compiler-core/src/index.ts index 723fe8c3..b1304a49 100644 --- a/packages/compiler-core/src/index.ts +++ b/packages/compiler-core/src/index.ts @@ -36,6 +36,7 @@ import { generateCodeFrame as _genCodeFrame } from '@vue/shared' const generateCodeFrame = _genCodeFrame as ( source: string, start?: number, - end?: number + end?: number, + lineOffset?: number ) => string export { generateCodeFrame } diff --git a/packages/shared/src/codeframe.ts b/packages/shared/src/codeframe.ts index 89067543..22b283a2 100644 --- a/packages/shared/src/codeframe.ts +++ b/packages/shared/src/codeframe.ts @@ -2,8 +2,9 @@ const range: number = 2 export function generateCodeFrame( source: string, - start: number = 0, - end: number = source.length + start = 0, + end = source.length, + lineOffset = 0 ): string { const lines = source.split(/\r?\n/) let count = 0 @@ -13,9 +14,8 @@ export function generateCodeFrame( if (count >= start) { for (let j = i - range; j <= i + range || end > count; j++) { if (j < 0 || j >= lines.length) continue - res.push( - `${j + 1}${' '.repeat(3 - String(j + 1).length)}| ${lines[j]}` - ) + const line = j + 1 + lineOffset + res.push(`${line}${' '.repeat(3 - String(line).length)}| ${lines[j]}`) const lineLength = lines[j].length if (j === i) { // push underline