Skip to content

Commit

Permalink
prevent possibility of conflicts with __file
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed May 28, 2018
1 parent 48c885b commit 04ef203
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/compile/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export default class Compiler {
usesRefs: boolean;

file: string;
fileVar: string;
locate: (c: number) => { line: number, column: number };

stylesheet: Stylesheet;
Expand Down Expand Up @@ -182,6 +183,8 @@ export default class Compiler {
this.aliases = new Map();
this.usedNames = new Set();

this.fileVar = options.dev && this.getUniqueName('file');

this.computations = [];
this.templateProperties = {};

Expand Down
2 changes: 1 addition & 1 deletion src/compile/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default function dom(
}

if (compiler.options.dev) {
builder.addLine(`const __file = ${JSON.stringify(compiler.file)};`);
builder.addLine(`const ${compiler.fileVar} = ${JSON.stringify(compiler.file)};`);
}

const css = compiler.stylesheet.render(options.filename, !compiler.customElement);
Expand Down
2 changes: 1 addition & 1 deletion src/compile/nodes/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ export default class Element extends Node {
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});`
`@addLoc(${this.var}, ${this.compiler.fileVar}, ${loc.line}, ${loc.column}, ${this.start});`
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/cli/samples/dev/expected/Main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* src/Main.html generated by Svelte vx.y.z */

const __file = "src/Main.html";
const file = "src/Main.html";

function create_main_fragment(component, ctx) {
var p, text;
Expand All @@ -9,7 +9,7 @@ function create_main_fragment(component, ctx) {
c: function create() {
p = createElement("p");
text = createText("Hello world!");
addLoc(p, __file, 0, 0, 0);
addLoc(p, file, 0, 0, 0);
},

m: function mount(target, anchor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function bar({ foo }) {
return foo * 2;
}

const __file = undefined;
const file = undefined;

function create_main_fragment(component, ctx) {
var p, text_value = ctx.Math.max(0, ctx.foo), text, text_1, text_2;
Expand All @@ -176,7 +176,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);
addLoc(p, file, 0, 0, 0);
},

m: function mount(target, anchor) {
Expand Down
4 changes: 2 additions & 2 deletions test/js/samples/dev-warning-missing-data-computed/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function bar({ foo }) {
return foo * 2;
}

const __file = undefined;
const file = undefined;

function create_main_fragment(component, ctx) {
var p, text_value = ctx.Math.max(0, ctx.foo), text, text_1, text_2;
Expand All @@ -16,7 +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);
addLoc(p, file, 0, 0, 0);
},

m: function mount(target, anchor) {
Expand Down

0 comments on commit 04ef203

Please sign in to comment.