From 8929aebbecf3f01e751faf61ef1335ed3de7a1a2 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sat, 27 Oct 2018 14:32:26 -0400 Subject: [PATCH] better error for missing store - fixes #1807 --- src/compile/render-dom/index.ts | 15 ++++++++++++--- src/compile/render-ssr/index.ts | 8 ++++++++ test/cli/samples/dev/expected/Main.js | 5 ++++- test/runtime/index.js | 7 +++++-- .../samples/binding-indirect-computed/_config.js | 4 +++- .../samples/dev-error-missing-store/_config.js | 7 +++++++ .../samples/dev-error-missing-store/main.html | 1 + .../dev-warning-bad-set-argument/_config.js | 4 +++- .../samples/dev-warning-destroy-twice/_config.js | 4 +++- .../_config.js | 4 +++- .../runtime/samples/dev-warning-helper/_config.js | 4 +++- .../dev-warning-missing-data-binding/_config.js | 4 +++- .../dev-warning-missing-data-each/_config.js | 4 +++- .../_config.js | 4 +++- .../samples/dev-warning-missing-data/_config.js | 4 +++- .../dev-warning-readonly-computed/_config.js | 4 +++- .../_config.js | 4 +++- .../samples/element-source-location/_config.js | 4 +++- .../nested-transition-detach-each/_config.js | 6 +++--- test/runtime/samples/set-clones-input/_config.js | 4 +++- .../samples/store-onstate-dollar/_config.js | 4 +++- test/server-side-rendering/index.js | 12 ++++++++++-- 22 files changed, 92 insertions(+), 25 deletions(-) create mode 100644 test/runtime/samples/dev-error-missing-store/_config.js create mode 100644 test/runtime/samples/dev-error-missing-store/main.html diff --git a/src/compile/render-dom/index.ts b/src/compile/render-dom/index.ts index c6b1f1b9a8..a84f8fefa6 100644 --- a/src/compile/render-dom/index.ts +++ b/src/compile/render-dom/index.ts @@ -146,9 +146,18 @@ export default function dom( const hasInitHooks = !!(templateProperties.oncreate || templateProperties.onstate || templateProperties.onupdate); const constructorBody = deindent` - ${options.dev && `this._debugName = '${debugName}';`} - ${options.dev && !component.customElement && - `if (!options || (!options.target && !options.root)) throw new Error("'target' is a required option");`} + ${options.dev && deindent` + this._debugName = '${debugName}'; + ${!component.customElement && deindent` + if (!options || (!options.target && !options.root)) { + throw new Error("'target' is a required option"); + }`} + ${storeProps.length > 0 && deindent` + if (!options.store) { + throw new Error("${debugName} references store properties, but no store was provided"); + }`} + `} + @init(this, options); ${templateProperties.store && `this.store = %store();`} ${component.refs.size > 0 && `this.refs = {};`} diff --git a/src/compile/render-ssr/index.ts b/src/compile/render-ssr/index.ts index f53d35fdb1..a424b5c2ea 100644 --- a/src/compile/render-ssr/index.ts +++ b/src/compile/render-ssr/index.ts @@ -75,6 +75,8 @@ export default function ssr( ); } + const debugName = `<${component.customElement ? component.tag : name}>`; + // TODO concatenate CSS maps const result = (deindent` ${js} @@ -113,6 +115,12 @@ export default function ssr( ${templateProperties.store && `options.store = %store();`} __result.addComponent(${name}); + ${options.dev && storeProps.length > 0 && deindent` + if (!options.store) { + throw new Error("${debugName} references store properties, but no store was provided"); + } + `} + ctx = Object.assign(${initialState.join(', ')}); ${computations.map( diff --git a/test/cli/samples/dev/expected/Main.js b/test/cli/samples/dev/expected/Main.js index 37f062e397..be11885fa0 100644 --- a/test/cli/samples/dev/expected/Main.js +++ b/test/cli/samples/dev/expected/Main.js @@ -28,7 +28,10 @@ function create_main_fragment(component, ctx) { function Main(options) { this._debugName = '
'; - if (!options || (!options.target && !options.root)) throw new Error("'target' is a required option"); + if (!options || (!options.target && !options.root)) { + throw new Error("'target' is a required option"); + } + init(this, options); this._state = assign({}, options.data); this._intro = true; diff --git a/test/runtime/index.js b/test/runtime/index.js index 7e16f4d179..c16d5164dd 100644 --- a/test/runtime/index.js +++ b/test/runtime/index.js @@ -69,7 +69,6 @@ describe("runtime", () => { compileOptions = config.compileOptions || {}; compileOptions.shared = shared; compileOptions.hydratable = hydrate; - compileOptions.dev = config.dev; compileOptions.store = !!config.store; compileOptions.immutable = config.immutable; compileOptions.skipIntroByDefault = config.skipIntroByDefault; @@ -171,7 +170,11 @@ describe("runtime", () => { }) .catch(err => { if (config.error && !unintendedError) { - config.error(assert, err); + if (typeof config.error === 'function') { + config.error(assert, err); + } else { + assert.equal(config.error, err.message); + } } else { failed.add(dir); showOutput(cwd, { diff --git a/test/runtime/samples/binding-indirect-computed/_config.js b/test/runtime/samples/binding-indirect-computed/_config.js index 595c5f3a95..020a9f3afa 100644 --- a/test/runtime/samples/binding-indirect-computed/_config.js +++ b/test/runtime/samples/binding-indirect-computed/_config.js @@ -1,5 +1,7 @@ export default { - dev: true, + compileOptions: { + dev: true + }, html: `