Skip to content

Commit 5375af5

Browse files
authored
Merge pull request #1501 from sveltejs/gh-1499
add location info to nodes
2 parents 2537864 + 04ef203 commit 5375af5

File tree

24 files changed

+283
-355
lines changed

24 files changed

+283
-355
lines changed

src/compile/Compiler.ts

+7
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ export default class Compiler {
117117
expectedProperties: Set<string>;
118118
usesRefs: boolean;
119119

120+
file: string;
121+
fileVar: string;
120122
locate: (c: number) => { line: number, column: number };
121123

122124
stylesheet: Stylesheet;
@@ -159,6 +161,9 @@ export default class Compiler {
159161
this.bindingGroups = [];
160162
this.indirectDependencies = new Map();
161163

164+
this.file = options.filename && (
165+
typeof process !== 'undefined' ? options.filename.replace(process.cwd(), '').replace(/^[\/\\]/, '') : options.filename
166+
);
162167
this.locate = getLocator(this.source);
163168

164169
// track which properties are needed, so we can provide useful info
@@ -178,6 +183,8 @@ export default class Compiler {
178183
this.aliases = new Map();
179184
this.usedNames = new Set();
180185

186+
this.fileVar = options.dev && this.getUniqueName('file');
187+
181188
this.computations = [];
182189
this.templateProperties = {};
183190

src/compile/dom/index.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ export default function dom(
100100
builder.addBlock(compiler.javascript);
101101
}
102102

103+
if (compiler.options.dev) {
104+
builder.addLine(`const ${compiler.fileVar} = ${JSON.stringify(compiler.file)};`);
105+
}
106+
103107
const css = compiler.stylesheet.render(options.filename, !compiler.customElement);
104108
const styles = compiler.stylesheet.hasStyles && stringify(options.dev ?
105109
`${css.code}\n/*# sourceMappingURL=${css.map.toUrl()} */` :
@@ -354,12 +358,8 @@ export default function dom(
354358

355359
let result = builder.toString();
356360

357-
const filename = options.filename && (
358-
typeof process !== 'undefined' ? options.filename.replace(process.cwd(), '').replace(/^[\/\\]/, '') : options.filename
359-
);
360-
361361
return compiler.generate(result, options, {
362-
banner: `/* ${filename ? `${filename} ` : ``}generated by Svelte v${"__VERSION__"} */`,
362+
banner: `/* ${compiler.file ? `${compiler.file} ` : ``}generated by Svelte v${"__VERSION__"} */`,
363363
sharedPath,
364364
name,
365365
format,

src/compile/nodes/Element.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export default class Element extends Node {
143143
stripWhitespace: boolean,
144144
nextSibling: Node
145145
) {
146-
if (this.name === 'slot' || this.name === 'option') {
146+
if (this.name === 'slot' || this.name === 'option' || this.compiler.options.dev) {
147147
this.cannotUseInnerHTML();
148148
}
149149

@@ -395,6 +395,13 @@ export default class Element extends Node {
395395

396396
return `${open}>${node.children.map(toHTML).join('')}</${node.name}>`;
397397
}
398+
399+
if (this.compiler.options.dev) {
400+
const loc = this.compiler.locate(this.start);
401+
block.builders.hydrate.addLine(
402+
`@addLoc(${this.var}, ${this.compiler.fileVar}, ${loc.line}, ${loc.column}, ${this.start});`
403+
);
404+
}
398405
}
399406

400407
addBindings(

src/shared/utils.js

+6
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,10 @@ export function callAfter(fn, i) {
1818
return () => {
1919
if (!--i) fn();
2020
};
21+
}
22+
23+
export function addLoc(element, file, line, column, char) {
24+
element.__svelte_meta = {
25+
loc: { file, line, column, char }
26+
};
2127
}

test/cli/samples/basic/expected/Main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* src/Main.html generated by Svelte v2.5.1 */
1+
/* src/Main.html generated by Svelte vx.y.z */
22

33
function create_main_fragment(component, ctx) {
44
var p;

test/cli/samples/custom-element/expected/Main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* src/Main.html generated by Svelte v2.5.1 */
1+
/* src/Main.html generated by Svelte vx.y.z */
22

33
function create_main_fragment(component, ctx) {
44
var p;

test/cli/samples/dev/expected/Main.js

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
/* src/Main.html generated by Svelte v2.5.1 */
1+
/* src/Main.html generated by Svelte vx.y.z */
2+
3+
const file = "src/Main.html";
24

35
function create_main_fragment(component, ctx) {
4-
var p;
6+
var p, text;
57

68
return {
79
c: function create() {
810
p = createElement("p");
9-
p.textContent = "Hello world!";
11+
text = createText("Hello world!");
12+
addLoc(p, file, 0, 0, 0);
1013
},
1114

1215
m: function mount(target, anchor) {
1316
insertNode(p, target, anchor);
17+
appendNode(text, p);
1418
},
1519

1620
p: noop,
@@ -59,10 +63,24 @@ function createElement(name) {
5963
return document.createElement(name);
6064
}
6165

66+
function createText(data) {
67+
return document.createTextNode(data);
68+
}
69+
70+
function addLoc(element, file, line, column, char) {
71+
element.__svelte_meta = {
72+
loc: { file, line, column, char }
73+
};
74+
}
75+
6276
function insertNode(node, target, anchor) {
6377
target.insertBefore(node, anchor);
6478
}
6579

80+
function appendNode(node, target) {
81+
target.appendChild(node);
82+
}
83+
6684
function noop() {}
6785

6886
function detachNode(node) {

test/cli/samples/dir-sourcemap/expected/Main.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/cli/samples/dir-sourcemap/expected/Widget.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/cli/samples/dir-subdir/expected/Main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* src/Main.html generated by Svelte v2.5.1 */
1+
/* src/Main.html generated by Svelte vx.y.z */
22
import Widget from './widget/Widget.html';
33

44

test/cli/samples/dir-subdir/expected/widget/Widget.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* src/widget/Widget.html generated by Svelte v2.5.1 */
1+
/* src/widget/Widget.html generated by Svelte vx.y.z */
22

33
function create_main_fragment(component, ctx) {
44
var p;

test/cli/samples/dir/expected/Main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* src/Main.html generated by Svelte v2.5.1 */
1+
/* src/Main.html generated by Svelte vx.y.z */
22
import Widget from './Widget.html';
33

44

test/cli/samples/dir/expected/Widget.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* src/Widget.html generated by Svelte v2.5.1 */
1+
/* src/Widget.html generated by Svelte vx.y.z */
22

33
function create_main_fragment(component, ctx) {
44
var p;

test/cli/samples/globals/expected/Main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* src/Main.html generated by Svelte v2.5.1 */
1+
/* src/Main.html generated by Svelte vx.y.z */
22
var Main = (function(answer) { "use strict";
33
answer = (answer && answer.__esModule) ? answer["default"] : answer;
44

test/cli/samples/sourcemap-inline/expected/Main.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/cli/samples/sourcemap/expected/Main.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/cli/samples/store/expected/Main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* src/Main.html generated by Svelte v2.5.1 */
1+
/* src/Main.html generated by Svelte vx.y.z */
22

33
function create_main_fragment(component, ctx) {
44
var h1, text, text_1;

test/cli/update.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
const sander = require('sander');
2+
const glob = require('tiny-glob/sync');
23

34
process.chdir(__dirname);
45

56
sander.readdirSync('samples').forEach(dir => {
67
if (dir[0] === '.') return;
78

89
sander.rimrafSync(`samples/${dir}/expected`);
9-
sander.copydirSync(`samples/${dir}/actual`).to(`samples/${dir}/expected`);
10+
11+
const files = glob(`**`, { cwd: `samples/${dir}/actual`, filesOnly: true });
12+
files.forEach(file => {
13+
const source = sander.readFileSync(`samples/${dir}/actual/${file}`, { encoding: 'utf-8' });
14+
15+
sander.writeFileSync(
16+
`samples/${dir}/expected/${file}`,
17+
source.replace(/generated by Svelte v(\d+\.\d+\.\d+)/, 'generated by Svelte vx.y.z')
18+
);
19+
});
1020
});

test/js/samples/dev-warning-missing-data-computed/expected-bundle.js

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ function assign(tar, src) {
55
return tar;
66
}
77

8+
function addLoc(element, file, line, column, char) {
9+
element.__svelte_meta = {
10+
loc: { file, line, column, char }
11+
};
12+
}
13+
814
function appendNode(node, target) {
915
target.appendChild(node);
1016
}
@@ -159,6 +165,8 @@ function bar({ foo }) {
159165
return foo * 2;
160166
}
161167

168+
const file = undefined;
169+
162170
function create_main_fragment(component, ctx) {
163171
var p, text_value = ctx.Math.max(0, ctx.foo), text, text_1, text_2;
164172

@@ -168,6 +176,7 @@ function create_main_fragment(component, ctx) {
168176
text = createText(text_value);
169177
text_1 = createText("\n\t");
170178
text_2 = createText(ctx.bar);
179+
addLoc(p, file, 0, 0, 0);
171180
},
172181

173182
m: function mount(target, anchor) {

test/js/samples/dev-warning-missing-data-computed/expected.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
/* generated by Svelte vX.Y.Z */
2-
import { appendNode, assign, createElement, createText, detachNode, init, insertNode, protoDev } from "svelte/shared.js";
2+
import { addLoc, appendNode, assign, createElement, createText, detachNode, init, insertNode, protoDev } from "svelte/shared.js";
33

44
function bar({ foo }) {
55
return foo * 2;
66
}
77

8+
const file = undefined;
9+
810
function create_main_fragment(component, ctx) {
911
var p, text_value = ctx.Math.max(0, ctx.foo), text, text_1, text_2;
1012

@@ -14,6 +16,7 @@ function create_main_fragment(component, ctx) {
1416
text = createText(text_value);
1517
text_1 = createText("\n\t");
1618
text_2 = createText(ctx.bar);
19+
addLoc(p, file, 0, 0, 0);
1720
},
1821

1922
m: function mount(target, anchor) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div>
2+
<p>this is a paragraph</p>
3+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import path from 'path';
2+
3+
export default {
4+
dev: true,
5+
6+
test(assert, component, target) {
7+
const h1 = target.querySelector('h1');
8+
const p = target.querySelector('p');
9+
10+
assert.deepEqual(h1.__svelte_meta.loc, {
11+
file: path.relative(process.cwd(), path.resolve(__dirname, 'main.html')),
12+
line: 0,
13+
column: 0,
14+
char: 0
15+
});
16+
17+
assert.deepEqual(p.__svelte_meta.loc, {
18+
file: path.relative(process.cwd(), path.resolve(__dirname, 'Foo.html')),
19+
line: 1,
20+
column: 1,
21+
char: 7
22+
});
23+
}
24+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<h1>this is a header</h1>
2+
<Foo/>
3+
4+
<script>
5+
export default {
6+
components: {
7+
Foo: './Foo.html'
8+
}
9+
};
10+
</script>

0 commit comments

Comments
 (0)