Merge pull request #464 from sveltejs/gh-451

Avoid component name as global identifier
pull/467/head
Rich Harris 9 years ago committed by GitHub
commit 92444ca0e1

@ -39,7 +39,7 @@ export default class Generator {
// Svelte's builtin `import { get, ... } from 'svelte/shared.js'`; // Svelte's builtin `import { get, ... } from 'svelte/shared.js'`;
this.importedNames = new Set(); this.importedNames = new Set();
this._aliases = new Map(); this._aliases = new Map();
this._usedNames = new Set(); this._usedNames = new Set( [ name ] );
} }
addSourcemapLocations ( node ) { addSourcemapLocations ( node ) {

@ -44,6 +44,15 @@ export default function validate ( parsed, source, { onerror, onwarn, name, file
onerror( error ); onerror( error );
} }
if ( name && !/^[A-Z]/.test( name ) ) {
const message = `options.name should be capitalised`;
onwarn({
message,
filename,
toString: () => message
});
}
if ( parsed.js ) { if ( parsed.js ) {
validateJs( validator, parsed.js ); validateJs( validator, parsed.js );
} }

@ -11,8 +11,7 @@ export default function components ( validator, prop ) {
checkForComputedKeys( validator, prop.value.properties ); checkForComputedKeys( validator, prop.value.properties );
prop.value.properties.forEach( component => { prop.value.properties.forEach( component => {
const char = component.key.name[0]; if ( !/^[A-Z]/.test( component.key.name ) ) {
if ( char === char.toLowerCase() ) {
validator.warn( `Component names should be capitalised`, component.start ); validator.warn( `Component names should be capitalised`, component.start );
} }
}); });

@ -69,4 +69,19 @@ describe( 'validate', () => {
}); });
}, /options\.name must be a valid identifier/ ); }, /options\.name must be a valid identifier/ );
}); });
it( 'warns if options.name is not capitalised', () => {
const warnings = [];
svelte.compile( '<div></div>', {
name: 'lowercase',
onwarn ( warning ) {
warnings.push({
message: warning.message,
pos: warning.pos,
loc: warning.loc
});
}
});
assert.deepEqual( warnings, [ { message: 'options.name should be capitalised', pos: undefined, loc: undefined } ] );
});
}); });

Loading…
Cancel
Save