Skip to content

Commit

Permalink
Fix declaration-block-no-redundant-longhand-properties false negati…
Browse files Browse the repository at this point in the history
…ve for `font-synthesis` (#7214)

Note: `font-synthesis` actually supports *four* longhand properties. However, `font-synthesis-position` currently has [**0.41%** adoption according to caniuse](https://caniuse.com/mdn-css_properties_font-synthesis-position), which is far too small for it to be relevant. I've elected to not include it for now; however, we could also include it if we think that better future-proofs the rule? 

---------

Co-authored-by: Masafumi Koba <473530+ybiquitous@users.noreply.github.com>
  • Loading branch information
mattxwang and ybiquitous committed Oct 9, 2023
1 parent 3374bb4 commit dfd1ffc
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/wicked-lions-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"stylelint": patch
---

Fixed: `declaration-block-no-redundant-longhand-properties` false negative for `font-synthesis`
9 changes: 9 additions & 0 deletions lib/reference/properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,15 @@ const longhandSubPropertiesOfShorthandProperties = new Map([
'font-family',
]),
],
[
'font-synthesis',
new Set([
// prettier-ignore
'font-synthesis-weight',
'font-synthesis-style',
'font-synthesis-small-caps',
]),
],
[
'gap',
new Set([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ This rule complains when the following shorthand properties can be used:
- `flex`
- `flex-flow`
- `font`
- `font-synthesis`
- `gap`
- `grid`
- `grid-area`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,30 @@ testRule({
description: 'explicit border-inline test',
message: messages.expected('border-inline'),
},
{
code: 'a { font-synthesis-weight: none; font-synthesis-style: none; font-synthesis-small-caps: none; }',
fixed: 'a { font-synthesis: none; }',
description: 'font-synthesis - all none',
message: messages.expected('font-synthesis'),
},
{
code: 'a { font-synthesis-weight: auto; font-synthesis-style: auto; font-synthesis-small-caps: auto; }',
fixed: 'a { font-synthesis: weight style small-caps; }',
description: 'font-synthesis - all auto',
message: messages.expected('font-synthesis'),
},
{
code: 'a { font-synthesis-weight: auto; font-synthesis-style: none; font-synthesis-small-caps: auto; }',
fixed: 'a { font-synthesis: weight small-caps; }',
description: 'font-synthesis - mixed',
message: messages.expected('font-synthesis'),
},
{
code: 'a { font-synthesis-weight: aut3; font-synthesis-style: none; font-synthesis-small-caps: auto; }',
unfixable: true,
description: 'font-synthesis - invalid value cannot be combined',
message: messages.expected('font-synthesis'),
},
{
code: 'div { overflow-y: visible; overflow-x: hidden; }',
fixed: 'div { overflow: hidden visible; }',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,43 @@ const meta = {

/** @type {Map<string, (decls: Map<string, Declaration>) => (string | undefined)>} */
const customResolvers = new Map([
[
'font-synthesis',
(decls) => {
const weight = decls.get('font-synthesis-weight')?.value.trim();
const style = decls.get('font-synthesis-style')?.value.trim();
const smallCaps = decls.get('font-synthesis-small-caps')?.value.trim();

/** @type {(s: string | undefined) => boolean} */
const isValidFontSynthesisValue = (s) => s === 'none' || s === 'auto';

if (
!isValidFontSynthesisValue(weight) ||
!isValidFontSynthesisValue(style) ||
!isValidFontSynthesisValue(smallCaps)
) {
return;
}

const autoShorthands = [];

if (weight === 'auto') {
autoShorthands.push('weight');
}

if (style === 'auto') {
autoShorthands.push('style');
}

if (smallCaps === 'auto') {
autoShorthands.push('small-caps');
}

if (autoShorthands.length === 0) return 'none';

return autoShorthands.join(' ');
},
],
[
'grid-column',
(decls) => {
Expand Down
1 change: 1 addition & 0 deletions types/stylelint/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ declare namespace stylelint {
| 'flex'
| 'flex-flow'
| 'font'
| 'font-synthesis'
| 'gap'
| 'grid'
| 'grid-area'
Expand Down

0 comments on commit dfd1ffc

Please sign in to comment.