Skip to content

Commit

Permalink
Merge pull request #397 from bgriffith/hotfix/fix-mixin-name-format
Browse files Browse the repository at this point in the history
Hotfix master - Fix issue where mixin-name-format incorrectly lints extends
  • Loading branch information
DanPurdy committed Nov 16, 2015
2 parents b4956f9 + 250fe3e commit fda9fac
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 39 deletions.
94 changes: 55 additions & 39 deletions lib/rules/mixin-name-format.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Note that this file is nearly identical to function-name-format.js, variable-name-format.js, and placeholder-name-format.js
'use strict';

var helpers = require('../helpers');
Expand All @@ -18,54 +17,71 @@ module.exports = {
strippedName,
violationMessage = false;

if (node.type === 'mixin') {
name = node.first('ident').content;
if (node.is('mixin')) {
if (node.contains('ident')) {
name = node.first('ident').content;
}
}
else {
name = node.first('simpleSelector').first('ident').content;
}

strippedName = name;
// We're not linting extends here
if (node.contains('atkeyword')) {
if (node.first('atkeyword').contains('ident')) {
if (node.first('atkeyword').first('ident').content === 'extend') {
return false;
}
}
}

if (parser.options['allow-leading-underscore'] && name[0] === '_') {
strippedName = name.slice(1);
if (node.contains('simpleSelector')) {
if (node.first('simpleSelector').contains('ident')) {
name = node.first('simpleSelector').first('ident').content;
}
}
}

switch (parser.options.convention) {
case 'hyphenatedlowercase':
if (!helpers.isHyphenatedLowercase(strippedName)) {
violationMessage = 'Mixin \'' + name + '\' should be written in lowercase with hyphens';
}
break;
case 'camelcase':
if (!helpers.isCamelCase(strippedName)) {
violationMessage = 'Mixin \'' + name + '\' should be written in camelCase';
}
break;
case 'snakecase':
if (!helpers.isSnakeCase(strippedName)) {
violationMessage = 'Mixin \'' + name + '\' should be written in snake_case';
if (name) {
strippedName = name;

if (parser.options['allow-leading-underscore'] && name[0] === '_') {
strippedName = name.slice(1);
}
break;
default:
if (!(new RegExp(parser.options.convention).test(strippedName))) {
violationMessage = 'Mixin \'' + name + '\' should match regular expression /' + parser.options.convention + '/';

// convention-message overrides violationMessage
if (parser.options['convention-explanation']) {
violationMessage = parser.options['convention-explanation'];
switch (parser.options.convention) {
case 'hyphenatedlowercase':
if (!helpers.isHyphenatedLowercase(strippedName)) {
violationMessage = 'Mixin \'' + name + '\' should be written in lowercase with hyphens';
}
break;
case 'camelcase':
if (!helpers.isCamelCase(strippedName)) {
violationMessage = 'Mixin \'' + name + '\' should be written in camelCase';
}
break;
case 'snakecase':
if (!helpers.isSnakeCase(strippedName)) {
violationMessage = 'Mixin \'' + name + '\' should be written in snake_case';
}
break;
default:
if (!(new RegExp(parser.options.convention).test(strippedName))) {
violationMessage = 'Mixin \'' + name + '\' should match regular expression /' + parser.options.convention + '/';

// convention-message overrides violationMessage
if (parser.options['convention-explanation']) {
violationMessage = parser.options['convention-explanation'];
}
}
}
}

if (violationMessage) {
result = helpers.addUnique(result, {
'ruleId': parser.rule.name,
'line': node.start.line,
'column': node.start.column,
'message': violationMessage,
'severity': parser.severity
});
if (violationMessage) {
result = helpers.addUnique(result, {
'ruleId': parser.rule.name,
'line': node.start.line,
'column': node.start.column,
'message': violationMessage,
'severity': parser.severity
});
}
}
});

Expand Down
21 changes: 21 additions & 0 deletions tests/sass/mixin-name-format.sass
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,24 @@

.class
+snake_case()

.test
@extend .pure-u-1-6

.test
@at-root .child_snake_case
content: 'foo'

@import '_foo.scss'

@keyframes fade_it
0%
transform: scale(1.0)
25%
transform: scale(1.1)
50%
transform: scale(1.0)
75%
transform: scale(1.2)
100%
transform: scale(1.1)
20 changes: 20 additions & 0 deletions tests/sass/mixin-name-format.scss
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,23 @@
.class {
@include snake_case();
}

.test {
@extend .pure-u-1-6
}

.test {
@at-root .child_snake_case {
content: 'foo';
}
}

@import 'foo.scss';

@keyframes fade_it {
0% { transform: scale(1.0); }
25% { transform: scale(1.1); }
50% { transform: scale(1.0); }
75% { transform: scale(1.2); }
100% { transform: scale(1.1); }
}

0 comments on commit fda9fac

Please sign in to comment.