-
-
Notifications
You must be signed in to change notification settings - Fork 26
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
base: main
Are you sure you want to change the base?
Changes from all commits
837497e
c8f7753
1045d30
1d43501
cc22870
00a2a3e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,14 +16,23 @@ import { isSyntaxMatchError } from "../util.js"; | |
/** | ||
* @import { AtrulePlain } from "@eslint/css-tree" | ||
* @import { CSSRuleDefinition } from "../types.js" | ||
* @typedef {"unknownAtRule" | "invalidPrelude" | "unknownDescriptor" | "invalidDescriptor" | "invalidExtraPrelude" | "missingPrelude"} NoInvalidAtRulesMessageIds | ||
* @typedef {"unknownAtRule" | "invalidPrelude" | "unknownDescriptor" | "invalidDescriptor" | "invalidExtraPrelude" | "missingPrelude" | "invalidCharsetSyntax"} NoInvalidAtRulesMessageIds | ||
* @typedef {CSSRuleDefinition<{ RuleOptions: [], MessageIds: NoInvalidAtRulesMessageIds }>} NoInvalidAtRulesRuleDefinition | ||
*/ | ||
|
||
//----------------------------------------------------------------------------- | ||
// Helpers | ||
//----------------------------------------------------------------------------- | ||
|
||
/** | ||
* A valid `@charset` rule must: | ||
* - Enclose the encoding name in double quotes | ||
* - Include exactly one space character after `@charset` | ||
* - End immediately with a semicolon | ||
*/ | ||
const charsetPattern = /^@charset "[^"]+";$/u; | ||
const charsetEncodingPattern = /^['"]?([^"';]+)['"]?/u; | ||
|
||
/** | ||
* Extracts metadata from an error object. | ||
* @param {SyntaxError} error The error object to extract metadata from. | ||
|
@@ -57,6 +66,8 @@ export default { | |
meta: { | ||
type: "problem", | ||
|
||
fixable: "code", | ||
|
||
docs: { | ||
description: "Disallow invalid at-rules", | ||
recommended: true, | ||
|
@@ -74,15 +85,91 @@ export default { | |
invalidExtraPrelude: | ||
"At-rule '@{{name}}' should not contain a prelude.", | ||
missingPrelude: "At-rule '@{{name}}' should contain a prelude.", | ||
invalidCharsetSyntax: | ||
"Invalid @charset syntax. Expected '@charset \"{{encoding}}\";'.", | ||
}, | ||
}, | ||
|
||
create(context) { | ||
const { sourceCode } = context; | ||
const lexer = sourceCode.lexer; | ||
|
||
/** | ||
* Validates a `@charset` rule for correct syntax: | ||
* - Verifies the rule name is exactly "charset" (case-sensitive) | ||
* - Ensures the rule has a prelude | ||
* - Validates the prelude matches the expected pattern | ||
* @param {AtrulePlain} node The node representing the rule. | ||
*/ | ||
function validateCharsetRule(node) { | ||
const { name, prelude, loc } = node; | ||
|
||
const charsetNameLoc = { | ||
start: loc.start, | ||
end: { | ||
line: loc.start.line, | ||
column: loc.start.column + name.length + 1, | ||
}, | ||
}; | ||
|
||
if (name !== "charset") { | ||
context.report({ | ||
loc: charsetNameLoc, | ||
messageId: "unknownAtRule", | ||
data: { | ||
name, | ||
}, | ||
fix(fixer) { | ||
return fixer.replaceTextRange( | ||
[ | ||
loc.start.offset, | ||
loc.start.offset + name.length + 1, | ||
], | ||
"@charset", | ||
); | ||
}, | ||
}); | ||
return; | ||
} | ||
|
||
if (!prelude) { | ||
context.report({ | ||
loc: charsetNameLoc, | ||
messageId: "missingPrelude", | ||
data: { | ||
name, | ||
}, | ||
}); | ||
return; | ||
} | ||
|
||
const nodeText = sourceCode.getText(node); | ||
if (!charsetPattern.test(nodeText)) { | ||
const preludeText = sourceCode.getText(prelude); | ||
const encoding = | ||
preludeText.match(charsetEncodingPattern)?.[1] ?? "UTF-8"; | ||
Comment on lines
+149
to
+150
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When the encoding is missing, e.g., There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add this test case? empty string. |
||
|
||
context.report({ | ||
loc: prelude.loc, | ||
messageId: "invalidCharsetSyntax", | ||
data: { encoding }, | ||
fix(fixer) { | ||
return fixer.replaceText( | ||
node, | ||
`@charset "${encoding}";`, | ||
); | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
return { | ||
Atrule(node) { | ||
if (node.name.toLowerCase() === "charset") { | ||
validateCharsetRule(node); | ||
return; | ||
} | ||
|
||
// checks both name and prelude | ||
const { error } = lexer.matchAtrulePrelude( | ||
node.name, | ||
|
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.
Should be
end
?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.
Using
start
is correct because we only want to highlight the at-rule name (@charset
).Uh oh!
There was an error while loading. Please reload this page.
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.
Shouldn't we report the whole node?