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
Changes from 1 commit
ff585bf
9abc435
cb2c0dc
49ff0c2
e109761
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,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}"`, | ||
invalidNamed: (name) => `Unexpected invalid font-weight name "${name}"`, | ||
ybiquitous marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
|
@@ -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([ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [note] Rename for more accuracy because both |
||
['400', 'normal'], | ||
['700', 'bold'], | ||
]); | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [note] Use |
||
|
||
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; | ||
} | ||
|
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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
andbold
rather than relative words too. Now we have an odd mix of thenamed-where-possible
andnumeric
primary options along with theignore: ["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:stylelint/lib/rules/font-weight-notation/__tests__/index.js
Lines 175 to 183 in 9abc435