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 autofix to font-weight-notation #6347

Merged
merged 3 commits into from Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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/cool-zebras-sparkle.md
@@ -0,0 +1,5 @@
---
"stylelint": minor
---

Added: `font-weight-notation` autofix
2 changes: 1 addition & 1 deletion docs/user-guide/rules/list.md
Expand Up @@ -123,7 +123,7 @@ Within each cateogory, the rules are grouped by the [_thing_](http://apps.workfl

### Font weight

- [`font-weight-notation`](../../../lib/rules/font-weight-notation/README.md): Require numeric or named (where possible) `font-weight` values. Also, when named values are expected, require only valid names.
- [`font-weight-notation`](../../../lib/rules/font-weight-notation/README.md): Require numeric or named (where possible) `font-weight` values. Also, when named values are expected, require only valid names (Autofixable).

### Function

Expand Down
12 changes: 9 additions & 3 deletions lib/reference/keywords.js
Expand Up @@ -28,7 +28,12 @@ const fontFamilyKeywords = uniteSets(basicKeywords, [

const fontWeightRelativeKeywords = new Set(['bolder', 'lighter']);

const fontWeightAbsoluteKeywords = new Set(['bold']);
const fontWeightAbsoluteKeywords = new Set(['normal', 'bold']);

const fontWeightNonNumericKeywords = uniteSets(
fontWeightRelativeKeywords,
fontWeightAbsoluteKeywords,
);

const fontWeightNumericKeywords = new Set([
'100',
Expand All @@ -44,8 +49,7 @@ const fontWeightNumericKeywords = new Set([

const fontWeightKeywords = uniteSets(
basicKeywords,
fontWeightRelativeKeywords,
fontWeightAbsoluteKeywords,
fontWeightNonNumericKeywords,
fontWeightNumericKeywords,
);

Expand Down Expand Up @@ -311,7 +315,9 @@ module.exports = {
fontFamilyKeywords,
fontShorthandKeywords,
fontSizeKeywords,
fontWeightAbsoluteKeywords,
fontWeightKeywords,
fontWeightNonNumericKeywords,
fontWeightRelativeKeywords,
gridAreaKeywords,
gridColumnKeywords,
Expand Down
2 changes: 2 additions & 0 deletions lib/rules/font-weight-notation/README.md
Expand Up @@ -21,6 +21,8 @@ Valid font-weight names are `normal`, `bold`, `bolder`, and `lighter`.

This rule ignores `$sass`, `@less`, and `var(--custom-property)` variable syntaxes.

The [`fix` option](../../../docs/user-guide/usage/options.md#fix) can automatically fix all of the problems reported by this rule.

## Options

`string`: `"numeric"|"named-where-possible"`
Expand Down
59 changes: 54 additions & 5 deletions lib/rules/font-weight-notation/__tests__/index.js
Expand Up @@ -5,6 +5,7 @@ const { messages, ruleName } = require('..');
testRule({
ruleName,
config: ['numeric'],
fix: true,

accept: [
{
Expand Down Expand Up @@ -83,6 +84,10 @@ testRule({
code: 'a { font-weight: INITIAL; }',
description: 'ignore initial value',
},
{
code: 'a { font-weight: /* bold */ 400; }',
description: 'ignore comment',
},
{
code: '@font-face { font-weight: 400; }',
},
Expand Down Expand Up @@ -115,6 +120,7 @@ testRule({
reject: [
{
code: 'a { font-weight: normal; }',
fixed: 'a { font-weight: 400; }',
message: messages.expected('numeric'),
line: 1,
column: 18,
Expand All @@ -123,6 +129,7 @@ testRule({
},
{
code: 'a { fOnT-wEiGhT: normal; }',
fixed: 'a { fOnT-wEiGhT: 400; }',
message: messages.expected('numeric'),
line: 1,
column: 18,
Expand All @@ -131,6 +138,7 @@ testRule({
},
{
code: 'a { FONT-WEIGHT: normal; }',
fixed: 'a { FONT-WEIGHT: 400; }',
message: messages.expected('numeric'),
line: 1,
column: 18,
Expand All @@ -139,6 +147,7 @@ testRule({
},
{
code: 'a { font-weight: nOrMaL; }',
fixed: 'a { font-weight: 400; }',
message: messages.expected('numeric'),
line: 1,
column: 18,
Expand All @@ -147,14 +156,25 @@ testRule({
},
{
code: 'a { font-weight: NORMAL; }',
fixed: 'a { font-weight: 400; }',
message: messages.expected('numeric'),
line: 1,
column: 18,
endLine: 1,
endColumn: 24,
},
{
code: 'a { font-weight: /* bold */ normal; }',
fixed: 'a { font-weight: /* bold */ 400; }',
message: messages.expected('numeric'),
line: 1,
column: 29,
endLine: 1,
endColumn: 35,
},
{
code: 'a { font: italic small-caps bolder 16px/3 cursive; }',
unfixable: true,
message: messages.expected('numeric'),
line: 1,
column: 29,
Expand All @@ -163,6 +183,7 @@ testRule({
},
{
code: 'a { font: normal 16px/3 cursive; }',
fixed: 'a { font: 400 16px/3 cursive; }',
description: 'one normal and no numbered weight',
message: messages.expected('numeric'),
line: 1,
Expand All @@ -172,6 +193,7 @@ testRule({
},
{
code: 'a { font: normal normal 16px/3 cursive; }',
fixed: 'a { font: 400 normal 16px/3 cursive; }',
description: 'two normals and no numbered weight',
message: messages.expected('numeric'),
line: 1,
Expand All @@ -181,6 +203,7 @@ testRule({
},
{
code: 'a { font: normal normal normal 16px/3 cursive; }',
fixed: 'a { font: 400 normal normal 16px/3 cursive; }',
description: 'three normals and no numbered weight',
message: messages.expected('numeric'),
line: 1,
Expand All @@ -190,14 +213,27 @@ testRule({
},
{
code: '@font-face { font-weight: normal bold; }',
message: messages.expected('numeric'),
line: 1,
column: 27,
endLine: 1,
endColumn: 33,
fixed: '@font-face { font-weight: 400 700; }',
warnings: [
{
message: messages.expected('numeric'),
line: 1,
column: 27,
endLine: 1,
endColumn: 33,
},
{
message: messages.expected('numeric'),
line: 1,
column: 34,
endLine: 1,
endColumn: 38,
},
],
},
{
code: '@font-face { font-weight: 400 bold; }',
fixed: '@font-face { font-weight: 400 700; }',
message: messages.expected('numeric'),
line: 1,
column: 31,
Expand All @@ -206,6 +242,7 @@ testRule({
},
{
code: '@font-face { font-weight: normal 700; }',
fixed: '@font-face { font-weight: 400 700; }',
message: messages.expected('numeric'),
line: 1,
column: 27,
Expand All @@ -214,6 +251,7 @@ testRule({
},
{
code: '@font-face { font-weight: /* 400 */ normal 700; }',
fixed: '@font-face { font-weight: /* 400 */ 400 700; }',
message: messages.expected('numeric'),
line: 1,
column: 37,
Expand All @@ -222,6 +260,7 @@ testRule({
},
{
code: '@font-face { font-weight: normal /* 400 */700; }',
fixed: '@font-face { font-weight: 400 /* 400 */700; }',
message: messages.expected('numeric'),
line: 1,
column: 27,
Expand All @@ -230,6 +269,7 @@ testRule({
},
{
code: '@font-face { font-weight: normal/* 400 */ 700; }',
fixed: '@font-face { font-weight: 400/* 400 */ 700; }',
message: messages.expected('numeric'),
line: 1,
column: 27,
Expand All @@ -242,6 +282,7 @@ testRule({
testRule({
ruleName,
config: ['numeric', { ignore: ['relative'] }],
fix: true,

accept: [
{
Expand All @@ -267,6 +308,7 @@ testRule({
reject: [
{
code: 'a { font-weight: normal; }',
fixed: 'a { font-weight: 400; }',
message: messages.expected('numeric'),
line: 1,
column: 18,
Expand All @@ -279,6 +321,7 @@ testRule({
testRule({
ruleName,
config: ['named-where-possible'],
fix: true,

accept: [
{
Expand Down Expand Up @@ -357,6 +400,7 @@ testRule({
reject: [
{
code: 'a { font-weight: 400; }',
fixed: 'a { font-weight: normal; }',
message: messages.expected('named'),
line: 1,
column: 18,
Expand All @@ -365,6 +409,7 @@ testRule({
},
{
code: 'a { font-weight: boldd; }',
unfixable: true,
description: 'invalid font-weight value',
message: messages.invalidNamed('boldd'),
line: 1,
Expand All @@ -374,6 +419,7 @@ testRule({
},
{
code: 'a { font: italic small-caps 700 16px/3 cursive; }',
fixed: 'a { font: italic small-caps bold 16px/3 cursive; }',
message: messages.expected('named'),
line: 1,
column: 29,
Expand All @@ -382,6 +428,7 @@ testRule({
},
{
code: 'a { font: normal 400 normal 16px serif; }',
fixed: 'a { font: normal normal normal 16px serif; }',
description: 'two normals and a numbered weight',
message: messages.expected('named'),
line: 1,
Expand All @@ -391,6 +438,7 @@ testRule({
},
{
code: 'a { font: 400 normal 16px serif; }',
fixed: 'a { font: normal normal 16px serif; }',
description: 'one normal and a numbered weight',
message: messages.expected('named'),
line: 1,
Expand All @@ -400,6 +448,7 @@ testRule({
},
{
code: 'a { font: 400 16px serif; }',
fixed: 'a { font: normal 16px serif; }',
description: 'no normals and a numbered weight',
message: messages.expected('named'),
line: 1,
Expand Down