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

Add ignoreFunctions: [] to length-zero-no-unit #5082

Closed
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions lib/rules/length-zero-no-unit/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -620,3 +620,14 @@ testRule({
},
],
});

testRule({
ruleName,
config: [true, { ignoreFunctions: ['var', '--custom'] }],
accept: [
{
code: '.foo { margin-top: var(--y, 0px); margin-bottom: --custom(0px); };',
description: 'Should ignore especified functions',
},
],
});
42 changes: 39 additions & 3 deletions lib/rules/length-zero-no-unit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,29 @@ const ruleMessages = require('../../utils/ruleMessages');
const styleSearch = require('style-search');
const validateOptions = require('../../utils/validateOptions');
const valueParser = require('postcss-value-parser');
//const { truncate } = require('lodash');
vankop marked this conversation as resolved.
Show resolved Hide resolved

const ruleName = 'length-zero-no-unit';

const messages = ruleMessages(ruleName, {
rejected: 'Unexpected unit',
});

function rule(actual, secondary, context) {
function rule(actual, options, context) {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, { actual });
const validOptions = validateOptions(
result,
ruleName,
{ actual },
{
actual: options,
possible: {
ignore: ['custom-properties'],
ignoreFunctions: [_.isString],
},
optional: true,
},
);

if (!validOptions) {
return;
Expand Down Expand Up @@ -83,13 +96,36 @@ function rule(actual, secondary, context) {
});

function check(value, node, ignorableIndexes = []) {
if (optionsMatches(secondary, 'ignore', 'custom-properties') && isCustomProperty(value)) {
if (optionsMatches(options, 'ignore', 'custom-properties') && isCustomProperty(value)) {
return;
}

// This was another option although I didn't know how to make it work
// const parsedValue = valueParser(value);
// parsedValue.walk((valueNode) => {
// const functionName = valueNode.value;
// if (optionsMatches(options, 'ignoreFunctions', valueNode.value)) {
// console.log(valueNode.value);
// return;
// }
// });

const fixPositions = [];

styleSearch({ source: value, target: '0' }, (match) => {
const ignoreFunctions = (options && options.ignoreFunctions) || [];

if (
ignoreFunctions.length > 0 &&
ignoreFunctions.map((functionName) => {
vankop marked this conversation as resolved.
Show resolved Hide resolved
if (value.includes(functionName)) return true;

return false;
})
) {
return;
}

const index = match.startIndex;

// Given a 0 somewhere in the full property value (not in a string, thanks
Expand Down