@soujvnunes/eslint-config 0.6.0 → 0.7.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 (2) hide show
  1. package/dist/index.js +116 -47
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -15,89 +15,158 @@ import globals from "globals";
15
15
  import tseslint from "typescript-eslint";
16
16
 
17
17
  // oneLineComments.ts
18
- var DIRECTIVE = /^\s*(?:eslint-disable\b|eslint-enable\b|@ts-expect-error|@ts-ignore|@ts-nocheck|@ts-check\b|\/\s*<reference|prettier-ignore|biome-ignore|(?:istanbul|c8|v8|node:coverage)\s+ignore\b|@jsx\b|@jsxImportSource\b|@jsxRuntime\b|@vitest-environment\b|vitest-environment\b|@vite-ignore\b|webpack[A-Z]|@license\b|@preserve\b|@__PURE__|#__PURE__|#__NO_SIDE_EFFECTS__|sourceMappingURL=|sourceURL=)/u;
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
19
  var BLOCK_DIRECTIVE = /^\s*(?:eslint\s|eslint-env\b|global\s|globals\s|exported\s)/u;
20
- var isComment = (comment) => comment.type === "Line" || comment.type === "Block";
21
- var isDirective = (comment) => DIRECTIVE.test(comment.value) || comment.type === "Block" && BLOCK_DIRECTIVE.test(comment.value);
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));
22
28
  var spansLines = (comment) => (comment.loc?.start.line ?? 0) !== (comment.loc?.end.line ?? 0);
23
- var isOneLine = (text) => !/[\r\n\u2028\u2029]/u.test(text);
29
+ var isBare = (comment) => comment.type === "Line" && comment.value.trim() === "";
24
30
  var collapse = (text) => text.replace(/\s+/gu, " ").trim();
