Skip to content

Commit

Permalink
Refactor to improve types and docs for *-list rules (#5936)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous committed Feb 23, 2022
1 parent ae9bbb3 commit ca1238d
Show file tree
Hide file tree
Showing 59 changed files with 138 additions and 119 deletions.
2 changes: 1 addition & 1 deletion lib/rules/at-rule-allowed-list/__tests__/index.js
Expand Up @@ -80,7 +80,7 @@ testRule({
ruleName,
skipBasicChecks: true,

config: ['keyframes'],
config: 'keyframes',

accept: [
{
Expand Down
12 changes: 4 additions & 8 deletions lib/rules/at-rule-allowed-list/index.js
Expand Up @@ -17,23 +17,19 @@ const meta = {
url: 'https://stylelint.io/user-guide/rules/list/at-rule-allowed-list',
};

/** @type {import('stylelint').Rule} */
/** @type {import('stylelint').Rule<string | string[]>} */
const rule = (primary) => {
// To allow for just a string as a parameter (not only arrays of strings)
const primaryValues = [primary].flat();

return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: primaryValues,
actual: primary,
possible: [isString],
});

if (!validOptions) {
return;
}

/** @type {string[]} */
const atRuleNames = primaryValues;
const primaryValues = [primary].flat();

root.walkAtRules((atRule) => {
const name = atRule.name;
Expand All @@ -42,7 +38,7 @@ const rule = (primary) => {
return;
}

if (atRuleNames.includes(vendor.unprefixed(name).toLowerCase())) {
if (primaryValues.includes(vendor.unprefixed(name).toLowerCase())) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/rules/at-rule-disallowed-list/__tests__/index.js
Expand Up @@ -91,7 +91,7 @@ testRule({
testRule({
ruleName,

config: ['keyframes'],
config: 'keyframes',

accept: [
{
Expand Down
12 changes: 4 additions & 8 deletions lib/rules/at-rule-disallowed-list/index.js
Expand Up @@ -17,23 +17,19 @@ const meta = {
url: 'https://stylelint.io/user-guide/rules/list/at-rule-disallowed-list',
};

/** @type {import('stylelint').Rule} */
/** @type {import('stylelint').Rule<string | string[]>} */
const rule = (primary) => {
// To allow for just a string as a parameter (not only arrays of strings)
const primaryValues = [primary].flat();

return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: primaryValues,
actual: primary,
possible: [isString],
});

if (!validOptions) {
return;
}

/** @type {string[]} */
const atRuleNames = primaryValues;
const primaryValues = [primary].flat();

root.walkAtRules((atRule) => {
const name = atRule.name;
Expand All @@ -42,7 +38,7 @@ const rule = (primary) => {
return;
}

if (!atRuleNames.includes(vendor.unprefixed(name).toLowerCase())) {
if (!primaryValues.includes(vendor.unprefixed(name).toLowerCase())) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/rules/comment-word-disallowed-list/README.md
Expand Up @@ -13,7 +13,7 @@ Specify a list of disallowed words within comments.

## Options

`array|string|regexp`: `["array", "of", "words", /or/, "/regex/"]|"word"|"/regex/"`
`array|string|regexp`: `["array", "of", "words", /or/, "/regex/"]|"word"|"/regex/"|/regex/`

If a string is surrounded with `"/"` (e.g. `"/^TODO:/"`), it is interpreted as a regular expression.

Expand Down
4 changes: 2 additions & 2 deletions lib/rules/comment-word-disallowed-list/__tests__/index.js
Expand Up @@ -4,7 +4,7 @@ const { messages, ruleName } = require('..');

testRule({
ruleName,
config: ['bad-word'],
config: 'bad-word',

accept: [
{
Expand Down Expand Up @@ -219,7 +219,7 @@ testRule({
testRule({
ruleName,
customSyntax: 'postcss-scss',
config: [['/^TODO:/', 'bad-word']],
config: ['/^TODO:/', 'bad-word'],

accept: [
{
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/comment-word-disallowed-list/index.js
Expand Up @@ -17,7 +17,7 @@ const meta = {
url: 'https://stylelint.io/user-guide/rules/list/comment-word-disallowed-list',
};

/** @type {import('stylelint').Rule} */
/** @type {import('stylelint').Rule<string | RegExp | Array<string | RegExp>>} */
const rule = (primary) => {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/function-allowed-list/README.md
Expand Up @@ -11,14 +11,14 @@ a { transform: scale(1); }

## Options

`array|string`: `["array", "of", "unprefixed", /functions/ or "regex"]|"function"|"/regex/"`
`array|string|regex`: `["array", "of", "unprefixed", /functions/, "/regex/"]|"function"|"/regex/"|/regex/`

If a string is surrounded with `"/"` (e.g. `"/^rgb/"`), it is interpreted as a regular expression.

Given:

```json
["scale", "rgba", "linear-gradient"]
["scale", "rgba", "/linear-gradient/"]
```

The following patterns are considered problems:
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/function-allowed-list/__tests__/index.js
Expand Up @@ -135,7 +135,7 @@ testRule({
testRule({
ruleName,

config: ['/rgb/'],
config: '/rgb/',

accept: [
{
Expand Down
8 changes: 3 additions & 5 deletions lib/rules/function-allowed-list/index.js
Expand Up @@ -20,13 +20,11 @@ const meta = {
url: 'https://stylelint.io/user-guide/rules/list/function-allowed-list',
};

/** @type {import('stylelint').Rule} */
/** @type {import('stylelint').Rule<string | RegExp | Array<string | RegExp>>} */
const rule = (primary) => {
const list = [primary].flat();

return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: list,
actual: primary,
possible: [isString, isRegExp],
});

Expand All @@ -46,7 +44,7 @@ const rule = (primary) => {
return;
}

if (matchesStringOrRegExp(vendor.unprefixed(node.value), list)) {
if (matchesStringOrRegExp(vendor.unprefixed(node.value), primary)) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/rules/function-disallowed-list/README.md
Expand Up @@ -11,7 +11,7 @@ a { transform: scale(1); }

## Options

`array|string`: `["array", "of", "unprefixed", /functions/ or "regex"]|"function"|"/regex/"`
`array|string|regex`: `["array", "of", "unprefixed", /functions/, "regex"]|"function"|"/regex/"|/regex/`

If a string is surrounded with `"/"` (e.g. `"/^rgb/"`), it is interpreted as a regular expression.

Expand Down
2 changes: 1 addition & 1 deletion lib/rules/function-disallowed-list/__tests__/index.js
Expand Up @@ -102,7 +102,7 @@ testRule({
testRule({
ruleName,

config: ['/rgb/'],
config: '/rgb/',

accept: [
{
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/function-disallowed-list/index.js
Expand Up @@ -20,7 +20,7 @@ const meta = {
url: 'https://stylelint.io/user-guide/rules/list/function-disallowed-list',
};

/** @type {import('stylelint').Rule} */
/** @type {import('stylelint').Rule<string | RegExp | Array<string | RegExp>>} */
const rule = (primary) => {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/function-url-scheme-allowed-list/README.md
Expand Up @@ -18,7 +18,7 @@ This rule ignores:

## Options

`array|string|regex`: `["array", "of", /schemes/ or "/regex/"]|"scheme"|/regex/`
`array|string|regex`: `["array", "of", /schemes/, "/regex/"]|"scheme"|"/regex/"|/regex/`

Given:

Expand Down
9 changes: 4 additions & 5 deletions lib/rules/function-url-scheme-allowed-list/__tests__/index.js
Expand Up @@ -158,7 +158,7 @@ testRule({

testRule({
ruleName,
config: [[]],
config: [],

accept: [
{
Expand Down Expand Up @@ -193,7 +193,7 @@ testRule({

testRule({
ruleName,
config: [''],
config: '',

accept: [
{
Expand Down Expand Up @@ -228,7 +228,6 @@ testRule({

testRule({
ruleName,
// primaryOptionArray
config: ['uri', 'file', 'https'],

accept: [
Expand All @@ -250,7 +249,7 @@ testRule({
testRule({
ruleName,

config: [['/^http/']],
config: ['/^http/'],

accept: [
{
Expand Down Expand Up @@ -283,7 +282,7 @@ testRule({
testRule({
ruleName,

config: [[/^http/]],
config: [/^http/],

accept: [
{
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/function-url-scheme-allowed-list/index.js
Expand Up @@ -19,7 +19,7 @@ const meta = {
url: 'https://stylelint.io/user-guide/rules/list/function-url-scheme-allowed-list',
};

/** @type {import('stylelint').Rule} */
/** @type {import('stylelint').Rule<string | RegExp | Array<string | RegExp>>} */
const rule = (primary) => {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/function-url-scheme-disallowed-list/README.md
Expand Up @@ -18,7 +18,7 @@ This rule ignores:

## Options

`array|string|regex`: `["array", "of", /schemes/ or "/regex/"]|"scheme"|/regex/`
`array|string|regex`: `["array", "of", /schemes/, "/regex/"]|"scheme"|"/regex/"|/regex/`

Given:

Expand Down
Expand Up @@ -4,7 +4,7 @@ const { messages, ruleName } = require('..');

testRule({
ruleName,
config: [[]],
config: [],

accept: [
{
Expand All @@ -15,7 +15,7 @@ testRule({

testRule({
ruleName,
config: [''],
config: '',

accept: [
{
Expand Down Expand Up @@ -184,7 +184,7 @@ testRule({
testRule({
ruleName,

config: [['/^http/']],
config: ['/^http/'],

accept: [
{
Expand Down Expand Up @@ -220,7 +220,7 @@ testRule({
testRule({
ruleName,

config: [[/^http/]],
config: [/^http/],

accept: [
{
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/function-url-scheme-disallowed-list/index.js
Expand Up @@ -19,7 +19,7 @@ const meta = {
url: 'https://stylelint.io/user-guide/rules/list/function-url-scheme-disallowed-list',
};

/** @type {import('stylelint').Rule} */
/** @type {import('stylelint').Rule<string | RegExp | Array<string | RegExp>>} */
const rule = (primary) => {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/media-feature-name-allowed-list/README.md
Expand Up @@ -11,7 +11,7 @@ Specify a list of allowed media feature names.

## Options

`array|string|regex`: `["array", "of", "unprefixed", /media-features/ or "regex"]|"media-feature"|/regex/`
`array|string|regex`: `["array", "of", "unprefixed", /media-features/, "regex"]|"media-feature"|"/regex/"|/regex/`

Given:

Expand Down
Expand Up @@ -101,7 +101,7 @@ testRule({

testRule({
ruleName,
config: [/^my-/],
config: /^my-/,

accept: [
{
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/media-feature-name-allowed-list/index.js
Expand Up @@ -22,7 +22,7 @@ const meta = {
url: 'https://stylelint.io/user-guide/rules/list/media-feature-name-allowed-list',
};

/** @type {import('stylelint').Rule} */
/** @type {import('stylelint').Rule<string | RegExp | Array<string | RegExp>>} */
const rule = (primary) => {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/media-feature-name-disallowed-list/README.md
Expand Up @@ -11,7 +11,7 @@ Specify a list of disallowed media feature names.

## Options

`array|string|regex`: `["array", "of", "unprefixed", /media-features/ or "regex"]|"media-feature"|/regex/`
`array|string|regex`: `["array", "of", "unprefixed", /media-features/, "regex"]|"media-feature"|"/regex/"|/regex/`

Given:

Expand Down
Expand Up @@ -99,7 +99,7 @@ testRule({

testRule({
ruleName,
config: [/^my-/],
config: /^my-/,

accept: [
{
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/media-feature-name-disallowed-list/index.js
Expand Up @@ -22,7 +22,7 @@ const meta = {
url: 'https://stylelint.io/user-guide/rules/list/media-feature-name-disallowed-list',
};

/** @type {import('stylelint').Rule} */
/** @type {import('stylelint').Rule<string | RegExp | Array<string | RegExp>>} */
const rule = (primary) => {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/property-allowed-list/README.md
Expand Up @@ -13,7 +13,7 @@ This rule ignores variables (`$sass`, `@less`, `--custom-property`).

## Options

`array|string`: `["array", "of", "unprefixed", /properties/ or "regex"]|"property"|"/regex/"`|/regex/
`array|string|regex`: `["array", "of", "unprefixed", /properties/, "regex"]|"property"|"/regex/"|/regex/`

If a string is surrounded with `"/"` (e.g. `"/^background/"`), it is interpreted as a regular expression. This allows, for example, easy targeting of shorthands: `/^background/` will match `background`, `background-size`, `background-color`, etc.

Expand Down

0 comments on commit ca1238d

Please sign in to comment.