Skip to content

Commit

Permalink
chore: moved Buffer handling for debug_sources to read phase
Browse files Browse the repository at this point in the history
  • Loading branch information
tmont committed Dec 7, 2020
1 parent 7ea9f36 commit 399b9e4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 35 deletions.
7 changes: 2 additions & 5 deletions packages/pug-runtime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,16 +249,13 @@ function pug_rethrow(err, filename, lineno, str) {
}
var context, lines, start, end;
try {
var encoding = 'utf8';
str = str || require('fs').readFileSync(filename, {encoding: encoding});
if (str.type === 'Buffer') {
str = Buffer.from(str.data).toString(encoding);
}
str = str || require('fs').readFileSync(filename, {encoding: 'utf8'});
context = 3;
lines = str.split('\n');
start = Math.max(lineno - context, 0);
end = Math.min(lines.length, lineno + context);
} catch (ex) {
err.message += ' - could not read from ' + filename + ' (' + ex.message + ')';
pug_rethrow(err, null, lineno);
return;
}
Expand Down
13 changes: 5 additions & 8 deletions packages/pug-runtime/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,18 +252,15 @@ foo.pug:3
throw new Error('expected rethrow to throw');
});

it("should rethrow error with toJSON()'d Buffer", () => {
const err = new Error();
const str = Buffer.from('hello world').toJSON();
it("should handle bad arguments gracefully", () => {
const err = new Error('hello world');
const str = { not: 'a string' };
try {
runtime.rethrow(err, 'foo.pug', 3, str);
} catch (e) {
expect(e).toBe(err);
expect(e.message.trim()).toBe(
`
foo.pug:3
1| hello world`.trim()
);
expect(e.message)
.toBe('hello world - could not read from foo.pug (str.split is not a function) on line 3');
return;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/pug/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ function compileBody(str, options) {
contents = load.read(filename, loadOptions);
}

debug_sources[filename] = contents;
debug_sources[filename] = Buffer.isBuffer(contents) ? contents.toString('utf8') : contents;
return contents;
},
});
Expand Down
44 changes: 23 additions & 21 deletions packages/pug/test/__snapshots__/pug.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -56,33 +56,35 @@ exports[`pug .compileClient() should support module syntax in pug.compileClient(
return c !== r ? s + a.substring(c, r) : s;
}
var pug_match_html = /[\\"&<>]/;
function pug_rethrow(n, t, e, r) {
if (!(n instanceof Error)) throw n;
if (!((\\"undefined\\" == typeof window && t) || r))
throw ((n.message += \\" on line \\" + e), n);
var i, o, a, f;
function pug_rethrow(e, n, r, t) {
if (!(e instanceof Error)) throw e;
if (!((\\"undefined\\" == typeof window && n) || t))
throw ((e.message += \\" on line \\" + r), e);
var o, a, i, s;
try {
(r = r || require(\\"fs\\").readFileSync(t, { encoding: \\"utf8\\" })),
\\"Buffer\\" === r.type && (r = Buffer.from(r.data).toString(\\"utf8\\")),
(i = 3),
(o = r.split(\\"\\\\n\\")),
(a = Math.max(e - i, 0)),
(f = Math.min(o.length, e + i));
(t = t || require(\\"fs\\").readFileSync(n, { encoding: \\"utf8\\" })),
(o = 3),
(a = t.split(\\"\\\\n\\")),
(i = Math.max(r - o, 0)),
(s = Math.min(a.length, r + o));
} catch (t) {
return void pug_rethrow(n, null, e);
return (
(e.message += \\" - could not read from \\" + n + \\" (\\" + t.message + \\")\\"),
void pug_rethrow(e, null, r)
);
}
(i = o
.slice(a, f)
.map(function(n, t) {
var r = t + a + 1;
return (r == e ? \\" > \\" : \\" \\") + r + \\"| \\" + n;
(o = a
.slice(i, s)
.map(function(e, n) {
var t = n + i + 1;
return (t == r ? \\" > \\" : \\" \\") + t + \\"| \\" + e;
})
.join(\\"\\\\n\\")),
(n.path = t);
(e.path = n);
try {
n.message = (t || \\"Pug\\") + \\":\\" + e + \\"\\\\n\\" + i + \\"\\\\n\\\\n\\" + n.message;
} catch (n) {}
throw n;
e.message = (n || \\"Pug\\") + \\":\\" + r + \\"\\\\n\\" + o + \\"\\\\n\\\\n\\" + e.message;
} catch (e) {}
throw e;
}
function template(locals) {
var pug_html = \\"\\",
Expand Down

0 comments on commit 399b9e4

Please sign in to comment.