@so1ve/eslint-plugin 3.3.2 → 3.4.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 +64 -35
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -32,28 +32,36 @@ var function_style_default = createEslintRule({
32
32
  defaultOptions: [],
33
33
  create: (context) => {
34
34
  const sourceCode = context.sourceCode;
35
- function getLoneReturnStatement(node) {
35
+ const scopeStack = [];
36
+ let haveThisAccess = false;
37
+ function getSingleReturnStatement(node) {
36
38
  const { body } = node;
37
39
  if (body.type !== AST_NODE_TYPES.BlockStatement) return;
38
40
  const { body: blockBody } = body;
39
- const allComments = sourceCode.getCommentsInside(node);
40
41
  if (blockBody.length !== 1) return;
41
42
  const [statement] = blockBody;
43
+ if (statement?.type !== AST_NODE_TYPES.ReturnStatement) return;
44
+ const allComments = sourceCode.getCommentsInside(node);
42
45
  const statementComments = sourceCode.getCommentsInside(statement);
43
46
  if (allComments.length !== statementComments.length) return;
44
- if (statement?.type === AST_NODE_TYPES.ReturnStatement) return statement;
47
+ return statement;
45
48
  }
46
- function generateFunction(type, name, node, rawStatement, asVariable = true) {
47
- const async = node.async ? "async " : "";
48
- const generics = node.typeParameters ? sourceCode.getText(node.typeParameters) : "";
49
- const params = node.params.map((param) => sourceCode.getText(param)).join(", ");
50
- const returnType = node.returnType ? sourceCode.getText(node.returnType) : "";
51
- const body = sourceCode.getText(node.body);
49
+ const extractFunctionInfo = (node) => ({
50
+ async: node.async,
51
+ generics: node.typeParameters ? sourceCode.getText(node.typeParameters) : "",
52
+ params: node.params.map((param) => sourceCode.getText(param)).join(", "),
53
+ returnType: node.returnType ? sourceCode.getText(node.returnType) : "",
54
+ body: sourceCode.getText(node.body)
55
+ });
56
+ function generateArrowFunction(name, info, returnValue, asVariable = true) {
57
+ const asyncKeyword = info.async ? "async " : "";
52
58
  const variableDeclaration = asVariable && name ? `const ${name} = ` : "";
53
- return type === "arrow" ? `${variableDeclaration}${async}${generics}(${params})${returnType} => ${rawStatement};` : `${async}function ${name}${generics}(${params})${returnType} ${body}`;
59
+ return `${variableDeclaration}${asyncKeyword}${info.generics}(${info.params})${info.returnType} => ${returnValue};`;
60
+ }
61
+ function generateFunctionDeclaration(name, info) {
62
+ const asyncKeyword = info.async ? "async " : "";
63
+ return `${asyncKeyword}function ${name}${info.generics}(${info.params})${info.returnType} ${info.body}`;
54
64
  }
55
- const scopeStack = [];
56
- let haveThisAccess = false;
57
65
  function setupScope(node) {
58
66
  scopeStack.push(sourceCode.getScope(node));
59
67
  }
@@ -61,37 +69,56 @@ var function_style_default = createEslintRule({
61
69
  scopeStack.pop();
62
70
  haveThisAccess = false;
63
71
  }
72
+ function getFunctionExpressionParentNameString(node) {
73
+ const parent = node.parent;
74
+ if (parent?.id?.typeAnnotation || parent?.type !== AST_NODE_TYPES.VariableDeclarator || haveThisAccess) return null;
75
+ return parent.id.name;
76
+ }
77
+ function shouldConvertFunctionDeclarationToArrow(node) {
78
+ if (haveThisAccess) return { shouldConvert: false };
79
+ const previousNode = getPreviousNode(node.parent);
80
+ if (previousNode?.type === AST_NODE_TYPES.ExportNamedDeclaration && previousNode.declaration?.type === AST_NODE_TYPES.TSDeclareFunction) return { shouldConvert: false };
81
+ const returnStatement = getSingleReturnStatement(node);
82
+ const isExportDefault = node.parent?.type === AST_NODE_TYPES.ExportDefaultDeclaration;
83
+ if (!returnStatement?.argument || !node.id?.name && !isExportDefault || node.generator) return { shouldConvert: false };
84
+ return {
85
+ shouldConvert: true,
86
+ returnStatement
87
+ };
88
+ }
64
89
  return {
65
90
  "FunctionExpression": setupScope,
66
91
  "FunctionExpression:exit"(node) {
67
- if (node.parent?.id?.typeAnnotation || node.parent?.type !== AST_NODE_TYPES.VariableDeclarator || haveThisAccess) {
92
+ const name = getFunctionExpressionParentNameString(node);
93
+ if (!name) {
68
94
  clearThisAccess();
69
95
  return;
70
96
  }
71
- const name = node.parent.id.name;
97
+ const info = extractFunctionInfo(node);
98
+ const grandParent = node.parent?.parent;
99
+ if (!grandParent) return;
72
100
  context.report({
73
101
  node,
74
102
  messageId: "declaration",
75
- fix: (fixer) => fixer.replaceText(node.parent.parent, generateFunction("declaration", name, node))
103
+ fix: (fixer) => fixer.replaceText(grandParent, generateFunctionDeclaration(name, info))
76
104
  });
77
105
  clearThisAccess();
78
106
  },
79
107
  "FunctionDeclaration:not(TSDeclareFunction + FunctionDeclaration)": setupScope,
80
108
  "FunctionDeclaration:not(TSDeclareFunction + FunctionDeclaration):exit"(node) {
81
109
  if (haveThisAccess) return;
82
- const previousNode = getPreviousNode(node.parent);
83
- if (previousNode?.type === AST_NODE_TYPES.ExportNamedDeclaration && previousNode.declaration?.type === AST_NODE_TYPES.TSDeclareFunction) return;
84
- const statement = getLoneReturnStatement(node);
85
- const isExportDefault = node.parent?.type === AST_NODE_TYPES.ExportDefaultDeclaration;
86
- if (!statement?.argument || !node.id?.name && !isExportDefault || node.generator) {
110
+ const { shouldConvert, returnStatement } = shouldConvertFunctionDeclarationToArrow(node);
111
+ if (!shouldConvert || !returnStatement?.argument) {
87
112
  clearThisAccess();
88
113
  return;
89
114
  }
90
- const returnVal = `(${sourceCode.getText(statement.argument)})`;
115
+ const info = extractFunctionInfo(node);
116
+ const isExportDefault = node.parent?.type === AST_NODE_TYPES.ExportDefaultDeclaration;
117
+ const returnValue = `(${sourceCode.getText(returnStatement.argument)})`;
91
118
  context.report({
92
119
  node,
93
120
  messageId: "arrow",
94
- fix: (fixer) => fixer.replaceText(node, generateFunction("arrow", node.id?.name ?? null, node, returnVal, !isExportDefault))
121
+ fix: (fixer) => fixer.replaceText(node, generateArrowFunction(node.id?.name ?? null, info, returnValue, !isExportDefault))
95
122
  });
96
123
  clearThisAccess();
97
124
  },
@@ -99,22 +126,24 @@ var function_style_default = createEslintRule({
99
126
  "ArrowFunctionExpression:exit"(node) {
100
127
  if (haveThisAccess) return;
101
128
  const { body, parent } = node;
102
- const statement = getLoneReturnStatement(node);
103
- if (statement?.argument) {
104
- const returnVal = `(${sourceCode.getText(statement.argument)})`;
129
+ const returnStatement = getSingleReturnStatement(node);
130
+ if (returnStatement?.argument) {
131
+ const returnValue = `(${sourceCode.getText(returnStatement.argument)})`;
105
132
  context.report({
106
133
  node,
107
134
  messageId: "arrow",
108
- fix: (fixer) => fixer.replaceText(node.body, returnVal)
135
+ fix: (fixer) => fixer.replaceText(node.body, returnValue)
109
136
  });
110
137
  } else if (body.type === AST_NODE_TYPES.BlockStatement && !parent?.id?.typeAnnotation) {
111
138
  const { body: blockBody } = body;
112
139
  if (blockBody.length > 0 && node.parent?.parent?.type === AST_NODE_TYPES.VariableDeclaration) {
113
- const { parent: grandParent } = node.parent;
140
+ const grandParent = node.parent.parent;
141
+ const name = node.parent.id.name;
142
+ const info = extractFunctionInfo(node);
114
143
  context.report({
115
144
  node: grandParent,
116
145
  messageId: "declaration",
117
- fix: (fixer) => fixer.replaceText(grandParent, generateFunction("declaration", node.parent.id.name, node))
146
+ fix: (fixer) => fixer.replaceText(grandParent, generateFunctionDeclaration(name, info))
118
147
  });
119
148
  }
120
149
  }
@@ -168,13 +197,12 @@ var import_dedupe_default = createEslintRule({
168
197
  //#endregion
169
198
  //#region src/rules/no-import-promises-as.ts
170
199
  const RULE_NAME$6 = "no-import-promises-as";
171
- const _POSSIBLE_IMPORT_SOURCES = [
200
+ const POSSIBLE_IMPORT_SOURCES = [
172
201
  "dns",
173
202
  "fs",
174
203
  "readline",
175
204
  "stream"
176
- ];
177
- const POSSIBLE_IMPORT_SOURCES = [..._POSSIBLE_IMPORT_SOURCES, ..._POSSIBLE_IMPORT_SOURCES.map((s) => `node:${s}`)];
205
+ ].flatMap((s) => [s, `node:${s}`]);
178
206
  var no_import_promises_as_default = createEslintRule({
179
207
  name: RULE_NAME$6,
180
208
  meta: {
@@ -452,10 +480,11 @@ var vue_root_element_sort_attributes_default = createEslintRule({
452
480
  node: element.startTag,
453
481
  messageId: "wrongOrder",
454
482
  *fix(fixer) {
455
- const sortedAttributes = attributesToCheck.sort((a, b) => expectedOrder.indexOf(a.key.name) - expectedOrder.indexOf(b.key.name));
456
- const attributeText = ` ${sortedAttributes.map((attr) => sourceCode.getText(attr)).join(" ")}`;
457
- for (const attribute of sortedAttributes) yield fixer.remove(attribute);
458
- yield fixer.replaceTextRange([element.startTag.range[1] - 1, element.startTag.range[1] - 1], attributeText);
483
+ const sortedAttributes = [...attributesToCheck].sort((a, b) => expectedOrder.indexOf(a.key.name) - expectedOrder.indexOf(b.key.name));
484
+ for (const [i, originalAttr] of attributesToCheck.entries()) {
485
+ const sortedAttr = sortedAttributes[i];
486
+ if (originalAttr.key.name !== sortedAttr.key.name) yield fixer.replaceText(originalAttr, sourceCode.getText(sortedAttr));
487
+ }
459
488
  }
460
489
  });
461
490
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@so1ve/eslint-plugin",
3
- "version": "3.3.2",
3
+ "version": "3.4.0",
4
4
  "author": "Ray <i@mk1.io> (https://github.com/so1ve/)",
5
5
  "type": "module",
6
6
  "keywords": [