-
-
Notifications
You must be signed in to change notification settings - Fork 24
feat: add font-family-fallbacks
rule
#174
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
Conversation
Converting to a draft PR so we don't accidentally review and merge this. |
font-family-fallbacks
rule (draft)font-family-fallbacks
rule
docs/rules/font-family-fallbacks.md
Outdated
|
||
The browser checks if the first font is available to the user's system, if the first font is not available, it tries the next font in the list. This process continues until a font is found. If none of the specified fonts are available, the browser uses the generic font family (like `serif`, `sans-serif`, etc.) at the end of the list. | ||
|
||
It is often suggested to use a fallback font and a generic font last in the `font-family` list to ensure that text is always displayed in readable and consistent way, even if the preferred fonts are not available on the user's system or if the browser doesn't support the preferred font. |
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.
It is often suggested to use a fallback font and a generic font last in the `font-family` list to ensure that text is always displayed in readable and consistent way, even if the preferred fonts are not available on the user's system or if the browser doesn't support the preferred font. | |
It is a best practice to use a fallback font and a generic font last in the `font-family` list to ensure that text is always displayed in readable and consistent way, even if the preferred fonts are not available on the user's system or if the browser doesn't support the preferred font. |
src/index.js
Outdated
@@ -9,8 +9,18 @@ | |||
|
|||
import { CSSLanguage } from "./languages/css-language.js"; | |||
import { CSSSourceCode } from "./languages/css-source-code.js"; | |||
import recommendedRules from "./build/recommended-config.js"; | |||
import rules from "./build/rules.js"; | |||
import fontFamilyFallbacks from "./rules/font-family-fallbacks.js"; |
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.
It looks like this is reverting the recent work that we did to build up the rules list. Can you rebase and double-check?
src/rules/font-family-fallbacks.js
Outdated
const variableMap = new Map(); | ||
|
||
return { | ||
Rule(node) { |
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.
This logic to find variables looks more complicated than necessary and potentially buggy. Please see how it's done in no-invalid-properties
.
src/rules/font-family-fallbacks.js
Outdated
} | ||
}, | ||
|
||
Declaration(node) { |
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.
This function does a lot of traversing, which we try to avoid inside of rules. I think you can simplify this by using an ESQuery selector, like this:
[Rule > Block > Declaration[property="font-family"] > Value](node) {
src/rules/font-family-fallbacks.js
Outdated
} | ||
} | ||
|
||
node.value.children.forEach(child => { |
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.
See if we can avoid multiple lookups like this (a.b.c.d
) as the perf hit for continually looking up properties can add up over time.
src/rules/font-family-fallbacks.js
Outdated
|
||
// If the value is a variable function, we need to check the variable value | ||
if ( | ||
node.value.children[0].type === "Function" && |
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.
Similar here, node.value.children[0]
is used in a lot of places, so let's store that in a variable to avoid unnecessary lookups.
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.
Done!
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.
LGTM. Thanks!
Prerequisites checklist
What is the purpose of this pull request?
Previous description
: This is a draft PR to strategize the variable support for the rulefont-family-fallbacks
.Latest description
: Add a new rule calledfont-family-fallbacks
to enforce the use of fallback fonts and a generic font last.What changes did you make? (Give an overview)
Previous description
: Added a rule calledfont-family-fallbacks
. Right now, this PR just shows the strategy to enable variable support for this rule. So rule only contains the code to validate the use ofsans-serif
font in the variable (--my-font
) used infont-family
, and added two tests.Latest description
: Added a rule that will report following cases:Related Issues
fixes #153
Is there anything you'd like reviewers to focus on?