Merge pull request #1759 from sveltejs/gh-1745

deprecate onerror
pull/1775/head
Rich Harris 6 years ago committed by GitHub
commit f1204bfad9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -5,19 +5,16 @@ import renderDOM from './render-dom/index';
import renderSSR from './render-ssr/index'; 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';
import deprecate from '../utils/deprecate';
function normalize_options(options: CompileOptions): CompileOptions { function normalize_options(options: CompileOptions): CompileOptions {
let normalized = assign({ generate: 'dom', dev: false }, options); let normalized = assign({ generate: 'dom', dev: false }, options);
const { onwarn, onerror } = normalized; const { onwarn } = normalized;
normalized.onwarn = onwarn normalized.onwarn = onwarn
? (warning: Warning) => onwarn(warning, default_onwarn) ? (warning: Warning) => onwarn(warning, default_onwarn)
: default_onwarn; : default_onwarn;
normalized.onerror = onerror
? (error: Error) => onerror(error, default_onerror)
: default_onerror;
return normalized; return normalized;
} }
@ -29,10 +26,6 @@ function default_onwarn({ start, message }: Warning) {
} }
} }
function default_onerror(error: Error) {
throw error;
}
function validate_options(options: CompileOptions, stats: Stats) { function validate_options(options: CompileOptions, stats: Stats) {
const { name, filename } = options; const { name, filename } = options;
@ -52,7 +45,17 @@ function validate_options(options: CompileOptions, stats: Stats) {
} }
} }
export default function compile(source: string, options: CompileOptions) { export default function compile(source: string, options: CompileOptions = {}) {
const onerror = options.onerror || (err => {
throw err;
});
if (options.onerror) {
// TODO remove in v3
deprecate(`Instead of using options.onerror, wrap svelte.compile in a try-catch block`);
delete options.onerror;
}
options = normalize_options(options); options = normalize_options(options);
const stats = new Stats({ const stats = new Stats({
@ -88,7 +91,6 @@ export default function compile(source: string, options: CompileOptions) {
return renderDOM(component, options); return renderDOM(component, options);
} catch (err) { } catch (err) {
options.onerror(err); onerror(err);
return;
} }
} }

@ -276,7 +276,7 @@ function getCompatibilityStatements(dependencies: Dependency[]) {
} }
function getGlobals(dependencies: Dependency[], options: CompileOptions) { function getGlobals(dependencies: Dependency[], options: CompileOptions) {
const { globals, onerror, onwarn } = options; const { globals, onwarn } = options;
const globalFn = getGlobalFn(globals); const globalFn = getGlobalFn(globals);
return dependencies.map(d => { return dependencies.map(d => {
@ -284,10 +284,9 @@ function getGlobals(dependencies: Dependency[], options: CompileOptions) {
if (!name) { if (!name) {
if (d.name.startsWith('__import')) { if (d.name.startsWith('__import')) {
const error = new Error( throw new Error(
`Could not determine name for imported module '${d.source}' use options.globals` `Could not determine name for imported module '${d.source}' use options.globals`
); );
onerror(error);
} else { } else {
const warning = { const warning = {
code: `options-missing-globals`, code: `options-missing-globals`,

@ -1,24 +1,30 @@
import compile from './compile/index'; import compile from './compile/index';
import { CompileOptions } from './interfaces'; import { CompileOptions } from './interfaces';
import deprecate from './utils/deprecate';
export function create(source: string, options: CompileOptions = {}) { export function create(source: string, options: CompileOptions = {}) {
const onerror = options.onerror || (err => {
throw err;
});
if (options.onerror) {
// TODO remove in v3
deprecate(`Instead of using options.onerror, wrap svelte.create in a try-catch block`);
delete options.onerror;
}
options.format = 'eval'; options.format = 'eval';
try {
const compiled = compile(source, options); const compiled = compile(source, options);
if (!compiled || !compiled.js.code) { if (!compiled || !compiled.js.code) {
return; return;
} }
try {
return (new Function(`return ${compiled.js.code}`))(); return (new Function(`return ${compiled.js.code}`))();
} catch (err) { } catch (err) {
if (options.onerror) { onerror(err);
options.onerror(err);
return;
} else {
throw err;
}
} }
} }

@ -60,10 +60,10 @@ export interface CompileOptions {
preserveComments?: boolean | false; preserveComments?: boolean | false;
onerror?: (error: Error) => void;
onwarn?: (warning: Warning) => void; onwarn?: (warning: Warning) => void;
// to remove in v3 // to remove in v3
onerror?: (error: Error) => void;
skipIntroByDefault?: boolean; skipIntroByDefault?: boolean;
nestedTransitions?: boolean; nestedTransitions?: boolean;
} }

@ -0,0 +1,8 @@
const seen = new Set();
export default function deprecate(message: string, code = message) {
if (seen.has(code)) return;
seen.add(code);
console.warn(`[svelte] DEPRECATION: ${message}`);
}

@ -48,6 +48,7 @@ describe('parse', () => {
}); });
}); });
// TODO remove in v3
it('handles errors with options.onerror', () => { it('handles errors with options.onerror', () => {
let errored = false; let errored = false;
@ -61,6 +62,7 @@ describe('parse', () => {
assert.ok(errored); assert.ok(errored);
}); });
// TODO remove in v3
it('throws without options.onerror', () => { it('throws without options.onerror', () => {
assert.throws(() => { assert.throws(() => {
svelte.compile(`<h1>unclosed`); svelte.compile(`<h1>unclosed`);

Loading…
Cancel
Save