@kutur-kit/oxc 0.1.0-alpha.1 → 0.1.0-alpha.3

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 (117) hide show
  1. package/README.md +48 -8
  2. package/dist/anti-slop.d.ts.map +1 -1
  3. package/dist/anti-slop.js +3 -0
  4. package/dist/anti-slop.js.map +1 -1
  5. package/dist/base.d.ts.map +1 -1
  6. package/dist/base.js +7 -1
  7. package/dist/base.js.map +1 -1
  8. package/dist/effect.d.ts.map +1 -1
  9. package/dist/effect.js +19 -0
  10. package/dist/effect.js.map +1 -1
  11. package/dist/format.d.ts.map +1 -1
  12. package/dist/format.js.map +1 -1
  13. package/dist/plugin.d.ts.map +1 -1
  14. package/dist/plugin.js +7 -1
  15. package/dist/plugin.js.map +1 -1
  16. package/dist/prose/cli.d.ts +3 -0
  17. package/dist/prose/cli.d.ts.map +1 -0
  18. package/dist/prose/cli.js +63 -0
  19. package/dist/prose/cli.js.map +1 -0
  20. package/dist/prose/problems.d.ts +18 -0
  21. package/dist/prose/problems.d.ts.map +1 -0
  22. package/dist/prose/problems.js +49 -0
  23. package/dist/prose/problems.js.map +1 -0
  24. package/dist/rules/comment-prose.d.ts +2 -0
  25. package/dist/rules/comment-prose.d.ts.map +1 -0
  26. package/dist/rules/comment-prose.js +49 -0
  27. package/dist/rules/comment-prose.js.map +1 -0
  28. package/dist/rules/directives.d.ts +4 -0
  29. package/dist/rules/directives.d.ts.map +1 -0
  30. package/dist/rules/directives.js +12 -0
  31. package/dist/rules/directives.js.map +1 -0
  32. package/dist/rules/no-suppression-directives.d.ts +2 -0
  33. package/dist/rules/no-suppression-directives.d.ts.map +1 -0
  34. package/dist/rules/no-suppression-directives.js +93 -0
  35. package/dist/rules/no-suppression-directives.js.map +1 -0
  36. package/dist/rules/no-yield-in-if.d.ts.map +1 -1
  37. package/dist/rules/no-yield-in-if.js.map +1 -1
  38. package/dist/vendor/anti-slop/effect/index.d.ts.map +1 -1
  39. package/dist/vendor/anti-slop/effect/index.js.map +1 -1
  40. package/dist/vendor/anti-slop/effect/rules/no-manual-effect-error-tag.d.ts.map +1 -1
  41. package/dist/vendor/anti-slop/effect/rules/no-manual-effect-error-tag.js.map +1 -1
  42. package/dist/vendor/anti-slop/effect/rules/no-manual-tag-comparison.d.ts.map +1 -1
  43. package/dist/vendor/anti-slop/effect/rules/no-manual-tag-comparison.js.map +1 -1
  44. package/dist/vendor/anti-slop/effect/rules/no-manual-tagged-construction.d.ts.map +1 -1
  45. package/dist/vendor/anti-slop/effect/rules/no-manual-tagged-construction.js.map +1 -1
  46. package/dist/vendor/anti-slop/effect/rules/no-service-constructor-imports.d.ts.map +1 -1
  47. package/dist/vendor/anti-slop/effect/rules/no-service-constructor-imports.js.map +1 -1
  48. package/dist/vendor/anti-slop/effect/rules/prefer-effect-match.d.ts.map +1 -1
  49. package/dist/vendor/anti-slop/effect/rules/prefer-effect-match.js.map +1 -1
  50. package/dist/vendor/anti-slop/effect/shared/tagged-values.d.ts.map +1 -1
  51. package/dist/vendor/anti-slop/effect/shared/tagged-values.js.map +1 -1
  52. package/dist/vendor/anti-slop/index.d.ts.map +1 -1
  53. package/dist/vendor/anti-slop/index.js.map +1 -1
  54. package/dist/vendor/anti-slop/rules/no-array-filter-map.d.ts.map +1 -1
  55. package/dist/vendor/anti-slop/rules/no-array-filter-map.js.map +1 -1
  56. package/dist/vendor/anti-slop/rules/no-chained-type-assertions.d.ts.map +1 -1
  57. package/dist/vendor/anti-slop/rules/no-chained-type-assertions.js.map +1 -1
  58. package/dist/vendor/anti-slop/rules/no-conditional-empty-object-spread.d.ts.map +1 -1
  59. package/dist/vendor/anti-slop/rules/no-conditional-empty-object-spread.js.map +1 -1
  60. package/dist/vendor/anti-slop/rules/no-known-value-widening.d.ts.map +1 -1
  61. package/dist/vendor/anti-slop/rules/no-known-value-widening.js.map +1 -1
  62. package/dist/vendor/anti-slop/rules/no-module-mocking.d.ts.map +1 -1
  63. package/dist/vendor/anti-slop/rules/no-module-mocking.js.map +1 -1
  64. package/dist/vendor/anti-slop/rules/no-object-parameters.d.ts.map +1 -1
  65. package/dist/vendor/anti-slop/rules/no-object-parameters.js.map +1 -1
  66. package/dist/vendor/anti-slop/rules/no-reduce-accumulator-copy.d.ts.map +1 -1
  67. package/dist/vendor/anti-slop/rules/no-reduce-accumulator-copy.js.map +1 -1
  68. package/dist/vendor/anti-slop/rules/no-reflect-apply.d.ts.map +1 -1
  69. package/dist/vendor/anti-slop/rules/no-reflect-apply.js.map +1 -1
  70. package/dist/vendor/anti-slop/rules/no-reflect-get.d.ts.map +1 -1
  71. package/dist/vendor/anti-slop/rules/no-reflect-get.js.map +1 -1
  72. package/dist/vendor/anti-slop/rules/no-runtime-typeof.d.ts.map +1 -1
  73. package/dist/vendor/anti-slop/rules/no-runtime-typeof.js.map +1 -1
  74. package/dist/vendor/anti-slop/rules/no-shape-in-symbol-names.d.ts.map +1 -1
  75. package/dist/vendor/anti-slop/rules/no-shape-in-symbol-names.js.map +1 -1
  76. package/dist/vendor/anti-slop/rules/no-unknown-parameters.d.ts.map +1 -1
  77. package/dist/vendor/anti-slop/rules/no-unknown-parameters.js.map +1 -1
  78. package/dist/vendor/anti-slop/rules/no-unknown-returns.d.ts.map +1 -1
  79. package/dist/vendor/anti-slop/rules/no-unknown-returns.js.map +1 -1
  80. package/dist/vendor/anti-slop/rules/no-unknown-type-aliases.d.ts.map +1 -1
  81. package/dist/vendor/anti-slop/rules/no-unknown-type-aliases.js.map +1 -1
  82. package/dist/vendor/anti-slop/rules/no-unsafe-dictionary-type.d.ts.map +1 -1
  83. package/dist/vendor/anti-slop/rules/no-unsafe-dictionary-type.js.map +1 -1
  84. package/dist/vendor/anti-slop/rules/no-widen-then-assert.d.ts.map +1 -1
  85. package/dist/vendor/anti-slop/rules/no-widen-then-assert.js.map +1 -1
  86. package/dist/vendor/anti-slop/rules/require-readable-spacing.d.ts.map +1 -1
  87. package/dist/vendor/anti-slop/rules/require-readable-spacing.js.map +1 -1
  88. package/dist/vendor/anti-slop/rules/require-safety-comment-for-type-assertion.d.ts.map +1 -1
  89. package/dist/vendor/anti-slop/rules/require-safety-comment-for-type-assertion.js.map +1 -1
  90. package/dist/vendor/anti-slop/shared/array-method.d.ts.map +1 -1
  91. package/dist/vendor/anti-slop/shared/array-method.js.map +1 -1
  92. package/dist/vendor/anti-slop/shared/dictionary-types.d.ts.map +1 -1
  93. package/dist/vendor/anti-slop/shared/dictionary-types.js.map +1 -1
  94. package/dist/vendor/anti-slop/shared/function-parameters.d.ts.map +1 -1
  95. package/dist/vendor/anti-slop/shared/function-parameters.js.map +1 -1
  96. package/dist/vendor/anti-slop/shared/lexical-type-parameters.d.ts.map +1 -1
  97. package/dist/vendor/anti-slop/shared/lexical-type-parameters.js.map +1 -1
  98. package/dist/vendor/anti-slop/shared/reflect-method.d.ts.map +1 -1
  99. package/dist/vendor/anti-slop/shared/reflect-method.js.map +1 -1
  100. package/dist/vendor/anti-slop/shared/scope.d.ts.map +1 -1
  101. package/dist/vendor/anti-slop/shared/scope.js.map +1 -1
  102. package/dist/vendor/anti-slop/shared/type-alias-resolution.d.ts.map +1 -1
  103. package/dist/vendor/anti-slop/shared/type-alias-resolution.js.map +1 -1
  104. package/dist/vendor/anti-slop/vendor/eslint-stylistic/padding-line-ast.d.ts.map +1 -1
  105. package/dist/vendor/anti-slop/vendor/eslint-stylistic/padding-line-ast.js.map +1 -1
  106. package/dist/vendor/anti-slop/vendor/eslint-stylistic/padding-line-between-statements.d.ts.map +1 -1
  107. package/dist/vendor/anti-slop/vendor/eslint-stylistic/padding-line-between-statements.js.map +1 -1
  108. package/package.json +18 -16
  109. package/src/anti-slop.ts +3 -0
  110. package/src/base.ts +7 -1
  111. package/src/effect.ts +19 -0
  112. package/src/plugin.ts +7 -1
  113. package/src/prose/cli.ts +86 -0
  114. package/src/prose/problems.ts +68 -0
  115. package/src/rules/comment-prose.ts +58 -0
  116. package/src/rules/directives.ts +16 -0
  117. package/src/rules/no-suppression-directives.ts +140 -0
