Skip to content

Commit 298bfb8

Browse files
authored
Merge pull request #207 from sveltejs/gh-102
throw if options.name is illegal
2 parents c1fc808 + 811bef0 commit 298bfb8

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/validate/index.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import validateHtml from './html/index.js';
33
import { getLocator } from 'locate-character';
44
import getCodeFrame from '../utils/getCodeFrame.js';
55

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

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

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

21-
if ( onerror ) {
22-
onerror( error );
23-
} else {
24-
throw error;
25-
}
21+
onerror( error );
2622
},
2723

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

46+
if ( name && !/^[a-zA-Z_$][a-zA-Z_$0-9]*$/.test( name ) ) {
47+
const error = new Error( `options.name must be a valid identifier` );
48+
onerror( error );
49+
}
50+
5051
if ( parsed.js ) {
5152
validateJs( validator, parsed.js );
5253
}

test/validate.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,12 @@ describe( 'validate', () => {
5858
}
5959
});
6060
});
61+
62+
it( 'errors if options.name is illegal', () => {
63+
assert.throws( () => {
64+
svelte.compile( '<div></div>', {
65+
name: 'not.valid'
66+
});
67+
}, /options\.name must be a valid identifier/ );
68+
});
6169
});

0 commit comments

Comments
 (0)