From b251c574eb40d7601df400012f15e19418e492e3 Mon Sep 17 00:00:00 2001 From: Timothy Johnson Date: Sat, 1 Dec 2018 15:47:55 -0800 Subject: [PATCH] Attempt to add shifted error frames --- src/preprocess/index.ts | 129 +++++++++++++++++++++++++--------------- 1 file changed, 80 insertions(+), 49 deletions(-) diff --git a/src/preprocess/index.ts b/src/preprocess/index.ts index 8af0699027..f2249f9268 100644 --- a/src/preprocess/index.ts +++ b/src/preprocess/index.ts @@ -1,5 +1,6 @@ import { SourceMap } from 'magic-string'; import { SourceMapConsumer, SourceMapGenerator } from 'source-map'; +import getCodeFrame from '../utils/getCodeFrame.js'; export interface PreprocessOptions { markup?: (options: { @@ -62,52 +63,67 @@ async function replaceTagContents( if (match) { const attributes: Record = parseAttributes(match[1]); const content: string = match[2]; - const processed: { code: string, map?: SourceMap | string } = await preprocessor({ - content, - attributes, - filename : options.filename - }); - if (processed && processed.code) { - const code = ( - source.slice(0, match.index) + - `<${type}>${processed.code}` + - source.slice(match.index + match[0].length) - ); + // Line number of the match + let line = 0; + for (let i = 0; i <= match.index; i = source.indexOf('\n', i + 1)) { + line++; + } - // Shift sourcemap to the appropriate line - if (processed.map) { - const consumer = new SourceMapConsumer(processed.map); + line--; + + try { + const processed: { + code: string; + map?: SourceMap | string; + } = await preprocessor({ + content, + attributes, + filename: options.filename, + }); + + if (processed && processed.code) { + const code = + source.slice(0, match.index) + + `<${type}>${processed.code}` + + source.slice(match.index + match[0].length); + + // Shift sourcemap to the appropriate line + if (processed.map) { + const consumer = new SourceMapConsumer(processed.map); + + const generator = new SourceMapGenerator({ file: options.filename }); + consumer.eachMapping(mapping => { + generator.addMapping({ + source: mapping.source, + name: mapping.name, + original: { + line: mapping.originalLine + line, + column: mapping.originalColumn, + }, + generated: { + line: mapping.generatedLine + line, + column: mapping.generatedColumn, + }, + }); + }); - // Line number of the match - let line = 0; - for(let i = 0; i <= match.index; i = source.indexOf('\n', i + 1)) { - line++; + return { code, map: generator.toJSON() }; } - // Skip the - line++; - - const generator = new SourceMapGenerator({ file: options.filename }) - consumer.eachMapping(mapping => { - generator.addMapping({ - source: mapping.source, - name: mapping.name, - original: { - line: mapping.originalLine + line, - column: mapping.originalColumn - }, - generated: { - line: mapping.generatedLine + line, - column: mapping.generatedColumn - } - }); - }); - - return { code, map: generator.toJSON() }; + return { code }; + } + } catch (err) { + if (err.line && err.column) { + err.frame = getCodeFrame(source, line + err.line - 1, err.column); + } else if (err.start && err.start.line && err.start.column) { + err.frame = getCodeFrame(source, line + err.start.line - 1, err.column); + } else if (typeof err.start === 'number') { + const start = locate(contents, err.start, { offsetLine: 1 }); + err.frame = getCodeFrame(source, line + start.line - 1, start.column); } - return { code }; + throw err; } } @@ -124,17 +140,32 @@ export default async function preprocess( let markupMap: SourceMapGenerator; if (!!markup) { - const processed: { - code: string, - map?: SourceMap | string - } = await markup({ - content: source, - filename: options.filename - }); + try { + const processed: { + code: string; + map?: SourceMap | string; + } = await markup({ + content: source, + filename: options.filename, + }); + + source = processed.code; + if (processed.map) { + markupMap = SourceMapGenerator.fromSourceMap( + new SourceMapConsumer(processed.map) + ); + } + } catch (err) { + if (err.line && err.column) { + err.frame = getCodeFrame(source, err.line - 1, err.column); + } else if (err.start && err.start.line && err.start.column) { + err.frame = getCodeFrame(source, err.start.line - 1, err.column); + } else if (typeof err.start === 'number') { + const start = locate(source, err.start, { offsetLine: 1 }); + err.frame = getCodeFrame(source, start.line - 1, start.column); + } - source = processed.code; - if (processed.map) { - markupMap = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(processed.map)); + throw err; } }