@uinaf/oxlint-plugin-no-comments 1.0.1 → 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.
Files changed (3) hide show
  1. package/README.md +6 -1
  2. package/dist/index.js +214 -43
  3. 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 an exported declaration, or before a public member of an exported class, interface, type, or enum |
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
@@ -420,29 +420,64 @@ function boundNames(pattern) {
420
420
  }
421
421
  function declaredNames(node) {
422
422
  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 [];
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] : [];
425
427
  }
426
- function lastJSDocBefore(sourceCode, node) {
427
- return sourceCode.getCommentsBefore(node).findLast(isJSDoc);
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;
428
439
  }
429
- function isAmbient(node, declarationFile) {
430
- if (declarationFile) return true;
440
+ function rootIdentifier(node) {
431
441
  let current = node;
432
- while (current !== null) {
433
- if (current.type === "TSModuleDeclaration" && current.declare) return true;
434
- current = current.parent;
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);
435
468
  }
436
- return false;
437
469
  }
438
- function isPublicApi(member, api, declarationFile) {
470
+ function lastJSDocBefore(sourceCode, node) {
471
+ return sourceCode.getCommentsBefore(node).findLast(isJSDoc);
472
+ }
473
+ function isPublicApi(member, api) {
439
474
  if (isPrivateMember(member)) return false;
440
475
  let previous = member;
441
476
  let current = member.parent;
442
477
  while (current !== null) {
443
478
  if (api.has(current)) return true;
444
479
  if (scopeBoundaryKinds.has(current.type) || isPrivateMember(current)) return false;
445
- if (current.type === "TSModuleBlock" && !isAmbient(current, declarationFile)) return false;
480
+ if (current.type === "TSModuleBlock") return false;
446
481
  if (current.type === "ArrowFunctionExpression" && current.body === previous) return false;
447
482
  previous = current;
448
483
  current = current.parent;
@@ -466,6 +501,152 @@ function safetyCommentsFor(sourceCode, assertion, pattern) {
466
501
  current = current.parent;
467
502
  }
468
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
+ }
469
650
  // src/index.ts
470
651
  /** Oxlint plugin that keeps intent in names, types, tests, and commit messages instead of comments. */
471
652
  const plugin = eslintCompatPlugin({
@@ -473,6 +654,7 @@ const plugin = eslintCompatPlugin({
473
654
  rules: { "no-comments": defineRule({
474
655
  meta: {
475
656
  type: "suggestion",
657
+ fixable: "code",
476
658
  docs: { description: "Disallow comments other than tool directives, license headers, JSDoc on exported API, and SAFETY justifications for type assertions." },
477
659
  messages: { comment: "Remove this comment. Names, types, and tests carry intent; the commit message carries the why." },
478
660
  schema: [{
@@ -505,10 +687,9 @@ const plugin = eslintCompatPlugin({
505
687
  },
506
688
  createOnce(context) {
507
689
  const matchersByOption = /* @__PURE__ */ new Map();
508
- const apiNodes = /* @__PURE__ */ new Set();
509
690
  const members = [];
510
691
  const assertions = [];
511
- const localExports = /* @__PURE__ */ new Set();
692
+ const emptyContainers = [];
512
693
  const addMember = (node) => {
513
694
  members.push(node);
514
695
  };
@@ -517,26 +698,12 @@ const plugin = eslintCompatPlugin({
517
698
  };
518
699
  return {
519
700
  before() {
520
- apiNodes.clear();
521
701
  members.length = 0;
522
702
  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);
703
+ emptyContainers.length = 0;
529
704
  },
530
- ExportDefaultDeclaration(node) {
531
- apiNodes.add(node);
532
- if (node.declaration.type === "Identifier") localExports.add(node.declaration.name);
533
- },
534
- TSExportAssignment(node) {
535
- apiNodes.add(node);
536
- if (node.expression.type === "Identifier") localExports.add(node.expression.name);
537
- },
538
- TSModuleBlock(node) {
539
- 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);
540
707
  },
541
708
  AccessorProperty: addMember,
542
709
  MethodDefinition: addMember,
@@ -559,32 +726,36 @@ const plugin = eslintCompatPlugin({
559
726
  const optionKey = JSON.stringify(option ?? null);
560
727
  const matchers = matchersByOption.get(optionKey) ?? buildMatchers(option);
561
728
  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
- }
729
+ const apiNodes = /* @__PURE__ */ new Set();
730
+ const declarationFile = declarationFilePattern.test(context.filename);
731
+ collectApi(program.body, true, declarationFile, apiNodes);
566
732
  const allowed = /* @__PURE__ */ new Set();
567
733
  for (const node of apiNodes) {
568
734
  const doc = lastJSDocBefore(sourceCode, node);
569
735
  if (doc !== void 0) allowed.add(doc.start);
570
736
  }
571
- const declarationFile = declarationFilePattern.test(context.filename);
572
737
  for (const member of members) {
573
- if (!isPublicApi(member, apiNodes, declarationFile)) continue;
738
+ if (!isPublicApi(member, apiNodes)) continue;
574
739
  const doc = lastJSDocBefore(sourceCode, member);
575
740
  if (doc !== void 0) allowed.add(doc.start);
576
741
  }
577
742
  for (const assertion of assertions) for (const comment of safetyCommentsFor(sourceCode, assertion, matchers.safety)) allowed.add(comment.start);
578
743
  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;
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;
582
748
  const text = commentText(comment);
583
- if (directivePatterns.some((pattern) => pattern.test(text))) continue;
584
- if (matchers.allow.some((pattern) => pattern.test(text))) continue;
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);
585
755
  context.report({
586
756
  node: comment,
587
- messageId: "comment"
757
+ messageId: "comment",
758
+ ...edit === void 0 ? {} : { fix: (fixer) => fixer.replaceTextRange([edit.start, edit.end], edit.text) }
588
759
  });
589
760
  }
590
761
  }
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.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.16.0"
55
+ "packageManager": "pnpm@12.18.0"
56
56
  }