-
-
Notifications
You must be signed in to change notification settings - Fork 19
feat: add relative-font-units
rule
#133
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
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.
Pull Request Overview
This pull request introduces a new ESLint rule, "relative-font-units", which enforces the use of relative units for font size. Key changes include:
- A new rule implementation in src/rules/relative-font-units.js with support for custom allowed units.
- Updates to the plugin's index file, enabling the new rule.
- Documentation updates in docs/rules/relative-font-units.md and README.md to describe and list the rule.
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
File | Description |
---|---|
src/rules/relative-font-units.js | New rule logic enforcing allowed relative font units. |
src/index.js | Updated plugin registry to include the new rule. |
docs/rules/relative-font-units.md | Documentation for the new rule including background and examples. |
README.md | Rule table updated to list the new relative-font-units rule. |
src/rules/relative-font-units.js
Outdated
return { | ||
Declaration(node) { | ||
if (node.property === "font-size") { | ||
if (node.value.type === "Value") { |
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.
Accessing the first element of node.value.children without checking if the array is non-empty may lead to runtime errors. Consider verifying that the children array has at least one element before accessing it.
if (node.value.type === "Value") { | |
if (node.value.type === "Value" && node.value.children.length > 0) { |
Copilot uses AI. Check for mistakes.
src/rules/relative-font-units.js
Outdated
if (!allowedFontUnits.includes("%") && percentageNode) { | ||
shouldReport = true; | ||
location = percentageNode.loc; | ||
} | ||
|
||
if (identifierNode) { | ||
shouldReport = true; | ||
location = identifierNode.loc; | ||
} | ||
|
||
if ( | ||
dimensionNode && | ||
!allowedFontUnits.includes(dimensionNode.unit) | ||
) { | ||
shouldReport = true; | ||
location = dimensionNode.loc; | ||
} | ||
|
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.
[nitpick] The condition chain in the 'font' property branch is somewhat complex. Consider refactoring the conditions to improve readability and maintainability.
if (!allowedFontUnits.includes("%") && percentageNode) { | |
shouldReport = true; | |
location = percentageNode.loc; | |
} | |
if (identifierNode) { | |
shouldReport = true; | |
location = identifierNode.loc; | |
} | |
if ( | |
dimensionNode && | |
!allowedFontUnits.includes(dimensionNode.unit) | |
) { | |
shouldReport = true; | |
location = dimensionNode.loc; | |
} | |
const conditions = [ | |
{ | |
check: !allowedFontUnits.includes("%") && percentageNode, | |
loc: percentageNode?.loc, | |
}, | |
{ | |
check: identifierNode, | |
loc: identifierNode?.loc, | |
}, | |
{ | |
check: dimensionNode && !allowedFontUnits.includes(dimensionNode.unit), | |
loc: dimensionNode?.loc, | |
}, | |
]; | |
for (const condition of conditions) { | |
if (condition.check) { | |
shouldReport = true; | |
location = condition.loc; | |
break; | |
} | |
} |
Copilot uses AI. Check for mistakes.
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 is looking really good. Can you add some tests for:
font-size: var(--foo);
font: Arial var(-foo);
To make sure these are considered valid?
} | ||
``` | ||
|
||
## When Not to Use It |
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.
Can you add this link in a Further Reading section?
https://www.joshwcomeau.com/css/surprising-truth-about-pixels-and-accessibility/
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. Just left a bit of cleanup in the docs.
docs/rules/relative-font-units.md
Outdated
``` | ||
|
||
## When Not to Use It | ||
|
||
If your project does not prioritize the use of relative font-size units—such as in cases requiring absolute sizing for print styles, pixel-perfect UI components, or embedded widgets—you may safely disable this rule without impacting your intended design precision. | ||
|
||
## Prior Art |
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.
## Prior Art | |
## Further Reading |
docs/rules/relative-font-units.md
Outdated
1. Keywords (e.g., small, medium, large). | ||
1. Length units (e.g., px, em, rem, pt). | ||
1. Percentages (%, relative to the parent element's font size). |
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.
1. Keywords (e.g., small, medium, large). | |
1. Length units (e.g., px, em, rem, pt). | |
1. Percentages (%, relative to the parent element's font size). | |
1. Keywords (e.g., `small`, `medium`, `large`). | |
1. Length units (e.g., `px`, `em`, `rem`, `pt`). | |
1. Percentages (`%`, relative to the parent element's font size). |
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! Would like another review before merging.
docs/rules/relative-font-units.md
Outdated
|
||
This rule enforces the use of relative units for font size. | ||
|
||
### Options |
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.
I think it should be h2.
### Options | |
## Options |
docs/rules/relative-font-units.md
Outdated
1. Length units (e.g., `px`, `em`, `rem`, `pt`). | ||
1. Percentages (`%`, relative to the parent element's font size). | ||
|
||
Generally, relative unit such as `rem` or `em` are preferred over absolute ones like `px`, `pt` because of following reasons: |
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.
Generally, relative unit such as `rem` or `em` are preferred over absolute ones like `px`, `pt` because of following reasons: | |
Generally, relative units such as `rem` or `em` are preferred over absolute ones like `px`, `pt` because of the following reasons: |
docs/rules/relative-font-units.md
Outdated
|
||
### Options | ||
|
||
This rule accepts an option which is an object with following property: |
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 rule accepts an option which is an object with following property: | |
This rule accepts an option which is an object with the following property: |
docs/rules/relative-font-units.md
Outdated
|
||
This rule accepts an option which is an object with following property: | ||
|
||
- `allow` (default: `["rem"]`) - Specify an array of relative units that are allowed to be used. You can use following units: |
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.
- `allow` (default: `["rem"]`) - Specify an array of relative units that are allowed to be used. You can use following units: | |
- `allow` (default: `["rem"]`) - Specify an array of relative units that are allowed to be used. You can use the following units: |
|
||
Example of **incorrect** code for default `{ allow: ["rem"] }` option: | ||
|
||
```css |
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.
```css | |
```css | |
/* eslint css/relative-font-units: ["error", { allow: ["rem"] }] */ | |
docs/rules/relative-font-units.md
Outdated
```css | ||
a { |
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.
```css | |
a { | |
```css | |
/* eslint css/relative-font-units: ["error", { allow: ["rem"] }] */ | |
a { |
docs/rules/relative-font-units.md
Outdated
```css | ||
a { |
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.
```css | |
a { | |
```css | |
/* eslint css/relative-font-units: ["error", { allow: ["em", "%"] }] */ | |
a { |
"a { font: 2rem Arial, sans-serif; }", | ||
"a { font: 1.2rem/2 Arial, sans-serif; }", |
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.
Let's add test cases for all possible syntax for the font
shorthand. https://developer.mozilla.org/en-US/docs/Web/CSS/font#syntax
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.
Re-approving. Waiting for @snitin315 to review before merging.
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?
Add a new rule called
relative-font-units
that will enforce the relative units for font size.What changes did you make? (Give an overview)
Added the rule
relative-font-units
with an option calledallow
and related tests and docs and updated README.md file.Related Issues
fixes #73
Is there anything you'd like reviewers to focus on?