Skip to content

Commit 4cb2b8d

Browse files
authored
Merge pull request #1926 from sveltejs/gh-1918
always use stats.warn instead of options.onwarn
2 parents ff2a21e + f482927 commit 4cb2b8d

File tree

7 files changed

+34
-53
lines changed

7 files changed

+34
-53
lines changed

src/compile/Component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ export default class Component {
164164
this.fragment = new Fragment(this, ast.html);
165165
if (!options.customElement) this.stylesheet.reify();
166166

167-
this.stylesheet.warnOnUnusedSelectors(options.onwarn);
167+
this.stylesheet.warnOnUnusedSelectors(stats);
168168

169169
if (!this.instance_script) {
170170
const props = [...this.template_references];
@@ -229,6 +229,7 @@ export default class Component {
229229
format,
230230
name,
231231
options,
232+
this.stats,
232233
banner,
233234
options.sveltePath,
234235
importedHelpers,

src/compile/css/Stylesheet.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import removeCSSPrefix from '../../utils/removeCSSPrefix';
88
import Element from '../nodes/Element';
99
import { Node, Ast, Warning } from '../../interfaces';
1010
import Component from '../Component';
11+
import Stats from '../../Stats';
1112

1213
const isKeyframesNode = (node: Node) => removeCSSPrefix(node.name) === 'keyframes'
1314

@@ -391,7 +392,7 @@ export default class Stylesheet {
391392
});
392393
}
393394

394-
warnOnUnusedSelectors(onwarn: (warning: Warning) => void) {
395+
warnOnUnusedSelectors(stats: Stats) {
395396
let locator;
396397

397398
const handler = (selector: Selector) => {
@@ -404,7 +405,7 @@ export default class Stylesheet {
404405
const frame = getCodeFrame(this.source, start.line - 1, start.column);
405406
const message = `Unused CSS selector`;
406407

407-
onwarn({
408+
stats.warn({
408409
code: `css-unused-selector`,
409410
message,
410411
frame,

src/compile/index.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,6 @@ import renderSSR from './render-ssr/index';
66
import { CompileOptions, Warning, Ast } from '../interfaces';
77
import Component from './Component';
88

9-
function normalize_options(options: CompileOptions): CompileOptions {
10-
let normalized = assign({ generate: 'dom', dev: false }, options);
11-
const { onwarn } = normalized;
12-
13-
normalized.onwarn = onwarn
14-
? (warning: Warning) => onwarn(warning, default_onwarn)
15-
: default_onwarn;
16-
17-
return normalized;
18-
}
19-
209
function default_onwarn({ start, message }: Warning) {
2110
if (start) {
2211
console.warn(`(${start.line}:${start.column}) – ${message}`);
@@ -45,10 +34,12 @@ function validate_options(options: CompileOptions, stats: Stats) {
4534
}
4635

4736
export default function compile(source: string, options: CompileOptions = {}) {
48-
options = normalize_options(options);
37+
options = assign({ generate: 'dom', dev: false }, options);
4938

5039
const stats = new Stats({
5140
onwarn: options.onwarn
41+
? (warning: Warning) => options.onwarn(warning, default_onwarn)
42+
: default_onwarn
5243
});
5344

5445
let ast: Ast;

src/compile/wrapModule.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import deindent from '../utils/deindent';
22
import list from '../utils/list';
33
import { CompileOptions, ModuleFormat, Node } from '../interfaces';
4+
import Stats from '../Stats';
45

56
interface Dependency {
67
name: string;
@@ -20,6 +21,7 @@ export default function wrapModule(
2021
format: ModuleFormat,
2122
name: string,
2223
options: CompileOptions,
24+
stats: Stats,
2325
banner: string,
2426
sveltePath = 'svelte',
2527
helpers: { name: string, alias: string }[],
@@ -34,7 +36,7 @@ export default function wrapModule(
3436
}
3537

3638
if (format === 'cjs') return cjs(code, name, banner, sveltePath, internalPath, helpers, imports, module_exports);
37-
if (format === 'eval') return expr(code, name, options, banner, imports);
39+
if (format === 'eval') return expr(code, name, options, stats, banner, imports);
3840

3941
throw new Error(`options.format is invalid (must be ${list(Object.keys(wrappers))})`);
4042
}
@@ -142,6 +144,7 @@ function expr(
142144
code: string,
143145
name: string,
144146
options: CompileOptions,
147+
stats: Stats,
145148
banner: string,
146149
imports: Node[]
147150
) {
@@ -182,7 +185,7 @@ function expr(
182185
return { name, statements, source: declaration.source.value };
183186
});
184187

185-
const globals = getGlobals(dependencies, options);
188+
const globals = getGlobals(dependencies, options, stats);
186189

187190
return deindent`
188191
(function (${paramString(dependencies)}) { "use strict";
@@ -212,8 +215,8 @@ function getCompatibilityStatements(dependencies: Dependency[]) {
212215
return statements.join('\n');
213216
}
214217

215-
function getGlobals(dependencies: Dependency[], options: CompileOptions) {
216-
const { globals, onwarn } = options;
218+
function getGlobals(dependencies: Dependency[], options: CompileOptions, stats: Stats) {
219+
const { globals } = options;
217220
const globalFn = getGlobalFn(globals);
218221

219222
return dependencies.map(d => {
@@ -225,12 +228,10 @@ function getGlobals(dependencies: Dependency[], options: CompileOptions) {
225228
`Could not determine name for imported module '${d.source}' – use options.globals`
226229
);
227230
} else {
228-
const warning = {
231+
stats.warn({
229232
code: `options-missing-globals`,
230233
message: `No name was supplied for imported module '${d.source}'. Guessing '${d.name}', but you should use options.globals`,
231-
};
232-
233-
onwarn(warning);
234+
});
234235
}
235236

236237
name = d.name;

src/interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export interface CompileOptions {
6060

6161
preserveComments?: boolean | false;
6262

63-
onwarn?: (warning: Warning) => void;
63+
onwarn?: (warning: Warning, default_onwarn?: (warning: Warning) => void) => void;
6464
}
6565

6666
export interface Visitor {

test/css/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ describe('css', () => {
6969
})
7070
);
7171

72+
assert.deepEqual(dom.stats.warnings, domWarnings);
73+
7274
const ssr = svelte.compile(
7375
input,
7476
Object.assign(config, {
@@ -81,6 +83,8 @@ describe('css', () => {
8183
})
8284
);
8385

86+
assert.deepEqual(dom.stats.warnings, domWarnings);
87+
8488
assert.equal(dom.css.code, ssr.css.code);
8589

8690
assert.deepEqual(

test/validator/index.js

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -83,43 +83,26 @@ describe("validate", () => {
8383
});
8484

8585
it("warns if options.name is not capitalised", () => {
86-
const warnings = [];
87-
svelte.compile("<div></div>", {
86+
const { stats } = svelte.compile("<div></div>", {
8887
name: "lowercase",
89-
onwarn(warning) {
90-
warnings.push({
91-
code: warning.code,
92-
message: warning.message,
93-
pos: warning.pos,
94-
start: warning.start
95-
});
96-
},
9788
generate: false
9889
});
99-
assert.deepEqual(warnings, [
100-
{
101-
code: `options-lowercase-name`,
102-
message: "options.name should be capitalised",
103-
pos: undefined,
104-
start: undefined
105-
}
106-
]);
90+
91+
assert.deepEqual(stats.warnings.map(w => ({
92+
code: w.code,
93+
message: w.message
94+
})), [{
95+
code: `options-lowercase-name`,
96+
message: "options.name should be capitalised"
97+
}]);
10798
});
10899

109100
it("does not warn if options.name begins with non-alphabetic character", () => {
110-
const warnings = [];
111-
svelte.compile("<div></div>", {
101+
const { stats } = svelte.compile("<div></div>", {
112102
name: "_",
113-
onwarn(warning) {
114-
warnings.push({
115-
code: warning.code,
116-
message: warning.message,
117-
pos: warning.pos,
118-
start: warning.start
119-
});
120-
},
121103
generate: false
122104
});
123-
assert.deepEqual(warnings, []);
105+
106+
assert.deepEqual(stats.warnings, []);
124107
});
125108
});

0 commit comments

Comments
 (0)