diff --git a/lib/rules/template-names.js b/lib/rules/template-names.js index 41b114d4c1..713ea92db4 100644 --- a/lib/rules/template-names.js +++ b/lib/rules/template-names.js @@ -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 diff --git a/tests/lib/rules/template-names.js b/tests/lib/rules/template-names.js index 954b20d356..f6bca00aba 100644 --- a/tests/lib/rules/template-names.js +++ b/tests/lib/rules/template-names.js @@ -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' }, ], }, ],