diff --git a/.changeset/wicked-wombats-look.md b/.changeset/wicked-wombats-look.md new file mode 100644 index 0000000000..6029390915 --- /dev/null +++ b/.changeset/wicked-wombats-look.md @@ -0,0 +1,5 @@ +--- +"stylelint": patch +--- + +Fixed: `font-weight-notation` messages diff --git a/lib/rules/font-weight-notation/README.md b/lib/rules/font-weight-notation/README.md index 83ddb04e66..16b1729af8 100644 --- a/lib/rules/font-weight-notation/README.md +++ b/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. ```css @@ -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. diff --git a/lib/rules/font-weight-notation/__tests__/index.js b/lib/rules/font-weight-notation/__tests__/index.js index 5cc2418852..686670b83a 100644 --- a/lib/rules/font-weight-notation/__tests__/index.js +++ b/lib/rules/font-weight-notation/__tests__/index.js @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, diff --git a/lib/rules/font-weight-notation/index.js b/lib/rules/font-weight-notation/index.js index 2fcdecdf77..bd6e73fc36 100644 --- a/lib/rules/font-weight-notation/index.js +++ b/lib/rules/font-weight-notation/index.js @@ -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'); @@ -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 = { @@ -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'], ]); @@ -124,9 +124,9 @@ 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; @@ -134,35 +134,29 @@ const rule = (primary, secondaryOptions, context) => { } } - 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; }