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
@@ -1,9 +1,14 @@
// Rule: COMMENT FORMAT: bounded blocks and bounded density.
// maxBlockLines caps one comment block; maxRatio caps the share of comment
// lines in files with minLines+ non-blank lines. Warn-only tripwires:
// excess means refactor the code, not squeeze the comment.
// lines in files with minLines+ non-blank lines. In .svelte, <script>,
// template <!-- --> and <style> CSS (postcss AST) count alike.
// Details: web/eslint-plugins/README.md.
import {
getStyleNodeLoc,
getStyleRoot,
} from "../design-tokens/style-context.js";
function lineSpan(comment) {
return comment.loc.end.line - comment.loc.start.line + 1;
}
@@ -40,10 +45,42 @@ export default {
const maxRatio = context.options[0]?.maxRatio ?? 0.15;
const minLines = context.options[0]?.minLines ?? 0;
const htmlComments = [];
// `<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);
return {
Program(node) {
// 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"(node) {
const sourceCode = context.sourceCode;
const comments = sourceCode.getAllComments();
// `type: "Block"` keeps a CSS comment from merging with the run
// above it: only adjacent `//` lines continue one block.
const styleComments = [];
if (styleRoot && styleNodeLoc) {
styleRoot.walkComments((comment) => {
const loc = styleNodeLoc(comment);
if (loc.start) styleComments.push({ loc, type: "Block" });
});
}
const comments = [
...sourceCode.getAllComments(),
...htmlComments,
...styleComments,
].sort(
(a, b) =>
a.loc.start.line - b.loc.start.line ||
a.loc.start.column - b.loc.start.column,
);
if (comments.length === 0) return;
const commentLines = new Set();
@@ -67,7 +104,8 @@ export default {
commentLines.add(l);
}
if (i === 0) continue;
// Adjacent `//` lines form one block; anything else starts a new one.
// Adjacent `//` lines form one block; HTML template comments and
// everything else start a new one (their own span is the block).
if (
comment.type === "Line" &&
comments[i - 1].type === "Line" &&
@@ -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));
});
},
};
},