Skip to content

Issue with prepending selectors (separated by whitespace) #130

Description

@saulhardman

Hi @simonsmith, first of all, thanks a lot for maintaining this project 🙇

We're using SCSS syntax on the project in question and I'm attempting to extend the componentSelectors RegExp to allow for 'global state' classes to be prepended. E.g. .no-js .c-responsive-image etc.

The regular expression that I've conjured up appears to do the job when run separately, but it's outputting warnings when used with BEM Linter.

A (relatively) reduced test case can be found here on Glitch.

/** @define responsive-image */
.c-responsive-image {
  display: block;

  .no-js & {
    display: none;
  }
}

/* without `&` */
.no-js .c-responsive-image {
  display: none;
}
const { join } = require('path');
const { promisify } = require('util');
const readFile = promisify(require('fs').readFile)

const postcss = require('postcss');
const bemLinter = require('postcss-bem-linter');
const reporter = require('postcss-reporter');

const kebabCaseString = '[a-z]+[-[a-z]+]*';
const kebabCaseStringWithNumbers = '[a-z]+[-[a-z0-9]+]*';
const globalStateClassNames = `\\.(no|has|supports|t)-${kebabCaseString}`;
const localStateClassNames = `\\.(is|has)-${kebabCaseString}`;

const componentName = `^${kebabCaseString}$`;
const componentSelectors = name => new RegExp(`^(${globalStateClassNames}\\s)?\\.[co]-${name}(_{2}${kebabCaseString})?(-{2}${kebabCaseString})?(${localStateClassNames})?$`);
const utilitySelectors = `^\\.u-${kebabCaseStringWithNumbers}$`;

const bemLinterOptions = {
  componentName,
  componentSelectors,
  utilitySelectors,
};

console.log('Standalone RegExp test:', componentSelectors('responsive-image').test('.no-js .c-responsive-image'));
            
module.exports = (async () => {
  const scss = await readFile(join(process.cwd(), 'index.scss'), 'utf8');

  await postcss()
    .use(bemLinter(bemLinterOptions))
    .use(reporter())
    .process(scss)
})();

Can you spot anything obvious that I'm doing wrong? If this is in fact a bug then I'm more than willing to contribute to the fix, but I'll need a little guidance.

Edit:

# error output
6:3	⚠  Invalid component selector ".no-js .c-responsive-image" [postcss-bem-linter]
5:50 PM
12:1	⚠  Invalid component selector ".no-js .c-responsive-image" [postcss-bem-linter]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions