@soujvnunes/eslint-config 0.5.1 → 0.7.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/dist/index.d.ts CHANGED
@@ -1,4 +1,14 @@
1
- import { Linter } from 'eslint';
1
+ import { Rule, Linter } from 'eslint';
2
+
3
+ declare const oneLineComments: Rule.RuleModule;
4
+ declare const oneLineCommentsPlugin: {
5
+ meta: {
6
+ name: string;
7
+ };
8
+ rules: {
9
+ 'one-line-comments': Rule.RuleModule;
10
+ };
11
+ };
2
12
 
3
13
  interface ConfigOptions {
4
14
  /** Extra ignore globs, merged after the defaults. */
@@ -7,14 +17,7 @@ interface ConfigOptions {
7
17
  importGroups?: (string | string[])[];
8
18
  /** Root for typescript-eslint's project service. Defaults to cwd. */
9
19
  tsconfigRootDir?: string;
10
- /**
11
- * Path to the Tailwind v4 CSS entry (the file with `@import "tailwindcss"` + `@theme`, e.g.
12
- * `./app/tailwind.config.css`). When set on the Next preset, it wires the bundled
13
- * `eslint-plugin-better-tailwindcss` correctness rules, chiefly `no-unknown-classes`, which flags a
14
- * class not registered in the theme (a dead token `tsc`/build cannot see; see DESIGN-TOKENS). Leave it
15
- * unset and the plugin stays off, since without the entry the rule cannot resolve the theme and would
16
- * flag every class.
17
- */
20
+ /** Path to the Tailwind v4 CSS entry (the file with `@import "tailwindcss"` + `@theme`, e.g. `./app/tailwind.config.css`). When set on the Next preset, it wires the bundled `eslint-plugin-better-tailwindcss` correctness rules, chiefly `no-unknown-classes`, which flags a class not registered in the theme (a dead token `tsc`/build cannot see; see DESIGN-TOKENS). Leave it unset and the plugin stays off, since without the entry the rule cannot resolve the theme and would flag every class. */
18
21
  tailwindEntryPoint?: string;
19
22
  /** Extra flat-config objects appended at the end. */
20
23
  extend?: Linter.Config[];
@@ -24,4 +27,4 @@ declare const createBaseConfig: (options?: ConfigOptions) => any[];
24
27
  /** Full config for Next.js apps: base plus React, React Hooks, jsx-a11y and Next plugins. */
25
28
  declare const createNextConfig: (options?: ConfigOptions) => any[];
26
29
 
27
- export { type ConfigOptions, createBaseConfig, createNextConfig };
30
+ export { type ConfigOptions, createBaseConfig, createNextConfig, oneLineComments, oneLineCommentsPlugin };
package/dist/index.js CHANGED
@@ -13,6 +13,170 @@ import security from "eslint-plugin-security";
13
13
  import unusedImports from "eslint-plugin-unused-imports";
14
14
  import globals from "globals";
15
15
  import tseslint from "typescript-eslint";
16
+
17
+ // oneLineComments.ts
18
+ var DIRECTIVE = /^\s*(?:eslint-disable\b|eslint-enable\b|\/\s*<|prettier-ignore|biome-ignore|(?:istanbul|c8|v8)\s+ignore\b|webpack[A-Z])/u;
19
+ var BLOCK_DIRECTIVE = /^\s*(?:eslint\s|eslint-env\b|global\s|globals\s|exported\s)/u;
20
+ var ANNOTATION = /^\s*[@#]/u;
21
+ var BLOCK_ANNOTATION = /^\s*[\w-]+:\S/u;
22
+ var isComment = (token) => token?.type === "Line" || token?.type === "Block";
23
+ var isDocShaped = (comment) => comment.type === "Block" && comment.value.startsWith("*");
24
+ var isBanner = (comment) => comment.type === "Block" && comment.value.startsWith("!");
25
+ var pragmaText = (comment) => comment.value.replace(/^\*+/u, "");
26
+ var isDirectiveText = (text) => DIRECTIVE.test(text) || ANNOTATION.test(text);
27
+ var isDirective = (comment) => isDirectiveText(pragmaText(comment)) || comment.type === "Block" && (BLOCK_DIRECTIVE.test(comment.value) || !isDocShaped(comment) && BLOCK_ANNOTATION.test(comment.value));
28
+ var spansLines = (comment) => (comment.loc?.start.line ?? 0) !== (comment.loc?.end.line ?? 0);
29
+ var isBare = (comment) => comment.type === "Line" && comment.value.trim() === "";
30
+ var collapse = (text) => text.replace(/\s+/gu, " ").trim();
31
+ var asLine = (body) => body ? `// ${body}` : null;
32
+ var followedOnLine = (comment, after) => !!after?.loc && !!comment.loc && after.loc.start.line === comment.loc.end.line;
33
+ var blockBody = (comment) => collapse(
34
+ comment.value.replace(/^!/u, "").split(/\r\n|[\r\n\u2028\u2029]/u).map((line) => line.trim().replace(/^\*(?= |$)/u, "")).filter(Boolean).join(" ")
35
+ );
36
+ var opener = (comment) => {
37
+ if (isDocShaped(comment)) return "/**";
38
+ return isBanner(comment) ? "/*!" : "/*";
39
+ };
40
+ var collapsedBlock = (comment) => {
41
+ const open = opener(comment);
42
+ const body = blockBody(comment);
43
+ const text = body ? `${open} ${body} */` : `${open} */`;
44
+ return text.indexOf("*/") === text.length - 2 ? text : null;
45
+ };
46
+ var isJsDoc = (comment, after) => {
47
+ if (!isComment(after)) return true;
48
+ const next = after;
49
+ return isDirective(next) || isDocShaped(next) && /^\*+\s*@/u.test(comment.value);
50
+ };
51
+ var oneLineComments = {
52
+ meta: {
53
+ type: "layout",
54
+ docs: {
55
+ description: "Require a comment to occupy exactly one line, however long it runs, and reserve block comments for JSDoc and JSX."
56
+ },
57
+ fixable: "code",
58
+ schema: [],
59
+ messages: {
60
+ adjacent: "Two comment lines are touching. A comment is one line, however long it runs: join them, or write one line per fact beside the code it describes.",
61
+ paragraphs: "A bare `//` between comment lines marks a paragraph break, so this is a set of comments rather than one wrapped line, and joining it would bury every fact but the first. Write one line per fact beside the code it describes, or move the rationale to the README or the project MANIFEST.",
62
+ block: "This block comment spans lines. A comment is one line, however long it runs: collapse it to one `//` line, or to one `/** \u2026 */` line when it is JSDoc, or move the rationale to the README or the project MANIFEST.",
63
+ notDoc: "A block comment is for JSDoc and JSX only. Write this one as `//`, on its own line or at the end of one, or delete it if it is empty.",
64
+ orphanDoc: "`/**` is JSDoc, and JSDoc sits directly above the code it documents. Move it there, join it with the doc under it, or write it as `//`."
65
+ }
66
+ },
67
+ create(context) {
68
+ const sourceCode = context.sourceCode;
69
+ const comments = sourceCode.getAllComments().filter(isComment);
70
+ const enclosing = /* @__PURE__ */ new Map();
71
+ const enclosingNode = (comment) => {
72
+ const known = enclosing.get(comment);
73
+ if (known !== void 0) return known;
74
+ const node = sourceCode.getNodeByRangeIndex(comment.range?.[0] ?? 0);
75
+ enclosing.set(comment, node);
76
+ return node;
77
+ };
78
+ const insideJsx = (comment) => enclosingNode(comment)?.type?.startsWith("JSX") ?? false;
79
+ const jsxContainer = (comment) => {
80
+ const node = comment.type === "Block" ? enclosingNode(comment) : null;
81
+ return node?.type === "JSXEmptyExpression" ? node.parent ?? null : null;
82
+ };
83
+ const isOwnLine = (comment) => {
84
+ const span = jsxContainer(comment)?.loc ?? comment.loc;
85
+ if (!span) return false;
86
+ const lines = sourceCode.getLines();
87
+ const before = (lines[span.start.line - 1] ?? "").slice(0, span.start.column);
88
+ const after = (lines[span.end.line - 1] ?? "").slice(span.end.column);
89
+ return before.trim() === "" && after.trim() === "";
90
+ };
91
+ const breaksSemicolonInsertion = (comment, after) => {
92
+ if (insideJsx(comment)) return false;
93
+ const before = sourceCode.getTokenBefore(comment, { includeComments: true });
94
+ return !!before?.loc && before.loc.end.line === comment.loc?.start.line && followedOnLine(comment, after);
95
+ };
96
+ const oneLineForm = (comment, after) => {
97
+ if (insideJsx(comment) || isDocShaped(comment) || isBanner(comment)) {
98
+ return collapsedBlock(comment);
99
+ }
100
+ const body = blockBody(comment);
101
+ if (followedOnLine(comment, after) || isDirectiveText(body)) return null;
102
+ return asLine(body);
103
+ };
104
+ return {
105
+ "Program:exit"() {
106
+ const docs = /* @__PURE__ */ new Set();
107
+ for (const comment of comments) {
108
+ if (comment.type !== "Block" || isDirective(comment)) continue;
109
+ const after = sourceCode.getTokenAfter(comment, { includeComments: true });
110
+ if (spansLines(comment)) {
111
+ const text2 = oneLineForm(comment, after);
112
+ context.report({
113
+ loc: comment.loc,
114
+ messageId: "block",
115
+ fix: text2 && !breaksSemicolonInsertion(comment, after) ? (fixer) => fixer.replaceText(comment, text2) : null
116
+ });
117
+ continue;
118
+ }
119
+ if (insideJsx(comment) || isBanner(comment)) continue;
120
+ if (isDocShaped(comment)) {
121
+ if (isJsDoc(comment, after)) docs.add(comment);
122
+ else context.report({ loc: comment.loc, messageId: "orphanDoc" });
123
+ continue;
124
+ }
125
+ const text = oneLineForm(comment, after);
126
+ context.report({
127
+ loc: comment.loc,
128
+ messageId: "notDoc",
129
+ fix: text ? (fixer) => fixer.replaceText(comment, text) : null
130
+ });
131
+ }
132
+ const joinable = comments.filter(
133
+ (comment) => !spansLines(comment) && !docs.has(comment) && isOwnLine(comment)
134
+ );
135
+ let run = [];
136
+ const flush = () => {
137
+ const current = run;
138
+ run = [];
139
+ if (current.length < 2 || current.some(isDirective)) return;
140
+ const first = current[0];
141
+ const last = current[current.length - 1];
142
+ if (!first?.loc || !last?.loc) return;
143
+ const block = current.some((comment) => comment.type === "Block");
144
+ let segments = 0;
145
+ let inText = false;
146
+ for (const comment of current) {
147
+ if (isBare(comment)) inText = false;
148
+ else if (!inText) {
149
+ inText = true;
150
+ segments += 1;
151
+ }
152
+ }
153
+ const paragraphs = segments > 1;
154
+ const body = collapse(current.map((comment) => comment.value.trim()).join(" "));
155
+ const text = asLine(body);
156
+ context.report({
157
+ loc: { start: first.loc.start, end: last.loc.end },
158
+ messageId: paragraphs ? "paragraphs" : "adjacent",
159
+ fix: paragraphs || block || !text ? null : (fixer) => fixer.replaceTextRange([first.range?.[0] ?? 0, last.range?.[1] ?? 0], text)
160
+ });
161
+ };
162
+ for (const comment of joinable) {
163
+ const previous = run[run.length - 1];
164
+ const touching = previous && (previous.loc?.end.line ?? 0) + 1 === (comment.loc?.start.line ?? 0);
165
+ if (!touching) flush();
166
+ run.push(comment);
167
+ }
168
+ flush();
169
+ }
170
+ };
171
+ }
172
+ };
173
+ var oneLineCommentsPlugin = {
174
+ // No `version`: it feeds ESLint's cache key, and a hardcoded one drifts from package.json and serves stale cached results after a behaviour change.
175
+ meta: { name: "@soujvnunes/eslint-config" },
176
+ rules: { "one-line-comments": oneLineComments }
177
+ };
178
+
179
+ // index.ts
16
180
  var DEFAULT_IGNORES = [
17
181
  "**/node_modules/**",
18
182
  "**/.next/**",
@@ -224,6 +388,11 @@ var buildConfig = ({
224
388
  ],
225
389
  rules: { "import-x/no-default-export": "off", "no-restricted-syntax": "off" }
226
390
  };
391
+ const oneLineCommentsOverride = {
392
+ files: ["**/*.{js,jsx,mjs,cjs,ts,tsx,mts,cts}"],
393
+ plugins: { soujvnunes: oneLineCommentsPlugin },
394
+ rules: { "soujvnunes/one-line-comments": "error" }
395
+ };
227
396
  const scriptsOverride = {
228
397
  files: ["scripts/**/*.mjs"],
229
398
  languageOptions: { globals: { ...globals.node } },
@@ -252,6 +421,7 @@ var buildConfig = ({
252
421
  rules
253
422
  },
254
423
  prettier,
424
+ oneLineCommentsOverride,
255
425
  rootConfigOverride,
256
426
  ...next ? [nextFileConventionsOverride] : [],
257
427
  scriptsOverride,
@@ -262,5 +432,7 @@ var createBaseConfig = (options) => buildConfig({ ...options, next: false });
262
432
  var createNextConfig = (options) => buildConfig({ ...options, next: true });
263
433
  export {
264
434
  createBaseConfig,
265
- createNextConfig
435
+ createNextConfig,
436
+ oneLineComments,
437
+ oneLineCommentsPlugin
266
438
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soujvnunes/eslint-config",
3
- "version": "0.5.1",
3
+ "version": "0.7.0",
4
4
  "description": "Shared flat ESLint config factories (base + Next.js).",
5
5
  "license": "MIT",
6
6
  "author": "soujvnunes",