Skip to content

Commit 5101c4f

Browse files
authored
Enforce "hyphenated BEM" for classes and "hyphenated lowercase" for mixins. Fix #1 (#5)
1 parent 51ebcb9 commit 5101c4f

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
1010

1111
- Enforce accessibility-related rules with [stylelint-a11y](https://github.com/YozhikM/stylelint-a11y) ([#2](https://github.com/torchbox/stylelint-config-torchbox/issues/2), [#3](https://github.com/torchbox/stylelint-config-torchbox/pull/3)).
1212
- Enforce usage of variables for colors with [stylelint-declaration-strict-value](https://github.com/AndyOGo/stylelint-declaration-strict-value) ([#2](https://github.com/torchbox/stylelint-config-torchbox/issues/2), [#4](https://github.com/torchbox/stylelint-config-torchbox/pull/4)).
13+
- Enforce "hyphenated BEM" nomenclature for class names with `selector-class-pattern` ([#1](https://github.com/torchbox/stylelint-config-torchbox/issues/1), [#5](https://github.com/torchbox/stylelint-config-torchbox/pull/5)).
14+
- Enforce "hyphenated lowercase" nomenclature for mixins with `scss/at-mixin-pattern` ([#1](https://github.com/torchbox/stylelint-config-torchbox/issues/1), [#5](https://github.com/torchbox/stylelint-config-torchbox/pull/5)).
1315

1416
### BREAKING CHANGES
1517

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ To get the most out of this config, it is assumed that projects have the followi
5656
- [`declaration-no-important`](https://stylelint.io/user-guide/rules/declaration-no-important/)
5757
- [`selector-max-id`](https://stylelint.io/user-guide/rules/selector-max-id/): `0`
5858
- [`selector-max-type`](https://stylelint.io/user-guide/rules/selector-max-type/): `2, ignore: child, compounded, next-sibling`
59+
- [`selector-class-pattern`](https://stylelint.io/user-guide/rules/selector-class-pattern/): `, resolveNestedSelectors: true`
5960
- [`max-nesting-depth`](https://stylelint.io/user-guide/rules/max-nesting-depth/): `4`
6061
- [`selector-max-specificity`](https://stylelint.io/user-guide/rules/selector-max-specificity/): `0,4,0`
6162
- [`value-no-vendor-prefix`](https://stylelint.io/user-guide/rules/value-no-vendor-prefix/)
@@ -70,6 +71,7 @@ To get the most out of this config, it is assumed that projects have the followi
7071
- [`scss/percent-placeholder-pattern`](https://github.com/kristerkari/stylelint-scss#readme): `^do-not-use-placeholders$`
7172
- [`scss/dollar-variable-no-missing-interpolation`](https://github.com/kristerkari/stylelint-scss#readme)
7273
- [`scss/at-mixin-argumentless-call-parentheses`](https://github.com/kristerkari/stylelint-scss#readme): `always`
74+
- [`scss/at-mixin-pattern`](https://github.com/kristerkari/stylelint-scss#readme): ``
7375
- [`order/order`](https://github.com/hudochenkov/stylelint-order): `dollar-variables, custom-properties, type: at-rule, hasBlock: false, declarations`
7476
- [`a11y/content-property-no-static-value`](https://github.com/YozhikM/stylelint-a11y#readme)
7577
- [`a11y/no-obsolete-attribute`](https://github.com/YozhikM/stylelint-a11y#readme)
@@ -233,7 +235,6 @@ To get the most out of this config, it is assumed that projects have the followi
233235
- [`scss/selector-nest-combinators`](https://github.com/kristerkari/stylelint-scss#readme)
234236
- [`selector-attribute-operator-blacklist`](https://stylelint.io/user-guide/rules/selector-attribute-operator-blacklist/)
235237
- [`selector-attribute-operator-whitelist`](https://stylelint.io/user-guide/rules/selector-attribute-operator-whitelist/)
236-
- [`selector-class-pattern`](https://stylelint.io/user-guide/rules/selector-class-pattern/)
237238
- [`selector-combinator-blacklist`](https://stylelint.io/user-guide/rules/selector-combinator-blacklist/)
238239
- [`selector-combinator-whitelist`](https://stylelint.io/user-guide/rules/selector-combinator-whitelist/)
239240
- [`selector-max-attribute`](https://stylelint.io/user-guide/rules/selector-max-attribute/)
@@ -280,7 +281,6 @@ To get the most out of this config, it is assumed that projects have the followi
280281
- [`scss/at-if-closing-brace-space-after`](https://github.com/kristerkari/stylelint-scss#readme)
281282
- [`scss/at-import-no-partial-leading-underscore`](https://github.com/kristerkari/stylelint-scss#readme)
282283
- [`scss/at-mixin-parentheses-space-before`](https://github.com/kristerkari/stylelint-scss#readme)
283-
- [`scss/at-mixin-pattern`](https://github.com/kristerkari/stylelint-scss#readme)
284284
- [`scss/declaration-nested-properties-no-divided-groups`](https://github.com/kristerkari/stylelint-scss#readme)
285285
- [`scss/dollar-variable-colon-newline-after`](https://github.com/kristerkari/stylelint-scss#readme)
286286
- [`scss/dollar-variable-colon-space-after`](https://github.com/kristerkari/stylelint-scss#readme)

config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ module.exports = {
1919
2,
2020
{ ignore: ['child', 'compounded', 'next-sibling'] },
2121
],
22+
'selector-class-pattern': [
23+
// Loose pattern for hyphenated BEM. This also allows simple words to be used as class names, .e.g. `.active`, `.button`.
24+
// Based on:
25+
// - https://github.com/postcss/postcss-bem-linter/issues/89#issuecomment-255482072
26+
// - https://gist.github.com/Potherca/f2a65491e63338659c3a0d2b07eee382
27+
// See also: https://github.com/simonsmith/stylelint-selector-bem-pattern.
28+
// Proceed with caution if reviewing this – and use https://regexper.com.
29+
/^[a-z]+(-[a-z0-9]+)*(__[a-z0-9]+(-[a-z0-9]+)*)?(--[a-z0-9]+(-[a-z0-9]+)*)?$/,
30+
{ resolveNestedSelectors: true },
31+
],
2232
'max-nesting-depth': 4,
2333
'selector-max-specificity': '0,4,0',
2434
'value-no-vendor-prefix': true,
@@ -36,6 +46,7 @@ module.exports = {
3646
'scss/percent-placeholder-pattern': '^do-not-use-placeholders$',
3747
'scss/dollar-variable-no-missing-interpolation': true,
3848
'scss/at-mixin-argumentless-call-parentheses': 'always',
49+
'scss/at-mixin-pattern': /^[a-z0-9-]+$/,
3950
'order/order': [
4051
'dollar-variables',
4152
'custom-properties',

src/__snapshots__/semver.test.js.snap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ Object {
240240
"scss/at-mixin-argumentless-call-parentheses": Array [
241241
"always",
242242
],
243+
"scss/at-mixin-pattern": Array [
244+
/\\^\\[a-z0-9-\\]\\+\\$/,
245+
],
243246
"scss/at-rule-no-unknown": Array [
244247
true,
245248
],
@@ -267,6 +270,12 @@ Object {
267270
"selector-attribute-quotes": Array [
268271
"always",
269272
],
273+
"selector-class-pattern": Array [
274+
/\\^\\[a-z\\]\\+\\(-\\[a-z0-9\\]\\+\\)\\*\\(__\\[a-z0-9\\]\\+\\(-\\[a-z0-9\\]\\+\\)\\*\\)\\?\\(--\\[a-z0-9\\]\\+\\(-\\[a-z0-9\\]\\+\\)\\*\\)\\?\\$/,
275+
Object {
276+
"resolveNestedSelectors": true,
277+
},
278+
],
270279
"selector-combinator-space-after": null,
271280
"selector-combinator-space-before": null,
272281
"selector-descendant-combinator-no-non-space": null,

src/unused.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ const tooOpinionated = [
3737
'scss/selector-nest-combinators',
3838
'selector-attribute-operator-blacklist',
3939
'selector-attribute-operator-whitelist',
40-
'selector-class-pattern',
4140
'selector-combinator-blacklist',
4241
'selector-combinator-whitelist',
4342
'selector-max-attribute',
@@ -90,7 +89,6 @@ const formattingByPrettier = [
9089
'scss/at-if-closing-brace-space-after',
9190
'scss/at-import-no-partial-leading-underscore',
9291
'scss/at-mixin-parentheses-space-before',
93-
'scss/at-mixin-pattern',
9492
'scss/declaration-nested-properties-no-divided-groups',
9593
'scss/dollar-variable-colon-newline-after',
9694
'scss/dollar-variable-colon-space-after',

0 commit comments

Comments
 (0)