Skip to content

Commit

Permalink
Merge pull request #464 from sveltejs/gh-451
Browse files Browse the repository at this point in the history
Avoid component name as global identifier
  • Loading branch information
Rich-Harris authored Apr 10, 2017
2 parents 79c4563 + 86035c3 commit 92444ca
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/generators/Generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default class Generator {
// Svelte's builtin `import { get, ... } from 'svelte/shared.js'`;
this.importedNames = new Set();
this._aliases = new Map();
this._usedNames = new Set();
this._usedNames = new Set( [ name ] );
}

addSourcemapLocations ( node ) {
Expand Down
9 changes: 9 additions & 0 deletions src/validate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ export default function validate ( parsed, source, { onerror, onwarn, name, file
onerror( error );
}

if ( name && !/^[A-Z]/.test( name ) ) {
const message = `options.name should be capitalised`;
onwarn({
message,
filename,
toString: () => message
});
}

if ( parsed.js ) {
validateJs( validator, parsed.js );
}
Expand Down
3 changes: 1 addition & 2 deletions src/validate/js/propValidators/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ export default function components ( validator, prop ) {
checkForComputedKeys( validator, prop.value.properties );

prop.value.properties.forEach( component => {
const char = component.key.name[0];
if ( char === char.toLowerCase() ) {
if ( !/^[A-Z]/.test( component.key.name ) ) {
validator.warn( `Component names should be capitalised`, component.start );
}
});
Expand Down
15 changes: 15 additions & 0 deletions test/validator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,19 @@ describe( 'validate', () => {
});
}, /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 } ] );
});
});

0 comments on commit 92444ca

Please sign in to comment.