remove onwarn option, just use stats.warnings instead

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

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

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

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

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

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

Loading…
Cancel
Save