Skip to content

Commit

Permalink
Allow negative values for shorthand-property-redudant-values (#3840) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyCahoon authored and jeddy3 committed Jan 16, 2019
1 parent 387bda2 commit 6ddc90b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 26 deletions.
Expand Up @@ -11,6 +11,9 @@ testRule(rule, {
{
code: "a { margin: 1px; }"
},
{
code: "a { margin: -1px; }"
},
{
code: "a { margin: 1px 2px; }"
},
Expand Down Expand Up @@ -343,6 +346,34 @@ testRule(rule, {
message: messages.rejected("1em 0 2em 0", "1em 0 2em"),
line: 1,
column: 5
},
{
code: "a { margin: -1px -1px -1px -1px; }",
fixed: "a { margin: -1px; }",
message: messages.rejected("-1px -1px -1px -1px", "-1px"),
line: 1,
column: 5
},
{
code: "a { margin: -1px -1px -1px; }",
fixed: "a { margin: -1px; }",
message: messages.rejected("-1px -1px -1px", "-1px"),
line: 1,
column: 5
},
{
code: "a { margin: 0 -1px 0 -1px; }",
fixed: "a { margin: 0 -1px; }",
message: messages.rejected("0 -1px 0 -1px", "0 -1px"),
line: 1,
column: 5
},
{
code: "a { margin: -1em 0 2em 0; }",
fixed: "a { margin: -1em 0 2em; }",
message: messages.rejected("-1em 0 2em 0", "-1em 0 2em"),
line: 1,
column: 5
}
]
});
Expand Down
36 changes: 10 additions & 26 deletions lib/rules/shorthand-property-no-redundant-values/index.js
Expand Up @@ -15,8 +15,7 @@ const messages = ruleMessages(ruleName, {
`Unexpected longhand value '${unexpected}' instead of '${expected}'`
});

// Only these shorthand properties can have values, that can be written in shorter form (remove repetitions)
const shorthandableProperties = new Set([
const propertiesWithShorthandNotation = new Set([
"margin",
"padding",
"border-color",
Expand All @@ -26,23 +25,16 @@ const shorthandableProperties = new Set([
"grid-gap"
]);

const ignoredCharacters = [
"+",
"-",
"*",
"/",
"(",
")",
"$",
"@",
"--",
"var("
];

function isIgnoredCharacters(value) {
const ignoredCharacters = ["+", "*", "/", "(", ")", "$", "@", "--", "var("];

function hasIgnoredCharacters(value) {
return ignoredCharacters.some(char => value.indexOf(char) !== -1);
}

function isShorthandProperty(property) {
return propertiesWithShorthandNotation.has(property);
}

function canCondense(top, right) {
const bottom =
arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
Expand Down Expand Up @@ -109,11 +101,7 @@ const rule = function(actual, secondary, context) {

const normalizedProp = postcss.vendor.unprefixed(prop.toLowerCase());

// Ignore not shorthandable properties, and math operations
if (
isIgnoredCharacters(value) ||
!shorthandableProperties.has(normalizedProp)
) {
if (hasIgnoredCharacters(value) || !isShorthandProperty(normalizedProp)) {
return;
}

Expand All @@ -132,11 +120,7 @@ const rule = function(actual, secondary, context) {
}

const shortestForm = canCondense.apply(undefined, valuesToShorthand);
const shortestFormString = shortestForm
.filter(value => {
return value;
})
.join(" ");
const shortestFormString = shortestForm.filter(value => value).join(" ");
const valuesFormString = valuesToShorthand.join(" ");

if (shortestFormString.toLowerCase() === valuesFormString.toLowerCase()) {
Expand Down

0 comments on commit 6ddc90b

Please sign in to comment.