Skip to content
This repository has been archived by the owner on Oct 2, 2021. It is now read-only.

Commit

Permalink
fix(template-names): be more restrictive for snake-case
Browse files Browse the repository at this point in the history
snake-case now only allows letters, digits and underscores.
  • Loading branch information
dferber90 committed Mar 11, 2016
1 parent c505ada commit d623a79
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
18 changes: 11 additions & 7 deletions lib/rules/template-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
* See LICENSE file in root directory for full license.
*/

// -----------------------------------------------------------------------------
// Rule Definition
// -----------------------------------------------------------------------------

const templateProps = new Set([
'onCreated',
'onRendered',
Expand All @@ -29,27 +27,33 @@ 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)) {
context.report(node, getErrorMessage(NAMING_CONVENTIONS.PASCAL))
}
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
Expand Down
25 changes: 13 additions & 12 deletions tests/lib/rules/template-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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' },
],
},
],
Expand Down

0 comments on commit d623a79

Please sign in to comment.