Skip to content

fix: enforce strict syntax for @charset in no-invalid-at-rules #192

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

thecalamiity
Copy link
Contributor

@thecalamiity thecalamiity commented Jul 7, 2025

Prerequisites checklist

What is the purpose of this pull request?

To enforce strict syntax requirements for the @charset rule. This ensures that only valid @charset declarations are accepted.

What changes did you make? (Give an overview)

  • Updated the no-invalid-at-rules rule to strictly validate @charset syntax. Now enforces:
    • The encoding must be enclosed in double quotes.
    • There must be exactly one space after @charset.
    • The rule must be immediately terminated with a semicolon.
  • Added tests to cover valid and invalid @charset usages.
  • Updated the documentation to note the strict requirements for @charset.

Related Issues

Fixes #185

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

@eslint-github-bot eslint-github-bot bot added the bug Something isn't working label Jul 7, 2025
@github-project-automation github-project-automation bot moved this to Needs Triage in Triage Jul 7, 2025
@nzakas nzakas requested a review from Copilot July 8, 2025 15:13
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 PR enhances the no-invalid-at-rules rule to strictly validate @charset declarations, ensuring they use double quotes, exactly one space, and terminate with a semicolon.

  • Introduced charsetPattern and validateCharsetRule to enforce syntax in the rule implementation
  • Added test cases covering valid and invalid @charset variations
  • Updated documentation to detail the strict requirements and examples

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
tests/rules/no-invalid-at-rules.test.js Added valid/invalid test cases for @charset
src/rules/no-invalid-at-rules.js Implemented validateCharsetRule and regex check
docs/rules/no-invalid-at-rules.md Added note and examples for strict @charset
Comments suppressed due to low confidence (1)

docs/rules/no-invalid-at-rules.md:75

  • [nitpick] Describing @charset as "not an at-rule" is misleading; consider rephrasing to clarify that it is a special at-rule requiring placement as the very first statement in a stylesheet.
Note on `@charset`: Although it begins with an `@` symbol, it is not an at-rule. It is a specific byte sequence of the following form:

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.

Overall, this looks really good. What do you think about adding autofix to this rule? It seems like it should possible to just reformat the @charset so that it's correct.

@nzakas nzakas moved this from Needs Triage to Implementing in Triage Jul 8, 2025
@lumirlumir lumirlumir added the accepted There is consensus among the team that this change meets the criteria for inclusion label Jul 10, 2025
@lumirlumir
Copy link
Member

ping @thecalamiity

Comment on lines 365 to 382
{
code: "@charset 'UTF-8';",
output: '@charset "UTF-8";',
errors: [
{
messageId: "invalidPrelude",
data: {
name: "charset",
prelude: "'UTF-8'",
expected: "<string>",
},
line: 1,
column: 10,
endLine: 1,
endColumn: 17,
},
],
},
Copy link
Contributor

Choose a reason for hiding this comment

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

In the case of a syntax error in @charset, the error message invalidPrelude seems incorrect, such as in this test, the message would be:

Invalid prelude ''UTF-8'' found for at-rule '@charset'. Expected '<string>'.

Here it says, <string> is expected to be used, which is already used here, but with wrong quotes.

And in the case of @charset "UTF-8", where a semicolon is missing, it has the same error message.

What do you think about adding an error message only to report syntax errors?

Copy link
Contributor Author

@thecalamiity thecalamiity Aug 7, 2025

Choose a reason for hiding this comment

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

Are you suggesting that instead of reporting invalidPrelude, we should report a single, more general error for all these cases? Something like:

invalidCharsetSyntax: "Invalid @charset syntax. Expected: @charset \"<encoding-name>\"; (double quotes, exactly one space after @charset, and a semicolon at the end)."

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, but i feel this error message seems a bit descriptive; instead, we can just suggest the correct syntax, like for -
@charset "UTF-8" message can be something like this:

Invalid @charset syntax. Expected '@charset "UTF-8";'

Please feel free to choose the words that you find more accurate here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just updated the @charset error message - PTAL when you can.

Comment on lines +109 to +111
end: {
line: loc.start.line,
column: loc.start.column + name.length + 1,
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be end?

Suggested change
end: {
line: loc.start.line,
column: loc.start.column + name.length + 1,
end: {
line: loc.end.line,
column: loc.end.column + name.length + 1,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using start is correct because we only want to highlight the at-rule name (@charset).

Copy link
Contributor

@snitin315 snitin315 Aug 10, 2025

Choose a reason for hiding this comment

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

Shouldn't we report the whole node?

@charse "utf-8"
^^^^^^^^^^^^^^^

@charset "";
^^^^^^^^^^^^

Comment on lines +149 to +150
const encoding =
preludeText.match(charsetEncodingPattern)?.[1] ?? "UTF-8";
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need ?? "UTF-8"? In which case will we reach the right operand?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When the encoding is missing, e.g., @charset "";

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add this test case? empty string.

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 bug Something isn't working
Projects
Status: Implementing
Development

Successfully merging this pull request may close these issues.

Rule Change: Enforce strict syntax for @charset
5 participants