@@ -0,0 +1,68 @@
1
+ /** A vague word or a dash used as punctuation, at a UTF-16 offset into the checked text. */
2
+ export type ProseProblem = {
3
+ readonly index: number;
4
+ readonly text: string;
5
+ readonly message: string;
6
+ };
7
+
8
+ type Rule = { readonly pattern: RegExp; readonly message: string };
9
+
10
+ const vague = (pattern: RegExp, fix: string): Rule => ({
11
+ pattern,
12
+ message: `is vague. A plain word says what happens. Fix: ${fix}, or put a literal use in a code span.`,
13
+ });
14
+
15
+ const rules: ReadonlyArray<Rule> = [
16
+ vague(/\b(?:stamp|stamps|stamped|stamping)\b/giu, "say who records or approves it"),
17
+ vague(/\b(?:land|lands|landed)\b/giu, "say `merged`, `deployed` or `released`"),
18
+ vague(/\b(?:gate|gates|gated|gating)\b/giu, "say what checks, blocks or requires it"),
19
+ vague(/\b(?:unlock|unlocks|unlocked|unlocking)\b/giu, "say what it allows"),
20
+ vague(/\b(?:supercharge|supercharges|supercharged)\b/giu, "say what changes"),
21
+ vague(/\b(?:seamless|seamlessly)\b/giu, "delete it or say what keeps working"),
22
+ vague(/\bbattle-tested\b/giu, "say where it runs or what tests it"),
23
+ vague(/\b(?:leverage|leverages|leveraged|leveraging)\b/giu, "say `use`"),
24
+ vague(/\b(?:empower|empowers|empowered|empowering)\b/giu, "say `lets` or `supports`"),
25
+ {
26
+ // An em dash (U+2014) anywhere. An en dash (U+2013), `-` or `--` only between spaces, so
27
+ // ranges such as `2–4`, hyphenated words, flags and list items pass.
28
+ pattern: /\u2014|(?<=\S )(?:\u2013|--?)(?= \S)/gu,
29
+ message:
30
+ "is a dash used as punctuation. A period, comma, colon or parentheses say how the parts relate. Fix: use one of them, or put a literal use in a code span.",
31
+ },
32
+ ];
33
+
34
+ /**
35
+ * Replaces every character but line breaks with a space, so offsets and line numbers still point
36
+ * into the source. Without the `u` flag the pattern matches UTF-16 units, so a character outside
37
+ * the BMP becomes two spaces.
38
+ */
39
+ export const blank = (span: string) => span.replaceAll(/[^\n]/g, " ");
40
+
41
+ // Fenced code, indented in a list item or not, and HTML comments.
42
+ const block =
43
+ /^[ \t]*(?<fence>(?<char>[`~])\k<char>{2,})[^\n]*\n[\s\S]*?^[ \t]*\k<fence>\k<char>*[ \t]*$|<!--[\s\S]*?-->/gmu;
44
+
45
+ // Code spans, link targets, autolinks and bare URLs.
46
+ const inline = /(?<ticks>`+)[^\n]*?\k<ticks>|\]\([^)\n]*\)|<https?:[^>\n]*>|https?:\/\/\S+/gu;
47
+
48
+ // U+FFFC instead of a space, so a dash next to a code span or link still has a non-space
49
+ // neighbour. None of the inline spans holds a line break.
50
+ const mark = (span: string) => "\uFFFC".repeat(span.length);
51
+
52
+ /**
53
+ * Finds vague words and dashes used as punctuation in Markdown or comment text. Fenced code, HTML
54
+ * comments, code spans, link targets, autolinks and URLs are not prose.
55
+ */
56
+ export const proseProblems = (text: string): ReadonlyArray<ProseProblem> => {
57
+ const prose = text.replaceAll(block, blank).replaceAll(inline, mark);
58
+
59
+ return rules
60
+ .flatMap(({ pattern, message }) =>
61
+ Array.from(prose.matchAll(pattern), (match) => ({
62
+ index: match.index,
63
+ text: match[0],
64
+ message,
65
+ })),
66
+ )
67
+ .sort((a, b) => a.index - b.index);
68
+ };
@@ -0,0 +1,58 @@
1
+ import { defineRule } from "@oxlint/plugins";
2
+
3
+ import { blank, proseProblems } from "../prose/problems.ts";
4
+ import { effectDirective, lintDirective, reasonSeparator } from "./directives.ts";
5
+
6
+ // JSDoc margins are not prose; a list item after one would read as a dash.
7
+ const jsdocMargin = /^[ \t]*\*/gm;
8
+
9
+ // TypeScript reads a `@ts-` directive at the start of a comment. The description after it is prose.
10
+ const typeScriptDirective = /^\s*@ts-(?:expect-error|ignore|nocheck)(?:\s--\s)?/;
11
+
12
+ // Blanks a directive's keyword and rules up to and including the separator, so ` -- ` is not read
13
+ // as a dash. The reason after it is prose.
14
+ const blankSyntax = (directive: string) => {
15
+ const separator = reasonSeparator.exec(directive);
16
+ const end = separator === null ? directive.length : separator.index + separator[0].length;
17
+
18
+ return blank(directive.slice(0, end)) + directive.slice(end);
19
+ };
20
+
21
+ const proseOf = (comment: string) =>
22
+ comment
23
+ .replaceAll(jsdocMargin, blank)
24
+ .replace(lintDirective, blankSyntax)
25
+ .replace(typeScriptDirective, blank)
26
+ .replaceAll(effectDirective, blankSyntax);
27
+
28
+ export const commentProse = defineRule({
29
+ meta: {
30
+ type: "suggestion",
31
+ docs: { description: "Disallow vague words and dashes used as punctuation in comments." },
32
+ schema: [],
33
+ messages: { prose: '"{{text}}" {{message}}' },
34
+ },
35
+ createOnce(context) {
36
+ return {
37
+ Program() {
38
+ const { sourceCode } = context;
39
+
40
+ for (const comment of sourceCode.getAllComments()) {
41
+ // The value starts after the comment's opening `//` or `/*`.
42
+ const start = comment.range[0] + 2;
43
+
44
+ for (const { index, text, message } of proseProblems(proseOf(comment.value))) {
45
+ context.report({
46
+ loc: {
47
+ start: sourceCode.getLocFromIndex(start + index),
48
+ end: sourceCode.getLocFromIndex(start + index + text.length),
49
+ },
50
+ messageId: "prose",
51
+ data: { text, message },
52
+ });
53
+ }
54
+ }
55
+ },
56
+ };
57
+ },
58
+ });
@@ -0,0 +1,16 @@
1
+ // Comment directives as Oxlint, ESLint and tsgo read them.
2
+
3
+ // Oxlint honours a directive only at the start of a comment. A JSDoc comment starts with `*`, so
4
+ // Oxlint ignores a directive there. `text` holds the rule list and the reason.
5
+ export const lintDirective =
6
+ /^\s*(?:oxlint|eslint)-(?:(?<disable>disable(?:-next-line|-line)?)|enable)(?=\s|$)(?<text>[\s\S]*)/;
7
+
8
+ // tsgo honours `@effect-diagnostics` anywhere in a comment, JSDoc included. It also reads string
9
+ // literals, which kit's rules do not check. A directive runs to the end of its line or to the next
10
+ // `@effect-diagnostics`, so a second directive on the same line cannot hide in the first one's
11
+ // reason.
12
+ export const effectDirective =
13
+ /@effect-diagnostics(?:-next-line)?(?=\s|$)(?<text>(?:(?!@effect-diagnostics)[^\n])*)/g;
14
+
15
+ // The reason follows ` -- `; neither tool reads it as a rule.
16
+ export const reasonSeparator = /\s--\s/;
@@ -0,0 +1,140 @@
1
+ import { defineRule } from "@oxlint/plugins";
2
+
3
+ import { effectDirective, lintDirective, reasonSeparator } from "./directives.ts";
4
+
5
+ const thisRule = "no-suppression-directives";
6
+
7
+ // Rules that keep the remaining suppressions visible. Suppressing them reopens what this rule closes.
8
+ const fenceRules = ["no-abusive-eslint-disable", "ban-ts-comment"];
9
+
10
+ // An Effect entry that sets one diagnostic to a level, such as `floatingEffect:off`. `*:off` and
11
+ // `skip-file` do not match.
12
+ const effectRuleLevel = /^[\w-]+:(?:off|warning|error|message|suggestion)$/;
13
+
14
+ // Oxlint splits a rule list on whitespace and commas, tsgo on whitespace.
15
+ const lintRuleSeparator = /[\s,]+/;
16
+
17
+ const effectEntrySeparator = /\s+/;
18
+
19
+ type Directive =
20
+ | { readonly kind: "lint-disable"; readonly rules: string[]; readonly reason: string }
21
+ | { readonly kind: "effect-diagnostics"; readonly entries: string[]; readonly reason: string };
22
+
23
+ // A type alias, unlike an interface, is assignable to `JsonObject`, so `context.options` can be
24
+ // asserted to it.
25
+ type Options = { readonly allowWithReason: boolean };
26
+
27
+ type MessageId = "suppression" | "unexplained" | "fence";
28
+
29
+ const splitReason = (text: string) => {
30
+ const separator = reasonSeparator.exec(text);
31
+
32
+ return separator === null
33
+ ? { list: text, reason: "" }
34
+ : {
35
+ list: text.slice(0, separator.index),
36
+ reason: text.slice(separator.index + separator[0].length).trim(),
37
+ };
38
+ };
39
+
40
+ const words = (list: string, separator: RegExp) =>
41
+ list.split(separator).filter((word) => word !== "");
42
+
43
+ const directivesIn = (comment: string): Directive[] => {
44
+ const effect = Array.from(comment.matchAll(effectDirective), (match): Directive => {
45
+ const { list, reason } = splitReason(match.groups?.text ?? "");
46
+
47
+ return { kind: "effect-diagnostics", entries: words(list, effectEntrySeparator), reason };
48
+ });
49
+
50
+ const lint = lintDirective.exec(comment);
51
+
52
+ if (lint?.groups?.disable === undefined) return effect;
53
+
54
+ const { list, reason } = splitReason(lint.groups.text ?? "");
55
+
56
+ return [{ kind: "lint-disable", rules: words(list, lintRuleSeparator), reason }, ...effect];
57
+ };
58
+
59
+ // A rule id may carry any plugin prefix, such as `kit/` or an alias the consumer chose.
60
+ const includesRule = (rules: string[], rule: string) =>
61
+ rules.some((id) => id === rule || id.endsWith(`/${rule}`));
62
+
63
+ const namesEachRule = (directive: Directive) =>
64
+ directive.kind === "lint-disable"
65
+ ? directive.rules.length > 0
66
+ : directive.entries.length > 0 &&
67
+ directive.entries.every((entry) => effectRuleLevel.test(entry));
68
+
69
+ const violation = (directive: Directive, allowWithReason: boolean): MessageId | undefined => {
70
+ if (!allowWithReason) return "suppression";
71
+
72
+ if (
73
+ directive.kind === "lint-disable" &&
74
+ fenceRules.some((fence) => includesRule(directive.rules, fence))
75
+ ) {
76
+ return "fence";
77
+ }
78
+
79
+ return namesEachRule(directive) && directive.reason !== "" ? undefined : "unexplained";
80
+ };
81
+
82
+ export const noSuppressionDirectives = defineRule({
83
+ meta: {
84
+ type: "problem",
85
+ docs: {
86
+ description:
87
+ "Disallow comments that suppress lint rules or Effect diagnostics, or allow only those that name their rules and give a reason.",
88
+ },
89
+ schema: [
90
+ {
91
+ type: "object",
92
+ properties: { allowWithReason: { type: "boolean" } },
93
+ additionalProperties: false,
94
+ },
95
+ ],
96
+ defaultOptions: [{ allowWithReason: false }],
97
+ messages: {
98
+ suppression:
99
+ "Suppression comment. It hides a finding instead of fixing its cause. Fix: delete the comment and fix the finding.",
100
+ unexplained:
101
+ "Suppression comment without named rules and a reason. A blanket or unexplained suppression also hides later findings. Fix: name each rule (`rule:level` for `@effect-diagnostics`) and append ` -- <reason>`, or delete the comment and fix the finding.",
102
+ fence:
103
+ "Suppression of `no-abusive-eslint-disable` or `ban-ts-comment`. These rules keep the other suppressions visible. Fix: delete the comment and fix what the rule reports.",
104
+ },
105
+ },
106
+ createOnce(context) {
107
+ return {
108
+ Program() {
109
+ // SAFETY: Oxlint validates the options against `meta.schema` and merges them over
110
+ // `meta.defaultOptions`, so the first one is a complete `Options`.
111
+ const { allowWithReason } = context.options[0] as Options;
112
+
113
+ const comments = context.sourceCode
114
+ .getAllComments()
115
+ .map((comment) => ({ comment, directives: directivesIn(comment.value) }));
116
+
117
+ // A disable directive naming this rule would hide its reports. Reporting nothing leaves
118
+ // that directive unused, and `reportUnusedDisableDirectives` fails on it instead.
119
+ if (
120
+ comments.some(({ directives }) =>
121
+ directives.some(
122
+ (directive) =>
123
+ directive.kind === "lint-disable" && includesRule(directive.rules, thisRule),
124
+ ),
125
+ )
126
+ ) {
127
+ return;
128
+ }
129
+
130
+ for (const { comment, directives } of comments) {
131
+ const messageId = directives
132
+ .map((directive) => violation(directive, allowWithReason))
133
+ .find((id) => id !== undefined);
134
+
135
+ if (messageId !== undefined) context.report({ node: comment, messageId });
136
+ }
137
+ },
138
+ };
139
+ },
140
+ });