include warnings in stats object

pull/1299/head
Rich-Harris 7 years ago
parent a5d5a6c952
commit c0287f2080

@ -1,4 +1,4 @@
import { Node } from './interfaces'; import { Node, Warning } from './interfaces';
import Generator from './generators/Generator'; import Generator from './generators/Generator';
const now = (typeof process !== 'undefined' && process.hrtime) const now = (typeof process !== 'undefined' && process.hrtime)
@ -31,11 +31,14 @@ export default class Stats {
currentChildren: Timing[]; currentChildren: Timing[];
timings: Timing[]; timings: Timing[];
stack: Timing[]; stack: Timing[];
warnings: Warning[];
constructor() { constructor() {
this.startTime = now(); this.startTime = now();
this.stack = []; this.stack = [];
this.currentChildren = this.timings = []; this.currentChildren = this.timings = [];
this.warnings = [];
} }
start(label) { start(label) {
@ -91,7 +94,7 @@ export default class Stats {
return { return {
timings, timings,
warnings: [], // TODO warnings: this.warnings,
imports, imports,
hooks hooks
}; };

@ -126,6 +126,14 @@ export function compile(source: string, _options: CompileOptions) {
stats.stop('stylesheet'); stats.stop('stylesheet');
stats.start('validate'); stats.start('validate');
// TODO remove this when we remove svelte.validate from public API — we
// can use the stats object instead
const onwarn = options.onwarn;
options.onwarn = warning => {
stats.warnings.push(warning);
onwarn(warning);
};
validate(parsed, source, stylesheet, options); validate(parsed, source, stylesheet, options);
stats.stop('validate'); stats.stop('validate');

@ -3,6 +3,7 @@ import validateHtml from './html/index';
import { getLocator, Location } from 'locate-character'; import { getLocator, Location } from 'locate-character';
import getCodeFrame from '../utils/getCodeFrame'; import getCodeFrame from '../utils/getCodeFrame';
import CompileError from '../utils/CompileError'; import CompileError from '../utils/CompileError';
import Stats from '../Stats';
import Stylesheet from '../css/Stylesheet'; import Stylesheet from '../css/Stylesheet';
import { Node, Parsed, CompileOptions, Warning } from '../interfaces'; import { Node, Parsed, CompileOptions, Warning } from '../interfaces';

@ -26,7 +26,7 @@ describe("validate", () => {
try { try {
const warnings = []; const warnings = [];
svelte.compile(input, { const { stats } = svelte.compile(input, {
onwarn(warning) { onwarn(warning) {
warnings.push({ warnings.push({
message: warning.message, message: warning.message,
@ -38,6 +38,17 @@ describe("validate", () => {
dev: config.dev dev: config.dev
}); });
assert.equal(stats.warnings.length, warnings.length);
stats.warnings.forEach((full, i) => {
const lite = warnings[i];
assert.deepEqual({
message: full.message,
pos: full.pos,
loc: full.loc,
end: full.end
}, lite);
});
assert.deepEqual(warnings, expectedWarnings); assert.deepEqual(warnings, expectedWarnings);
} catch (e) { } catch (e) {
error = e; error = e;

Loading…
Cancel
Save