Skip to content

Commit

Permalink
Fixed selector|value-no-vendor-prefix performance (#7016)
Browse files Browse the repository at this point in the history
Co-authored-by: Masafumi Koba <473530+ybiquitous@users.noreply.github.com>
  • Loading branch information
jeddy3 and ybiquitous committed Jun 30, 2023
1 parent d2c398c commit 3065425
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-onions-play.md
@@ -0,0 +1,5 @@
---
"stylelint": patch
---

Fixed: `{selector,value}-no-vendor-prefix` performance
5 changes: 5 additions & 0 deletions lib/reference/prefixes.js
@@ -0,0 +1,5 @@
const prefixes = new Set(['-webkit-', '-moz-', '-ms-', '-o-']);

module.exports = {
prefixes,
};
3 changes: 3 additions & 0 deletions lib/rules/selector-no-vendor-prefix/index.js
@@ -1,5 +1,6 @@
'use strict';

const hasPrefix = require('../../utils/hasPrefix');
const isAutoprefixable = require('../../utils/isAutoprefixable');
const isStandardSyntaxRule = require('../../utils/isStandardSyntaxRule');
const optionsMatches = require('../../utils/optionsMatches');
Expand Down Expand Up @@ -41,6 +42,8 @@ const rule = (primary, secondaryOptions, context) => {
}

root.walkRules((ruleNode) => {
if (!hasPrefix(ruleNode.selector)) return;

if (!isStandardSyntaxRule(ruleNode)) {
return;
}
Expand Down
25 changes: 4 additions & 21 deletions lib/rules/value-no-vendor-prefix/index.js
Expand Up @@ -2,6 +2,7 @@

const valueParser = require('postcss-value-parser');

const hasPrefix = require('../../utils/hasPrefix');
const isAutoprefixable = require('../../utils/isAutoprefixable');
const isStandardSyntaxDeclaration = require('../../utils/isStandardSyntaxDeclaration');
const isStandardSyntaxProperty = require('../../utils/isStandardSyntaxProperty');
Expand All @@ -24,18 +25,6 @@ const meta = {
fixable: true,
};

const valuePrefixes = ['-webkit-', '-moz-', '-ms-', '-o-'];

/**
* @param {string} value
* @returns {boolean}
*/
const hasPrefix = (value) => {
const lowerValue = value.toLowerCase();

return valuePrefixes.some((prefix) => lowerValue.startsWith(prefix));
};

/** @type {import('stylelint').Rule} */
const rule = (primary, secondaryOptions, context) => {
return (root, result) => {
Expand All @@ -59,11 +48,9 @@ const rule = (primary, secondaryOptions, context) => {
root.walkDecls((decl) => {
const { value } = decl;

if (
!isStandardSyntaxDeclaration(decl) ||
!isStandardSyntaxProperty(decl.prop) ||
!value.startsWith('-')
) {
if (!hasPrefix(value)) return;

if (!isStandardSyntaxDeclaration(decl) || !isStandardSyntaxProperty(decl.prop)) {
return;
}

Expand All @@ -74,10 +61,6 @@ const rule = (primary, secondaryOptions, context) => {
const parsedValue = valueParser(value);

parsedValue.walk((node) => {
if (!hasPrefix(node.value)) {
return;
}

if (!isAutoprefixable.propertyValue(node.value)) {
return;
}
Expand Down
15 changes: 15 additions & 0 deletions lib/utils/hasPrefix.js
@@ -0,0 +1,15 @@
'use strict';

const { prefixes } = require('../reference/prefixes');

const HAS_PREFIX_REGEX = new RegExp(`(?:${[...prefixes].join('|')})`, 'i');

/**
* Check if a string contains any prefix
*
* @param {string} string
* @returns {boolean}
*/
module.exports = function hasPrefix(string) {
return HAS_PREFIX_REGEX.test(string);
};

0 comments on commit 3065425

Please sign in to comment.