@soujvnunes/eslint-config 0.6.0 → 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.
Files changed (2) hide show
  1. package/dist/index.js +107 -48
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -15,89 +15,148 @@ 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;
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) ? "/*!" : "/*";
29
39
  };
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() === "";
38
- };
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);
44
- };
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
- );
40
+ var collapsedBlock = (comment) => {
41
+ const open = opener(comment);
42
+ const body = blockBody(comment);
51
43
  const text = body ? `${open} ${body} */` : `${open} */`;
52
- return isOneLine(text) && text.indexOf("*/") === text.length - 2 ? text : null;
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);
53
50
  };
54
51
  var oneLineComments = {
55
52
  meta: {
56
53
  type: "layout",
57
- docs: { description: "Require a comment to occupy exactly one line, however long it runs." },
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
+ },
58
57
  fixable: "code",
59
58
  schema: [],
60
59
  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."
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 `//`."
63
65
  }
64
66
  },
65
67
  create(context) {
66
68
  const sourceCode = context.sourceCode;
67
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
+ };
68
104
  return {
69
105
  "Program:exit"() {
106
+ const docs = /* @__PURE__ */ new Set();
70
107
  for (const comment of comments) {
71
- if (!spansLines(comment) || isDirective(comment)) continue;
72
- const text = collapsedBlock(sourceCode, comment);
73
- const risky = breaksSemicolonInsertion(sourceCode, comment);
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);
74
126
  context.report({
75
127
  loc: comment.loc,
76
- messageId: "block",
77
- fix: text && !risky ? (fixer) => fixer.replaceText(comment, text) : null
128
+ messageId: "notDoc",
129
+ fix: text ? (fixer) => fixer.replaceText(comment, text) : null
78
130
  });
79
131
  }
80
132
  const joinable = comments.filter(
81
- (comment) => !spansLines(comment) && isOwnLine(sourceCode, comment)
133
+ (comment) => !spansLines(comment) && !docs.has(comment) && isOwnLine(comment)
82
134
  );
83
135
  let run = [];
84
136
  const flush = () => {
85
137
  const current = run;
86
138
  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}`;
139
+ if (current.length < 2 || current.some(isDirective)) return;
93
140
  const first = current[0];
94
141
  const last = current[current.length - 1];
95
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);
96
156
  context.report({
97
157
  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)
158
+ messageId: paragraphs ? "paragraphs" : "adjacent",
159
+ fix: paragraphs || block || !text ? null : (fixer) => fixer.replaceTextRange([first.range?.[0] ?? 0, last.range?.[1] ?? 0], text)
101
160
  });
102
161
  };
103
162
  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.0",
4
4
  "description": "Shared flat ESLint config factories (base + Next.js).",
5
5
  "license": "MIT",
6
6
  "author": "soujvnunes",