chore: update lint rules for comments

This commit is contained in:
2026-10-12 12:55:31 +05:00
parent 66b23ec3bf
commit 0e6a8464af
10 changed files with 317 additions and 48 deletions
@@ -4,6 +4,11 @@
// checked: localized dictionary values legitimately contain other scripts.
// Details: web/eslint-plugins/README.md.
import {
getStyleNodeLoc,
getStyleRoot,
} from "../design-tokens/style-context.js";
export default {
meta: {
type: "suggestion",
@@ -33,30 +38,56 @@ export default {
},
create(context) {
const allowed = new Set(context.options[0]?.allowChars ?? []);
const check = (text, loc) => {
const offenders = new Set();
for (const match of text.matchAll(
// eslint-disable-next-line no-control-regex -- the printable-ASCII whitelist needs explicit control escapes
/[^\x09\x0A\x0D\x20-\x7E]/g,
)) {
if (!allowed.has(match[0])) offenders.add(match[0]);
}
if (offenders.size === 0) return;
context.report({
loc,
messageId: "nonAscii",
data: {
chars: [...offenders]
.map(
(c) =>
`'${c}' (U+${c.codePointAt(0).toString(16).toUpperCase().padStart(4, "0")})`,
)
.join(", "),
},
});
};
// `<style>` is a postcss AST, not a token stream, so ESLint's comment
// tokens never include those comments; they come from the shared helper
// the design-tokens rules use (null outside Svelte components).
const styleRoot = getStyleRoot(context);
const styleNodeLoc = getStyleNodeLoc(context);
const htmlComments = [];
return {
Program() {
// svelte-eslint-parser keeps `<!-- -->` template comments out of the
// token stream (they are AST nodes, not comment tokens), so
// getAllComments() below never sees them. Collected during traversal
// to catch template comments nested inside markup elements too.
SvelteHTMLComment(node) {
htmlComments.push(node);
},
"Program:exit"() {
for (const comment of context.sourceCode.getAllComments()) {
const offenders = new Set();
for (const match of comment.value.matchAll(
// eslint-disable-next-line no-control-regex -- the printable-ASCII whitelist needs explicit control escapes
/[^\x09\x0A\x0D\x20-\x7E]/g,
)) {
if (!allowed.has(match[0])) offenders.add(match[0]);
}
if (offenders.size === 0) continue;
context.report({
loc: comment.loc,
messageId: "nonAscii",
data: {
chars: [...offenders]
.map(
(c) =>
`'${c}' (U+${c.codePointAt(0).toString(16).toUpperCase().padStart(4, "0")})`,
)
.join(", "),
},
});
check(comment.value, comment.loc);
}
for (const node of htmlComments) {
check(node.value, node.loc);
}
if (!styleRoot || !styleNodeLoc) return;
styleRoot.walkComments((comment) => {
check(comment.text ?? "", styleNodeLoc(comment));
});
},
};
},