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 messages of font-weight-notation #6350

Merged
merged 5 commits into from Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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/wicked-wombats-look.md
@@ -0,0 +1,5 @@
---
"stylelint": patch
---

Fixed: `font-weight-notation` messages
ybiquitous marked this conversation as resolved.
Show resolved Hide resolved
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
41 changes: 18 additions & 23 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,11 +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`,
expectedWithActual: (actual, expected) => `Expected "${actual}" to be "${expected}"`,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[note] Add expectedWithActual for backward compatibility.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right to add this.

When we first built the rule, it should have only checked normal and bold rather than relative words too. Now we have an odd mix of the named-where-possible and numeric primary options along with the ignore: ["relative"] secondary option.

It's too late to change it now.

Adding expectedWithActual is the best way to deal with backwards compatibility for things like:

{
code: 'a { font: italic small-caps bolder 16px/3 cursive; }',
unfixable: true,
message: messages.expected('numeric'),
line: 1,
column: 29,
endLine: 1,
endColumn: 35,
},

invalidNamed: (name) => `Unexpected invalid font-weight name "${name}"`,
ybiquitous marked this conversation as resolved.
Show resolved Hide resolved
});

Expand All @@ -32,11 +33,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([
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[note] Rename for more accuracy because both 400 and normal are keywords.

['400', 'normal'],
['700', 'bold'],
]);
Expand Down Expand Up @@ -124,45 +125,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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[note] Use assertString for less if branches. NUMERIC_TO_NAMED.has() === true ensures the return value of NUMERIC_TO_NAMED.get() is a string.


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