Skip to content

Commit 2d6b988

Browse files
committed
Fix loader: throw validation errors
* add test verifying that we properly throw validation errors * restructure test suite
1 parent 43bb971 commit 2d6b988

File tree

5 files changed

+116
-54
lines changed

5 files changed

+116
-54
lines changed

index.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,7 @@ module.exports = function(source, map) {
1111
let { code, map } = compile(source, {
1212
filename: filename,
1313
format: query.format || 'es',
14-
name: query.name,
15-
onerror: (err) => {
16-
this.emitError(err);
17-
},
18-
onwarn: (warn) => {
19-
this.emitWarn(warn);
20-
}
14+
name: query.name
2115
});
2216

2317
this.callback(null, code, map);

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>
File renamed without changes.

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: 99 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ describe('loader', function() {
2828
loader.call({
2929
cacheable: cacheableSpy,
3030
callback: callbackSpy,
31-
emitWarn: function(warning) {},
32-
emitError: function(e) {},
3331
filename: fileName,
3432
query,
3533
}, fileContents, null);
@@ -40,7 +38,7 @@ describe('loader', function() {
4038
}
4139

4240

43-
it('should compile good',
41+
it('should compile',
4442
testLoader('test/fixtures/good.html', function(err, code, map) {
4543
expect(err).not.to.exist;
4644

@@ -50,65 +48,119 @@ describe('loader', function() {
5048
);
5149

5250

53-
it('should compile bad',
54-
testLoader('test/fixtures/bad.html', function(err, code, map, context) {
51+
describe('error handling', function() {
5552

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

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-
);
56+
expect(err).to.exist;
6457

65-
expect(code).not.to.exist;
66-
expect(map).not.to.exist;
67-
})
68-
);
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+
);
6964

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

71-
it('should compile with import / ES2015 features',
72-
testLoader('test/fixtures/es2015.html', function(err, code, map) {
73-
expect(err).not.to.exist;
7470

75-
expect(code).to.exist;
76-
expect(map).to.exist;
71+
it('should handle wrong export',
72+
testLoader('test/fixtures/export-error.html', function(err, code, map, context) {
7773

78-
// es2015 statements remain
79-
expect(code).to.contain('import { hello } from \'./utils\';');
80-
expect(code).to.contain('data() {');
81-
})
82-
);
74+
expect(err).to.exist;
8375

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+
);
8485

85-
it('should compile Component with with nesting',
86-
testLoader('test/fixtures/parent.html', function(err, code, map) {
87-
expect(err).not.to.exist;
86+
expect(code).not.to.exist;
87+
expect(map).not.to.exist;
88+
})
89+
);
8890

89-
// es2015 statements remain
90-
expect(code).to.contain('import Nested from \'./nested\';');
9191

92-
expect(code).to.exist;
93-
expect(map).to.exist;
94-
})
95-
);
92+
it('should validation error',
93+
testLoader('test/fixtures/validation-error.html', function(err, code, map, context) {
9694

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

98-
it('should compile Component to CJS if requested',
99-
testLoader('test/fixtures/good.html', function(err, code, map) {
100-
expect(err).not.to.exist;
101-
expect(code).to.contain('module.exports = SvelteComponent;');
102-
}, { format: 'cjs' })
103-
);
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+
);
104106

107+
expect(code).not.to.exist;
108+
expect(map).not.to.exist;
109+
})
110+
);
105111

106-
it('should compile Component to UMD if requested',
107-
testLoader('test/fixtures/good.html', function(err, code, map) {
108-
expect(err).not.to.exist;
109-
expect(code).to.contain('(global.FooComponent = factory());');
110-
}, { format: 'umd', name: 'FooComponent' })
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+
});
112164

113165
});
114166

0 commit comments

Comments
 (0)