Skip to content

Provide source context with error #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,15 @@ module.exports = function(source, map) {

try {
let { code, map } = compile(source, {
// name: CamelCase component name
filename: filename,
format: query.format || 'es',
name: query.name,
onerror: (err) => {
console.log(err.message);
this.emitError(err.message);
},
onwarn: (warn) => {
console.log(warn.message);
this.emitWarn(warn.message);
}
name: query.name
});

this.callback(null, code, map);
} catch (err) {
this.callback(err);
// wrap error to provide correct
// context when logging to console
this.callback(new Error(err.toString()));
}
};
2 changes: 0 additions & 2 deletions test/fixtures/bad.html

This file was deleted.

7 changes: 7 additions & 0 deletions test/fixtures/export-error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<p>Count: {{count}}</p>

<script>
export {
foo: 'BAR'
};
</script>
2 changes: 2 additions & 0 deletions test/fixtures/parse-error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<p>Count: {{{count}}</p>
<button on:click='set({ count: count + 1 })'>+1</button>
9 changes: 9 additions & 0 deletions test/fixtures/validation-error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<p>Count: {{count}}</p>

<script>
export default {
computed: {
foo: 'BAR'
}
};
</script>
146 changes: 104 additions & 42 deletions test/loader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ describe('loader', function() {

return function() {

const fileContents = readFile(fileName);

const cacheableSpy = spy(function() { });

const callbackSpy = spy(callback);

const fileContents = readFile(fileName);

loader.call({
cacheable: cacheableSpy,
callback: callbackSpy,
filename: fileName,
query: query,
query,
}, fileContents, null);

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


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

Expand All @@ -48,57 +48,119 @@ describe('loader', function() {
);


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

expect(code).not.to.exist;
expect(map).not.to.exist;
})
);
it('should handle parse error',
testLoader('test/fixtures/parse-error.html', function(err, code, map, context) {

expect(err).to.exist;

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

expect(code).to.exist;
expect(map).to.exist;
expect(code).not.to.exist;
expect(map).not.to.exist;
})
);

// es2015 statements remain
expect(code).to.contain('import { hello } from \'./utils\';');
expect(code).to.contain('data() {');
})
);

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

it('should compile Component with with nesting',
testLoader('test/fixtures/parent.html', function(err, code, map) {
expect(err).not.to.exist;
expect(err).to.exist;

// es2015 statements remain
expect(code).to.contain('import Nested from \'./nested\';');
expect(err.message).to.eql(
'Unexpected token (5:7)\n' +
'3: <script>\n' +
'4: export {\n' +
'5: foo: \'BAR\'\n' +
' ^\n' +
'6: };\n' +
'7: </script>'
);

expect(code).to.exist;
expect(map).to.exist;
})
);
expect(code).not.to.exist;
expect(map).not.to.exist;
})
);


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

expect(err).to.exist;

it('should compile Component to UMD if requested',
testLoader('test/fixtures/good.html', function(err, code, map) {
expect(err).not.to.exist;
expect(code).to.contain('(global.FooComponent = factory());');
}, { format: 'umd', name: 'FooComponent' })
);
expect(err.message).to.eql(
'Computed properties can be function expressions or arrow function expressions (6:11)\n' +
'4: export default {\n' +
'5: computed: {\n' +
'6: foo: \'BAR\'\n' +
' ^\n' +
'7: }\n' +
'8: };'
);

expect(code).not.to.exist;
expect(map).not.to.exist;
})
);

});


describe('ES2015 features', function() {

it('should keep imports / methods',
testLoader('test/fixtures/es2015.html', function(err, code, map) {
expect(err).not.to.exist;

expect(code).to.exist;
expect(map).to.exist;

// es2015 statements remain
expect(code).to.contain('import { hello } from \'./utils\';');
expect(code).to.contain('data() {');
})
);


it('should keep nested Component import',
testLoader('test/fixtures/parent.html', function(err, code, map) {
expect(err).not.to.exist;

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

expect(code).to.exist;
expect(map).to.exist;
})
);

});


describe('configuration via query', function() {

it('should configure CommonJS output',
testLoader('test/fixtures/good.html', function(err, code, map) {
expect(err).not.to.exist;
expect(code).to.contain('module.exports = SvelteComponent;');
}, { format: 'cjs' })
);


it('should configure named UMD output',
testLoader('test/fixtures/good.html', function(err, code, map) {
expect(err).not.to.exist;
expect(code).to.contain('(global.FooComponent = factory());');
}, { format: 'umd', name: 'FooComponent' })
);

});

});

Expand Down