Skip to content

Commit

Permalink
add regex for rule option
Browse files Browse the repository at this point in the history
  • Loading branch information
kaysonwu committed May 26, 2020
1 parent 733a6c0 commit 4160359
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/rules/selector-no-redundant-nesting-selector/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ p {

## Options

`string[]`: `[keywords]`
`ignoreKeywords`: `["/regex/", /regex/, "string"]`

if you are using Less or some other non-SCSS syntax, the warnings can be disabled by using `ignoreKeywords` option.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ testRule(rule, {

testRule(rule, {
ruleName,
config: [true, { ignoreKeywords: ["when"] }],
config: [true, { ignoreKeywords: ["when", /regex/] }],
syntax: "less",
accept: [
{
Expand All @@ -394,6 +394,16 @@ testRule(rule, {
}
`,
description: "when there are multiple reference nesting"
},
{
code: `
@theme: ~'dark';
p {
& regex (@theme = dark) {}
& regex not (@theme = dark) {}
}
`,
description: "should support the use of regular option"
}
]
});
28 changes: 22 additions & 6 deletions src/rules/selector-no-redundant-nesting-selector/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { utils } from "stylelint";
import optionsMatches from "stylelint/lib/utils/optionsMatches";
import { isString, isRegExp } from "lodash";
import {
namespace,
parseSelector,
hasNestedSibling,
isKeyword
isType
} from "../../utils";

export const ruleName = namespace("selector-no-redundant-nesting-selector");
Expand All @@ -14,14 +16,23 @@ export const messages = utils.ruleMessages(ruleName, {

export default function(actual, options) {
return (root, result) => {
const validOptions = utils.validateOptions(result, ruleName, { actual });
const validOptions = utils.validateOptions(
result,
ruleName,
{ actual },
{
actual: options,
possible: {
ignoreKeywords: [isString, isRegExp]
},
optional: true
}
);

if (!validOptions) {
return;
}

const { ignoreKeywords = [] } = options || {};

root.walkRules(/&/, rule => {
parseSelector(rule.selector, result, rule, fullSelector => {
// "Ampersand followed by a combinator followed by non-combinator non-ampersand and not the selector end"
Expand All @@ -45,8 +56,13 @@ export default function(actual, options) {
const nextNext = next ? next.next() : null;

if (
(nextNext && nextNext.type === "combinator") ||
isKeyword(nextNext, ignoreKeywords)
(isType(nextNext, "tag") &&
optionsMatches(
options,
"ignoreKeywords",
nextNext.value.trim()
)) ||
isType(nextNext, "combinator")
) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ export { removeEmptyLinesBefore } from "./removeEmptyLinesBefore";
export { default as findOperators } from "./sassValueParser";
export { default as whitespaceChecker } from "./whitespaceChecker";
export { default as hasNestedSibling } from "./hasNestedSibling";
export { default as isKeyword } from "./isKeyword";
export { default as isType } from "./isType";
14 changes: 0 additions & 14 deletions src/utils/isKeyword.js

This file was deleted.

10 changes: 10 additions & 0 deletions src/utils/isType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Determine whether the given node is of the given type.
*
* @param {import('postcss').Node} node
* @param {string} type
* @return {boolean}
*/
export default function(node, type) {
return node && node.type === type;
}

0 comments on commit 4160359

Please sign in to comment.