question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for linting BEM selectors without component definition?

See original GitHub issue

Background: I have an existing project using scss-lint which I’m migrating to stylelint for faster performance. One of the things I want to lint is selector format in which I’m following the Harry Roberts convention (originally described here)

The Ask: The way that postcss-bem-linter is written, it requires a “component definition” in order for the linter to check anything. My concern is that this represents an opt-in scenario for developers. If they never setup a component definition then their selectors will never be linted.

Using postcss-bem-linter, is it possible to just check that all selectors conform to the general pattern for naming (block__element--modifier) according to the convention above but without a component definition?

The Followup: Where does this concept of a “component definition” come from? I haven’t been able to find any documentation on its use with BEM apart from the postcss-bem-linter README.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:3
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
bookwyrmcommented, Mar 8, 2016

@davidtheclark Thanks for the quick response. I think that your suggestion is a good one - I overlooked selector-class-pattern when I found your stylelint-selector-bem-pattern.

I added a rule:

    "selector-class-pattern": "^(?:(?:o|c|u|t|s|is|has|_|js|qa)-)?[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*(?:__[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*)?(?:--[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*)?(?:\\[.+\\])?$",

It’s petty gnarly but it works.

For now though I’m going to shelve the migration to stylelint as it isn’t as feature-rich as scss-lint. The main things I’ve come across that I’m missing are:

I’ll see about adding plugins to stylelint for the above features as I have time.

4reactions
jitendravyascommented, Apr 7, 2017

@bookwyrm Is it for Harry potter’s BEM style?

"selector-class-pattern": "^(?:(?:o|c|u|t|s|is|has|_|js|qa)-)?[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*(?:__[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*)?(?:--[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*)?(?:\\[.+\\])?$"
.block {}
.block__element {}
.block--modifier {}

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to lint for a harry robert's BEM convention with stylelint?
I'm trying to use a SCSS selector of &.is-open {} as a sort of state-like class. However I'm getting: "Stylelint: Invalid component selector...
Read more >
postcss-bem-linter | Yarn - Package Manager
A PostCSS plugin to lint BEM-style CSS. BEM-style describes CSS that follows a more-or-less strict set of conventions determining what selectors can be...
Read more >
Level up your CSS linting using Stylelint - LogRocket Blog
Level up your CSS, SCSS, and Sass linting skills and improve your code quality using this advanced guide on Stylelint.
Read more >
Style Guide - Vue.js
Style Guide. This is the official style guide for Vue-specific code. If you use Vue in a project, it's a great reference to...
Read more >
A comprehensive guide to using BEM with React - Medium
When you define a React component following BEM conventions, ... (no need to disable the linter here, as global selectors are not processed): ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found