@so1ve/eslint-plugin 3.23.0 → 3.25.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.mjs +33 -23
  2. package/package.json +1 -1
package/dist/index.mjs CHANGED
@@ -259,23 +259,26 @@ const rule$7 = createEslintRule({
259
259
  function checkExport(node) {
260
260
  exportNodes.push(node);
261
261
  }
262
- function shouldHaveNewline(node, direction) {
262
+ function checkNewline(node, direction) {
263
263
  let token;
264
- let commentLine;
265
264
  let expectedLine;
266
265
  let tokenLine;
267
266
  if (direction === "after") {
268
- token = sourceCode.getTokenAfter(node);
269
- commentLine = sourceCode.getCommentsAfter(node)[0]?.loc.start.line;
270
- expectedLine = node.loc.end.line + 1;
267
+ const endNode = sourceCode.getCommentsAfter(node).find((c) => c.loc.start.line === node.loc.end.line) ?? node;
268
+ token = sourceCode.getTokenAfter(endNode);
269
+ const nextComment = sourceCode.getCommentsAfter(endNode)[0];
270
+ expectedLine = endNode.loc.end.line + 1;
271
271
  tokenLine = token?.loc.start.line;
272
- } else {
273
- token = sourceCode.getTokenBefore(node);
274
- commentLine = sourceCode.getCommentsBefore(node)[0]?.loc.end.line;
275
- expectedLine = node.loc.start.line - 1;
276
- tokenLine = token?.loc.end.line;
272
+ const commentLine = nextComment?.loc.start.line;
273
+ if (token && (expectedLine === tokenLine || expectedLine === commentLine || tokenLine === endNode.loc.end.line) && token.value !== "}" && token.value !== "<\/script>") return endNode;
274
+ return null;
277
275
  }
278
- return token && (expectedLine === tokenLine || expectedLine === commentLine) && (direction === "after" ? token.value !== "}" && token.value !== "<\/script>" : true);
276
+ const startNode = sourceCode.getCommentsBefore(node)[0] || node;
277
+ token = sourceCode.getTokenBefore(startNode);
278
+ expectedLine = startNode.loc.start.line - 1;
279
+ tokenLine = token?.loc.end.line;
280
+ if (token && expectedLine === tokenLine) return startNode;
281
+ return null;
279
282
  }
280
283
  return {
281
284
  ImportDeclaration(node) {
@@ -285,24 +288,31 @@ const rule$7 = createEslintRule({
285
288
  ExportDefaultDeclaration: checkExport,
286
289
  ExportAllDeclaration: checkExport,
287
290
  "Program:exit"() {
288
- if (lastImportNode && shouldHaveNewline(lastImportNode, "after")) context.report({
291
+ const lastImportFixNode = lastImportNode ? checkNewline(lastImportNode, "after") : null;
292
+ if (lastImportNode && lastImportFixNode) context.report({
289
293
  node: lastImportNode,
290
294
  messageId: "newlineAfterLastImport",
291
- fix: (fixer) => fixer.insertTextAfter(lastImportNode, "\n")
295
+ fix: (fixer) => fixer.insertTextAfter(lastImportFixNode, "\n")
292
296
  });
293
297
  for (const node of exportNodes) {
294
298
  const prevNode = getPreviousNode(node);
295
- if ((!prevNode || !isExportDeclaration(prevNode)) && !(lastImportNode && prevNode === lastImportNode) && shouldHaveNewline(node, "before")) context.report({
296
- node,
297
- messageId: "newlineBeforeExport",
298
- fix: (fixer) => fixer.insertTextBefore(node, "\n")
299
- });
299
+ if ((!prevNode || !isExportDeclaration(prevNode)) && (!lastImportNode || prevNode !== lastImportNode)) {
300
+ const beforeFixNode = checkNewline(node, "before");
301
+ if (beforeFixNode) context.report({
302
+ node,
303
+ messageId: "newlineBeforeExport",
304
+ fix: (fixer) => fixer.insertTextBefore(beforeFixNode, "\n")
305
+ });
306
+ }
300
307
  const nextNode = getNextNode(node);
301
- if (nextNode && !isExportDeclaration(nextNode) && shouldHaveNewline(node, "after")) context.report({
302
- node,
303
- messageId: "newlineAfterExport",
304
- fix: (fixer) => fixer.insertTextAfter(node, "\n")
305
- });
308
+ if (nextNode && !isExportDeclaration(nextNode)) {
309
+ const afterFixNode = checkNewline(node, "after");
310
+ if (afterFixNode) context.report({
311
+ node,
312
+ messageId: "newlineAfterExport",
313
+ fix: (fixer) => fixer.insertTextAfter(afterFixNode, "\n")
314
+ });
315
+ }
306
316
  }
307
317
  }
308
318
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@so1ve/eslint-plugin",
3
- "version": "3.23.0",
3
+ "version": "3.25.0",
4
4
  "author": "Ray <i@mk1.io> (https://github.com/so1ve/)",
5
5
  "type": "module",
6
6
  "keywords": [