simonsmith/stylelint-selector-bem-pattern

Pass function instead of regex via .stylelintrc?

rgrjoh opened this issue · 5 comments

Is it possible to pass a function instead of a regex string when configuring this plugin via a JSON-formatted .stylelintrc?

Eg. like this:

"rules": {
  "plugin/selector-bem-pattern": {
    "componentName": "[A-Z]+",
    "componentSelectors": {
      "initial": "function(componentName) {
        return new RegExp('^\\.' + componentName + '(?:-[a-z]+)?$');
      }",
      "combined": "function(componentName) {
        return new RegExp('^\\.combined-' + componentName + '-[a-z]+$');
      }"
    }
  }
}

+1, came here for this question. Without possibility to pass a function we cant really customize codestyle conventions for original module :(. Like, at all.

You can use the special string {componentName}, as documented in the original module, to achieve what you're looking for.

Yes it is possible and is what I’m doing now. The reason I’m asking about passing a function is that for more complex scenarios functions often make the construction of the regexp much easier for fellow developers (and your future self :-)) to understand since you can split it into variables that are reused.

May be that would help you, i realized that might be restriction on stylelint side, because its just yaml-json file. So i just put a function that generates regexp (like these ones https://github.com/postcss/postcss-bem-linter/blob/master/lib/preset-patterns.js) in a comment in .stylelintrc. And just ran it on brawser console to get actual regexp. Kinda workaround, but still.

You can also write a .stylelintrc.js file as a CommonJS module that exports your configuration object (http://stylelint.io/user-guide/configuration/#loading-the-configuration-object). Give that a try!