@uinaf/oxlint-plugin-no-comments 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -1
- package/dist/index.js +216 -44
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -44,12 +44,17 @@ lint: {
|
|
|
44
44
|
| --------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
45
45
|
| Tool directives | `eslint-disable*`, `eslint-enable*`, `oxlint-disable*`, `oxlint-enable*`, `@ts-expect-error`, `@ts-ignore`, `@ts-nocheck`, `@ts-check`, `/// <reference …>`, `prettier-ignore`, `istanbul`/`c8`/`v8 ignore`, `node:coverage`, `@__PURE__`, `@__NO_SIDE_EFFECTS__`, `@vite-ignore`, `@vitest-environment` |
|
|
46
46
|
| License headers | `/*! … */`, `@license`, or `SPDX-*` before the first statement |
|
|
47
|
-
| API docs | The last JSDoc block before
|
|
47
|
+
| API docs | The last JSDoc block before a declaration TypeScript exports, or before a public member of an exported class, interface, type, or enum. This counts implicit exports in `.d.ts` files, `declare module`, and `declare global` |
|
|
48
48
|
| Lint-required | `SAFETY:` comments that anti-slop's `require-safety-comment-for-type-assertion` accepts for a non-`const` type assertion |
|
|
49
49
|
|
|
50
50
|
Everything else is reported, including `TODO`, JSX comments, JSDoc on
|
|
51
51
|
internal code, and `SAFETY:` comments with no assertion to justify.
|
|
52
52
|
|
|
53
|
+
`oxlint --fix` deletes reported comments. The fix keeps tokens apart and keeps
|
|
54
|
+
line breaks that automatic semicolon insertion depends on. It removes a JSX
|
|
55
|
+
`{/* … */}` container as a whole, and it does nothing when the container sits
|
|
56
|
+
between two runs of JSX text.
|
|
57
|
+
|
|
53
58
|
## Options
|
|
54
59
|
|
|
55
60
|
```json
|
package/dist/index.js
CHANGED
|
@@ -391,7 +391,8 @@ function buildMatchers(option) {
|
|
|
391
391
|
};
|
|
392
392
|
}
|
|
393
393
|
function commentText(comment) {
|
|
394
|
-
|
|
394
|
+
if (comment.type !== "Block") return comment.value.trim();
|
|
395
|
+
return comment.value.split("\n").map((line) => line.replace(/^\s*\**\s*/u, "")).join("\n").trim();
|
|
395
396
|
}
|
|
396
397
|
function isJSDoc(comment) {
|
|
397
398
|
return comment.type === "Block" && comment.value.startsWith("*");
|
|
@@ -419,29 +420,64 @@ function boundNames(pattern) {
|
|
|
419
420
|
}
|
|
420
421
|
function declaredNames(node) {
|
|
421
422
|
if (node.type === "VariableDeclaration") return node.declarations.flatMap((declarator) => boundNames(declarator.id));
|
|
422
|
-
if ("id" in node
|
|
423
|
-
|
|
423
|
+
if (!("id" in node) || node.id === null) return [];
|
|
424
|
+
let id = node.id;
|
|
425
|
+
while (id.type === "TSQualifiedName") id = id.left;
|
|
426
|
+
return id.type === "Identifier" ? [id.name] : [];
|
|
424
427
|
}
|
|
425
|
-
function
|
|
426
|
-
|
|
428
|
+
function localExportNames(statements) {
|
|
429
|
+
const names = /* @__PURE__ */ new Set();
|
|
430
|
+
for (const statement of statements) {
|
|
431
|
+
if (statement.type === "ExportNamedDeclaration" && statement.source === null) {
|
|
432
|
+
for (const specifier of statement.specifiers) if (specifier.local.type === "Identifier") names.add(specifier.local.name);
|
|
433
|
+
}
|
|
434
|
+
const target = statement.type === "ExportDefaultDeclaration" ? statement.declaration : statement.type === "TSExportAssignment" ? statement.expression : null;
|
|
435
|
+
const root = target === null ? null : rootIdentifier(target);
|
|
436
|
+
if (root !== null) names.add(root);
|
|
437
|
+
}
|
|
438
|
+
return names;
|
|
427
439
|
}
|
|
428
|
-
function
|
|
429
|
-
if (declarationFile) return true;
|
|
440
|
+
function rootIdentifier(node) {
|
|
430
441
|
let current = node;
|
|
431
|
-
while (current
|
|
432
|
-
|
|
433
|
-
|
|
442
|
+
while (current.type === "MemberExpression" || current.type === "TSQualifiedName") current = current.type === "MemberExpression" ? current.object : current.left;
|
|
443
|
+
return current.type === "Identifier" ? current.name : null;
|
|
444
|
+
}
|
|
445
|
+
function hasExportDeclarations(statements) {
|
|
446
|
+
return statements.some((statement) => statement.type === "ExportNamedDeclaration" && statement.declaration === null || statement.type === "ExportAllDeclaration" || statement.type === "TSExportAssignment" || statement.type === "ExportDefaultDeclaration" && !declarationKinds.has(statement.declaration.type));
|
|
447
|
+
}
|
|
448
|
+
function collectApi(statements, visible, ambient, api) {
|
|
449
|
+
const listed = localExportNames(statements);
|
|
450
|
+
const exportContext = ambient && !hasExportDeclarations(statements);
|
|
451
|
+
for (const statement of statements) {
|
|
452
|
+
let declaration = null;
|
|
453
|
+
let explicit = false;
|
|
454
|
+
let implicit = false;
|
|
455
|
+
if (statement.type === "ExportNamedDeclaration") {
|
|
456
|
+
declaration = statement.declaration;
|
|
457
|
+
explicit = declaration !== null;
|
|
458
|
+
} else if (statement.type === "ExportDefaultDeclaration" || statement.type === "TSExportAssignment") explicit = true;
|
|
459
|
+
else if (declarationKinds.has(statement.type)) {
|
|
460
|
+
declaration = statement;
|
|
461
|
+
implicit = exportContext || declaredNames(statement).some((name) => listed.has(name));
|
|
462
|
+
}
|
|
463
|
+
const exported = explicit || visible && implicit;
|
|
464
|
+
if (exported) api.add(statement);
|
|
465
|
+
if (declaration?.type !== "TSModuleDeclaration" || declaration.body?.type !== "TSModuleBlock") continue;
|
|
466
|
+
const external = declaration.global || declaration.id.type === "Literal";
|
|
467
|
+
collectApi(declaration.body.body, external || visible && exported, ambient || declaration.declare, api);
|
|
434
468
|
}
|
|
435
|
-
return false;
|
|
436
469
|
}
|
|
437
|
-
function
|
|
470
|
+
function lastJSDocBefore(sourceCode, node) {
|
|
471
|
+
return sourceCode.getCommentsBefore(node).findLast(isJSDoc);
|
|
472
|
+
}
|
|
473
|
+
function isPublicApi(member, api) {
|
|
438
474
|
if (isPrivateMember(member)) return false;
|
|
439
475
|
let previous = member;
|
|
440
476
|
let current = member.parent;
|
|
441
477
|
while (current !== null) {
|
|
442
478
|
if (api.has(current)) return true;
|
|
443
479
|
if (scopeBoundaryKinds.has(current.type) || isPrivateMember(current)) return false;
|
|
444
|
-
if (current.type === "TSModuleBlock"
|
|
480
|
+
if (current.type === "TSModuleBlock") return false;
|
|
445
481
|
if (current.type === "ArrowFunctionExpression" && current.body === previous) return false;
|
|
446
482
|
previous = current;
|
|
447
483
|
current = current.parent;
|
|
@@ -465,6 +501,152 @@ function safetyCommentsFor(sourceCode, assertion, pattern) {
|
|
|
465
501
|
current = current.parent;
|
|
466
502
|
}
|
|
467
503
|
}
|
|
504
|
+
const lineBreakPattern = /[\n\r\u2028\u2029]/u;
|
|
505
|
+
const blankPattern = /^\s*$/u;
|
|
506
|
+
const isLineBreak = (char) => char !== void 0 && lineBreakPattern.test(char);
|
|
507
|
+
function isInlineSpace(char) {
|
|
508
|
+
return char !== void 0 && /\s/u.test(char) && !isLineBreak(char);
|
|
509
|
+
}
|
|
510
|
+
function nextLineStart(text, end) {
|
|
511
|
+
if (text.startsWith("\r\n", end)) return end + 2;
|
|
512
|
+
return Math.min(end + 1, text.length);
|
|
513
|
+
}
|
|
514
|
+
function renderedJSXText(value) {
|
|
515
|
+
const lines = value.split(/\r\n|[\n\r]/u);
|
|
516
|
+
return lines.map((line, index) => {
|
|
517
|
+
const start = index > 0 ? line.replace(/^[ \t]+/u, "") : line;
|
|
518
|
+
return index < lines.length - 1 ? start.replace(/[ \t]+$/u, "") : start;
|
|
519
|
+
}).filter((line) => line.length > 0).join(" ");
|
|
520
|
+
}
|
|
521
|
+
function renderChildren(text, from, to, marks) {
|
|
522
|
+
let output = "";
|
|
523
|
+
let pending = "";
|
|
524
|
+
let cursor = from;
|
|
525
|
+
for (const mark of marks.toSorted((a, b) => a.start - b.start)) {
|
|
526
|
+
pending += text.slice(cursor, Math.max(cursor, mark.start)) + mark.text;
|
|
527
|
+
cursor = Math.max(cursor, mark.end);
|
|
528
|
+
if (mark.render === null) continue;
|
|
529
|
+
output += renderedJSXText(pending) + mark.render;
|
|
530
|
+
pending = "";
|
|
531
|
+
}
|
|
532
|
+
return output + renderedJSXText(pending + text.slice(cursor, to));
|
|
533
|
+
}
|
|
534
|
+
function keepsRenderedText(text, parent, planned) {
|
|
535
|
+
if (parent.type !== "JSXElement" && parent.type !== "JSXFragment") return true;
|
|
536
|
+
let from = parent.children[0]?.start ?? parent.start;
|
|
537
|
+
let to = parent.children.at(-1)?.end ?? parent.end;
|
|
538
|
+
for (const edit of planned.values()) {
|
|
539
|
+
from = Math.min(from, edit.start);
|
|
540
|
+
to = Math.max(to, edit.end);
|
|
541
|
+
}
|
|
542
|
+
const before = [];
|
|
543
|
+
const after = [];
|
|
544
|
+
for (const child of parent.children) {
|
|
545
|
+
if (child.type === "JSXText") continue;
|
|
546
|
+
const render = child.type === "JSXExpressionContainer" ? "" : "\0";
|
|
547
|
+
const separator = {
|
|
548
|
+
start: child.start,
|
|
549
|
+
end: child.end,
|
|
550
|
+
text: "",
|
|
551
|
+
render
|
|
552
|
+
};
|
|
553
|
+
before.push(separator);
|
|
554
|
+
const edit = planned.get(child);
|
|
555
|
+
after.push(edit === void 0 ? separator : {
|
|
556
|
+
...edit,
|
|
557
|
+
render: null
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
return renderChildren(text, from, to, before) === renderChildren(text, from, to, after);
|
|
561
|
+
}
|
|
562
|
+
function removal(text, start, end, jsx) {
|
|
563
|
+
let from = start;
|
|
564
|
+
while (isInlineSpace(text[from - 1])) from--;
|
|
565
|
+
let to = end;
|
|
566
|
+
while (isInlineSpace(text[to])) to++;
|
|
567
|
+
const blankBefore = from === 0 || isLineBreak(text[from - 1]);
|
|
568
|
+
const blankAfter = to === text.length || isLineBreak(text[to]);
|
|
569
|
+
if (blankBefore && blankAfter) return {
|
|
570
|
+
start: from,
|
|
571
|
+
end: nextLineStart(text, to),
|
|
572
|
+
text: ""
|
|
573
|
+
};
|
|
574
|
+
if (jsx) return {
|
|
575
|
+
start,
|
|
576
|
+
end,
|
|
577
|
+
text: ""
|
|
578
|
+
};
|
|
579
|
+
if (blankAfter) return {
|
|
580
|
+
start: from,
|
|
581
|
+
end,
|
|
582
|
+
text: ""
|
|
583
|
+
};
|
|
584
|
+
if (blankBefore) return {
|
|
585
|
+
start,
|
|
586
|
+
end: to,
|
|
587
|
+
text: ""
|
|
588
|
+
};
|
|
589
|
+
const separator = lineBreakPattern.test(text.slice(from, end)) ? "\n" : isInlineSpace(text[end]) ? "" : " ";
|
|
590
|
+
return {
|
|
591
|
+
start: from,
|
|
592
|
+
end,
|
|
593
|
+
text: separator
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
function commentRuns(text, reported) {
|
|
597
|
+
const runs = [];
|
|
598
|
+
for (const comment of reported) {
|
|
599
|
+
const run = runs.at(-1);
|
|
600
|
+
const previous = run?.at(-1);
|
|
601
|
+
if (run !== void 0 && previous !== void 0) {
|
|
602
|
+
if (blankPattern.test(text.slice(previous.end, comment.start))) {
|
|
603
|
+
run.push(comment);
|
|
604
|
+
continue;
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
runs.push([comment]);
|
|
608
|
+
}
|
|
609
|
+
return runs;
|
|
610
|
+
}
|
|
611
|
+
function planEdits(text, comments, reported, containers) {
|
|
612
|
+
const reportedSet = new Set(reported);
|
|
613
|
+
const sorted = containers.toSorted((a, b) => a.start - b.start);
|
|
614
|
+
const keepsComment = /* @__PURE__ */ new Set();
|
|
615
|
+
let index = 0;
|
|
616
|
+
for (const comment of comments) {
|
|
617
|
+
let container = sorted[index];
|
|
618
|
+
while (container !== void 0 && container.end <= comment.start) container = sorted[++index];
|
|
619
|
+
if (container !== void 0 && container.start < comment.start && !reportedSet.has(comment)) keepsComment.add(container);
|
|
620
|
+
}
|
|
621
|
+
const edits = /* @__PURE__ */ new Map();
|
|
622
|
+
const byParent = /* @__PURE__ */ new Map();
|
|
623
|
+
const owners = /* @__PURE__ */ new Map();
|
|
624
|
+
index = 0;
|
|
625
|
+
for (const run of commentRuns(text, reported)) {
|
|
626
|
+
const first = run[0];
|
|
627
|
+
const last = run.at(-1);
|
|
628
|
+
if (first === void 0 || last === void 0) continue;
|
|
629
|
+
let candidate = sorted[index];
|
|
630
|
+
while (candidate !== void 0 && candidate.end <= first.start) candidate = sorted[++index];
|
|
631
|
+
const container = candidate !== void 0 && candidate.start < first.start && last.end < candidate.end ? candidate : void 0;
|
|
632
|
+
if (container !== void 0 && !keepsComment.has(container)) {
|
|
633
|
+
const planned = byParent.get(container.parent) ?? /* @__PURE__ */ new Map();
|
|
634
|
+
planned.set(container, removal(text, container.start, container.end, true));
|
|
635
|
+
byParent.set(container.parent, planned);
|
|
636
|
+
owners.set(container, first);
|
|
637
|
+
continue;
|
|
638
|
+
}
|
|
639
|
+
edits.set(first, removal(text, first.start, last.end, false));
|
|
640
|
+
}
|
|
641
|
+
for (const [parent, planned] of byParent) {
|
|
642
|
+
if (!keepsRenderedText(text, parent, planned)) continue;
|
|
643
|
+
for (const [container, edit] of planned) {
|
|
644
|
+
const owner = owners.get(container);
|
|
645
|
+
if (owner !== void 0) edits.set(owner, edit);
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
return edits;
|
|
649
|
+
}
|
|
468
650
|
// src/index.ts
|
|
469
651
|
/** Oxlint plugin that keeps intent in names, types, tests, and commit messages instead of comments. */
|
|
470
652
|
const plugin = eslintCompatPlugin({
|
|
@@ -472,6 +654,7 @@ const plugin = eslintCompatPlugin({
|
|
|
472
654
|
rules: { "no-comments": defineRule({
|
|
473
655
|
meta: {
|
|
474
656
|
type: "suggestion",
|
|
657
|
+
fixable: "code",
|
|
475
658
|
docs: { description: "Disallow comments other than tool directives, license headers, JSDoc on exported API, and SAFETY justifications for type assertions." },
|
|
476
659
|
messages: { comment: "Remove this comment. Names, types, and tests carry intent; the commit message carries the why." },
|
|
477
660
|
schema: [{
|
|
@@ -504,10 +687,9 @@ const plugin = eslintCompatPlugin({
|
|
|
504
687
|
},
|
|
505
688
|
createOnce(context) {
|
|
506
689
|
const matchersByOption = /* @__PURE__ */ new Map();
|
|
507
|
-
const apiNodes = /* @__PURE__ */ new Set();
|
|
508
690
|
const members = [];
|
|
509
691
|
const assertions = [];
|
|
510
|
-
const
|
|
692
|
+
const emptyContainers = [];
|
|
511
693
|
const addMember = (node) => {
|
|
512
694
|
members.push(node);
|
|
513
695
|
};
|
|
@@ -516,26 +698,12 @@ const plugin = eslintCompatPlugin({
|
|
|
516
698
|
};
|
|
517
699
|
return {
|
|
518
700
|
before() {
|
|
519
|
-
apiNodes.clear();
|
|
520
701
|
members.length = 0;
|
|
521
702
|
assertions.length = 0;
|
|
522
|
-
|
|
523
|
-
},
|
|
524
|
-
ExportNamedDeclaration(node) {
|
|
525
|
-
if (node.declaration !== null) apiNodes.add(node);
|
|
526
|
-
if (node.source !== null) return;
|
|
527
|
-
for (const specifier of node.specifiers) if (specifier.local.type === "Identifier") localExports.add(specifier.local.name);
|
|
703
|
+
emptyContainers.length = 0;
|
|
528
704
|
},
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
if (node.declaration.type === "Identifier") localExports.add(node.declaration.name);
|
|
532
|
-
},
|
|
533
|
-
TSExportAssignment(node) {
|
|
534
|
-
apiNodes.add(node);
|
|
535
|
-
if (node.expression.type === "Identifier") localExports.add(node.expression.name);
|
|
536
|
-
},
|
|
537
|
-
TSModuleBlock(node) {
|
|
538
|
-
for (const statement of node.body) if (declarationKinds.has(statement.type)) members.push(statement);
|
|
705
|
+
JSXExpressionContainer(node) {
|
|
706
|
+
if (node.expression.type === "JSXEmptyExpression") emptyContainers.push(node);
|
|
539
707
|
},
|
|
540
708
|
AccessorProperty: addMember,
|
|
541
709
|
MethodDefinition: addMember,
|
|
@@ -558,32 +726,36 @@ const plugin = eslintCompatPlugin({
|
|
|
558
726
|
const optionKey = JSON.stringify(option ?? null);
|
|
559
727
|
const matchers = matchersByOption.get(optionKey) ?? buildMatchers(option);
|
|
560
728
|
matchersByOption.set(optionKey, matchers);
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
}
|
|
729
|
+
const apiNodes = /* @__PURE__ */ new Set();
|
|
730
|
+
const declarationFile = declarationFilePattern.test(context.filename);
|
|
731
|
+
collectApi(program.body, true, declarationFile, apiNodes);
|
|
565
732
|
const allowed = /* @__PURE__ */ new Set();
|
|
566
733
|
for (const node of apiNodes) {
|
|
567
734
|
const doc = lastJSDocBefore(sourceCode, node);
|
|
568
735
|
if (doc !== void 0) allowed.add(doc.start);
|
|
569
736
|
}
|
|
570
|
-
const declarationFile = declarationFilePattern.test(context.filename);
|
|
571
737
|
for (const member of members) {
|
|
572
|
-
if (!isPublicApi(member, apiNodes
|
|
738
|
+
if (!isPublicApi(member, apiNodes)) continue;
|
|
573
739
|
const doc = lastJSDocBefore(sourceCode, member);
|
|
574
740
|
if (doc !== void 0) allowed.add(doc.start);
|
|
575
741
|
}
|
|
576
742
|
for (const assertion of assertions) for (const comment of safetyCommentsFor(sourceCode, assertion, matchers.safety)) allowed.add(comment.start);
|
|
577
743
|
const headerEnd = program.body[0]?.start ?? program.end;
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
if (
|
|
744
|
+
const comments = sourceCode.getAllComments();
|
|
745
|
+
const reported = comments.filter((comment) => {
|
|
746
|
+
if (comment.type === "Shebang" || allowed.has(comment.start)) return false;
|
|
747
|
+
if (isLicenseHeader(comment, headerEnd)) return false;
|
|
581
748
|
const text = commentText(comment);
|
|
582
|
-
if (directivePatterns.some((pattern) => pattern.test(text)))
|
|
583
|
-
|
|
749
|
+
if (directivePatterns.some((pattern) => pattern.test(text))) return false;
|
|
750
|
+
return !matchers.allow.some((pattern) => pattern.test(text));
|
|
751
|
+
});
|
|
752
|
+
const edits = planEdits(sourceCode.text, comments, reported, emptyContainers);
|
|
753
|
+
for (const comment of reported) {
|
|
754
|
+
const edit = edits.get(comment);
|
|
584
755
|
context.report({
|
|
585
756
|
node: comment,
|
|
586
|
-
messageId: "comment"
|
|
757
|
+
messageId: "comment",
|
|
758
|
+
...edit === void 0 ? {} : { fix: (fixer) => fixer.replaceTextRange([edit.start, edit.end], edit.text) }
|
|
587
759
|
});
|
|
588
760
|
}
|
|
589
761
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uinaf/oxlint-plugin-no-comments",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Oxlint rule that bans code comments, except tool directives, license headers, API docs, and SAFETY justifications",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"comments",
|
|
@@ -52,5 +52,5 @@
|
|
|
52
52
|
"engines": {
|
|
53
53
|
"node": ">=20.19.0"
|
|
54
54
|
},
|
|
55
|
-
"packageManager": "pnpm@12.
|
|
55
|
+
"packageManager": "pnpm@12.18.0"
|
|
56
56
|
}
|