25
- var enclosingJsxComment = (sourceCode, comment) => {
26
- if (comment.type !== "Block") return null;
27
- const node = sourceCode.getNodeByRangeIndex(comment.range?.[0] ?? 0);
28
- return node?.type === "JSXEmptyExpression" ? node.parent ?? null : null;
29
- };
30
- var isOwnLine = (sourceCode, comment) => {
31
- const container = enclosingJsxComment(sourceCode, comment);
32
- const span = container?.loc ?? comment.loc;
33
- if (!span) return false;
34
- const lines = sourceCode.getLines();
35
- const before = (lines[span.start.line - 1] ?? "").slice(0, span.start.column);
36
- const after = (lines[span.end.line - 1] ?? "").slice(span.end.column);
37
- return before.trim() === "" && after.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 blockLines = (comment) => comment.value.replace(/^!/u, "").split(/\r\n|[\r\n\u2028\u2029]/u).map(
34
+ (line) => line.trim().replace(/^\*(?= |$)/u, "").trim()
35
+ );
36
+ var blockBody = (lines) => collapse(lines.filter(Boolean).join(" "));
37
+ var proseParagraphs = (lines, skipTags) => {
38
+ let count = 0;
39
+ let inSegment = false;
40
+ for (const line of lines) {
41
+ if (line === "") {
42
+ inSegment = false;
43
+ continue;
44
+ }
45
+ if (inSegment) continue;
46
+ inSegment = true;
47
+ if (!(skipTags && line.startsWith("@"))) count += 1;
48
+ }
49
+ return count;
38
50
  };
39
- var breaksSemicolonInsertion = (sourceCode, comment) => {
40
- const before = sourceCode.getTokenBefore(comment, { includeComments: true });
41
- const after = sourceCode.getTokenAfter(comment, { includeComments: true });
42
- if (!before?.loc || !after?.loc) return false;
43
- return before.loc.end.line === (comment.loc?.start.line ?? 0) && after.loc.start.line === (comment.loc?.end.line ?? 0);
51
+ var opener = (comment) => {
52
+ if (isDocShaped(comment)) return "/**";
53
+ return isBanner(comment) ? "/*!" : "/*";
44
54
  };
45
- var collapsedBlock = (sourceCode, comment) => {
46
- const raw = sourceCode.getText(comment);
47
- const open = raw.startsWith("/**") || raw.startsWith("/*!") ? raw.slice(0, 3) : "/*";
48
- const body = collapse(
49
- raw.slice(open.length, -2).split(/\r\n|[\r\n\u2028\u2029]/u).map((line) => line.trim().replace(/^\*+ ?/u, "")).filter(Boolean).join(" ")
50
- );
55
+ var collapsedBlock = (comment, lines) => {
56
+ const open = opener(comment);
57
+ const body = blockBody(lines);
51
58
  const text = body ? `${open} ${body} */` : `${open} */`;
52
- return isOneLine(text) && text.indexOf("*/") === text.length - 2 ? text : null;
59
+ return text.indexOf("*/") === text.length - 2 ? text : null;
60
+ };
61
+ var isJsDoc = (comment, after) => {
62
+ if (!isComment(after)) return true;
63
+ const next = after;
64
+ return isDirective(next) || isDocShaped(next) && /^\*+\s*@/u.test(comment.value);
53
65
  };
54
66
  var oneLineComments = {
55
67
  meta: {
56
68
  type: "layout",
57
- docs: { description: "Require a comment to occupy exactly one line, however long it runs." },
69
+ docs: {
70
+ description: "Require a comment to occupy exactly one line, however long it runs, and reserve block comments for JSDoc and JSX."
71
+ },
58
72
  fixable: "code",
59
73
  schema: [],
60
74
  messages: {
61
- adjacent: "Two comment lines are touching, and a bare `//` between them does not make them two comments. A comment is one line, however long it runs: join them, 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, or move the rationale to the README or the project MANIFEST."
75
+ 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.",
76
+ paragraphs: "A paragraph break inside a comment, a bare `//` between comment lines or an empty line inside a block, makes it 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.",
77
+ 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.",
78
+ 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.",
79
+ 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 `//`."
63
80
  }
64
81
  },
65
82
  create(context) {
66
83
  const sourceCode = context.sourceCode;
67
84
  const comments = sourceCode.getAllComments().filter(isComment);
85
+ const enclosing = /* @__PURE__ */ new Map();
86
+ const enclosingNode = (comment) => {
87
+ const known = enclosing.get(comment);
88
+ if (known !== void 0) return known;
89
+ const node = sourceCode.getNodeByRangeIndex(comment.range?.[0] ?? 0);
90
+ enclosing.set(comment, node);
91
+ return node;
92
+ };
93
+ const insideJsx = (comment) => enclosingNode(comment)?.type?.startsWith("JSX") ?? false;
94
+ const jsxContainer = (comment) => {
95
+ const node = comment.type === "Block" ? enclosingNode(comment) : null;
96
+ return node?.type === "JSXEmptyExpression" ? node.parent ?? null : null;
97
+ };
98
+ const isOwnLine = (comment) => {
99
+ const span = jsxContainer(comment)?.loc ?? comment.loc;
100
+ if (!span) return false;
101
+ const lines = sourceCode.getLines();
102
+ const before = (lines[span.start.line - 1] ?? "").slice(0, span.start.column);
103
+ const after = (lines[span.end.line - 1] ?? "").slice(span.end.column);
104
+ return before.trim() === "" && after.trim() === "";
105
+ };
106
+ const breaksSemicolonInsertion = (comment, after) => {
107
+ if (insideJsx(comment)) return false;
108
+ const before = sourceCode.getTokenBefore(comment, { includeComments: true });
109
+ return !!before?.loc && before.loc.end.line === comment.loc?.start.line && followedOnLine(comment, after);
110
+ };
111
+ const blockVerdict = (comment, after) => {
112
+ const lines = blockLines(comment);
113
+ const keepBlock = insideJsx(comment) || isDocShaped(comment) || isBanner(comment);
114
+ const messageId = spansLines(comment) ? "block" : "notDoc";
115
+ if (!insideJsx(comment) && !isBanner(comment) && proseParagraphs(lines, isDocShaped(comment)) > 1) {
116
+ return { messageId: "paragraphs", text: null };
117
+ }
118
+ if (keepBlock) return { messageId, text: collapsedBlock(comment, lines) };
119
+ const body = blockBody(lines);
120
+ const text = followedOnLine(comment, after) || isDirectiveText(body) ? null : asLine(body);
121
+ return { messageId, text };
122
+ };
68
123
  return {
69
124
  "Program:exit"() {
125
+ const docs = /* @__PURE__ */ new Set();
70
126
  for (const comment of comments) {
71
- if (!spansLines(comment) || isDirective(comment)) continue;
72
- const text = collapsedBlock(sourceCode, comment);
73
- const risky = breaksSemicolonInsertion(sourceCode, comment);
127
+ if (comment.type !== "Block" || isDirective(comment)) continue;
128
+ const after = sourceCode.getTokenAfter(comment, { includeComments: true });
129
+ if (spansLines(comment)) {
130
+ const { messageId: messageId2, text: text2 } = blockVerdict(comment, after);
131
+ context.report({
132
+ loc: comment.loc,
133
+ messageId: messageId2,
134
+ fix: text2 && !breaksSemicolonInsertion(comment, after) ? (fixer) => fixer.replaceText(comment, text2) : null
135
+ });
136
+ continue;
137
+ }
138
+ if (insideJsx(comment) || isBanner(comment)) continue;
139
+ if (isDocShaped(comment)) {
140
+ if (isJsDoc(comment, after)) docs.add(comment);
141
+ else context.report({ loc: comment.loc, messageId: "orphanDoc" });
142
+ continue;
143
+ }
144
+ const { messageId, text } = blockVerdict(comment, after);
74
145
  context.report({
75
146
  loc: comment.loc,
76
- messageId: "block",
77
- fix: text && !risky ? (fixer) => fixer.replaceText(comment, text) : null
147
+ messageId,
148
+ fix: text ? (fixer) => fixer.replaceText(comment, text) : null
78
149
  });
79
150
  }
80
151
  const joinable = comments.filter(
81
- (comment) => !spansLines(comment) && isOwnLine(sourceCode, comment)
152
+ (comment) => !spansLines(comment) && !docs.has(comment) && isOwnLine(comment)
82
153
  );
83
154
  let run = [];
84
155
  const flush = () => {
85
156
  const current = run;
86
157
  run = [];
87
- if (current.length < 2) return;
88
- if (current.some(isDirective)) return;
89
- const jsx = current.some((comment) => enclosingJsxComment(sourceCode, comment));
90
- const block = current.some((comment) => comment.type === "Block");
91
- const body = collapse(current.map((comment) => comment.value.trim()).join(" "));
92
- const text = `// ${body}`;
158
+ if (current.length < 2 || current.some(isDirective)) return;
93
159
  const first = current[0];
94
160
  const last = current[current.length - 1];
95
161
  if (!first?.loc || !last?.loc) return;
162
+ const block = current.some((comment) => comment.type === "Block");
163
+ const texts = current.map((comment) => isBare(comment) ? "" : comment.value.trim() || " ");
164
+ const paragraphs = proseParagraphs(texts, false) > 1;
165
+ const text = asLine(collapse(texts.join(" ")));
96
166
  context.report({
97
167
  loc: { start: first.loc.start, end: last.loc.end },
98
- messageId: "adjacent",
99
- // A JSX run reports without a fix: merging `{/* a */}` with `{/* b */}` means editing sibling containers, and raw range surgery there can swallow a value-bearing expression.
100
- fix: jsx || block || !body || !isOneLine(text) ? null : (fixer) => fixer.replaceTextRange([first.range?.[0] ?? 0, last.range?.[1] ?? 0], text)
168
+ messageId: paragraphs ? "paragraphs" : "adjacent",
169
+ fix: paragraphs || block || !text ? null : (fixer) => fixer.replaceTextRange([first.range?.[0] ?? 0, last.range?.[1] ?? 0], text)
101
170
  });
102
171
  };
103
172
  for (const comment of joinable) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soujvnunes/eslint-config",
3
- "version": "0.6.0",
3
+ "version": "0.7.1",
4
4
  "description": "Shared flat ESLint config factories (base + Next.js).",
5
5
  "license": "MIT",
6
6
  "author": "soujvnunes",