fix(template-names): be more restrictive for snake-case

snake-case now only allows letters, digits and underscores.
This commit is contained in:
Dominik Ferber
2016-03-11 11:04:23 +01:00
parent c505ada789
commit d623a79901
2 changed files with 24 additions and 19 deletions

View File

@@ -5,9 +5,7 @@
* See LICENSE file in root directory for full license.
*/
// -----------------------------------------------------------------------------
// Rule Definition
// -----------------------------------------------------------------------------
const templateProps = new Set([
'onCreated',
'onRendered',
@@ -29,19 +27,25 @@ const isTemplateMemberExpression = node => (
node.object.type === 'MemberExpression' &&
node.object.object.type === 'Identifier' &&
node.object.object.name === 'Template' &&
node.object.property.type === 'Identifier' &&
(
node.object.property.type === 'Identifier' ||
node.object.property.type === 'Literal'
) &&
node.property.type === 'Identifier' &&
templateProps.has(node.property.name)
)
const getErrorMessage = expected => `Invalid template naming convention, expected "${expected}"`
// assuming node type is always either Identifier or Literal
const getNameOfProperty = node => node.type === 'Identifier' ? node.name : node.value
const getErrorMessage = expected => `Invalid template name, expected name to be in ${expected}`
export default context => ({
MemberExpression: node => {
if (!isTemplateMemberExpression(node)) return
const [namingConvention] = context.options
const templateName = node.object.property.name
const templateName = getNameOfProperty(node.object.property)
switch (namingConvention) {
case NAMING_CONVENTIONS.PASCAL:
if (!/^[A-Z]([A-Z]|[a-z]|[0-9])*$/.test(templateName)) {
@@ -49,7 +53,7 @@ export default context => ({
}
break
case NAMING_CONVENTIONS.SNAKE:
if (templateName.toLowerCase() !== templateName) {
if (!/^([a-z]|[0-9]|_)+$/i.test(templateName)) {
context.report(node, getErrorMessage(NAMING_CONVENTIONS.SNAKE))
}
break

View File

@@ -15,6 +15,7 @@ const ruleTester = new RuleTester()
ruleTester.run('template-names', rule, {
valid: [
'Template["foo"].helpers',
'Template.foo.helpers',
'Template.foo01.helpers',
'Template.foo19bar.helpers',
@@ -42,69 +43,69 @@ ruleTester.run('template-names', rule, {
{
code: 'Template.foo_bar.onCreated',
errors: [
{ message: 'Invalid template naming convention, expected "camel-case"', type: 'MemberExpression' },
{ message: 'Invalid template name, expected name to be in camel-case', type: 'MemberExpression' },
],
},
{
code: 'Template.foo_bar.onRendered',
errors: [
{ message: 'Invalid template naming convention, expected "camel-case"', type: 'MemberExpression' },
{ message: 'Invalid template name, expected name to be in camel-case', type: 'MemberExpression' },
],
},
{
code: 'Template.foo_bar.onDestroyed',
errors: [
{ message: 'Invalid template naming convention, expected "camel-case"', type: 'MemberExpression' },
{ message: 'Invalid template name, expected name to be in camel-case', type: 'MemberExpression' },
],
},
{
code: 'Template.foo_bar.events',
errors: [
{ message: 'Invalid template naming convention, expected "camel-case"', type: 'MemberExpression' },
{ message: 'Invalid template name, expected name to be in camel-case', type: 'MemberExpression' },
],
},
{
code: 'Template.foo_bar.helpers',
errors: [
{ message: 'Invalid template naming convention, expected "camel-case"', type: 'MemberExpression' },
{ message: 'Invalid template name, expected name to be in camel-case', type: 'MemberExpression' },
],
},
{
code: 'Template.foo_bar.created',
errors: [
{ message: 'Invalid template naming convention, expected "camel-case"', type: 'MemberExpression' },
{ message: 'Invalid template name, expected name to be in camel-case', type: 'MemberExpression' },
],
},
{
code: 'Template.foo_bar.rendered',
errors: [
{ message: 'Invalid template naming convention, expected "camel-case"', type: 'MemberExpression' },
{ message: 'Invalid template name, expected name to be in camel-case', type: 'MemberExpression' },
],
},
{
code: 'Template.foo_bar.destroyed',
errors: [
{ message: 'Invalid template naming convention, expected "camel-case"', type: 'MemberExpression' },
{ message: 'Invalid template name, expected name to be in camel-case', type: 'MemberExpression' },
],
},
{
code: 'Template.foo_bar.helpers({})',
errors: [
{ message: 'Invalid template naming convention, expected "camel-case"', type: 'MemberExpression' },
{ message: 'Invalid template name, expected name to be in camel-case', type: 'MemberExpression' },
],
},
{
code: 'Template.foo_bar.helpers({})',
options: ['pascal-case'],
errors: [
{ message: 'Invalid template naming convention, expected "pascal-case"', type: 'MemberExpression' },
{ message: 'Invalid template name, expected name to be in pascal-case', type: 'MemberExpression' },
],
},
{
code: 'Template.fooBar.helpers({})',
code: 'Template["foo-bar"].helpers({})',
options: ['snake-case'],
errors: [
{ message: 'Invalid template naming convention, expected "snake-case"', type: 'MemberExpression' },
{ message: 'Invalid template name, expected name to be in snake-case', type: 'MemberExpression' },
],
},
],