preserve original location

18704-preserve-locations
adiguba 2 weeks ago
parent f2648b3537
commit beb71e81de

@ -26,7 +26,7 @@ export function compile(source, options) {
const validated = validate_component_options(options, ''); const validated = validate_component_options(options, '');
let parsed = _parse(source); let parsed = _parse(source, false, options.sourcemap);
const { customElement: customElementOptions, ...parsed_options } = parsed.options || {}; const { customElement: customElementOptions, ...parsed_options } = parsed.options || {};

@ -327,10 +327,11 @@ export class Parser {
/** /**
* @param {string} template * @param {string} template
* @param {boolean} [loose] * @param {boolean} [loose]
* @param {object | string} [sourcemap]
* @returns {AST.Root} * @returns {AST.Root}
*/ */
export function parse(template, loose = false) { export function parse(template, loose = false, sourcemap) {
state.set_source(template); state.set_source(template, sourcemap);
const parser = new Parser(template, loose); const parser = new Parser(template, loose);
return parser.root; return parser.root;

@ -15,7 +15,7 @@ function build_locations(nodes) {
for (const node of nodes) { for (const node of nodes) {
if (node.type !== 'element') continue; if (node.type !== 'element') continue;
const { line, column } = locator(node.start); const { line, column } = locator(node.start, true);
const expression = b.array([b.literal(line), b.literal(column)]); const expression = b.array([b.literal(line), b.literal(column)]);
const children = build_locations(node.children); const children = build_locations(node.children);

@ -1,6 +1,7 @@
/** @import { Location } from 'locate-character' */ /** @import { Location } from 'locate-character' */
/** @import { CompileOptions } from './types' */ /** @import { CompileOptions } from './types' */
/** @import { AST, Warning } from '#compiler' */ /** @import { AST, Warning } from '#compiler' */
import { TraceMap, originalPositionFor } from '@jridgewell/trace-mapping';
import { getLocator } from 'locate-character'; import { getLocator } from 'locate-character';
import { sanitize_location } from '../utils.js'; import { sanitize_location } from '../utils.js';
@ -46,20 +47,33 @@ export let dev;
export let runes = false; export let runes = false;
/** @type {(index: number) => Location} */ /** @type {(index: number, use_sourcemap?: boolean) => Location} */
export let locator; export let locator;
/** @param {string} value */ /**
export function set_source(value) { * @param {string} value
* @param {object | string} [sourcemap]
*/
export function set_source(value, sourcemap) {
source = value; source = value;
source_lines = source.split('\n'); source_lines = source.split('\n');
const map = (sourcemap && typeof sourcemap === 'object')
? new TraceMap( /** @type {any} */ (sourcemap))
: null;
const l = getLocator(source, { offsetLine: 1 }); const l = getLocator(source, { offsetLine: 1 });
locator = (i) => { locator = (i, use_sourcemap=false) => {
const loc = l(i); const loc = l(i);
if (!loc) throw new Error('An impossible situation occurred'); if (!loc) throw new Error('An impossible situation occurred');
if (use_sourcemap && map) {
const original = originalPositionFor(map, loc);
if (original.line != null && original.column != null) {
loc.line = original.line;
loc.column = original.column;
}
}
return loc; return loc;
}; };
} }

Loading…
Cancel
Save