Skip to content

Commit

Permalink
#4 add possibility to disable prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ernscht committed Jan 12, 2018
1 parent 83f3221 commit eae72d4
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 7 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## 4.0.0-SNAPSHOT
## 4.0.0

- [feature] add rule to disallow hyphens at start and end of block parts
- [feature] BREAKING: add rule to disallow hyphens at start and end of block parts
- [feature] add possibility to disable prefixes by configuration
- [maintenance] update dependecies

## 3.1.3

Expand Down
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,23 @@ and in case of emergency you can overwrite the default prefixes
"rules": {
"plugin/stylelint-bem-namics": {
"patternPrefixes": [ "a", "m", "o", "t", "p" ],
"helperPrefixes": [ "state" ]
"helperPrefixes": [ "is", "has" ]
}
}
}
```

... or you can pass empty prefixes to disable prefixes completely

```js
{
"plugins": [
"@namics/stylelint-bem"
],
"rules": {
"plugin/stylelint-bem-namics": {
"patternPrefixes": [],
"helperPrefixes": []
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ module.exports = stylelint.createPlugin(ruleName, (options) => {
}
if (parsedClassName.pattern) {
validSyntax += `${parsedClassName.pattern}-`;
} else {
} else if (validPatternPrefixes.length) {
validSyntax += '[prefix]-';
}
validSyntax += '[block]';
Expand Down Expand Up @@ -117,7 +117,7 @@ module.exports = stylelint.createPlugin(ruleName, (options) => {

// Valid helper but invalid pattern prefix
// e.g. 'state-zz-button'
if (parsedClassName.helper && !parsedClassName.pattern) {
if (validPatternPrefixes.length && parsedClassName.helper && !parsedClassName.pattern) {
const validPrefixExamples = validPatternPrefixes
.map((prefix) => `"${namespace}${parsedClassName.helper}-${prefix}-"`)
.join(', ');
Expand All @@ -126,7 +126,7 @@ module.exports = stylelint.createPlugin(ruleName, (options) => {
}

// Invalid pattern prefix
if (!parsedClassName.pattern) {
if (validPatternPrefixes.length && !parsedClassName.pattern) {
const validPrefixExamples = validPrefixes
.map((prefix) => `"${namespace}${prefix}-"`)
.join(', ');
Expand Down
180 changes: 179 additions & 1 deletion test/prefixes.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ testRule(plugin.rule, {
],
});

// invalid patternPrefixes configuration
// invalid patternPrefixes configuration - uses default
testRule(plugin.rule, {
ruleName: plugin.ruleName,
config: {
Expand Down Expand Up @@ -151,3 +151,181 @@ testRule(plugin.rule, {
},
],
});

// empty prefixes
testRule(plugin.rule, {
ruleName: plugin.ruleName,
config: {
patternPrefixes: [],
helperPrefixes: [],
},
skipBasicChecks: true,
accept: [
{
code: '.block {}',
},
{
code: '.block__element {}',
},
{
code: '.block--modifier {}',
},
{
code: '.block__element--modifier {}',
},
{
code: '.m--modifier {}',
},
{
code: '.a-block--state-name {}',
},
{
code: '.x-block--modifier {}',
},
{
code: '.f-block__element {}',
},
{
code: '.z-block__element--modifier {}',
},
{
code: '.state-m-block {}',
},
{
code: '.state-m--state {}',
},
{
code: '.state-a-block--state-name {}',
},
{
code: '.state-a-block__element--state-name {}',
},
],
reject: [
{
code: '.block___x {}',
message: 'Expected class name "block___x" to use only two "_" as element separator. (' + plugin.ruleName + ')',
},
{
code: '.block---x {}',
message: 'Expected class name "block---x" to use only one "--" modifier separator. (' + plugin.ruleName + ')',
},
{
code: '.block--y--x {}',
message: 'Expected class name "block--y--x" to use only one "--" modifier separator. (' + plugin.ruleName + ')',
},
{
code: '.Block {}',
message: 'Expected class name "Block" to contain no uppercase letters. (' + plugin.ruleName + ')',
},
{
code: '.a-__element {}',
message: 'Expected class name "a-__element" to use "-" only for composite names. (' + plugin.ruleName + ')',
},
{
code: '.-block {}',
message: 'Expected class name "-block" to use the [block] syntax. (' + plugin.ruleName + ')',
},
{
code: '.--block {}',
message: 'Expected class name "--block" to use the [block]--[modifier] syntax. (' + plugin.ruleName + ')',
},
{
code: '.m--modifier__block {}',
message: 'Expected class name "m--modifier__block" to use the [block]__[element]--[modifier] syntax. (' + plugin.ruleName + ')',
},
],
});

// empty pattern prefixes
testRule(plugin.rule, {
ruleName: plugin.ruleName,
config: {
patternPrefixes: [],
helperPrefixes: ['is', 'has'],
},
skipBasicChecks: true,
accept: [
{
code: '.block {}',
},
{
code: '.block__element {}',
},
{
code: '.block--modifier {}',
},
{
code: '.block__element--modifier {}',
},
{
code: '.m--modifier {}',
},
{
code: '.a-block--state-name {}',
},
{
code: '.x-block--modifier {}',
},
{
code: '.f-block__element {}',
},
{
code: '.z-block__element--modifier {}',
},
{
code: '.is-m--state {}',
},
{
code: '.is-a-block--state-name {}',
},
{
code: '.is-a-block__element--state-name {}',
},
{
code: '.has-a-block__element--state-name {}',
},
],
reject: [
{
code: '.block___x {}',
message: 'Expected class name "block___x" to use only two "_" as element separator. (' + plugin.ruleName + ')',
},
{
code: '.block---x {}',
message: 'Expected class name "block---x" to use only one "--" modifier separator. (' + plugin.ruleName + ')',
},
{
code: '.block--y--x {}',
message: 'Expected class name "block--y--x" to use only one "--" modifier separator. (' + plugin.ruleName + ')',
},
{
code: '.Block {}',
message: 'Expected class name "Block" to contain no uppercase letters. (' + plugin.ruleName + ')',
},
{
code: '.a-__element {}',
message: 'Expected class name "a-__element" to use "-" only for composite names. (' + plugin.ruleName + ')',
},
{
code: '.-block {}',
message: 'Expected class name "-block" to use the [block] syntax. (' + plugin.ruleName + ')',
},
{
code: '.--block {}',
message: 'Expected class name "--block" to use the [block]--[modifier] syntax. (' + plugin.ruleName + ')',
},
{
code: '.m--modifier__block {}',
message: 'Expected class name "m--modifier__block" to use the [block]__[element]--[modifier] syntax. (' + plugin.ruleName + ')',
},
{
code: '.is--state-name {}',
message: 'Expected class name "is--state-name" to use the is-[block]--[is] syntax. (' + plugin.ruleName + ')',
},
{
code: '.is-m-block {}',
message: 'Expected class name "is-m-block" to use the is-[block]--[is] syntax. (' + plugin.ruleName + ')',
},
],
});

0 comments on commit eae72d4

Please sign in to comment.