remove onwarn option, just use stats.warnings instead

pull/2093/head
Richard Harris 6 years ago
parent 871147260c
commit f1d4ff6268

@ -26,8 +26,6 @@ function collapseTimings(timings) {
} }
export default class Stats { export default class Stats {
onwarn: (warning: Warning) => void;
startTime: number; startTime: number;
currentTiming: Timing; currentTiming: Timing;
currentChildren: Timing[]; currentChildren: Timing[];
@ -35,15 +33,11 @@ export default class Stats {
stack: Timing[]; stack: Timing[];
warnings: Warning[]; warnings: Warning[];
constructor({ onwarn }: { constructor() {
onwarn: (warning: Warning) => void
}) {
this.startTime = now(); this.startTime = now();
this.stack = []; this.stack = [];
this.currentChildren = this.timings = []; this.currentChildren = this.timings = [];
this.onwarn = onwarn;
this.warnings = []; this.warnings = [];
} }
@ -114,6 +108,5 @@ export default class Stats {
warn(warning) { warn(warning) {
this.warnings.push(warning); this.warnings.push(warning);
this.onwarn(warning);
} }
} }

@ -54,11 +54,7 @@ function get_name(filename) {
export default function compile(source: string, options: CompileOptions = {}) { export default function compile(source: string, options: CompileOptions = {}) {
options = assign({ generate: 'dom', dev: false }, options); options = assign({ generate: 'dom', dev: false }, options);
const stats = new Stats({ const stats = new Stats();
onwarn: options.onwarn
? (warning: Warning) => options.onwarn(warning, default_onwarn)
: default_onwarn
});
let ast: Ast; let ast: Ast;

@ -60,8 +60,6 @@ export interface CompileOptions {
css?: boolean; css?: boolean;
preserveComments?: boolean | false; preserveComments?: boolean | false;
onwarn?: (warning: Warning, default_onwarn?: (warning: Warning) => void) => void;
} }
export interface Visitor { export interface Visitor {

@ -2,7 +2,7 @@ import * as assert from 'assert';
import * as fs from 'fs'; import * as fs from 'fs';
import { env, normalizeHtml, svelte } from '../helpers.js'; import { env, normalizeHtml, svelte } from '../helpers.js';
function tryRequire(file) { function try_require(file) {
try { try {
const mod = require(file); const mod = require(file);
return mod.default || mod; return mod.default || mod;
@ -12,7 +12,7 @@ function tryRequire(file) {
} }
} }
function normalizeWarning(warning) { function normalize_warning(warning) {
warning.frame = warning.frame warning.frame = warning.frame
.replace(/^\n/, '') .replace(/^\n/, '')
.replace(/^\t+/gm, '') .replace(/^\t+/gm, '')
@ -49,47 +49,30 @@ describe('css', () => {
} }
(solo ? it.only : skip ? it.skip : it)(dir, () => { (solo ? it.only : skip ? it.skip : it)(dir, () => {
const config = tryRequire(`./samples/${dir}/_config.js`) || {}; const config = try_require(`./samples/${dir}/_config.js`) || {};
const input = fs const input = fs
.readFileSync(`test/css/samples/${dir}/input.svelte`, 'utf-8') .readFileSync(`test/css/samples/${dir}/input.svelte`, 'utf-8')
.replace(/\s+$/, ''); .replace(/\s+$/, '');
const expectedWarnings = (config.warnings || []).map(normalizeWarning); const expected_warnings = (config.warnings || []).map(normalize_warning);
const domWarnings = [];
const ssrWarnings = [];
const dom = svelte.compile( const dom = svelte.compile(
input, input,
Object.assign(config, { Object.assign(config, { format: 'cjs' })
format: 'cjs',
onwarn: warning => {
domWarnings.push(warning);
}
})
); );
assert.deepEqual(dom.stats.warnings, domWarnings);
const ssr = svelte.compile( const ssr = svelte.compile(
input, input,
Object.assign(config, { Object.assign(config, { format: 'cjs', generate: 'ssr' })
format: 'cjs',
generate: 'ssr',
onwarn: warning => {
ssrWarnings.push(warning);
}
})
); );
assert.deepEqual(dom.stats.warnings, domWarnings);
assert.equal(dom.css.code, ssr.css.code); assert.equal(dom.css.code, ssr.css.code);
assert.deepEqual( const dom_warnings = dom.stats.warnings.map(normalize_warning);
domWarnings.map(normalizeWarning), const ssr_warnings = ssr.stats.warnings.map(normalize_warning);
ssrWarnings.map(normalizeWarning)
); assert.deepEqual(dom_warnings, ssr_warnings);
assert.deepEqual(domWarnings.map(normalizeWarning), expectedWarnings); assert.deepEqual(dom_warnings.map(normalize_warning), expected_warnings);
fs.writeFileSync(`test/css/samples/${dir}/_actual.css`, dom.css.code); fs.writeFileSync(`test/css/samples/${dir}/_actual.css`, dom.css.code);
const expected = { const expected = {

@ -18,42 +18,32 @@ describe("validate", () => {
const config = loadConfig(`./validator/samples/${dir}/_config.js`); const config = loadConfig(`./validator/samples/${dir}/_config.js`);
const input = fs.readFileSync(`test/validator/samples/${dir}/input.svelte`, "utf-8").replace(/\s+$/, ""); const input = fs.readFileSync(`test/validator/samples/${dir}/input.svelte`, "utf-8").replace(/\s+$/, "");
const expectedWarnings = tryToLoadJson(`test/validator/samples/${dir}/warnings.json`) || []; const expected_warnings = tryToLoadJson(`test/validator/samples/${dir}/warnings.json`) || [];
const expectedErrors = tryToLoadJson(`test/validator/samples/${dir}/errors.json`); const expected_errors = tryToLoadJson(`test/validator/samples/${dir}/errors.json`);
let error; let error;
try { try {
const warnings = [];
const { stats } = svelte.compile(input, { const { stats } = svelte.compile(input, {
onwarn(warning) {
const { code, message, pos, start, end } = warning;
warnings.push({ code, message, pos, start, end });
},
dev: config.dev, dev: config.dev,
legacy: config.legacy, legacy: config.legacy,
generate: false generate: false
}); });
assert.equal(stats.warnings.length, warnings.length); const warnings = stats.warnings.map(w => ({
stats.warnings.forEach((full, i) => { code: w.code,
const lite = warnings[i]; message: w.message,
assert.deepEqual({ pos: w.pos,
code: full.code, start: w.start,
message: full.message, end: w.end
pos: full.pos, }));
start: full.start,
end: full.end
}, lite);
});
assert.deepEqual(warnings, expectedWarnings); assert.deepEqual(warnings, expected_warnings);
} catch (e) { } catch (e) {
error = e; error = e;
} }
const expected = expectedErrors && expectedErrors[0]; const expected = expected_errors && expected_errors[0];
if (error || expected) { if (error || expected) {
if (error && !expected) { if (error && !expected) {

Loading…
Cancel
Save