@so1ve/eslint-plugin 0.93.2 → 0.93.4

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.cjs CHANGED
@@ -23,6 +23,7 @@ function getPreviousNode(node) {
23
23
 
24
24
  const RULE_NAME$6 = "function-style";
25
25
  const START_RETURN = /^return /;
26
+ const RETURN_ONLY = /^return\s*(?:;\s*)?$/;
26
27
  const END_SEMICOLON = /;$/;
27
28
  const functionStyle = createEslintRule({
28
29
  name: RULE_NAME$6,
@@ -43,7 +44,7 @@ const functionStyle = createEslintRule({
43
44
  create: (context) => {
44
45
  const sourceCode = context.getSourceCode();
45
46
  const text = sourceCode.getText();
46
- const getStatementRaw = (statement) => `(${text.slice(statement.range[0], statement.range[1]).replace(START_RETURN, "").replace(END_SEMICOLON, "")})`;
47
+ const getRawStatement = (statement) => `(${text.slice(statement.range[0], statement.range[1]).replace(START_RETURN, "").replace(END_SEMICOLON, "")})`;
47
48
  function getLoneReturnStatement(node) {
48
49
  const { body } = node;
49
50
  if (body.type === types.AST_NODE_TYPES.BlockStatement) {
@@ -62,7 +63,7 @@ const functionStyle = createEslintRule({
62
63
  const params = node.params.map((param) => sourceCode.getText(param)).join(", ");
63
64
  const returnType = node.returnType ? sourceCode.getText(node.returnType) : "";
64
65
  const body = sourceCode.getText(node.body);
65
- const variableDeclaration = asVariable ? `const ${name} = ` : "";
66
+ const variableDeclaration = asVariable && name ? `const ${name} = ` : "";
66
67
  return type === "arrow" ? `${variableDeclaration}${async}${generics}(${params})${returnType} => ${rawStatement};` : `${async}function ${name}${generics}(${params})${returnType} ${body}`;
67
68
  }
68
69
  let currentScope = null;
@@ -106,11 +107,17 @@ const functionStyle = createEslintRule({
106
107
  return;
107
108
  }
108
109
  const statement = getLoneReturnStatement(node);
109
- if (!statement || !node.id?.name || node.generator) {
110
+ const isExportDefault = node.parent?.type === types.AST_NODE_TYPES.ExportDefaultDeclaration;
111
+ if (!statement || !node.id?.name && !isExportDefault || node.generator) {
112
+ clearThisAccess();
113
+ return;
114
+ }
115
+ const rawStatement = getRawStatement(statement);
116
+ const statementWithoutBrackets = rawStatement.slice(1, -1);
117
+ if (RETURN_ONLY.test(statementWithoutBrackets)) {
110
118
  clearThisAccess();
111
119
  return;
112
120
  }
113
- const asVariable = node.parent?.type !== types.AST_NODE_TYPES.ExportDefaultDeclaration;
114
121
  context.report({
115
122
  node,
116
123
  messageId: "arrow",
@@ -118,10 +125,10 @@ const functionStyle = createEslintRule({
118
125
  node.range,
119
126
  generateFunction(
120
127
  "arrow",
121
- node.id.name,
128
+ node.id?.name ?? null,
122
129
  node,
123
- getStatementRaw(statement),
124
- asVariable
130
+ getRawStatement(statement),
131
+ !isExportDefault
125
132
  )
126
133
  )
127
134
  });
@@ -142,7 +149,7 @@ const functionStyle = createEslintRule({
142
149
  messageId: "arrow",
143
150
  fix: (fixer) => fixer.replaceTextRange(
144
151
  node.body.range,
145
- getStatementRaw(statement)
152
+ getRawStatement(statement)
146
153
  )
147
154
  });
148
155
  clearThisAccess();
package/dist/index.mjs CHANGED
@@ -21,6 +21,7 @@ function getPreviousNode(node) {
21
21
 
22
22
  const RULE_NAME$6 = "function-style";
23
23
  const START_RETURN = /^return /;
24
+ const RETURN_ONLY = /^return\s*(?:;\s*)?$/;
24
25
  const END_SEMICOLON = /;$/;
25
26
  const functionStyle = createEslintRule({
26
27
  name: RULE_NAME$6,
@@ -41,7 +42,7 @@ const functionStyle = createEslintRule({
41
42
  create: (context) => {
42
43
  const sourceCode = context.getSourceCode();
43
44
  const text = sourceCode.getText();
44
- const getStatementRaw = (statement) => `(${text.slice(statement.range[0], statement.range[1]).replace(START_RETURN, "").replace(END_SEMICOLON, "")})`;
45
+ const getRawStatement = (statement) => `(${text.slice(statement.range[0], statement.range[1]).replace(START_RETURN, "").replace(END_SEMICOLON, "")})`;
45
46
  function getLoneReturnStatement(node) {
46
47
  const { body } = node;
47
48
  if (body.type === AST_NODE_TYPES.BlockStatement) {
@@ -60,7 +61,7 @@ const functionStyle = createEslintRule({
60
61
  const params = node.params.map((param) => sourceCode.getText(param)).join(", ");
61
62
  const returnType = node.returnType ? sourceCode.getText(node.returnType) : "";
62
63
  const body = sourceCode.getText(node.body);
63
- const variableDeclaration = asVariable ? `const ${name} = ` : "";
64
+ const variableDeclaration = asVariable && name ? `const ${name} = ` : "";
64
65
  return type === "arrow" ? `${variableDeclaration}${async}${generics}(${params})${returnType} => ${rawStatement};` : `${async}function ${name}${generics}(${params})${returnType} ${body}`;
65
66
  }
66
67
  let currentScope = null;
@@ -104,11 +105,17 @@ const functionStyle = createEslintRule({
104
105
  return;
105
106
  }
106
107
  const statement = getLoneReturnStatement(node);
107
- if (!statement || !node.id?.name || node.generator) {
108
+ const isExportDefault = node.parent?.type === AST_NODE_TYPES.ExportDefaultDeclaration;
109
+ if (!statement || !node.id?.name && !isExportDefault || node.generator) {
110
+ clearThisAccess();
111
+ return;
112
+ }
113
+ const rawStatement = getRawStatement(statement);
114
+ const statementWithoutBrackets = rawStatement.slice(1, -1);
115
+ if (RETURN_ONLY.test(statementWithoutBrackets)) {
108
116
  clearThisAccess();
109
117
  return;
110
118
  }
111
- const asVariable = node.parent?.type !== AST_NODE_TYPES.ExportDefaultDeclaration;
112
119
  context.report({
113
120
  node,
114
121
  messageId: "arrow",
@@ -116,10 +123,10 @@ const functionStyle = createEslintRule({
116
123
  node.range,
117
124
  generateFunction(
118
125
  "arrow",
119
- node.id.name,
126
+ node.id?.name ?? null,
120
127
  node,
121
- getStatementRaw(statement),
122
- asVariable
128
+ getRawStatement(statement),
129
+ !isExportDefault
123
130
  )
124
131
  )
125
132
  });
@@ -140,7 +147,7 @@ const functionStyle = createEslintRule({
140
147
  messageId: "arrow",
141
148
  fix: (fixer) => fixer.replaceTextRange(
142
149
  node.body.range,
143
- getStatementRaw(statement)
150
+ getRawStatement(statement)
144
151
  )
145
152
  });
146
153
  clearThisAccess();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@so1ve/eslint-plugin",
3
- "version": "0.93.2",
3
+ "version": "0.93.4",
4
4
  "author": "Ray <i@mk1.io> (https://github.com/so1ve/)",
5
5
  "keywords": [
6
6
  "eslint",