From e76f4fe6cd215069181210f8747cd87f72cd9ae8 Mon Sep 17 00:00:00 2001 From: Conduitry Date: Tue, 4 Apr 2017 17:13:12 -0400 Subject: [PATCH 1/3] recognize component name as a global identifier to be avoided (#451) --- src/generators/Generator.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/generators/Generator.js b/src/generators/Generator.js index 370771eb39..272e843142 100644 --- a/src/generators/Generator.js +++ b/src/generators/Generator.js @@ -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 ) { From 4fe03652f8ef564cb78ccc37c686b869d079fe12 Mon Sep 17 00:00:00 2001 From: Conduitry Date: Mon, 10 Apr 2017 18:42:33 -0400 Subject: [PATCH 2/3] warn if options.name does not begin with a capital letter; tidying --- src/validate/index.js | 9 +++++++++ src/validate/js/propValidators/components.js | 3 +-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/validate/index.js b/src/validate/index.js index d4bd6e7cb2..e1e536f4bd 100644 --- a/src/validate/index.js +++ b/src/validate/index.js @@ -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 ); } diff --git a/src/validate/js/propValidators/components.js b/src/validate/js/propValidators/components.js index d8fc4aadde..81745bdf92 100644 --- a/src/validate/js/propValidators/components.js +++ b/src/validate/js/propValidators/components.js @@ -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 ); } }); From 86035c3f9916bf4dec9e27a3f722a1df5061c805 Mon Sep 17 00:00:00 2001 From: Conduitry Date: Mon, 10 Apr 2017 18:56:55 -0400 Subject: [PATCH 3/3] unit test --- test/validator/index.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/validator/index.js b/test/validator/index.js index 9b88441f34..8ebe0be294 100644 --- a/test/validator/index.js +++ b/test/validator/index.js @@ -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( '
', { + 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 } ] ); + }); });