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

Fix declaration-block-no-shorthand-property-overrides false negatives for font and border #7606

Merged
merged 3 commits into from
Apr 18, 2024
Merged
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
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`
Mouvedia marked this conversation as resolved.
Show resolved Hide resolved
42 changes: 42 additions & 0 deletions lib/reference/properties.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,40 @@ const acceptCustomIdentsProperties = new Set([
'list-style-type',
]);

/** @type {import('stylelint').ShorthandToResetToInitialProperty} */
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} */
const longhandSubPropertiesOfShorthandProperties = new Map([
// Sort alphabetically
Expand Down Expand Up @@ -247,6 +281,13 @@ 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 Expand Up @@ -573,3 +614,4 @@ exports.acceptCustomIdentsProperties = acceptCustomIdentsProperties;
exports.longhandSubPropertiesOfShorthandProperties = longhandSubPropertiesOfShorthandProperties;
exports.longhandTimeProperties = longhandTimeProperties;
exports.shorthandTimeProperties = shorthandTimeProperties;
exports.shorthandToResetToInitialProperty = shorthandToResetToInitialProperty;
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',
Mouvedia marked this conversation as resolved.
Show resolved Hide resolved
new Set([
Mouvedia marked this conversation as resolved.
Show resolved Hide resolved
// 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
Expand Up @@ -3,11 +3,11 @@
'use strict';

const eachDeclarationBlock = require('../../utils/eachDeclarationBlock.cjs');
const properties = require('../../reference/properties.cjs');
const report = require('../../utils/report.cjs');
const ruleMessages = require('../../utils/ruleMessages.cjs');
const validateOptions = require('../../utils/validateOptions.cjs');
const vendor = require('../../utils/vendor.cjs');
const properties = require('../../reference/properties.cjs');

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

Expand Down Expand Up @@ -36,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>>} */ (
properties.longhandSubPropertiesOfShorthandProperties
).get(unprefixedProp);
const resettables = /** @type {Map<string, Set<string>>} */ (
properties.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
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);
Mouvedia marked this conversation as resolved.
Show resolved Hide resolved
// see microsoft/TypeScript#57228
Mouvedia marked this conversation as resolved.
Show resolved Hide resolved
const union = new Set([...(subProperties ?? []), ...(resettables ?? [])]);
Mouvedia marked this conversation as resolved.
Show resolved Hide resolved

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'>;
Mouvedia marked this conversation as resolved.
Show resolved Hide resolved
/** @internal */
export type ShorthandToResetToInitialProperty = ReadonlyMap<ImplicitResetters, Set<string>>;

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