diff --git a/src/Stats.ts b/src/Stats.ts index 5f8c7d4f94..873ae15b46 100644 --- a/src/Stats.ts +++ b/src/Stats.ts @@ -1,4 +1,4 @@ -import { Node } from './interfaces'; +import { Node, Warning } from './interfaces'; import Generator from './generators/Generator'; const now = (typeof process !== 'undefined' && process.hrtime) @@ -31,11 +31,14 @@ export default class Stats { currentChildren: Timing[]; timings: Timing[]; stack: Timing[]; + warnings: Warning[]; constructor() { this.startTime = now(); this.stack = []; this.currentChildren = this.timings = []; + + this.warnings = []; } start(label) { @@ -91,7 +94,7 @@ export default class Stats { return { timings, - warnings: [], // TODO + warnings: this.warnings, imports, hooks }; diff --git a/src/index.ts b/src/index.ts index 3167498f85..16081ea8b4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -126,6 +126,14 @@ export function compile(source: string, _options: CompileOptions) { stats.stop('stylesheet'); 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); stats.stop('validate'); diff --git a/src/validate/index.ts b/src/validate/index.ts index fd487ccb0f..96e10d9ae8 100644 --- a/src/validate/index.ts +++ b/src/validate/index.ts @@ -3,6 +3,7 @@ import validateHtml from './html/index'; import { getLocator, Location } from 'locate-character'; import getCodeFrame from '../utils/getCodeFrame'; import CompileError from '../utils/CompileError'; +import Stats from '../Stats'; import Stylesheet from '../css/Stylesheet'; import { Node, Parsed, CompileOptions, Warning } from '../interfaces'; diff --git a/test/validator/index.js b/test/validator/index.js index 4961e71265..ddfbfb8a4a 100644 --- a/test/validator/index.js +++ b/test/validator/index.js @@ -26,7 +26,7 @@ describe("validate", () => { try { const warnings = []; - svelte.compile(input, { + const { stats } = svelte.compile(input, { onwarn(warning) { warnings.push({ message: warning.message, @@ -38,6 +38,17 @@ describe("validate", () => { 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); } catch (e) { error = e;