Skip to content

Commit

Permalink
add some tests for missing blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
ernscht committed Jul 6, 2016
1 parent ded41b7 commit 2a40d00
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 14 deletions.
35 changes: 26 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,39 +30,56 @@ module.exports = stylelint.createPlugin(ruleName, function(options) {
.concat(validHelpers);

function getClassNameErrors(className, namespace) {
var patternStart = '';
var patternEnd = '';

if (/[A-Z]/.test(className)) {
return 'contain no uppercase letters';
}
if (namespace) {
if (className.indexOf(namespace) !== 0) {
return 'use the namespace "' + namespace + '"';
}
className = className.substr(namespace.length);
patternStart += namespace;
}

var prefix = className.split('-')[0];
var helper = '';
if (className.indexOf('-') === -1 || validPrefixes.indexOf(prefix) === -1) {
return 'start with a valid prefix: "' + namespace + validPrefixes.join('-", "' + namespace) + '-"'
}
if (validHelpers.indexOf(prefix) !== -1) {
var subPrefix = className.split('-')[1];
if (validComponents.indexOf(subPrefix) === -1) {
return 'use ' + namespace + prefix + '-[prefix]-[block] syntax. Valid ' + namespace + prefix + ' prefixes: "' + namespace +
helper = prefix;
prefix = className.split('-')[1];
patternStart += helper + '-';
patternEnd += '--[' + helper + ']';
className = className.substr(helper.length + 1 + prefix.length + 1);
if (validComponents.indexOf(prefix) === -1) {
return 'use ' + patternStart + '[prefix]-[block]' + patternEnd + ' syntax. Valid ' + namespace + helper + ' prefixes: "' + namespace +
validComponents.map(function(component) {
return prefix + '-' + component;
return helper + '-' + component;
}).join('-", "' + namespace) + '-"';
}
}
else {
className = className.substr(prefix.length + 1);
}

if (!/^[a-z]/.test(className)) {
return 'use ' + patternStart + '[prefix]-[block]' + patternEnd + ' syntax';
}
if (/__(_|.*__)/.test(className)) {
return 'use only one "__" element separator';
}
if (/--(-|.*--)/.test(className)) {
return 'use only one "--" modifier separator';
}
if (/[A-Z]/.test(className)) {
return 'contain no uppercase letters';
}
if (/(^|[^_])_([^_]|$)/.test(className)) {
return 'use "_" only as element separator'
}
if (prefix === 'state' && className.indexOf('--') === -1) {
return 'use ' + namespace + 'state-[prefix]-[block]--[state] syntax';
if (helper === 'state' && className.indexOf('--') === -1) {
return 'use ' + patternStart + '[prefix]-[block]' + patternEnd + ' syntax';
}
}

Expand Down
33 changes: 28 additions & 5 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,21 @@ testRule(plugin.rule, {
code: '.a-Block {}',
message: 'Expected class name "a-Block" to contain no uppercase letters. (' + plugin.ruleName + ')',
},
{
code: '.m-__element {}',
message: 'Expected class name "m-__element" to use [prefix]-[block] syntax. (' + plugin.ruleName + ')',
},
{
code: '.m--modifier {}',
message: 'Expected class name "m--modifier" to use [prefix]-[block] syntax. (' + plugin.ruleName + ')',
},
{
code: '.state-m--state {}',
message: 'Expected class name "state-m--state" to use state-[prefix]-[block]--[state] syntax. (' + plugin.ruleName + ')',
},
{
code: '.state-block {}',
message: 'Expected class name "state-block" to use state-[prefix]-[block] syntax. Valid state prefixes: "state-a-", "state-m-", "state-o-", "state-l-", "state-g-", "state-h-". (' + plugin.ruleName + ')',
message: 'Expected class name "state-block" to use state-[prefix]-[block]--[state] syntax. Valid state prefixes: "state-a-", "state-m-", "state-o-", "state-l-", "state-g-", "state-h-". (' + plugin.ruleName + ')',
},
{
code: '.state-a-block {}',
Expand Down Expand Up @@ -138,13 +150,25 @@ testRule(plugin.rule, {
code: '.namespace-z-block__element {}',
message: 'Expected class name "namespace-z-block__element" to start with a valid prefix: "namespace-a-", "namespace-m-", "namespace-o-", "namespace-l-", "namespace-g-", "namespace-h-", "namespace-state-". (' + plugin.ruleName + ')',
},
{
code: '.namespace-m-__element {}',
message: 'Expected class name "namespace-m-__element" to use namespace-[prefix]-[block] syntax. (' + plugin.ruleName + ')',
},
{
code: '.namespace-m--modifier {}',
message: 'Expected class name "namespace-m--modifier" to use namespace-[prefix]-[block] syntax. (' + plugin.ruleName + ')',
},
{
code: '.namespace-state-m--state {}',
message: 'Expected class name "namespace-state-m--state" to use namespace-state-[prefix]-[block]--[state] syntax. (' + plugin.ruleName + ')',
},
{
code: '.namespace-state-m__element {}',
message: 'Expected class name "namespace-state-m__element" to use namespace-state-[prefix]-[block] syntax. Valid namespace-state prefixes: "namespace-state-a-", "namespace-state-m-", "namespace-state-o-", "namespace-state-l-", "namespace-state-g-", "namespace-state-h-". (' + plugin.ruleName + ')',
message: 'Expected class name "namespace-state-m__element" to use namespace-state-[prefix]-[block]--[state] syntax. Valid namespace-state prefixes: "namespace-state-a-", "namespace-state-m-", "namespace-state-o-", "namespace-state-l-", "namespace-state-g-", "namespace-state-h-". (' + plugin.ruleName + ')',
},
{
code: '.namespace-state-block {}',
message: 'Expected class name "namespace-state-block" to use namespace-state-[prefix]-[block] syntax. Valid namespace-state prefixes: "namespace-state-a-", "namespace-state-m-", "namespace-state-o-", "namespace-state-l-", "namespace-state-g-", "namespace-state-h-". (' + plugin.ruleName + ')',
message: 'Expected class name "namespace-state-block" to use namespace-state-[prefix]-[block]--[state] syntax. Valid namespace-state prefixes: "namespace-state-a-", "namespace-state-m-", "namespace-state-o-", "namespace-state-l-", "namespace-state-g-", "namespace-state-h-". (' + plugin.ruleName + ')',
},
{
code: '.namespace-state-a-block {}',
Expand All @@ -153,7 +177,6 @@ testRule(plugin.rule, {
],
});


// Should not conflict with keyframes or less mixins
testRule(plugin.rule, {
ruleName: plugin.ruleName,
Expand All @@ -168,4 +191,4 @@ testRule(plugin.rule, {
reject: [
{ code: '.no-mixin:not(x) { }'},
],
});
});

0 comments on commit 2a40d00

Please sign in to comment.