Skip to content

Commit

Permalink
Refactor to add isIgnoredFunction utility (#7250)
Browse files Browse the repository at this point in the history
Added `isIgnoredFunction` utility function and replaced duplicated
 isIgnoredFunction functions throughout codebase. Added unit test for
 the function. closes #7249

Signed-off-by: Oscar <71343264+0scvr@users.noreply.github.com>
Co-authored-by: Masafumi Koba <473530+ybiquitous@users.noreply.github.com>
  • Loading branch information
0scvr and ybiquitous committed Oct 19, 2023
1 parent 8a45ca3 commit 958bf36
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
9 changes: 1 addition & 8 deletions lib/rules/color-hex-length/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const ruleMessages = require('../../utils/ruleMessages');
const setDeclarationValue = require('../../utils/setDeclarationValue');
const validateOptions = require('../../utils/validateOptions');
const isHexColor = require('../../utils/isHexColor');
const isIgnoredFunction = require('../../utils/isIgnoredFunction');

const ruleName = 'color-hex-length';

Expand All @@ -22,7 +23,6 @@ const meta = {
};

const CONTAINS_HEX = /#[\da-z]+/i;
const IGNORED_FUNCTIONS = new Set(['url']);

/** @type {import('stylelint').Rule} */
const rule = (primary, _secondaryOptions, context) => {
Expand Down Expand Up @@ -128,13 +128,6 @@ function longer(hex) {
return hexVariant;
}

/**
* @param {import('postcss-value-parser').Node} node
*/
function isIgnoredFunction({ type, value }) {
return type === 'function' && IGNORED_FUNCTIONS.has(value.toLowerCase());
}

rule.ruleName = ruleName;
rule.messages = messages;
rule.meta = meta;
Expand Down
9 changes: 1 addition & 8 deletions lib/rules/color-no-hex/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const getDeclarationValue = require('../../utils/getDeclarationValue');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
const isIgnoredFunction = require('../../utils/isIgnoredFunction');

const ruleName = 'color-no-hex';

Expand All @@ -20,7 +21,6 @@ const meta = {

const HEX = /^#[\da-z]+$/i;
const CONTAINS_HEX = /#[\da-z]+/i;
const IGNORED_FUNCTIONS = new Set(['url']);

/** @type {import('stylelint').Rule} */
const rule = (primary) => {
Expand Down Expand Up @@ -58,13 +58,6 @@ const rule = (primary) => {
};
};

/**
* @param {import('postcss-value-parser').Node} node
*/
function isIgnoredFunction({ type, value }) {
return type === 'function' && IGNORED_FUNCTIONS.has(value.toLowerCase());
}

/**
* @param {import('postcss-value-parser').Node} node
*/
Expand Down
17 changes: 17 additions & 0 deletions lib/utils/__tests__/isIgnoredFunction.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import isIgnoredFunction from '../isIgnoredFunction.js';
import valueParser from 'postcss-value-parser';

describe('isIgnoredFunction', () => {
it('ignores other functions', () => {
expect(isIgnoredFunction(valueParser('calc(10% + 10px)').nodes[0])).toBe(false);
expect(isIgnoredFunction(valueParser('calc(10px * pow(2, 10)').nodes[0])).toBe(false);
expect(isIgnoredFunction(valueParser('rem(10rem, 6rem)').nodes[0])).toBe(false);
expect(isIgnoredFunction(valueParser('var(--width, 20px)').nodes[0])).toBe(false);
expect(isIgnoredFunction(valueParser('round(var(--width), 50px)').nodes[0])).toBe(false);
});

it('matches url', () => {
expect(isIgnoredFunction(valueParser('url("photo.gif")').nodes[0])).toBe(true);
expect(isIgnoredFunction(valueParser('url("../images/bullet.jpg")').nodes[0])).toBe(true);
});
});
11 changes: 11 additions & 0 deletions lib/utils/isIgnoredFunction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

const IGNORED_FUNCTIONS = new Set(['url']);

/**
* @param {import('postcss-value-parser').Node} node
* @returns {boolean}
*/
module.exports = function isIgnoredFunction({ type, value }) {
return type === 'function' && IGNORED_FUNCTIONS.has(value.toLowerCase());
};

0 comments on commit 958bf36

Please sign in to comment.