@uinaf/oxlint-plugin-no-comments 1.0.1 → 1.1.1

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.
Files changed (3) hide show
  1. package/README.md +8 -1
  2. package/dist/index.js +249 -43
  3. package/package.json +5 -4
package/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ ![oxlint-plugin-no-comments — Oxlint rule that bans code comments.](https://uinaf.dev/og/banner/oxlint-plugin-no-comments.png)
2
+
1
3
  # @uinaf/oxlint-plugin-no-comments
2
4
 
3
5
  An Oxlint rule that bans code comments. Names, types, and tests carry intent;
@@ -44,12 +46,17 @@ lint: {
44
46
  | --------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
45
47
  | 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
48
  | License headers | `/*! … */`, `@license`, or `SPDX-*` before the first statement |
47
- | API docs | The last JSDoc block before an exported declaration, or before a public member of an exported class, interface, type, or enum |
49
+ | API docs | The last JSDoc block before a declaration TypeScript exports, or before a public member of an exported class, interface, type, enum, or object literal. This counts implicit exports in `.d.ts` files, `declare module`, and `declare global` |
48
50
  | Lint-required | `SAFETY:` comments that anti-slop's `require-safety-comment-for-type-assertion` accepts for a non-`const` type assertion |
49
51
 
50
52
  Everything else is reported, including `TODO`, JSX comments, JSDoc on
51
53
  internal code, and `SAFETY:` comments with no assertion to justify.
52
54
 
55
+ `oxlint --fix` deletes reported comments. The fix keeps tokens apart and keeps
56
+ line breaks that automatic semicolon insertion depends on. It removes a JSX
57
+ `{/* … */}` container as a whole, and it does nothing when the container sits
58
+ between two runs of JSX text.
59
+
53
60
  ## Options
54
61
 
55
62
  ```json
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- // node_modules/.pnpm/@oxlint+plugins@1.90.0/node_modules/@oxlint/plugins/index.js
1
+ // node_modules/.pnpm/@oxlint+plugins@1.94.0/node_modules/@oxlint/plugins/index.js
2
2
  /**
3
3
  * Define a rule.
4
4
  *
@@ -366,6 +366,11 @@ const declarationKinds = /* @__PURE__ */ new Set([
366
366
  ]);
367
367
  const scopeBoundaryKinds = /* @__PURE__ */ new Set(["BlockStatement", "StaticBlock"]);
368
368
  const declarationFilePattern = /\.d\.[cm]?ts$/u;
369
+ const objectLiteralPathKinds = /* @__PURE__ */ new Set([
370
+ "ObjectExpression",
371
+ "Property",
372
+ "TSSatisfiesExpression"
373
+ ]);
369
374
  const commentOwnerKinds = /* @__PURE__ */ new Set([
370
375
  "ExpressionStatement",
371
376
  "PropertyDefinition",
@@ -420,35 +425,85 @@ function boundNames(pattern) {
420
425
  }
421
426
  function declaredNames(node) {
422
427
  if (node.type === "VariableDeclaration") return node.declarations.flatMap((declarator) => boundNames(declarator.id));
423
- if ("id" in node && node.id !== null && node.id.type === "Identifier") return [node.id.name];
424
- return [];
428
+ if (!("id" in node) || node.id === null) return [];
429
+ let id = node.id;
430
+ while (id.type === "TSQualifiedName") id = id.left;
431
+ return id.type === "Identifier" ? [id.name] : [];
425
432
  }
426
- function lastJSDocBefore(sourceCode, node) {
427
- return sourceCode.getCommentsBefore(node).findLast(isJSDoc);
433
+ function localExportNames(statements) {
434
+ const names = /* @__PURE__ */ new Set();
435
+ for (const statement of statements) {
436
+ if (statement.type === "ExportNamedDeclaration" && statement.source === null) {
437
+ for (const specifier of statement.specifiers) if (specifier.local.type === "Identifier") names.add(specifier.local.name);
438
+ }
439
+ const target = statement.type === "ExportDefaultDeclaration" ? statement.declaration : statement.type === "TSExportAssignment" ? statement.expression : null;
440
+ const root = target === null ? null : rootIdentifier(target);
441
+ if (root !== null) names.add(root);
442
+ }
443
+ return names;
428
444
  }
429
- function isAmbient(node, declarationFile) {
430
- if (declarationFile) return true;
445
+ function rootIdentifier(node) {
431
446
  let current = node;
432
- while (current !== null) {
433
- if (current.type === "TSModuleDeclaration" && current.declare) return true;
434
- current = current.parent;
447
+ while (current.type === "MemberExpression" || current.type === "TSQualifiedName") current = current.type === "MemberExpression" ? current.object : current.left;
448
+ return current.type === "Identifier" ? current.name : null;
449
+ }
450
+ function hasExportDeclarations(statements) {
451
+ return statements.some((statement) => statement.type === "ExportNamedDeclaration" && statement.declaration === null || statement.type === "ExportAllDeclaration" || statement.type === "TSExportAssignment" || statement.type === "ExportDefaultDeclaration" && !declarationKinds.has(statement.declaration.type));
452
+ }
453
+ function collectApi(statements, visible, ambient, api, declarators) {
454
+ const listed = localExportNames(statements);
455
+ const exportContext = ambient && !hasExportDeclarations(statements);
456
+ for (const statement of statements) {
457
+ let declaration = null;
458
+ let explicit = false;
459
+ let implicit = false;
460
+ if (statement.type === "ExportNamedDeclaration") {
461
+ declaration = statement.declaration;
462
+ explicit = declaration !== null;
463
+ } else if (statement.type === "ExportDefaultDeclaration" || statement.type === "TSExportAssignment") explicit = true;
464
+ else if (declarationKinds.has(statement.type)) {
465
+ declaration = statement;
466
+ implicit = exportContext || declaredNames(statement).some((name) => listed.has(name));
467
+ }
468
+ const exported = explicit || visible && implicit;
469
+ if (exported) api.add(statement);
470
+ if (exported && declaration?.type === "VariableDeclaration") {
471
+ const all = explicit || exportContext;
472
+ for (const declarator of declaration.declarations) if (all || boundNames(declarator.id).some((name) => listed.has(name))) declarators.add(declarator);
473
+ }
474
+ if (declaration?.type !== "TSModuleDeclaration" || declaration.body?.type !== "TSModuleBlock") continue;
475
+ const external = declaration.global || declaration.id.type === "Literal";
476
+ collectApi(declaration.body.body, external || visible && exported, ambient || declaration.declare, api, declarators);
435
477
  }
436
- return false;
437
478
  }
438
- function isPublicApi(member, api, declarationFile) {
479
+ function lastJSDocBefore(sourceCode, node) {
480
+ return sourceCode.getCommentsBefore(node).findLast(isJSDoc);
481
+ }
482
+ function isPublicApi(member, api, declarators) {
439
483
  if (isPrivateMember(member)) return false;
440
484
  let previous = member;
441
485
  let current = member.parent;
442
486
  while (current !== null) {
443
487
  if (api.has(current)) return true;
444
488
  if (scopeBoundaryKinds.has(current.type) || isPrivateMember(current)) return false;
445
- if (current.type === "TSModuleBlock" && !isAmbient(current, declarationFile)) return false;
489
+ if (current.type === "VariableDeclarator" && !declarators.has(current)) return false;
490
+ if (current.type === "TSModuleBlock") return false;
446
491
  if (current.type === "ArrowFunctionExpression" && current.body === previous) return false;
447
492
  previous = current;
448
493
  current = current.parent;
449
494
  }
450
495
  return false;
451
496
  }
497
+ function isExportedObjectProperty(property, api, declarators) {
498
+ let current = property.parent;
499
+ while (current !== null) {
500
+ if (api.has(current)) return true;
501
+ if (current.type === "VariableDeclarator") return declarators.has(current);
502
+ if (!(objectLiteralPathKinds.has(current.type) || current.type === "TSAsExpression" && isConstAssertion(current))) return false;
503
+ current = current.parent;
504
+ }
505
+ return false;
506
+ }
452
507
  function safetyCommentsFor(sourceCode, assertion, pattern) {
453
508
  const found = [];
454
509
  const collect = (owner) => {
@@ -466,6 +521,152 @@ function safetyCommentsFor(sourceCode, assertion, pattern) {
466
521
  current = current.parent;
467
522
  }
468
523
  }
524
+ const lineBreakPattern = /[\n\r\u2028\u2029]/u;
525
+ const blankPattern = /^\s*$/u;
526
+ const isLineBreak = (char) => char !== void 0 && lineBreakPattern.test(char);
527
+ function isInlineSpace(char) {
528
+ return char !== void 0 && /\s/u.test(char) && !isLineBreak(char);
529
+ }
530
+ function nextLineStart(text, end) {
531
+ if (text.startsWith("\r\n", end)) return end + 2;
532
+ return Math.min(end + 1, text.length);
533
+ }
534
+ function renderedJSXText(value) {
535
+ const lines = value.split(/\r\n|[\n\r]/u);
536
+ return lines.map((line, index) => {
537
+ const start = index > 0 ? line.replace(/^[ \t]+/u, "") : line;
538
+ return index < lines.length - 1 ? start.replace(/[ \t]+$/u, "") : start;
539
+ }).filter((line) => line.length > 0).join(" ");
540
+ }
541
+ function renderChildren(text, from, to, marks) {
542
+ let output = "";
543
+ let pending = "";
544
+ let cursor = from;
545
+ for (const mark of marks.toSorted((a, b) => a.start - b.start)) {
546
+ pending += text.slice(cursor, Math.max(cursor, mark.start)) + mark.text;
547
+ cursor = Math.max(cursor, mark.end);
548
+ if (mark.render === null) continue;
549
+ output += renderedJSXText(pending) + mark.render;
550
+ pending = "";
551
+ }
552
+ return output + renderedJSXText(pending + text.slice(cursor, to));
553
+ }
554
+ function keepsRenderedText(text, parent, planned) {
555
+ if (parent.type !== "JSXElement" && parent.type !== "JSXFragment") return true;
556
+ let from = parent.children[0]?.start ?? parent.start;
557
+ let to = parent.children.at(-1)?.end ?? parent.end;
558
+ for (const edit of planned.values()) {
559
+ from = Math.min(from, edit.start);
560
+ to = Math.max(to, edit.end);
561
+ }
562
+ const before = [];
563
+ const after = [];
564
+ for (const child of parent.children) {
565
+ if (child.type === "JSXText") continue;
566
+ const render = child.type === "JSXExpressionContainer" ? "" : "\0";
567
+ const separator = {
568
+ start: child.start,
569
+ end: child.end,
570
+ text: "",
571
+ render
572
+ };
573
+ before.push(separator);
574
+ const edit = planned.get(child);
575
+ after.push(edit === void 0 ? separator : {
576
+ ...edit,
577
+ render: null
578
+ });
579
+ }
580
+ return renderChildren(text, from, to, before) === renderChildren(text, from, to, after);
581
+ }
582
+ function removal(text, start, end, jsx) {
583
+ let from = start;
584
+ while (isInlineSpace(text[from - 1])) from--;
585
+ let to = end;
586
+ while (isInlineSpace(text[to])) to++;
587
+ const blankBefore = from === 0 || isLineBreak(text[from - 1]);
588
+ const blankAfter = to === text.length || isLineBreak(text[to]);
589
+ if (blankBefore && blankAfter) return {
590
+ start: from,
591
+ end: nextLineStart(text, to),
592
+ text: ""
593
+ };
594
+ if (jsx) return {
595
+ start,
596
+ end,
597
+ text: ""
598
+ };
599
+ if (blankAfter) return {
600
+ start: from,
601
+ end,
602
+ text: ""
603
+ };
604
+ if (blankBefore) return {
605
+ start,
606
+ end: to,
607
+ text: ""
608
+ };
609
+ const separator = lineBreakPattern.test(text.slice(from, end)) ? "\n" : isInlineSpace(text[end]) ? "" : " ";
610
+ return {
611
+ start: from,
612
+ end,
613
+ text: separator
614
+ };
615
+ }
616
+ function commentRuns(text, reported) {
617
+ const runs = [];
618
+ for (const comment of reported) {
619
+ const run = runs.at(-1);
620
+ const previous = run?.at(-1);
621
+ if (run !== void 0 && previous !== void 0) {
622
+ if (blankPattern.test(text.slice(previous.end, comment.start))) {
623
+ run.push(comment);
624
+ continue;
625
+ }
626
+ }
627
+ runs.push([comment]);
628
+ }
629
+ return runs;
630
+ }
631
+ function planEdits(text, comments, reported, containers) {
632
+ const reportedSet = new Set(reported);
633
+ const sorted = containers.toSorted((a, b) => a.start - b.start);
634
+ const keepsComment = /* @__PURE__ */ new Set();
635
+ let index = 0;
636
+ for (const comment of comments) {
637
+ let container = sorted[index];
638
+ while (container !== void 0 && container.end <= comment.start) container = sorted[++index];
639
+ if (container !== void 0 && container.start < comment.start && !reportedSet.has(comment)) keepsComment.add(container);
640
+ }
641
+ const edits = /* @__PURE__ */ new Map();
642
+ const byParent = /* @__PURE__ */ new Map();
643
+ const owners = /* @__PURE__ */ new Map();
644
+ index = 0;
645
+ for (const run of commentRuns(text, reported)) {
646
+ const first = run[0];
647
+ const last = run.at(-1);
648
+ if (first === void 0 || last === void 0) continue;
649
+ let candidate = sorted[index];
650
+ while (candidate !== void 0 && candidate.end <= first.start) candidate = sorted[++index];
651
+ const container = candidate !== void 0 && candidate.start < first.start && last.end < candidate.end ? candidate : void 0;
652
+ if (container !== void 0 && !keepsComment.has(container)) {
653
+ const planned = byParent.get(container.parent) ?? /* @__PURE__ */ new Map();
654
+ planned.set(container, removal(text, container.start, container.end, true));
655
+ byParent.set(container.parent, planned);
656
+ owners.set(container, first);
657
+ continue;
658
+ }
659
+ edits.set(first, removal(text, first.start, last.end, false));
660
+ }
661
+ for (const [parent, planned] of byParent) {
662
+ if (!keepsRenderedText(text, parent, planned)) continue;
663
+ for (const [container, edit] of planned) {
664
+ const owner = owners.get(container);
665
+ if (owner !== void 0) edits.set(owner, edit);
666
+ }
667
+ }
668
+ return edits;
669
+ }
469
670
  // src/index.ts
470
671
  /** Oxlint plugin that keeps intent in names, types, tests, and commit messages instead of comments. */
471
672
  const plugin = eslintCompatPlugin({
@@ -473,6 +674,7 @@ const plugin = eslintCompatPlugin({
473
674
  rules: { "no-comments": defineRule({
474
675
  meta: {
475
676
  type: "suggestion",
677
+ fixable: "code",
476
678
  docs: { description: "Disallow comments other than tool directives, license headers, JSDoc on exported API, and SAFETY justifications for type assertions." },
477
679
  messages: { comment: "Remove this comment. Names, types, and tests carry intent; the commit message carries the why." },
478
680
  schema: [{
@@ -505,10 +707,10 @@ const plugin = eslintCompatPlugin({
505
707
  },
506
708
  createOnce(context) {
507
709
  const matchersByOption = /* @__PURE__ */ new Map();
508
- const apiNodes = /* @__PURE__ */ new Set();
509
710
  const members = [];
711
+ const properties = [];
510
712
  const assertions = [];
511
- const localExports = /* @__PURE__ */ new Set();
713
+ const emptyContainers = [];
512
714
  const addMember = (node) => {
513
715
  members.push(node);
514
716
  };
@@ -517,26 +719,16 @@ const plugin = eslintCompatPlugin({
517
719
  };
518
720
  return {
519
721
  before() {
520
- apiNodes.clear();
521
722
  members.length = 0;
723
+ properties.length = 0;
522
724
  assertions.length = 0;
523
- localExports.clear();
524
- },
525
- ExportNamedDeclaration(node) {
526
- if (node.declaration !== null) apiNodes.add(node);
527
- if (node.source !== null) return;
528
- for (const specifier of node.specifiers) if (specifier.local.type === "Identifier") localExports.add(specifier.local.name);
529
- },
530
- ExportDefaultDeclaration(node) {
531
- apiNodes.add(node);
532
- if (node.declaration.type === "Identifier") localExports.add(node.declaration.name);
725
+ emptyContainers.length = 0;
533
726
  },
534
- TSExportAssignment(node) {
535
- apiNodes.add(node);
536
- if (node.expression.type === "Identifier") localExports.add(node.expression.name);
727
+ JSXExpressionContainer(node) {
728
+ if (node.expression.type === "JSXEmptyExpression") emptyContainers.push(node);
537
729
  },
538
- TSModuleBlock(node) {
539
- for (const statement of node.body) if (declarationKinds.has(statement.type)) members.push(statement);
730
+ Property(node) {
731
+ properties.push(node);
540
732
  },
541
733
  AccessorProperty: addMember,
542
734
  MethodDefinition: addMember,
@@ -559,33 +751,47 @@ const plugin = eslintCompatPlugin({
559
751
  const optionKey = JSON.stringify(option ?? null);
560
752
  const matchers = matchersByOption.get(optionKey) ?? buildMatchers(option);
561
753
  matchersByOption.set(optionKey, matchers);
562
- for (const statement of program.body) {
563
- if (!declarationKinds.has(statement.type)) continue;
564
- if (declaredNames(statement).some((name) => localExports.has(name))) apiNodes.add(statement);
565
- }
754
+ const apiNodes = /* @__PURE__ */ new Set();
755
+ const exportedDeclarators = /* @__PURE__ */ new Set();
756
+ const declarationFile = declarationFilePattern.test(context.filename);
757
+ collectApi(program.body, true, declarationFile, apiNodes, exportedDeclarators);
566
758
  const allowed = /* @__PURE__ */ new Set();
567
759
  for (const node of apiNodes) {
568
760
  const doc = lastJSDocBefore(sourceCode, node);
569
761
  if (doc !== void 0) allowed.add(doc.start);
570
762
  }
571
- const declarationFile = declarationFilePattern.test(context.filename);
572
763
  for (const member of members) {
573
- if (!isPublicApi(member, apiNodes, declarationFile)) continue;
764
+ if (!isPublicApi(member, apiNodes, exportedDeclarators)) continue;
574
765
  const doc = lastJSDocBefore(sourceCode, member);
575
766
  if (doc !== void 0) allowed.add(doc.start);
576
767
  }
768
+ for (const property of properties) {
769
+ if (!isExportedObjectProperty(property, apiNodes, exportedDeclarators)) continue;
770
+ const doc = lastJSDocBefore(sourceCode, property);
771
+ if (doc !== void 0) allowed.add(doc.start);
772
+ }
577
773
  for (const assertion of assertions) for (const comment of safetyCommentsFor(sourceCode, assertion, matchers.safety)) allowed.add(comment.start);
578
774
  const headerEnd = program.body[0]?.start ?? program.end;
579
- for (const comment of sourceCode.getAllComments()) {
580
- if (comment.type === "Shebang" || allowed.has(comment.start)) continue;
581
- if (isLicenseHeader(comment, headerEnd)) continue;
775
+ const comments = sourceCode.getAllComments();
776
+ const reported = comments.filter((comment) => {
777
+ if (comment.type === "Shebang" || allowed.has(comment.start)) return false;
778
+ if (isLicenseHeader(comment, headerEnd)) return false;
582
779
  const text = commentText(comment);
583
- if (directivePatterns.some((pattern) => pattern.test(text))) continue;
584
- if (matchers.allow.some((pattern) => pattern.test(text))) continue;
585
- context.report({
780
+ if (directivePatterns.some((pattern) => pattern.test(text))) return false;
781
+ return !matchers.allow.some((pattern) => pattern.test(text));
782
+ });
783
+ const edits = planEdits(sourceCode.text, comments, reported, emptyContainers);
784
+ for (const comment of reported) {
785
+ const edit = edits.get(comment);
786
+ if (edit === void 0) context.report({
586
787
  node: comment,
587
788
  messageId: "comment"
588
789
  });
790
+ else context.report({
791
+ node: comment,
792
+ messageId: "comment",
793
+ fix: (fixer) => fixer.replaceTextRange([edit.start, edit.end], edit.text)
794
+ });
589
795
  }
590
796
  }
591
797
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uinaf/oxlint-plugin-no-comments",
3
- "version": "1.0.1",
3
+ "version": "1.1.1",
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",
@@ -36,9 +36,10 @@
36
36
  "prepublishOnly": "pnpm run verify:full"
37
37
  },
38
38
  "devDependencies": {
39
- "@oxlint/plugins": "1.90.0",
39
+ "@oxlint/plugins": "1.94.0",
40
40
  "@types/node": "^26.6.6",
41
- "oxlint": "1.90.0",
41
+ "@uinaf/oxlint-config": "1.0.0",
42
+ "oxlint": "1.94.0",
42
43
  "vite": "catalog:",
43
44
  "vite-plus": "catalog:"
44
45
  },
@@ -52,5 +53,5 @@
52
53
  "engines": {
53
54
  "node": ">=20.19.0"
54
55
  },
55
- "packageManager": "pnpm@12.16.0"
56
+ "packageManager": "pnpm@12.23.0"
56
57
  }