Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed selector|value-no-vendor-prefix performance #7016

Merged
merged 5 commits into from Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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-']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is to be reused, Ill eventually add some more rare ones to the set.

ref: https://stackoverflow.com/questions/11705144/webkit-and-its-legacy-prefixes


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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more agnostic way would be to use

prefix(prop) {
const match = prop.match(/^(-\w+-)/);
if (match) {
return match[0] || '';
}
return '';
},

instead.


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);
};