diff --git a/site/content/docs/04-compile-time.md b/site/content/docs/04-compile-time.md index 361c9cdbc4..8d4db35958 100644 --- a/site/content/docs/04-compile-time.md +++ b/site/content/docs/04-compile-time.md @@ -45,6 +45,7 @@ The following options can be passed to the compiler. None are required: | `name` | string | `"Component"` | `format` | `"esm"` or `"cjs"` | `"esm"` | `generate` | `"dom"` or `"ssr"` | `"dom"` +| `errorMode` | `"throw"` or `"warn"` | `"throw"` | `dev` | boolean | `false` | `immutable` | boolean | `false` | `hydratable` | boolean | `false` @@ -66,6 +67,7 @@ The following options can be passed to the compiler. None are required: | `name` | `"Component"` | `string` that sets the name of the resulting JavaScript class (though the compiler will rename it if it would otherwise conflict with other variables in scope). It will normally be inferred from `filename`. | `format` | `"esm"` | If `"esm"`, creates a JavaScript module (with `import` and `export`). If `"cjs"`, creates a CommonJS module (with `require` and `module.exports`), which is useful in some server-side rendering situations or for testing. | `generate` | `"dom"` | If `"dom"`, Svelte emits a JavaScript class for mounting to the DOM. If `"ssr"`, Svelte emits an object with a `render` method suitable for server-side rendering. If `false`, no JavaScript or CSS is returned; just metadata. +| `errorMode` | `"throw"` | If `"throw"`, Svelte throws when a compilation error occured. If `"warn"`, Svelte will treat errors as warnings and add them to the warning report. | `dev` | `false` | If `true`, causes extra code to be added to components that will perform runtime checks and provide debugging information during development. | `immutable` | `false` | If `true`, tells the compiler that you promise not to mutate any objects. This allows it to be less conservative about checking whether values have changed. | `hydratable` | `false` | If `true` when generating DOM code, enables the `hydrate: true` runtime option, which allows a component to upgrade existing DOM rather than creating new DOM from scratch. When generating SSR code, this adds markers to `
` elements so that hydration knows which to replace. diff --git a/src/compiler/compile/Component.ts b/src/compiler/compile/Component.ts index 155b64778b..0d1cddacaf 100644 --- a/src/compiler/compile/Component.ts +++ b/src/compiler/compile/Component.ts @@ -412,14 +412,18 @@ export default class Component { message: string; } ) { - error(e.message, { - name: 'ValidationError', - code: e.code, - source: this.source, - start: pos.start, - end: pos.end, - filename: this.compile_options.filename - }); + if (this.compile_options.errorMode === 'warn') { + this.warn(pos, e); + } else { + error(e.message, { + name: 'ValidationError', + code: e.code, + source: this.source, + start: pos.start, + end: pos.end, + filename: this.compile_options.filename + }); + } } warn( @@ -460,7 +464,7 @@ export default class Component { extract_exports(node) { if (node.type === 'ExportDefaultDeclaration') { - this.error(node, { + return this.error(node, { code: 'default-export', message: 'A component cannot have a default export' }); @@ -468,7 +472,7 @@ export default class Component { if (node.type === 'ExportNamedDeclaration') { if (node.source) { - this.error(node, { + return this.error(node, { code: 'not-implemented', message: 'A component currently cannot have an export ... from' }); @@ -550,7 +554,7 @@ export default class Component { scope.declarations.forEach((node, name) => { if (name[0] === '$') { - this.error(node as any, { + return this.error(node as any, { code: 'illegal-declaration', message: 'The $ prefix is reserved, and cannot be used for variable and import names' }); @@ -568,7 +572,7 @@ export default class Component { globals.forEach((node, name) => { if (name[0] === '$') { - this.error(node as any, { + return this.error(node as any, { code: 'illegal-subscription', message: 'Cannot reference store value inside + + + diff --git a/test/validator/samples/error-mode-warn/options.json b/test/validator/samples/error-mode-warn/options.json new file mode 100644 index 0000000000..8d48d2f34e --- /dev/null +++ b/test/validator/samples/error-mode-warn/options.json @@ -0,0 +1,3 @@ +{ + "errorMode": "warn" +} \ No newline at end of file diff --git a/test/validator/samples/error-mode-warn/warnings.json b/test/validator/samples/error-mode-warn/warnings.json new file mode 100644 index 0000000000..cce6324df5 --- /dev/null +++ b/test/validator/samples/error-mode-warn/warnings.json @@ -0,0 +1,47 @@ +[ + { + "code": "invalid-binding", + "message": "Cannot bind to a variable which is not writable", + "pos": 61, + "start": { + "line": 5, + "column": 19, + "character": 61 + }, + "end": { + "line": 5, + "column": 24, + "character": 66 + } + }, + { + "code": "missing-declaration", + "message": "'undeclared' is not defined", + "pos": 88, + "start": { + "character": 88, + "column": 19, + "line": 6 + }, + "end": { + "character": 98, + "column": 29, + "line": 6 + } + }, + { + "code": "binding-undeclared", + "message": "undeclared is not declared", + "pos": 88, + "end": { + "character": 98, + "column": 29, + "line": 6 + }, + "start": { + "character": 88, + "column": 19, + "line": 6 + } + } +] \ No newline at end of file