Skip to content

Commit

Permalink
Merge pull request #1811 from sveltejs/gh-1807
Browse files Browse the repository at this point in the history
better error for missing store
  • Loading branch information
Rich-Harris committed Oct 27, 2018
2 parents c29dc42 + 8929aeb commit 5f51856
Show file tree
Hide file tree
Showing 22 changed files with 92 additions and 25 deletions.
15 changes: 12 additions & 3 deletions src/compile/render-dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};`}
Expand Down
8 changes: 8 additions & 0 deletions src/compile/render-ssr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ export default function ssr(
);
}

const debugName = `<${component.customElement ? component.tag : name}>`;

// TODO concatenate CSS maps
const result = (deindent`
${js}
Expand Down Expand Up @@ -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(
Expand Down
5 changes: 4 additions & 1 deletion test/cli/samples/dev/expected/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ function create_main_fragment(component, ctx) {

function Main(options) {
this._debugName = '<Main>';
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;
Expand Down
7 changes: 5 additions & 2 deletions test/runtime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, {
Expand Down
4 changes: 3 additions & 1 deletion test/runtime/samples/binding-indirect-computed/_config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export default {
dev: true,
compileOptions: {
dev: true
},

html: `
<select>
Expand Down
7 changes: 7 additions & 0 deletions test/runtime/samples/dev-error-missing-store/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
compileOptions: {
dev: true
},

error: `<Main$> references store properties, but no store was provided`
};
1 change: 1 addition & 0 deletions test/runtime/samples/dev-error-missing-store/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>{$foo}</p>
4 changes: 3 additions & 1 deletion test/runtime/samples/dev-warning-bad-set-argument/_config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export default {
dev: true,
compileOptions: {
dev: true
},

error(assert, error) {
assert.equal(error.message, `<Main$>.set was called without an object of data key-values to update.`);
Expand Down
4 changes: 3 additions & 1 deletion test/runtime/samples/dev-warning-destroy-twice/_config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export default {
dev: true,
compileOptions: {
dev: true
},

test(assert, component) {
const warn = console.warn; // eslint-disable-line no-console
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export default {
dev: true,
compileOptions: {
dev: true
},

data: {
x: true
Expand Down
4 changes: 3 additions & 1 deletion test/runtime/samples/dev-warning-helper/_config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export default {
dev: true,
compileOptions: {
dev: true
},

data: {
bar: 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export default {
dev: true,
compileOptions: {
dev: true
},

warnings: [
`<Main$> was created without expected data property 'value'`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export default {
dev: true,
compileOptions: {
dev: true
},

data: {
letters: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export default {
dev: true,
compileOptions: {
dev: true
},

warnings: []
};
4 changes: 3 additions & 1 deletion test/runtime/samples/dev-warning-missing-data/_config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export default {
dev: true,
compileOptions: {
dev: true
},

warnings: [
`<Main$> was created without expected data property 'foo'`,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export default {
dev: true,
compileOptions: {
dev: true
},

data: {
a: 42
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export default {
dev: true,
compileOptions: {
dev: true
},

test ( assert, component ) {
try {
Expand Down
4 changes: 3 additions & 1 deletion test/runtime/samples/element-source-location/_config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import path from 'path';

export default {
dev: true,
compileOptions: {
dev: true
},

test(assert, component, target) {
const h1 = target.querySelector('h1');
Expand Down
6 changes: 3 additions & 3 deletions test/runtime/samples/nested-transition-detach-each/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ export default {
html: ``,

compileOptions: {
dev: true
dev: true,
nestedTransitions: true,
skipIntroByDefault: true,
},
nestedTransitions: true,
skipIntroByDefault: true,

test(assert, component, target, window, raf) {
component.set({ visible: true });
Expand Down
4 changes: 3 additions & 1 deletion test/runtime/samples/set-clones-input/_config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export default {
dev: true,
compileOptions: {
dev: true
},

data: {
a: 42
Expand Down
4 changes: 3 additions & 1 deletion test/runtime/samples/store-onstate-dollar/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export default {

html: `<h1>Hello world!</h1>`,

dev: true,
compileOptions: {
dev: true
},

test(assert, component) {
const names = [];
Expand Down
12 changes: 10 additions & 2 deletions test/server-side-rendering/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,16 @@ describe("ssr", () => {
assert.htmlEqual(html, config.html);
}
} catch (err) {
showOutput(cwd, { generate: "ssr" });
throw err;
if (config.error) {
if (typeof config.error === 'function') {
config.error(assert, err);
} else {
assert.equal(config.error, err.message);
}
} else {
showOutput(cwd, { generate: "ssr" });
throw err;
}
}
});
});
Expand Down

0 comments on commit 5f51856

Please sign in to comment.