add location info to nodes - fixes #1499

pull/1501/head
Rich Harris 6 years ago
parent 54799736b8
commit 63a712ccd9

@ -117,6 +117,7 @@ export default class Compiler {
expectedProperties: Set<string>;
usesRefs: boolean;
file: string;
locate: (c: number) => { line: number, column: number };
stylesheet: Stylesheet;
@ -159,6 +160,9 @@ export default class Compiler {
this.bindingGroups = [];
this.indirectDependencies = new Map();
this.file = options.filename && (
typeof process !== 'undefined' ? options.filename.replace(process.cwd(), '').replace(/^[\/\\]/, '') : options.filename
);
this.locate = getLocator(this.source);
// track which properties are needed, so we can provide useful info

@ -100,6 +100,10 @@ export default function dom(
builder.addBlock(compiler.javascript);
}
if (compiler.options.dev) {
builder.addLine(`const __file = ${JSON.stringify(compiler.file)};`);
}
const css = compiler.stylesheet.render(options.filename, !compiler.customElement);
const styles = compiler.stylesheet.hasStyles && stringify(options.dev ?
`${css.code}\n/*# sourceMappingURL=${css.map.toUrl()} */` :
@ -354,12 +358,8 @@ export default function dom(
let result = builder.toString();
const filename = options.filename && (
typeof process !== 'undefined' ? options.filename.replace(process.cwd(), '').replace(/^[\/\\]/, '') : options.filename
);
return compiler.generate(result, options, {
banner: `/* ${filename ? `${filename} ` : ``}generated by Svelte v${"__VERSION__"} */`,
banner: `/* ${compiler.file ? `${compiler.file} ` : ``}generated by Svelte v${"__VERSION__"} */`,
sharedPath,
name,
format,

@ -143,7 +143,7 @@ export default class Element extends Node {
stripWhitespace: boolean,
nextSibling: Node
) {
if (this.name === 'slot' || this.name === 'option') {
if (this.name === 'slot' || this.name === 'option' || this.compiler.options.dev) {
this.cannotUseInnerHTML();
}
@ -395,6 +395,13 @@ export default class Element extends Node {
return `${open}>${node.children.map(toHTML).join('')}</${node.name}>`;
}
if (this.compiler.options.dev) {
const loc = this.compiler.locate(this.start);
block.builders.hydrate.addLine(
`@addLoc(${this.var}, __file, ${loc.line}, ${loc.column}, ${this.start});`
);
}
}
addBindings(

@ -18,4 +18,8 @@ export function callAfter(fn, i) {
return () => {
if (!--i) fn();
};
}
export function addLoc(element, file, line, column, char) {
element.__svelte_meta = { file, line, column, char };
}

@ -1,4 +1,4 @@
/* src/Main.html generated by Svelte v2.5.1 */
/* src/Main.html generated by Svelte v2.6.6 */
function create_main_fragment(component, ctx) {
var p;

@ -1,4 +1,4 @@
/* src/Main.html generated by Svelte v2.5.1 */
/* src/Main.html generated by Svelte v2.6.6 */
function create_main_fragment(component, ctx) {
var p;

@ -1,16 +1,20 @@
/* src/Main.html generated by Svelte v2.5.1 */
/* src/Main.html generated by Svelte v2.6.6 */
const __file = "src/Main.html";
function create_main_fragment(component, ctx) {
var p;
var p, text;
return {
c: function create() {
p = createElement("p");
p.textContent = "Hello world!";
text = createText("Hello world!");
addLoc(p, __file, 0, 0, 0);
},
m: function mount(target, anchor) {
insertNode(p, target, anchor);
appendNode(text, p);
},
p: noop,
@ -59,10 +63,22 @@ function createElement(name) {
return document.createElement(name);
}
function createText(data) {
return document.createTextNode(data);
}
function addLoc(element, file, line, column, char) {
element.__svelte_meta = { file, line, column, char };
}
function insertNode(node, target, anchor) {
target.insertBefore(node, anchor);
}
function appendNode(node, target) {
target.appendChild(node);
}
function noop() {}
function detachNode(node) {

@ -1,4 +1,4 @@
/* src/Main.html generated by Svelte v2.5.1 */
/* src/Main.html generated by Svelte v2.6.6 */
import Widget from './Widget.html';

@ -1,4 +1,4 @@
/* src/Widget.html generated by Svelte v2.5.1 */
/* src/Widget.html generated by Svelte v2.6.6 */
function create_main_fragment(component, ctx) {
var p;

@ -1,4 +1,4 @@
/* src/Main.html generated by Svelte v2.5.1 */
/* src/Main.html generated by Svelte v2.6.6 */
import Widget from './widget/Widget.html';

@ -1,4 +1,4 @@
/* src/widget/Widget.html generated by Svelte v2.5.1 */
/* src/widget/Widget.html generated by Svelte v2.6.6 */
function create_main_fragment(component, ctx) {
var p;

@ -1,4 +1,4 @@
/* src/Main.html generated by Svelte v2.5.1 */
/* src/Main.html generated by Svelte v2.6.6 */
import Widget from './Widget.html';

@ -1,4 +1,4 @@
/* src/Widget.html generated by Svelte v2.5.1 */
/* src/Widget.html generated by Svelte v2.6.6 */
function create_main_fragment(component, ctx) {
var p;

@ -1,4 +1,4 @@
/* src/Main.html generated by Svelte v2.5.1 */
/* src/Main.html generated by Svelte v2.6.6 */
var Main = (function(answer) { "use strict";
answer = (answer && answer.__esModule) ? answer["default"] : answer;

@ -1,4 +1,4 @@
/* src/Main.html generated by Svelte v2.5.1 */
/* src/Main.html generated by Svelte v2.6.6 */
function create_main_fragment(component, ctx) {
var p;

@ -1,4 +1,4 @@
/* src/Main.html generated by Svelte v2.5.1 */
/* src/Main.html generated by Svelte v2.6.6 */
function create_main_fragment(component, ctx) {
var p;

@ -1,4 +1,4 @@
/* src/Main.html generated by Svelte v2.5.1 */
/* src/Main.html generated by Svelte v2.6.6 */
function create_main_fragment(component, ctx) {
var h1, text, text_1;

@ -5,6 +5,10 @@ function assign(tar, src) {
return tar;
}
function addLoc(element, file, line, column, char) {
element.__svelte_meta = { file, line, column, char };
}
function appendNode(node, target) {
target.appendChild(node);
}
@ -159,6 +163,8 @@ function bar({ foo }) {
return foo * 2;
}
const __file = undefined;
function create_main_fragment(component, ctx) {
var p, text_value = ctx.Math.max(0, ctx.foo), text, text_1, text_2;
@ -168,6 +174,7 @@ function create_main_fragment(component, ctx) {
text = createText(text_value);
text_1 = createText("\n\t");
text_2 = createText(ctx.bar);
addLoc(p, __file, 0, 0, 0);
},
m: function mount(target, anchor) {

@ -1,10 +1,12 @@
/* generated by Svelte vX.Y.Z */
import { appendNode, assign, createElement, createText, detachNode, init, insertNode, protoDev } from "svelte/shared.js";
import { addLoc, appendNode, assign, createElement, createText, detachNode, init, insertNode, protoDev } from "svelte/shared.js";
function bar({ foo }) {
return foo * 2;
}
const __file = undefined;
function create_main_fragment(component, ctx) {
var p, text_value = ctx.Math.max(0, ctx.foo), text, text_1, text_2;
@ -14,6 +16,7 @@ function create_main_fragment(component, ctx) {
text = createText(text_value);
text_1 = createText("\n\t");
text_2 = createText(ctx.bar);
addLoc(p, __file, 0, 0, 0);
},
m: function mount(target, anchor) {

@ -0,0 +1,3 @@
<div>
<p>this is a paragraph</p>
</div>

@ -0,0 +1,24 @@
import path from 'path';
export default {
dev: true,
test(assert, component, target) {
const h1 = target.querySelector('h1');
const p = target.querySelector('p');
assert.deepEqual(h1.__svelte_meta, {
file: path.relative(process.cwd(), path.resolve(__dirname, 'main.html')),
line: 0,
column: 0,
char: 0
});
assert.deepEqual(p.__svelte_meta, {
file: path.relative(process.cwd(), path.resolve(__dirname, 'Foo.html')),
line: 1,
column: 1,
char: 7
});
}
};

@ -0,0 +1,10 @@
<h1>this is a header</h1>
<Foo/>
<script>
export default {
components: {
Foo: './Foo.html'
}
};
</script>

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save