Skip to content
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

throw if options.name is illegal #207

Merged
merged 2 commits into from
Dec 17, 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
13 changes: 7 additions & 6 deletions src/validate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import validateHtml from './html/index.js';
import { getLocator } from 'locate-character';
import getCodeFrame from '../utils/getCodeFrame.js';

export default function validate ( parsed, source, { onerror, onwarn, filename } ) {
export default function validate ( parsed, source, { onerror, onwarn, name, filename } ) {
const locator = getLocator( source );

const validator = {
Expand All @@ -18,11 +18,7 @@ export default function validate ( parsed, source, { onerror, onwarn, filename }

error.toString = () => `${error.message} (${error.loc.line}:${error.loc.column})\n${error.frame}`;

if ( onerror ) {
onerror( error );
} else {
throw error;
}
onerror( error );
},

warn: ( message, pos ) => {
Expand All @@ -47,6 +43,11 @@ export default function validate ( parsed, source, { onerror, onwarn, filename }
namespace: null
};

if ( name && !/^[a-zA-Z_$][a-zA-Z_$0-9]*$/.test( name ) ) {
const error = new Error( `options.name must be a valid identifier` );
onerror( error );
}

if ( parsed.js ) {
validateJs( validator, parsed.js );
}
Expand Down
8 changes: 8 additions & 0 deletions test/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,12 @@ describe( 'validate', () => {
}
});
});

it( 'errors if options.name is illegal', () => {
assert.throws( () => {
svelte.compile( '<div></div>', {
name: 'not.valid'
});
}, /options\.name must be a valid identifier/ );
});
});