Skip to content

Commit

Permalink
Fix declaration-block-no-shorthand-property-overrides false negativ…
Browse files Browse the repository at this point in the history
…es for `font` and `border`
  • Loading branch information
Mouvedia committed Apr 12, 2024
1 parent 6f6abba commit 3175dad
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/silent-flies-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"stylelint": minor
---

Fixed: `declaration-block-no-shorthand-property-overrides` false negatives for `font` and `border`
41 changes: 41 additions & 0 deletions lib/reference/properties.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,40 @@ export const acceptCustomIdentsProperties = new Set([
'list-style-type',
]);

/** @type {import('stylelint').ShorthandToResetToInitialProperty} */
export const shorthandToResetToInitialProperty = new Map([
[
'border',
new Set([
'border-image',
'border-image-outset',
'border-image-repeat',
'border-image-slice',
'border-image-source',
'border-image-width',
]),
],
[
'font',
new Set([
// prettier-ignore
'font-feature-settings',
'font-kerning',
'font-language-override',
'font-optical-sizing',
'font-size-adjust',
'font-variant-alternates',
'font-variant-caps',
'font-variant-east-asian',
'font-variant-emoji',
'font-variant-ligatures',
'font-variant-numeric',
'font-variant-position',
'font-variation-settings',
]),
],
]);

/** @type {import('stylelint').LonghandSubPropertiesOfShorthandProperties} */
export const longhandSubPropertiesOfShorthandProperties = new Map([
// Sort alphabetically
Expand Down Expand Up @@ -243,6 +277,13 @@ export const longhandSubPropertiesOfShorthandProperties = new Map([
new Set([
// prettier-ignore
'font-style',
/**
* reset explicitly: normal | small-caps
* reset implicitly: all-small-caps | petite-caps | all-petite-caps | unicase | titling-caps
* i.e. either way it will be reset
* @todo add font-variant shorthand to longhandSubPropertiesOfShorthandProperties
* {@link https://www.w3.org/TR/css-fonts-4/#font-variant-prop World Wide Web Consortium}
*/
'font-variant',
'font-weight',
'font-stretch',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ testRule({
endLine: 1,
endColumn: 33,
},
{
code: 'a { border-image: url("foo.png"); border: 1px solid black; }',
message: messages.rejected('border', 'border-image'),
line: 1,
column: 35,
endLine: 1,
endColumn: 41,
},
{
code: 'a { border-image-source: url("foo.png"); border: 1px solid black; }',
message: messages.rejected('border', 'border-image-source'),
line: 1,
column: 42,
endLine: 1,
endColumn: 48,
},
{
code: 'a { pAdDiNg-lEfT: 10Px; pAdDiNg: 20Px; }',
message: messages.rejected('pAdDiNg', 'pAdDiNg-lEfT'),
Expand Down Expand Up @@ -161,6 +177,39 @@ testRule({
endLine: 1,
endColumn: 61,
},
{
code: 'a { font-variant: small-caps; font: sans-serif; }',
message: messages.rejected('font', 'font-variant'),
description: 'CSS2 explicit reset',
line: 1,
column: 31,
endLine: 1,
endColumn: 35,
},
{
code: 'a { font-variant: all-small-caps; font: sans-serif; }',
message: messages.rejected('font', 'font-variant'),
description: 'CSS3 implicit reset',
line: 1,
column: 35,
endLine: 1,
endColumn: 39,
},
{
code: 'a { font-size-adjust: 0.545; font: Verdana; }',
message: messages.rejected('font', 'font-size-adjust'),
line: 1,
column: 30,
endLine: 1,
endColumn: 34,
},
{
code: 'a { font-variant-caps: small-caps; font-variant: normal; }',
message: messages.rejected('font-variant', 'font-variant-caps'),
line: 1,
endLine: 1,
skip: true,
},
],
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import eachDeclarationBlock from '../../utils/eachDeclarationBlock.mjs';
import { longhandSubPropertiesOfShorthandProperties } from '../../reference/properties.mjs';
import report from '../../utils/report.mjs';
import ruleMessages from '../../utils/ruleMessages.mjs';
import validateOptions from '../../utils/validateOptions.mjs';
import vendor from '../../utils/vendor.mjs';

import {
longhandSubPropertiesOfShorthandProperties,
shorthandToResetToInitialProperty,
} from '../../reference/properties.mjs';

const ruleName = 'declaration-block-no-shorthand-property-overrides';

const messages = ruleMessages(ruleName, {
Expand Down Expand Up @@ -32,19 +36,21 @@ const rule = (primary) => {
const prop = decl.prop;
const unprefixedProp = vendor.unprefixed(prop).toLowerCase();
const prefix = vendor.prefix(prop).toLowerCase();

const overrideables = /** @type {Map<string, Set<string>>} */ (
const subProperties = /** @type {Map<string, Set<string>>} */ (
longhandSubPropertiesOfShorthandProperties
).get(unprefixedProp);
const resettables = /** @type {Map<string, Set<string>>} */ (
shorthandToResetToInitialProperty
).get(unprefixedProp);
// see microsoft/TypeScript#57228
const union = new Set([...(subProperties ?? []), ...(resettables ?? [])]);

declarations.set(prop.toLowerCase(), prop);

if (!overrideables) {
return;
}
if (union.size === 0) return;

for (const longhandProp of overrideables) {
const declaration = declarations.get(prefix + longhandProp);
for (const property of union) {
const declaration = declarations.get(prefix + property);

if (!declaration) {
continue;
Expand Down
1 change: 1 addition & 0 deletions types/stylelint/index.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export {
RuleSeverity,
Severity,
ShorthandProperties,
ShorthandToResetToInitialProperty,
StylelintPostcssResult,
Utils,
Warning,
Expand Down
4 changes: 4 additions & 0 deletions types/stylelint/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,10 @@ declare namespace stylelint {
| 'text-emphasis'
| 'transition';

type ImplicitResetters = Extract<ShorthandProperties, 'border' | 'flex' | 'font'>;
/** @internal */
export type ShorthandToResetToInitialProperty = ReadonlyMap<ImplicitResetters, Set<string>>;

/** @internal */
export type LonghandSubPropertiesOfShorthandProperties = ReadonlyMap<
ShorthandProperties,
Expand Down

0 comments on commit 3175dad

Please sign in to comment.