Merge pull request #1926 from sveltejs/gh-1918

always use stats.warn instead of options.onwarn
pull/1928/head
Rich Harris 6 years ago committed by GitHub
commit 4cb2b8dc90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -164,7 +164,7 @@ export default class Component {
this.fragment = new Fragment(this, ast.html); this.fragment = new Fragment(this, ast.html);
if (!options.customElement) this.stylesheet.reify(); if (!options.customElement) this.stylesheet.reify();
this.stylesheet.warnOnUnusedSelectors(options.onwarn); this.stylesheet.warnOnUnusedSelectors(stats);
if (!this.instance_script) { if (!this.instance_script) {
const props = [...this.template_references]; const props = [...this.template_references];
@ -229,6 +229,7 @@ export default class Component {
format, format,
name, name,
options, options,
this.stats,
banner, banner,
options.sveltePath, options.sveltePath,
importedHelpers, importedHelpers,

@ -8,6 +8,7 @@ import removeCSSPrefix from '../../utils/removeCSSPrefix';
import Element from '../nodes/Element'; import Element from '../nodes/Element';
import { Node, Ast, Warning } from '../../interfaces'; import { Node, Ast, Warning } from '../../interfaces';
import Component from '../Component'; import Component from '../Component';
import Stats from '../../Stats';
const isKeyframesNode = (node: Node) => removeCSSPrefix(node.name) === 'keyframes' const isKeyframesNode = (node: Node) => removeCSSPrefix(node.name) === 'keyframes'
@ -391,7 +392,7 @@ export default class Stylesheet {
}); });
} }
warnOnUnusedSelectors(onwarn: (warning: Warning) => void) { warnOnUnusedSelectors(stats: Stats) {
let locator; let locator;
const handler = (selector: Selector) => { const handler = (selector: Selector) => {
@ -404,7 +405,7 @@ export default class Stylesheet {
const frame = getCodeFrame(this.source, start.line - 1, start.column); const frame = getCodeFrame(this.source, start.line - 1, start.column);
const message = `Unused CSS selector`; const message = `Unused CSS selector`;
onwarn({ stats.warn({
code: `css-unused-selector`, code: `css-unused-selector`,
message, message,
frame, frame,

@ -6,17 +6,6 @@ import renderSSR from './render-ssr/index';
import { CompileOptions, Warning, Ast } from '../interfaces'; import { CompileOptions, Warning, Ast } from '../interfaces';
import Component from './Component'; import Component from './Component';
function normalize_options(options: CompileOptions): CompileOptions {
let normalized = assign({ generate: 'dom', dev: false }, options);
const { onwarn } = normalized;
normalized.onwarn = onwarn
? (warning: Warning) => onwarn(warning, default_onwarn)
: default_onwarn;
return normalized;
}
function default_onwarn({ start, message }: Warning) { function default_onwarn({ start, message }: Warning) {
if (start) { if (start) {
console.warn(`(${start.line}:${start.column}) ${message}`); console.warn(`(${start.line}:${start.column}) ${message}`);
@ -45,10 +34,12 @@ function validate_options(options: CompileOptions, stats: Stats) {
} }
export default function compile(source: string, options: CompileOptions = {}) { export default function compile(source: string, options: CompileOptions = {}) {
options = normalize_options(options); options = assign({ generate: 'dom', dev: false }, options);
const stats = new Stats({ const stats = new Stats({
onwarn: options.onwarn onwarn: options.onwarn
? (warning: Warning) => options.onwarn(warning, default_onwarn)
: default_onwarn
}); });
let ast: Ast; let ast: Ast;

@ -1,6 +1,7 @@
import deindent from '../utils/deindent'; import deindent from '../utils/deindent';
import list from '../utils/list'; import list from '../utils/list';
import { CompileOptions, ModuleFormat, Node } from '../interfaces'; import { CompileOptions, ModuleFormat, Node } from '../interfaces';
import Stats from '../Stats';
interface Dependency { interface Dependency {
name: string; name: string;
@ -20,6 +21,7 @@ export default function wrapModule(
format: ModuleFormat, format: ModuleFormat,
name: string, name: string,
options: CompileOptions, options: CompileOptions,
stats: Stats,
banner: string, banner: string,
sveltePath = 'svelte', sveltePath = 'svelte',
helpers: { name: string, alias: string }[], helpers: { name: string, alias: string }[],
@ -34,7 +36,7 @@ export default function wrapModule(
} }
if (format === 'cjs') return cjs(code, name, banner, sveltePath, internalPath, helpers, imports, module_exports); if (format === 'cjs') return cjs(code, name, banner, sveltePath, internalPath, helpers, imports, module_exports);
if (format === 'eval') return expr(code, name, options, banner, imports); if (format === 'eval') return expr(code, name, options, stats, banner, imports);
throw new Error(`options.format is invalid (must be ${list(Object.keys(wrappers))})`); throw new Error(`options.format is invalid (must be ${list(Object.keys(wrappers))})`);
} }
@ -142,6 +144,7 @@ function expr(
code: string, code: string,
name: string, name: string,
options: CompileOptions, options: CompileOptions,
stats: Stats,
banner: string, banner: string,
imports: Node[] imports: Node[]
) { ) {
@ -182,7 +185,7 @@ function expr(
return { name, statements, source: declaration.source.value }; return { name, statements, source: declaration.source.value };
}); });
const globals = getGlobals(dependencies, options); const globals = getGlobals(dependencies, options, stats);
return deindent` return deindent`
(function (${paramString(dependencies)}) { "use strict"; (function (${paramString(dependencies)}) { "use strict";
@ -212,8 +215,8 @@ function getCompatibilityStatements(dependencies: Dependency[]) {
return statements.join('\n'); return statements.join('\n');
} }
function getGlobals(dependencies: Dependency[], options: CompileOptions) { function getGlobals(dependencies: Dependency[], options: CompileOptions, stats: Stats) {
const { globals, onwarn } = options; const { globals } = options;
const globalFn = getGlobalFn(globals); const globalFn = getGlobalFn(globals);
return dependencies.map(d => { return dependencies.map(d => {
@ -225,12 +228,10 @@ function getGlobals(dependencies: Dependency[], options: CompileOptions) {
`Could not determine name for imported module '${d.source}' use options.globals` `Could not determine name for imported module '${d.source}' use options.globals`
); );
} else { } else {
const warning = { stats.warn({
code: `options-missing-globals`, code: `options-missing-globals`,
message: `No name was supplied for imported module '${d.source}'. Guessing '${d.name}', but you should use options.globals`, message: `No name was supplied for imported module '${d.source}'. Guessing '${d.name}', but you should use options.globals`,
}; });
onwarn(warning);
} }
name = d.name; name = d.name;

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

@ -69,6 +69,8 @@ describe('css', () => {
}) })
); );
assert.deepEqual(dom.stats.warnings, domWarnings);
const ssr = svelte.compile( const ssr = svelte.compile(
input, input,
Object.assign(config, { Object.assign(config, {
@ -81,6 +83,8 @@ describe('css', () => {
}) })
); );
assert.deepEqual(dom.stats.warnings, domWarnings);
assert.equal(dom.css.code, ssr.css.code); assert.equal(dom.css.code, ssr.css.code);
assert.deepEqual( assert.deepEqual(

@ -83,43 +83,26 @@ describe("validate", () => {
}); });
it("warns if options.name is not capitalised", () => { it("warns if options.name is not capitalised", () => {
const warnings = []; const { stats } = svelte.compile("<div></div>", {
svelte.compile("<div></div>", {
name: "lowercase", name: "lowercase",
onwarn(warning) {
warnings.push({
code: warning.code,
message: warning.message,
pos: warning.pos,
start: warning.start
});
},
generate: false generate: false
}); });
assert.deepEqual(warnings, [
{ assert.deepEqual(stats.warnings.map(w => ({
code: `options-lowercase-name`, code: w.code,
message: "options.name should be capitalised", message: w.message
pos: undefined, })), [{
start: undefined code: `options-lowercase-name`,
} message: "options.name should be capitalised"
]); }]);
}); });
it("does not warn if options.name begins with non-alphabetic character", () => { it("does not warn if options.name begins with non-alphabetic character", () => {
const warnings = []; const { stats } = svelte.compile("<div></div>", {
svelte.compile("<div></div>", {
name: "_", name: "_",
onwarn(warning) {
warnings.push({
code: warning.code,
message: warning.message,
pos: warning.pos,
start: warning.start
});
},
generate: false generate: false
}); });
assert.deepEqual(warnings, []);
assert.deepEqual(stats.warnings, []);
}); });
}); });

Loading…
Cancel
Save