@so1ve/eslint-plugin 0.78.2 → 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();
107
+ },
108
+ ArrowFunctionExpression() {
109
+ setupScope();
87
110
  },
88
- ArrowFunctionExpression(node) {
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) {
@@ -123,6 +149,10 @@ const functionStyle = createEslintRule({
123
149
  });
124
150
  }
125
151
  }
152
+ clearThisAccess();
153
+ },
154
+ ThisExpression() {
155
+ haveThisAccess = currentScope === context.getScope();
126
156
  }
127
157
  };
128
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();
105
+ },
106
+ ArrowFunctionExpression() {
107
+ setupScope();
85
108
  },
86
- ArrowFunctionExpression(node) {
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) {
@@ -121,6 +147,10 @@ const functionStyle = createEslintRule({
121
147
  });
122
148
  }
123
149
  }
150
+ clearThisAccess();
151
+ },
152
+ ThisExpression() {
153
+ haveThisAccess = currentScope === context.getScope();
124
154
  }
125
155
  };
126
156
  }
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.3",
4
4
  "author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
5
5
  "contributors": [
6
6
  {