diff --git a/src/validate/index.js b/src/validate/index.js index dbd2c0de8b..4809044db8 100644 --- a/src/validate/index.js +++ b/src/validate/index.js @@ -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 = { @@ -47,6 +47,16 @@ 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` ); + + if ( onerror ) { + onerror( error ); + } else { + throw error; + } + } + if ( parsed.js ) { validateJs( validator, parsed.js ); } diff --git a/test/validate.js b/test/validate.js index a9e68560f6..4f82cba06b 100644 --- a/test/validate.js +++ b/test/validate.js @@ -58,4 +58,12 @@ describe( 'validate', () => { } }); }); + + it( 'errors if options.name is illegal', () => { + assert.throws( () => { + svelte.compile( '
', { + name: 'not.valid' + }); + }, /options\.name must be a valid identifier/ ); + }); });