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 4 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:

Comment on lines +139 to +145
context.report({
loc: prelude.loc,
messageId: "invalidPrelude",
data: {
name,
prelude: preludeText,
expected: "<string>",
Copy link
Preview

Copilot AI Jul 8, 2025

Choose a reason for hiding this comment

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

[nitpick] Currently missing semicolon errors are reported as invalidPrelude; consider introducing a distinct messageId (e.g., missingSemicolon) to clearly differentiate terminator errors from prelude syntax issues.

Suggested change
context.report({
loc: prelude.loc,
messageId: "invalidPrelude",
data: {
name,
prelude: preludeText,
expected: "<string>",
const isMissingSemicolon = !nodeText.endsWith(";");
context.report({
loc: prelude.loc,
messageId: isMissingSemicolon ? "missingSemicolon" : "invalidPrelude",
data: {
name,
prelude: preludeText,
expected: isMissingSemicolon ? "; at the end" : "<string>",

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.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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
2 participants