Skip to content

Commit 119b619

Browse files
authored
Merge pull request #5 from sveltejs/error-context
Provide source context with error
2 parents 7c54d92 + 2d6b988 commit 119b619

File tree

6 files changed

+126
-55
lines changed

6 files changed

+126
-55
lines changed

index.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,15 @@ module.exports = function(source, map) {
99

1010
try {
1111
let { code, map } = compile(source, {
12-
// name: CamelCase component name
1312
filename: filename,
1413
format: query.format || 'es',
15-
name: query.name,
16-
onerror: (err) => {
17-
console.log(err.message);
18-
this.emitError(err.message);
19-
},
20-
onwarn: (warn) => {
21-
console.log(warn.message);
22-
this.emitWarn(warn.message);
23-
}
14+
name: query.name
2415
});
2516

2617
this.callback(null, code, map);
2718
} catch (err) {
28-
this.callback(err);
19+
// wrap error to provide correct
20+
// context when logging to console
21+
this.callback(new Error(err.toString()));
2922
}
3023
};

test/fixtures/bad.html

Lines changed: 0 additions & 2 deletions
This file was deleted.

test/fixtures/export-error.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<p>Count: {{count}}</p>
2+
3+
<script>
4+
export {
5+
foo: 'BAR'
6+
};
7+
</script>

test/fixtures/parse-error.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<p>Count: {{{count}}</p>
2+
<button on:click='set({ count: count + 1 })'>+1</button>

test/fixtures/validation-error.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<p>Count: {{count}}</p>
2+
3+
<script>
4+
export default {
5+
computed: {
6+
foo: 'BAR'
7+
}
8+
};
9+
</script>

test/loader.spec.js

Lines changed: 104 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ describe('loader', function() {
1919

2020
return function() {
2121

22+
const fileContents = readFile(fileName);
23+
2224
const cacheableSpy = spy(function() { });
2325

2426
const callbackSpy = spy(callback);
2527

26-
const fileContents = readFile(fileName);
27-
2828
loader.call({
2929
cacheable: cacheableSpy,
3030
callback: callbackSpy,
3131
filename: fileName,
32-
query: query,
32+
query,
3333
}, fileContents, null);
3434

3535
expect(callbackSpy).to.have.been.called;
@@ -38,7 +38,7 @@ describe('loader', function() {
3838
}
3939

4040

41-
it('should compile good',
41+
it('should compile',
4242
testLoader('test/fixtures/good.html', function(err, code, map) {
4343
expect(err).not.to.exist;
4444

@@ -48,57 +48,119 @@ describe('loader', function() {
4848
);
4949

5050

51-
it('should compile bad',
52-
testLoader('test/fixtures/bad.html', function(err, code, map) {
53-
expect(err).to.exist;
51+
describe('error handling', function() {
5452

55-
expect(code).not.to.exist;
56-
expect(map).not.to.exist;
57-
})
58-
);
53+
it('should handle parse error',
54+
testLoader('test/fixtures/parse-error.html', function(err, code, map, context) {
5955

56+
expect(err).to.exist;
6057

61-
it('should compile with import / ES2015 features',
62-
testLoader('test/fixtures/es2015.html', function(err, code, map) {
63-
expect(err).not.to.exist;
58+
expect(err.message).to.eql(
59+
'Expected }}} (1:18)\n' +
60+
'1: <p>Count: {{{count}}</p>\n' +
61+
' ^\n' +
62+
'2: <button on:click=\'set({ count: count + 1 })\'>+1</button>'
63+
);
6464

65-
expect(code).to.exist;
66-
expect(map).to.exist;
65+
expect(code).not.to.exist;
66+
expect(map).not.to.exist;
67+
})
68+
);
6769

68-
// es2015 statements remain
69-
expect(code).to.contain('import { hello } from \'./utils\';');
70-
expect(code).to.contain('data() {');
71-
})
72-
);
7370

71+
it('should handle wrong export',
72+
testLoader('test/fixtures/export-error.html', function(err, code, map, context) {
7473

75-
it('should compile Component with with nesting',
76-
testLoader('test/fixtures/parent.html', function(err, code, map) {
77-
expect(err).not.to.exist;
74+
expect(err).to.exist;
7875

79-
// es2015 statements remain
80-
expect(code).to.contain('import Nested from \'./nested\';');
76+
expect(err.message).to.eql(
77+
'Unexpected token (5:7)\n' +
78+
'3: <script>\n' +
79+
'4: export {\n' +
80+
'5: foo: \'BAR\'\n' +
81+
' ^\n' +
82+
'6: };\n' +
83+
'7: </script>'
84+
);
8185

82-
expect(code).to.exist;
83-
expect(map).to.exist;
84-
})
85-
);
86+
expect(code).not.to.exist;
87+
expect(map).not.to.exist;
88+
})
89+
);
8690

8791

88-
it('should compile Component to CJS if requested',
89-
testLoader('test/fixtures/good.html', function(err, code, map) {
90-
expect(err).not.to.exist;
91-
expect(code).to.contain('module.exports = SvelteComponent;');
92-
}, { format: 'cjs' })
93-
);
92+
it('should validation error',
93+
testLoader('test/fixtures/validation-error.html', function(err, code, map, context) {
9494

95+
expect(err).to.exist;
9596

96-
it('should compile Component to UMD if requested',
97-
testLoader('test/fixtures/good.html', function(err, code, map) {
98-
expect(err).not.to.exist;
99-
expect(code).to.contain('(global.FooComponent = factory());');
100-
}, { format: 'umd', name: 'FooComponent' })
101-
);
97+
expect(err.message).to.eql(
98+
'Computed properties can be function expressions or arrow function expressions (6:11)\n' +
99+
'4: export default {\n' +
100+
'5: computed: {\n' +
101+
'6: foo: \'BAR\'\n' +
102+
' ^\n' +
103+
'7: }\n' +
104+
'8: };'
105+
);
106+
107+
expect(code).not.to.exist;
108+
expect(map).not.to.exist;
109+
})
110+
);
111+
112+
});
113+
114+
115+
describe('ES2015 features', function() {
116+
117+
it('should keep imports / methods',
118+
testLoader('test/fixtures/es2015.html', function(err, code, map) {
119+
expect(err).not.to.exist;
120+
121+
expect(code).to.exist;
122+
expect(map).to.exist;
123+
124+
// es2015 statements remain
125+
expect(code).to.contain('import { hello } from \'./utils\';');
126+
expect(code).to.contain('data() {');
127+
})
128+
);
129+
130+
131+
it('should keep nested Component import',
132+
testLoader('test/fixtures/parent.html', function(err, code, map) {
133+
expect(err).not.to.exist;
134+
135+
// es2015 statements remain
136+
expect(code).to.contain('import Nested from \'./nested\';');
137+
138+
expect(code).to.exist;
139+
expect(map).to.exist;
140+
})
141+
);
142+
143+
});
144+
145+
146+
describe('configuration via query', function() {
147+
148+
it('should configure CommonJS output',
149+
testLoader('test/fixtures/good.html', function(err, code, map) {
150+
expect(err).not.to.exist;
151+
expect(code).to.contain('module.exports = SvelteComponent;');
152+
}, { format: 'cjs' })
153+
);
154+
155+
156+
it('should configure named UMD output',
157+
testLoader('test/fixtures/good.html', function(err, code, map) {
158+
expect(err).not.to.exist;
159+
expect(code).to.contain('(global.FooComponent = factory());');
160+
}, { format: 'umd', name: 'FooComponent' })
161+
);
162+
163+
});
102164

103165
});
104166

0 commit comments

Comments
 (0)