@so1ve/eslint-plugin 0.78.1 → 0.78.3

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
@@ -49,9 +49,21 @@ const functionStyle = createEslintRule({
49
49
  const variableDeclaration = asVariable ? `const ${name} = ` : "";
50
50
  return type === "arrow" ? `${variableDeclaration}${async}${generics}(${params})${returnType} => ${rawStatement};` : `${async}function ${name}${generics}(${params})${returnType} ${body}`;
51
51
  }
52
+ let currentScope = null;
53
+ let haveThisAccess = false;
54
+ function setupScope() {
55
+ currentScope = context.getScope();
56
+ }
57
+ function clearThisAccess() {
58
+ currentScope = null;
59
+ haveThisAccess = false;
60
+ }
52
61
  return {
53
- FunctionExpression(node) {
54
- if (node.parent?.id?.typeAnnotation || node.parent?.type !== types.AST_NODE_TYPES.VariableDeclarator) {
62
+ FunctionExpression() {
63
+ setupScope();
64
+ },
65
+ "FunctionExpression:exit"(node) {
66
+ if (node.parent?.id?.typeAnnotation || node.parent?.type !== types.AST_NODE_TYPES.VariableDeclarator || haveThisAccess) {
55
67
  return;
56
68
  }
57
69
  const name = node.parent.id.name;
@@ -63,8 +75,15 @@ const functionStyle = createEslintRule({
63
75
  generateFunction("declaration", name, node)
64
76
  )
65
77
  });
78
+ clearThisAccess();
66
79
  },
67
- "FunctionDeclaration:not(TSDeclareFunction + FunctionDeclaration)"(node) {
80
+ "FunctionDeclaration:not(TSDeclareFunction + FunctionDeclaration)"() {
81
+ setupScope();
82
+ },
83
+ "FunctionDeclaration:not(TSDeclareFunction + FunctionDeclaration):exit"(node) {
84
+ if (haveThisAccess) {
85
+ return;
86
+ }
68
87
  const statement = getLoneReturnStatement(node);
69
88
  if (!statement || !node.id?.name || node.generator) {
70
89
  return;
@@ -84,8 +103,15 @@ const functionStyle = createEslintRule({
84
103
  )
85
104
  )
86
105
  });
106
+ clearThisAccess();
87
107
  },
88
- ArrowFunctionExpression(node) {
108
+ ArrowFunctionExpression() {
109
+ setupScope();
110
+ },
111
+ "ArrowFunctionExpression:exit"(node) {
112
+ if (haveThisAccess) {
113
+ return;
114
+ }
89
115
  const { body, parent } = node;
90
116
  const statement = getLoneReturnStatement(node);
91
117
  if (statement) {
@@ -97,13 +123,14 @@ const functionStyle = createEslintRule({
97
123
  getStatementRaw(statement)
98
124
  )
99
125
  });
126
+ return;
100
127
  }
101
128
  if (parent?.id?.typeAnnotation) {
102
129
  return;
103
130
  }
104
131
  if (body.type === types.AST_NODE_TYPES.BlockStatement) {
105
132
  const { body: blockBody } = body;
106
- if (blockBody.length > 1 && node.parent?.parent?.type === types.AST_NODE_TYPES.VariableDeclaration) {
133
+ if (blockBody.length > 0 && node.parent?.parent?.type === types.AST_NODE_TYPES.VariableDeclaration) {
107
134
  const { parent: parent2 } = node.parent;
108
135
  context.report({
109
136
  node: parent2,
@@ -122,6 +149,10 @@ const functionStyle = createEslintRule({
122
149
  });
123
150
  }
124
151
  }
152
+ clearThisAccess();
153
+ },
154
+ ThisExpression() {
155
+ haveThisAccess = currentScope === context.getScope();
125
156
  }
126
157
  };
127
158
  }
package/dist/index.mjs CHANGED
@@ -47,9 +47,21 @@ const functionStyle = createEslintRule({
47
47
  const variableDeclaration = asVariable ? `const ${name} = ` : "";
48
48
  return type === "arrow" ? `${variableDeclaration}${async}${generics}(${params})${returnType} => ${rawStatement};` : `${async}function ${name}${generics}(${params})${returnType} ${body}`;
49
49
  }
50
+ let currentScope = null;
51
+ let haveThisAccess = false;
52
+ function setupScope() {
53
+ currentScope = context.getScope();
54
+ }
55
+ function clearThisAccess() {
56
+ currentScope = null;
57
+ haveThisAccess = false;
58
+ }
50
59
  return {
51
- FunctionExpression(node) {
52
- if (node.parent?.id?.typeAnnotation || node.parent?.type !== AST_NODE_TYPES.VariableDeclarator) {
60
+ FunctionExpression() {
61
+ setupScope();
62
+ },
63
+ "FunctionExpression:exit"(node) {
64
+ if (node.parent?.id?.typeAnnotation || node.parent?.type !== AST_NODE_TYPES.VariableDeclarator || haveThisAccess) {
53
65
  return;
54
66
  }
55
67
  const name = node.parent.id.name;
@@ -61,8 +73,15 @@ const functionStyle = createEslintRule({
61
73
  generateFunction("declaration", name, node)
62
74
  )
63
75
  });
76
+ clearThisAccess();
64
77
  },
65
- "FunctionDeclaration:not(TSDeclareFunction + FunctionDeclaration)"(node) {
78
+ "FunctionDeclaration:not(TSDeclareFunction + FunctionDeclaration)"() {
79
+ setupScope();
80
+ },
81
+ "FunctionDeclaration:not(TSDeclareFunction + FunctionDeclaration):exit"(node) {
82
+ if (haveThisAccess) {
83
+ return;
84
+ }
66
85
  const statement = getLoneReturnStatement(node);
67
86
  if (!statement || !node.id?.name || node.generator) {
68
87
  return;
@@ -82,8 +101,15 @@ const functionStyle = createEslintRule({
82
101
  )
83
102
  )
84
103
  });
104
+ clearThisAccess();
85
105
  },
86
- ArrowFunctionExpression(node) {
106
+ ArrowFunctionExpression() {
107
+ setupScope();
108
+ },
109
+ "ArrowFunctionExpression:exit"(node) {
110
+ if (haveThisAccess) {
111
+ return;
112
+ }
87
113
  const { body, parent } = node;
88
114
  const statement = getLoneReturnStatement(node);
89
115
  if (statement) {
@@ -95,13 +121,14 @@ const functionStyle = createEslintRule({
95
121
  getStatementRaw(statement)
96
122
  )
97
123
  });
124
+ return;
98
125
  }
99
126
  if (parent?.id?.typeAnnotation) {
100
127
  return;
101
128
  }
102
129
  if (body.type === AST_NODE_TYPES.BlockStatement) {
103
130
  const { body: blockBody } = body;
104
- if (blockBody.length > 1 && node.parent?.parent?.type === AST_NODE_TYPES.VariableDeclaration) {
131
+ if (blockBody.length > 0 && node.parent?.parent?.type === AST_NODE_TYPES.VariableDeclaration) {
105
132
  const { parent: parent2 } = node.parent;
106
133
  context.report({
107
134
  node: parent2,
@@ -120,6 +147,10 @@ const functionStyle = createEslintRule({
120
147
  });
121
148
  }
122
149
  }
150
+ clearThisAccess();
151
+ },
152
+ ThisExpression() {
153
+ haveThisAccess = currentScope === context.getScope();
123
154
  }
124
155
  };
125
156
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@so1ve/eslint-plugin",
3
- "version": "0.78.1",
3
+ "version": "0.78.3",
4
4
  "author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
5
5
  "contributors": [
6
6
  {