@so1ve/eslint-plugin 0.78.2 → 0.78.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
@@ -49,9 +49,22 @@ 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) {
67
+ clearThisAccess();
55
68
  return;
56
69
  }
57
70
  const name = node.parent.id.name;
@@ -63,10 +76,18 @@ const functionStyle = createEslintRule({
63
76
  generateFunction("declaration", name, node)
64
77
  )
65
78
  });
79
+ clearThisAccess();
66
80
  },
67
- "FunctionDeclaration:not(TSDeclareFunction + FunctionDeclaration)"(node) {
81
+ "FunctionDeclaration:not(TSDeclareFunction + FunctionDeclaration)"() {
82
+ setupScope();
83
+ },
84
+ "FunctionDeclaration:not(TSDeclareFunction + FunctionDeclaration):exit"(node) {
85
+ if (haveThisAccess) {
86
+ return;
87
+ }
68
88
  const statement = getLoneReturnStatement(node);
69
89
  if (!statement || !node.id?.name || node.generator) {
90
+ clearThisAccess();
70
91
  return;
71
92
  }
72
93
  const asVariable = node.parent?.type !== types.AST_NODE_TYPES.ExportDefaultDeclaration;
@@ -84,8 +105,15 @@ const functionStyle = createEslintRule({
84
105
  )
85
106
  )
86
107
  });
108
+ clearThisAccess();
109
+ },
110
+ ArrowFunctionExpression() {
111
+ setupScope();
87
112
  },
88
- ArrowFunctionExpression(node) {
113
+ "ArrowFunctionExpression:exit"(node) {
114
+ if (haveThisAccess) {
115
+ return;
116
+ }
89
117
  const { body, parent } = node;
90
118
  const statement = getLoneReturnStatement(node);
91
119
  if (statement) {
@@ -97,9 +125,11 @@ const functionStyle = createEslintRule({
97
125
  getStatementRaw(statement)
98
126
  )
99
127
  });
128
+ clearThisAccess();
100
129
  return;
101
130
  }
102
131
  if (parent?.id?.typeAnnotation) {
132
+ clearThisAccess();
103
133
  return;
104
134
  }
105
135
  if (body.type === types.AST_NODE_TYPES.BlockStatement) {
@@ -123,6 +153,10 @@ const functionStyle = createEslintRule({
123
153
  });
124
154
  }
125
155
  }
156
+ clearThisAccess();
157
+ },
158
+ ThisExpression() {
159
+ haveThisAccess = currentScope === context.getScope();
126
160
  }
127
161
  };
128
162
  }
package/dist/index.mjs CHANGED
@@ -47,9 +47,22 @@ 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) {
65
+ clearThisAccess();
53
66
  return;
54
67
  }
55
68
  const name = node.parent.id.name;
@@ -61,10 +74,18 @@ const functionStyle = createEslintRule({
61
74
  generateFunction("declaration", name, node)
62
75
  )
63
76
  });
77
+ clearThisAccess();
64
78
  },
65
- "FunctionDeclaration:not(TSDeclareFunction + FunctionDeclaration)"(node) {
79
+ "FunctionDeclaration:not(TSDeclareFunction + FunctionDeclaration)"() {
80
+ setupScope();
81
+ },
82
+ "FunctionDeclaration:not(TSDeclareFunction + FunctionDeclaration):exit"(node) {
83
+ if (haveThisAccess) {
84
+ return;
85
+ }
66
86
  const statement = getLoneReturnStatement(node);
67
87
  if (!statement || !node.id?.name || node.generator) {
88
+ clearThisAccess();
68
89
  return;
69
90
  }
70
91
  const asVariable = node.parent?.type !== AST_NODE_TYPES.ExportDefaultDeclaration;
@@ -82,8 +103,15 @@ const functionStyle = createEslintRule({
82
103
  )
83
104
  )
84
105
  });
106
+ clearThisAccess();
107
+ },
108
+ ArrowFunctionExpression() {
109
+ setupScope();
85
110
  },
86
- ArrowFunctionExpression(node) {
111
+ "ArrowFunctionExpression:exit"(node) {
112
+ if (haveThisAccess) {
113
+ return;
114
+ }
87
115
  const { body, parent } = node;
88
116
  const statement = getLoneReturnStatement(node);
89
117
  if (statement) {
@@ -95,9 +123,11 @@ const functionStyle = createEslintRule({
95
123
  getStatementRaw(statement)
96
124
  )
97
125
  });
126
+ clearThisAccess();
98
127
  return;
99
128
  }
100
129
  if (parent?.id?.typeAnnotation) {
130
+ clearThisAccess();
101
131
  return;
102
132
  }
103
133
  if (body.type === AST_NODE_TYPES.BlockStatement) {
@@ -121,6 +151,10 @@ const functionStyle = createEslintRule({
121
151
  });
122
152
  }
123
153
  }
154
+ clearThisAccess();
155
+ },
156
+ ThisExpression() {
157
+ haveThisAccess = currentScope === context.getScope();
124
158
  }
125
159
  };
126
160
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@so1ve/eslint-plugin",
3
- "version": "0.78.2",
3
+ "version": "0.78.4",
4
4
  "author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
5
5
  "contributors": [
6
6
  {