|
| 1 | +import { TraceMap, originalPositionFor } from '@jridgewell/trace-mapping'; |
1 | 2 | import { walk } from 'estree-walker';
|
2 | 3 | import { getLocator } from 'locate-character';
|
3 | 4 | import { reserved, is_valid } from '../utils/names.js';
|
@@ -142,9 +143,20 @@ export default class Component {
|
142 | 143 | /** @type {string} */
|
143 | 144 | file;
|
144 | 145 |
|
145 |
| - /** @type {(c: number) => { line: number; column: number }} */ |
| 146 | + /** |
| 147 | + * Use this for stack traces. It is 1-based and acts on pre-processed sources. |
| 148 | + * Use `meta_locate` for metadata on DOM elements. |
| 149 | + * @type {(c: number) => { line: number; column: number }} |
| 150 | + */ |
146 | 151 | locate;
|
147 | 152 |
|
| 153 | + /** |
| 154 | + * Use this for metadata on DOM elements. It is 1-based and acts on sources that have not been pre-processed. |
| 155 | + * Use `locate` for source mappings. |
| 156 | + * @type {(c: number) => { line: number; column: number }} |
| 157 | + */ |
| 158 | + meta_locate; |
| 159 | + |
148 | 160 | /** @type {import('./nodes/Element.js').default[]} */
|
149 | 161 | elements = [];
|
150 | 162 |
|
@@ -199,7 +211,25 @@ export default class Component {
|
199 | 211 | .replace(process.cwd(), '')
|
200 | 212 | .replace(regex_leading_directory_separator, '')
|
201 | 213 | : compile_options.filename);
|
| 214 | + |
| 215 | + // line numbers in stack trace frames are 1-based. source maps are 0-based |
202 | 216 | this.locate = getLocator(this.source, { offsetLine: 1 });
|
| 217 | + /** @type {TraceMap | null | undefined} initialise lazy because only used in dev mode */ |
| 218 | + let tracer; |
| 219 | + this.meta_locate = (c) => { |
| 220 | + /** @type {{ line: number, column: number }} */ |
| 221 | + let location = this.locate(c); |
| 222 | + if (tracer === undefined) { |
| 223 | + // @ts-expect-error - fix the type of CompileOptions.sourcemap |
| 224 | + tracer = compile_options.sourcemap ? new TraceMap(compile_options.sourcemap) : null; |
| 225 | + } |
| 226 | + if (tracer) { |
| 227 | + // originalPositionFor returns 1-based lines like locator |
| 228 | + location = originalPositionFor(tracer, location); |
| 229 | + } |
| 230 | + return location; |
| 231 | + }; |
| 232 | + |
203 | 233 | // styles
|
204 | 234 | this.stylesheet = new Stylesheet({
|
205 | 235 | source,
|
|
0 commit comments