Skip to content

Commit

Permalink
Make plugin compatible with stylelint 16
Browse files Browse the repository at this point in the history
Use import syntax over the require syntax and manually import required utils from stylelint package
  • Loading branch information
Marieke-C committed Feb 14, 2024
1 parent 97ea75e commit 2956bac
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 21 deletions.
49 changes: 35 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
const _ = require('lodash');
const stylelint = require('stylelint');
const isStandardSyntaxRule = require("stylelint/lib/utils/isStandardSyntaxRule");
const isStandardSyntaxSelector = require("stylelint/lib/utils/isStandardSyntaxSelector");
const parseSelector = require("stylelint/lib/utils/parseSelector");
const matchesStringOrRegExp = require("stylelint/lib/utils/matchesStringOrRegExp");
import _ from "lodash";
import stylelint from "stylelint";
import selectorParser from 'postcss-selector-parser';

const ruleName = 'plugin/selector-tag-no-without-class';
const messages = stylelint.utils.ruleMessages(ruleName, {
import hasInterpolation from 'stylelint/lib/utils/hasInterpolation.mjs';
import hasLessInterpolation from 'stylelint/lib/utils/hasLessInterpolation.mjs';
import hasPsvInterpolation from "stylelint/lib/utils/hasPsvInterpolation.mjs";
import hasScssInterpolation from "stylelint/lib/utils/hasScssInterpolation.mjs";
import hasTplInterpolation from "stylelint/lib/utils/hasTplInterpolation.mjs";
import isStandardSyntaxRule from 'stylelint/lib/utils/isStandardSyntaxRule.mjs';
import matchesStringOrRegExp from 'stylelint/lib/utils/matchesStringOrRegExp.mjs';
import isStandardSyntaxSelector from 'stylelint/lib/utils/isStandardSyntaxSelector.mjs';

const {
createPlugin,
utils: { report, ruleMessages, validateOptions }
} = stylelint;

export const ruleName = 'plugin/selector-tag-no-without-class';
export const messages = ruleMessages(ruleName, {
unexpected: (tagName) => `Unexpected tag ${tagName} without class qualifier`
});

/** @type {import('stylelint').Rule} */
const rule = function(primaryOption) {
return function(root, result) {
let validOptions = stylelint.utils.validateOptions(result, ruleName, {
let validOptions = validateOptions(result, ruleName, {
actual: primaryOption,
possible: [_.isString]
});
Expand All @@ -37,7 +49,7 @@ const rule = function(primaryOption) {
});

if (unqualifiedTagNode) {
stylelint.utils.report({
report({
ruleName: ruleName,
result: result,
node: ruleNode,
Expand All @@ -55,12 +67,16 @@ const rule = function(primaryOption) {
}

root.walkRules(ruleNode => {

if (!isStandardSyntaxRule(ruleNode)) {
return;
}

if (!isStandardSyntaxSelector(ruleNode.selector)) {
return;
}


if (
ruleNode.nodes.some(
node => ["rule", "atrule"].indexOf(node.type) !== -1
Expand All @@ -76,14 +92,19 @@ const rule = function(primaryOption) {
}

ruleNode.selectors.forEach(selector => {
parseSelector(selector, result, ruleNode, container =>
checkSelectorRoot(container, ruleNode)
);
const callback = container => checkSelectorRoot(container, ruleNode);
try {
return selectorParser(callback).processSync(selector);
} catch (err) {
result.warn(`Cannot parse selector (${err})`, { node, stylelintType: 'parseError' });
return undefined;
}
});
});
};
};
rule.primaryOptionArray = true;
rule.ruleName = ruleName;
rule.messages = messages;
module.exports = stylelint.createPlugin(ruleName, rule);

export default createPlugin(ruleName, rule);
6 changes: 3 additions & 3 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const {
rule: { ruleName, messages }
} = require('.');
import { ruleName, messages } from "./index";



testRule({
ruleName: ruleName,
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ const config = {
"setupFiles": ["./jest.setup.js"],
};

module.exports = config;
export default config;
2 changes: 1 addition & 1 deletion jest.setup.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const { getTestRule } = require("jest-preset-stylelint");
import { getTestRule } from "jest-preset-stylelint";

global.testRule = getTestRule({ plugins: ["./"] });
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
"stylelint",
"css"
],
"type": "module",
"main": "index.js",
"exports": "./index.js",
"scripts": {
"test": "jest"
"test": "cross-env NODE_OPTIONS=\"--experimental-vm-modules --no-warnings\" jest --runInBand"
},
"author": {
"name": "Moxio",
Expand All @@ -22,14 +24,19 @@
},
"license": "MIT",
"dependencies": {
"lodash": "^4.17.5"
"lodash": "^4.17.5",
"postcss-selector-parser": "^6.0.15"
},
"devDependencies": {
"cross-env": "^7.0.3",
"jest": "^29.5.0",
"jest-preset-stylelint": "^6.1.0",
"stylelint": "^16.0.0"
},
"peerDependencies": {
"stylelint": ">= 9.x < 17"
},
"engines": {
"node": ">=18.12.0"
}
}

0 comments on commit 2956bac

Please sign in to comment.