Skip to content

Commit

Permalink
Fix messages of font-weight-notation (#6350)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous committed Sep 19, 2022
1 parent ee5cc3d commit 6edbbdf
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 59 deletions.
5 changes: 5 additions & 0 deletions .changeset/wicked-wombats-look.md
@@ -0,0 +1,5 @@
---
"stylelint": patch
---

Fixed: `font-weight-notation` messages
4 changes: 1 addition & 3 deletions lib/rules/font-weight-notation/README.md
@@ -1,6 +1,6 @@
# font-weight-notation

Require numeric or named (where possible) `font-weight` values. Also, when named values are expected, require only valid names.
Require numeric or named (where possible) `font-weight` values.

<!-- prettier-ignore -->
```css
Expand All @@ -17,8 +17,6 @@ a { font: italic small-caps 600 16px/3 cursive; }
* Multiple notations are available in @font-face */
```

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.
Expand Down
54 changes: 22 additions & 32 deletions lib/rules/font-weight-notation/__tests__/index.js
Expand Up @@ -121,7 +121,7 @@ testRule({
{
code: 'a { font-weight: normal; }',
fixed: 'a { font-weight: 400; }',
message: messages.expected('numeric'),
message: messages.expectedWithActual('normal', '400'),
line: 1,
column: 18,
endLine: 1,
Expand All @@ -130,7 +130,7 @@ testRule({
{
code: 'a { fOnT-wEiGhT: normal; }',
fixed: 'a { fOnT-wEiGhT: 400; }',
message: messages.expected('numeric'),
message: messages.expectedWithActual('normal', '400'),
line: 1,
column: 18,
endLine: 1,
Expand All @@ -139,7 +139,7 @@ testRule({
{
code: 'a { FONT-WEIGHT: normal; }',
fixed: 'a { FONT-WEIGHT: 400; }',
message: messages.expected('numeric'),
message: messages.expectedWithActual('normal', '400'),
line: 1,
column: 18,
endLine: 1,
Expand All @@ -148,7 +148,7 @@ testRule({
{
code: 'a { font-weight: nOrMaL; }',
fixed: 'a { font-weight: 400; }',
message: messages.expected('numeric'),
message: messages.expectedWithActual('nOrMaL', '400'),
line: 1,
column: 18,
endLine: 1,
Expand All @@ -157,7 +157,7 @@ testRule({
{
code: 'a { font-weight: NORMAL; }',
fixed: 'a { font-weight: 400; }',
message: messages.expected('numeric'),
message: messages.expectedWithActual('NORMAL', '400'),
line: 1,
column: 18,
endLine: 1,
Expand All @@ -166,7 +166,7 @@ testRule({
{
code: 'a { font-weight: /* bold */ normal; }',
fixed: 'a { font-weight: /* bold */ 400; }',
message: messages.expected('numeric'),
message: messages.expectedWithActual('normal', '400'),
line: 1,
column: 29,
endLine: 1,
Expand All @@ -185,7 +185,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'),
message: messages.expectedWithActual('normal', '400'),
line: 1,
column: 11,
endLine: 1,
Expand All @@ -195,7 +195,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'),
message: messages.expectedWithActual('normal', '400'),
line: 1,
column: 11,
endLine: 1,
Expand All @@ -205,7 +205,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'),
message: messages.expectedWithActual('normal', '400'),
line: 1,
column: 11,
endLine: 1,
Expand All @@ -216,14 +216,14 @@ testRule({
fixed: '@font-face { font-weight: 400 700; }',
warnings: [
{
message: messages.expected('numeric'),
message: messages.expectedWithActual('normal', '400'),
line: 1,
column: 27,
endLine: 1,
endColumn: 33,
},
{
message: messages.expected('numeric'),
message: messages.expectedWithActual('bold', '700'),
line: 1,
column: 34,
endLine: 1,
Expand All @@ -234,7 +234,7 @@ testRule({
{
code: '@font-face { font-weight: 400 bold; }',
fixed: '@font-face { font-weight: 400 700; }',
message: messages.expected('numeric'),
message: messages.expectedWithActual('bold', '700'),
line: 1,
column: 31,
endLine: 1,
Expand All @@ -243,7 +243,7 @@ testRule({
{
code: '@font-face { font-weight: normal 700; }',
fixed: '@font-face { font-weight: 400 700; }',
message: messages.expected('numeric'),
message: messages.expectedWithActual('normal', '400'),
line: 1,
column: 27,
endLine: 1,
Expand All @@ -252,7 +252,7 @@ testRule({
{
code: '@font-face { font-weight: /* 400 */ normal 700; }',
fixed: '@font-face { font-weight: /* 400 */ 400 700; }',
message: messages.expected('numeric'),
message: messages.expectedWithActual('normal', '400'),
line: 1,
column: 37,
endLine: 1,
Expand All @@ -261,7 +261,7 @@ testRule({
{
code: '@font-face { font-weight: normal /* 400 */700; }',
fixed: '@font-face { font-weight: 400 /* 400 */700; }',
message: messages.expected('numeric'),
message: messages.expectedWithActual('normal', '400'),
line: 1,
column: 27,
endLine: 1,
Expand All @@ -270,7 +270,7 @@ testRule({
{
code: '@font-face { font-weight: normal/* 400 */ 700; }',
fixed: '@font-face { font-weight: 400/* 400 */ 700; }',
message: messages.expected('numeric'),
message: messages.expectedWithActual('normal', '400'),
line: 1,
column: 27,
endLine: 1,
Expand Down Expand Up @@ -309,7 +309,7 @@ testRule({
{
code: 'a { font-weight: normal; }',
fixed: 'a { font-weight: 400; }',
message: messages.expected('numeric'),
message: messages.expectedWithActual('normal', '400'),
line: 1,
column: 18,
endLine: 1,
Expand Down Expand Up @@ -401,26 +401,16 @@ testRule({
{
code: 'a { font-weight: 400; }',
fixed: 'a { font-weight: normal; }',
message: messages.expected('named'),
message: messages.expectedWithActual('400', 'normal'),
line: 1,
column: 18,
endLine: 1,
endColumn: 21,
},
{
code: 'a { font-weight: boldd; }',
unfixable: true,
description: 'invalid font-weight value',
message: messages.invalidNamed('boldd'),
line: 1,
column: 18,
endLine: 1,
endColumn: 23,
},
{
code: 'a { font: italic small-caps 700 16px/3 cursive; }',
fixed: 'a { font: italic small-caps bold 16px/3 cursive; }',
message: messages.expected('named'),
message: messages.expectedWithActual('700', 'bold'),
line: 1,
column: 29,
endLine: 1,
Expand All @@ -430,7 +420,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'),
message: messages.expectedWithActual('400', 'normal'),
line: 1,
column: 18,
endLine: 1,
Expand All @@ -440,7 +430,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'),
message: messages.expectedWithActual('400', 'normal'),
line: 1,
column: 11,
endLine: 1,
Expand All @@ -450,7 +440,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'),
message: messages.expectedWithActual('400', 'normal'),
line: 1,
column: 11,
endLine: 1,
Expand Down
42 changes: 18 additions & 24 deletions lib/rules/font-weight-notation/index.js
Expand Up @@ -8,7 +8,6 @@ const isNumbery = require('../../utils/isNumbery');
const isStandardSyntaxValue = require('../../utils/isStandardSyntaxValue');
const isVariable = require('../../utils/isVariable');
const {
fontWeightKeywords,
fontWeightNonNumericKeywords,
fontWeightRelativeKeywords,
} = require('../../reference/keywords');
Expand All @@ -17,12 +16,13 @@ const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const setDeclarationValue = require('../../utils/setDeclarationValue');
const validateOptions = require('../../utils/validateOptions');
const { assertString } = require('../../utils/validateTypes');

const ruleName = 'font-weight-notation';

const messages = ruleMessages(ruleName, {
expected: (type) => `Expected ${type} font-weight notation`,
invalidNamed: (name) => `Unexpected invalid font-weight name "${name}"`,
expectedWithActual: (actual, expected) => `Expected "${actual}" to be "${expected}"`,
});

const meta = {
Expand All @@ -32,11 +32,11 @@ const meta = {

const NORMAL_KEYWORD = 'normal';

const KEYWORD_TO_NUMERIC = new Map([
const NAMED_TO_NUMERIC = new Map([
['normal', '400'],
['bold', '700'],
]);
const NUMERIC_TO_KEYWORD = new Map([
const NUMERIC_TO_NAMED = new Map([
['400', 'normal'],
['700', 'bold'],
]);
Expand Down Expand Up @@ -124,45 +124,39 @@ const rule = (primary, secondaryOptions, context) => {

if (primary === 'numeric') {
if (!isNumbery(lowerWeightValue) && fontWeightNonNumericKeywords.has(lowerWeightValue)) {
if (context.fix) {
const numericValue = KEYWORD_TO_NUMERIC.get(lowerWeightValue);
const numericValue = NAMED_TO_NUMERIC.get(lowerWeightValue);

if (context.fix) {
if (numericValue) {
weightValueNode.value = numericValue;

return true;
}
}

complain(messages.expected('numeric'), weightValueNode);
const msg = numericValue
? messages.expectedWithActual(weightValue, numericValue)
: messages.expected('numeric');

complain(msg, weightValueNode);

return true;
}
}

if (primary === 'named-where-possible') {
if (isNumbery(lowerWeightValue) && NUMERIC_TO_KEYWORD.has(lowerWeightValue)) {
if (context.fix) {
const keyword = NUMERIC_TO_KEYWORD.get(lowerWeightValue);
if (isNumbery(lowerWeightValue) && NUMERIC_TO_NAMED.has(lowerWeightValue)) {
const namedValue = NUMERIC_TO_NAMED.get(lowerWeightValue);

if (keyword) {
weightValueNode.value = keyword;
}
assertString(namedValue);

if (context.fix) {
weightValueNode.value = namedValue;

return true;
}

complain(messages.expected('named'), weightValueNode);

return true;
}

if (
decl.prop.toLowerCase() === 'font-weight' &&
!fontWeightKeywords.has(lowerWeightValue) &&
lowerWeightValue !== NORMAL_KEYWORD
) {
complain(messages.invalidNamed(weightValue), weightValueNode);
complain(messages.expectedWithActual(weightValue, namedValue), weightValueNode);

return true;
}
Expand Down

0 comments on commit 6edbbdf

Please sign in to comment.