Skip to content

Commit

Permalink
Refactor to commonize regex patterns (#8272)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryo-manba authored Jan 8, 2025
1 parent a80cdf6 commit f6d5055
Show file tree
Hide file tree
Showing 53 changed files with 181 additions and 74 deletions.
9 changes: 2 additions & 7 deletions lib/rules/at-rule-descriptor-no-unknown/index.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 2 additions & 7 deletions lib/rules/at-rule-descriptor-no-unknown/index.mjs
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import { lexer } from 'css-tree';

import { atRuleRegexes } from '../../utils/regexes.mjs';
import isStandardSyntaxAtRule from '../../utils/isStandardSyntaxAtRule.mjs';
import isStandardSyntaxDeclaration from '../../utils/isStandardSyntaxDeclaration.mjs';
import { nestingSupportedAtKeywords } from '../../reference/atKeywords.mjs';
import report from '../../utils/report.mjs';
import ruleMessages from '../../utils/ruleMessages.mjs';
import validateOptions from '../../utils/validateOptions.mjs';

const UNSUPPORTED_NESTING_REGEX = new RegExp(
`^((?!${[...nestingSupportedAtKeywords.values()].join('|')}).)*$`,
'i',
);

const ruleName = 'at-rule-descriptor-no-unknown';

const messages = ruleMessages(ruleName, {
Expand All @@ -32,7 +27,7 @@ const rule = (primary) => {
return;
}

root.walkAtRules(UNSUPPORTED_NESTING_REGEX, (atRule) => {
root.walkAtRules(atRuleRegexes.unsupportedNesting, (atRule) => {
if (!isStandardSyntaxAtRule(atRule)) return;

atRule.walkDecls((decl) => {
Expand Down
9 changes: 2 additions & 7 deletions lib/rules/at-rule-descriptor-value-no-unknown/index.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 2 additions & 7 deletions lib/rules/at-rule-descriptor-value-no-unknown/index.mjs
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { lexer } from 'css-tree';

import { atRuleRegexes } from '../../utils/regexes.mjs';
import { declarationValueIndex } from '../../utils/nodeFieldIndices.mjs';
import isStandardSyntaxAtRule from '../../utils/isStandardSyntaxAtRule.mjs';
import isStandardSyntaxDeclaration from '../../utils/isStandardSyntaxDeclaration.mjs';
import { nestingSupportedAtKeywords } from '../../reference/atKeywords.mjs';
import report from '../../utils/report.mjs';
import ruleMessages from '../../utils/ruleMessages.mjs';
import validateOptions from '../../utils/validateOptions.mjs';

const UNSUPPORTED_NESTING_REGEX = new RegExp(
`^((?!${[...nestingSupportedAtKeywords.values()].join('|')}).)*$`,
'i',
);

const ruleName = 'at-rule-descriptor-value-no-unknown';

const messages = ruleMessages(ruleName, {
Expand All @@ -33,7 +28,7 @@ const rule = (primary) => {
return;
}

root.walkAtRules(UNSUPPORTED_NESTING_REGEX, (atRule) => {
root.walkAtRules(atRuleRegexes.unsupportedNesting, (atRule) => {
if (!isStandardSyntaxAtRule(atRule)) return;

atRule.walkDecls((decl) => {
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/custom-property-no-missing-var-function/index.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/rules/custom-property-no-missing-var-function/index.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import valueParser from 'postcss-value-parser';

import { atRuleRegexes } from '../../utils/regexes.mjs';
import { declarationValueIndex } from '../../utils/nodeFieldIndices.mjs';
import isVarFunction from '../../utils/isVarFunction.mjs';
import report from '../../utils/report.mjs';
Expand Down Expand Up @@ -50,7 +51,7 @@ const rule = (primary) => {
/** @type {Set<string>} */
const knownCustomProperties = new Set();

root.walkAtRules(/^property$/i, ({ params }) => {
root.walkAtRules(atRuleRegexes.property, ({ params }) => {
knownCustomProperties.add(params);
});

Expand Down
3 changes: 2 additions & 1 deletion lib/rules/declaration-property-value-no-unknown/index.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/rules/declaration-property-value-no-unknown/index.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { find, fork, parse, string } from 'css-tree';

import { isRegExp, isString } from '../../utils/validateTypes.mjs';
import { atRuleRegexes } from '../../utils/regexes.mjs';
import { declarationValueIndex } from '../../utils/nodeFieldIndices.mjs';
import { isDeclaration } from '../../utils/typeGuards.mjs';

Expand Down Expand Up @@ -85,7 +86,7 @@ const rule = (primary, secondaryOptions) => {
/** @type {Map<string, string>} */
const typedCustomPropertyNames = new Map();

root.walkAtRules(/^property$/i, (atRule) => {
root.walkAtRules(atRuleRegexes.property, (atRule) => {
const propName = atRule.params.trim();

if (!propName || !atRule.nodes || !isCustomProperty(propName)) return;
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/import-notation/index.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/rules/import-notation/index.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import valueParser from 'postcss-value-parser';

import { atRuleParamIndex } from '../../utils/nodeFieldIndices.mjs';
import { atRuleRegexes } from '../../utils/regexes.mjs';
import getAtRuleParams from '../../utils/getAtRuleParams.mjs';
import report from '../../utils/report.mjs';
import ruleMessages from '../../utils/ruleMessages.mjs';
Expand Down Expand Up @@ -41,7 +42,7 @@ const rule = (primary) => {

if (!validOptions) return;

root.walkAtRules(/^import$/i, checkAtRuleImportParams);
root.walkAtRules(atRuleRegexes.import, checkAtRuleImportParams);

/** @param {AtRule} atRule */
function checkAtRuleImportParams(atRule) {
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/keyframe-block-no-duplicate-selectors/index.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/rules/keyframe-block-no-duplicate-selectors/index.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { atRuleRegexes } from '../../utils/regexes.mjs';
import getRuleSelector from '../../utils/getRuleSelector.mjs';
import getStrippedSelectorSource from '../../utils/getStrippedSelectorSource.mjs';
import isStandardSyntaxRule from '../../utils/isStandardSyntaxRule.mjs';
Expand Down Expand Up @@ -25,7 +26,7 @@ const rule = (primary) => {
return;
}

root.walkAtRules(/^(-(o|moz|ms|webkit)-)?keyframes$/i, (atRuleKeyframes) => {
root.walkAtRules(atRuleRegexes.keyframes, (atRuleKeyframes) => {
const selectors = new Set();

atRuleKeyframes.walkRules((keyframeRule) => {
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/keyframe-declaration-no-important/index.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/rules/keyframe-declaration-no-important/index.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assert } from '../../utils/validateTypes.mjs';
import { atRuleRegexes } from '../../utils/regexes.mjs';
import getImportantPosition from '../../utils/getImportantPosition.mjs';
import report from '../../utils/report.mjs';
import ruleMessages from '../../utils/ruleMessages.mjs';
Expand All @@ -23,7 +24,7 @@ const rule = (primary) => {
return;
}

root.walkAtRules(/^(-(o|moz|ms|webkit)-)?keyframes$/i, (atRuleKeyframes) => {
root.walkAtRules(atRuleRegexes.keyframes, (atRuleKeyframes) => {
atRuleKeyframes.walkDecls((decl) => {
if (!decl.important) {
return;
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/keyframe-selector-notation/index.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/rules/keyframe-selector-notation/index.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { assertString } from '../../utils/validateTypes.mjs';
import { keyframeSelectorKeywords } from '../../reference/keywords.mjs';

import { atRuleRegexes } from '../../utils/regexes.mjs';
import getRuleSelector from '../../utils/getRuleSelector.mjs';
import parseSelector from '../../utils/parseSelector.mjs';
import parser from 'postcss-selector-parser';
Expand Down Expand Up @@ -70,7 +71,7 @@ const rule = (primary) => {

const { expFunc, fixFunc } = optionFuncs[primary];

root.walkAtRules(/^(-(o|moz|ms|webkit)-)?keyframes$/i, (atRuleKeyframes) => {
root.walkAtRules(atRuleRegexes.keyframes, (atRuleKeyframes) => {
const selectorsInBlock =
primary === 'percentage-unless-within-keyword-only-block'
? getSelectorsInBlock(atRuleKeyframes)
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/keyframes-name-pattern/index.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/rules/keyframes-name-pattern/index.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isRegExp, isString } from '../../utils/validateTypes.mjs';
import { atRuleParamIndex } from '../../utils/nodeFieldIndices.mjs';
import { atRuleRegexes } from '../../utils/regexes.mjs';
import isStandardSyntaxKeyframesName from '../../utils/isStandardSyntaxKeyframesName.mjs';
import report from '../../utils/report.mjs';
import ruleMessages from '../../utils/ruleMessages.mjs';
Expand Down Expand Up @@ -29,7 +30,7 @@ const rule = (primary) => {

const regex = isString(primary) ? new RegExp(primary) : primary;

root.walkAtRules(/keyframes/i, (keyframesNode) => {
root.walkAtRules(atRuleRegexes.keyframes, (keyframesNode) => {
const value = keyframesNode.params;

if (!isStandardSyntaxKeyframesName(value)) {
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/media-feature-name-allowed-list/index.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/rules/media-feature-name-allowed-list/index.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isRegExp, isString } from '../../utils/validateTypes.mjs';
import { atRuleParamIndex } from '../../utils/nodeFieldIndices.mjs';
import { atRuleRegexes } from '../../utils/regexes.mjs';
import findMediaFeatureNames from '../../utils/findMediaFeatureNames.mjs';
import getAtRuleParams from '../../utils/getAtRuleParams.mjs';
import isCustomMediaQuery from '../../utils/isCustomMediaQuery.mjs';
Expand Down Expand Up @@ -30,7 +31,7 @@ const rule = (primary) => {
return;
}

root.walkAtRules(/^media$/i, (atRule) => {
root.walkAtRules(atRuleRegexes.media, (atRule) => {
findMediaFeatureNames(getAtRuleParams(atRule), (mediaFeatureNameToken) => {
const [, , startIndex, endIndex, { value: featureName }] = mediaFeatureNameToken;

Expand Down
3 changes: 2 additions & 1 deletion lib/rules/media-feature-name-disallowed-list/index.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f6d5055

Please sign in to comment.