Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintignore

This file was deleted.

41 changes: 0 additions & 41 deletions .eslintrc

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node: [ '24' ]
node: ["24"]
name: Run tests on Node ${{ matrix.node }}
steps:
- name: Checkout repository
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
*.log
.vscode
.idea
56 changes: 56 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import js from '@eslint/js';
import jest from 'eslint-plugin-jest';
import globals from 'globals';

export default [
js.configs.recommended,
{
ignores: ['node_modules/**'],
},
{
files: ['**/*.js'],
languageOptions: {
ecmaVersion: 2022,
sourceType: 'module',
globals: {
...globals.node,
...globals.es2021,
testRule: 'readonly',
},
},
rules: {
'comma-dangle': ['error', 'always-multiline'],
'curly': 'off',
'radix': 'error',
'wrap-iife': 'error',
'brace-style': 'off',
'comma-style': 'error',
'consistent-this': 'off',
'indent': ['error', 2, { 'SwitchCase': 1 }],
'no-console': 'off',
'no-lonely-if': 'error',
'no-nested-ternary': 'error',
'no-use-before-define': ['error', 'nofunc'],
'quotes': ['error', 'single'],
'space-before-function-paren': ['error', 'never'],
'keyword-spacing': ['error', { 'before': true }],
'space-before-blocks': ['error', 'always'],
'space-in-parens': ['error', 'never'],
'space-unary-ops': 'error',
},
},
{
files: ['test/**/*.js'],
plugins: {
jest,
},
languageOptions: {
globals: {
...globals.jest,
},
},
rules: {
...jest.configs.recommended.rules,
},
},
];
76 changes: 36 additions & 40 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,75 @@
import _ from 'lodash';
import Result from 'postcss/lib/result';
import stylelint from 'stylelint';
import bemLinter from 'postcss-bem-linter';

var ruleName = 'plugin/selector-bem-pattern';
const ruleName = 'plugin/selector-bem-pattern';

var optionsSchema = {
const isStringOrRegExp = (x) => typeof x === 'string' || x instanceof RegExp;
const isStringOrFunction = (x) => typeof x === 'string' || typeof x === 'function';

const optionsSchema = {
preset: ['suit', 'bem'],
presetOptions: function() { return true; }, // Can't currently validate `presetOptions`
presetOptions: () => true, // Can't currently validate `presetOptions`
componentName: [isStringOrRegExp],
componentSelectors: [function(pattern) {
componentSelectors: [(pattern) => {
if (isStringOrFunction(pattern)) return true;
if (!pattern.initial) return false;
if (!isStringOrFunction(pattern.initial)) return false;
if (pattern.combined && !isStringOrFunction(pattern.combined)) return false;
return true;
}],
implicitComponents: [_.isBoolean, _.isString, function(pattern) {
return _.isArray(pattern) && _.every(pattern, _.isString);
}],
implicitUtilities: [_.isBoolean, _.isString, function(pattern) {
return _.isArray(pattern) && _.every(pattern, _.isString);
if (!pattern.initial || !isStringOrFunction(pattern.initial)) return false;
return !pattern.combined || isStringOrFunction(pattern.combined);
}],
implicitComponents: [
(x) => typeof x === 'boolean',
(x) => typeof x === 'string',
(pattern) => Array.isArray(pattern) && pattern.every((x) => typeof x === 'string'),
],
implicitUtilities: [
(x) => typeof x === 'boolean',
(x) => typeof x === 'string',
(pattern) => Array.isArray(pattern) && pattern.every((x) => typeof x === 'string'),
],
utilitySelectors: [isStringOrRegExp],
ignoreSelectors: [
isStringOrRegExp,
function(pattern) {
if (!_.isArray(pattern)) {
(pattern) => {
if (!Array.isArray(pattern)) {
return isStringOrRegExp(pattern);
}
return _.every(pattern, isStringOrRegExp);
return pattern.every(isStringOrRegExp);
},
],
ignoreCustomProperties: [
isStringOrRegExp,
function(pattern) {
if (!_.isArray(pattern)) {
(pattern) => {
if (!Array.isArray(pattern)) {
return isStringOrRegExp(pattern);
}
return _.every(pattern, isStringOrRegExp);
return pattern.every(isStringOrRegExp);
},
],
};

export default stylelint.createPlugin(ruleName, function(options) {
return function(root, result) {
export default stylelint.createPlugin(ruleName, (options) => {
return (root, result) => {
if (!options) return;
var validOptions = stylelint.utils.validateOptions(result, ruleName, {
const validOptions = stylelint.utils.validateOptions(result, ruleName, {
actual: options,
possible: optionsSchema,
});
if (!validOptions) return;

var bemLinterResult = new Result();
bemLinter(options).Once(root, { result: bemLinterResult })
var bemLinterWarnings = bemLinterResult.warnings();
const bemLinterResult = new Result();
bemLinter(options).Once(root, { result: bemLinterResult });
const bemLinterWarnings = bemLinterResult.warnings();

bemLinterWarnings.forEach(function(warning) {
var node = warning.node || root;
bemLinterWarnings.forEach((warning) => {
const node = warning.node || root;
stylelint.utils.report({
ruleName: ruleName,
result: result,
node: node,
ruleName,
result,
node,
index: 0,
endIndex: node.selector !== undefined ? node.selector.length : 0,
message: warning.text + ' (' + ruleName + ')',
});
});
};
});

function isStringOrRegExp(x) {
return _.isString(x) || _.isRegExp(x);
}

function isStringOrFunction(x) {
return _.isString(x) || _.isFunction(x);
}
Loading
Loading