Skip to content

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

Merged
merged 6 commits into from
May 21, 2025
Merged

Conversation

Tanujkanti4441
Copy link
Contributor

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 called allow and related tests and docs and updated README.md file.

Related Issues

fixes #73

Is there anything you'd like reviewers to focus on?

@github-project-automation github-project-automation bot moved this to Needs Triage in Triage May 15, 2025
@Tanujkanti4441 Tanujkanti4441 marked this pull request as draft May 15, 2025 14:40
@Tanujkanti4441 Tanujkanti4441 marked this pull request as ready for review May 16, 2025 09:24
@nzakas nzakas requested a review from Copilot May 16, 2025 14:45
Copy link

@Copilot Copilot AI left a 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.

return {
Declaration(node) {
if (node.property === "font-size") {
if (node.value.type === "Value") {
Copy link
Preview

Copilot AI May 16, 2025

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.

Suggested change
if (node.value.type === "Value") {
if (node.value.type === "Value" && node.value.children.length > 0) {

Copilot uses AI. Check for mistakes.

Comment on lines 137 to 154
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;
}

Copy link
Preview

Copilot AI May 16, 2025

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.

Suggested change
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.

Copy link
Member

@nzakas nzakas left a 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
Copy link
Member

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/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

@nzakas nzakas moved this from Needs Triage to Implementing in Triage May 16, 2025
@lumirlumir lumirlumir added the accepted There is consensus among the team that this change meets the criteria for inclusion label May 17, 2025
nzakas
nzakas previously approved these changes May 19, 2025
Copy link
Member

@nzakas nzakas left a 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.

```

## 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
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
## Prior Art
## Further Reading

Comment on lines 9 to 11
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).
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

nzakas
nzakas previously approved these changes May 20, 2025
Copy link
Member

@nzakas nzakas left a 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.

@nzakas nzakas moved this from Implementing to Second Review Needed in Triage May 20, 2025

This rule enforces the use of relative units for font size.

### Options
Copy link
Contributor

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.

Suggested change
### Options
## Options

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:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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:


### Options

This rule accepts an option which is an object with following property:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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:


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:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- `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
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
```css
```css
/* eslint css/relative-font-units: ["error", { allow: ["rem"] }] */

Comment on lines 62 to 63
```css
a {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
```css
a {
```css
/* eslint css/relative-font-units: ["error", { allow: ["rem"] }] */
a {

Comment on lines 81 to 82
```css
a {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
```css
a {
```css
/* eslint css/relative-font-units: ["error", { allow: ["em", "%"] }] */
a {

Comment on lines +29 to +30
"a { font: 2rem Arial, sans-serif; }",
"a { font: 1.2rem/2 Arial, sans-serif; }",
Copy link
Contributor

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

Copy link
Member

@nzakas nzakas left a 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.

Copy link
Contributor

@snitin315 snitin315 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@snitin315 snitin315 merged commit ce256da into eslint:main May 21, 2025
22 checks passed
@github-project-automation github-project-automation bot moved this from Second Review Needed to Complete in Triage May 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion feature
Projects
Status: Complete
Development

Successfully merging this pull request may close these issues.

New Rule: Require specific font size units
4 participants