Skip to content

deprecate onerror #1759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions src/compile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@ import renderDOM from './render-dom/index';
import renderSSR from './render-ssr/index';
import { CompileOptions, Warning, Ast } from '../interfaces';
import Component from './Component';
import deprecate from '../utils/deprecate';

function normalize_options(options: CompileOptions): CompileOptions {
let normalized = assign({ generate: 'dom', dev: false }, options);
const { onwarn, onerror } = normalized;
const { onwarn } = normalized;

normalized.onwarn = onwarn
? (warning: Warning) => onwarn(warning, default_onwarn)
: default_onwarn;

normalized.onerror = onerror
? (error: Error) => onerror(error, default_onerror)
: default_onerror;

return normalized;
}

Expand All @@ -29,10 +26,6 @@ function default_onwarn({ start, message }: Warning) {
}
}

function default_onerror(error: Error) {
throw error;
}

function validate_options(options: CompileOptions, stats: Stats) {
const { name, filename } = options;

Expand All @@ -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);

const stats = new Stats({
Expand Down Expand Up @@ -88,7 +91,6 @@ export default function compile(source: string, options: CompileOptions) {

return renderDOM(component, options);
} catch (err) {
options.onerror(err);
return;
onerror(err);
}
}
5 changes: 2 additions & 3 deletions src/compile/wrapModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,18 +276,17 @@ function getCompatibilityStatements(dependencies: Dependency[]) {
}

function getGlobals(dependencies: Dependency[], options: CompileOptions) {
const { globals, onerror, onwarn } = options;
const { globals, onwarn } = options;
const globalFn = getGlobalFn(globals);

return dependencies.map(d => {
let name = globalFn(d.source);

if (!name) {
if (d.name.startsWith('__import')) {
const error = new Error(
throw new Error(
`Could not determine name for imported module '${d.source}' – use options.globals`
);
onerror(error);
} else {
const warning = {
code: `options-missing-globals`,
Expand Down
28 changes: 17 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
import compile from './compile/index';
import { CompileOptions } from './interfaces';
import deprecate from './utils/deprecate';

export function create(source: string, options: CompileOptions = {}) {
options.format = 'eval';

const compiled = compile(source, options);
const onerror = options.onerror || (err => {
throw err;
});

if (!compiled || !compiled.js.code) {
return;
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';

try {
return (new Function(`return ${compiled.js.code}`))();
} catch (err) {
if (options.onerror) {
options.onerror(err);
const compiled = compile(source, options);

if (!compiled || !compiled.js.code) {
return;
} else {
throw err;
}

return (new Function(`return ${compiled.js.code}`))();
} catch (err) {
onerror(err);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ export interface CompileOptions {

preserveComments?: boolean | false;

onerror?: (error: Error) => void;
onwarn?: (warning: Warning) => void;

// to remove in v3
onerror?: (error: Error) => void;
skipIntroByDefault?: boolean;
nestedTransitions?: boolean;
}
Expand Down
8 changes: 8 additions & 0 deletions src/utils/deprecate.ts
Original file line number Diff line number Diff line change
@@ -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}`);
}
2 changes: 2 additions & 0 deletions test/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ describe('parse', () => {
});
});

// TODO remove in v3
it('handles errors with options.onerror', () => {
let errored = false;

Expand All @@ -61,6 +62,7 @@ describe('parse', () => {
assert.ok(errored);
});

// TODO remove in v3
it('throws without options.onerror', () => {
assert.throws(() => {
svelte.compile(`<h1>unclosed`);
Expand Down