@so1ve/eslint-plugin 1.0.0-alpha.6 → 1.0.0-alpha.8

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
@@ -22,9 +22,6 @@ function getPreviousNode(node) {
22
22
  }
23
23
 
24
24
  const RULE_NAME$7 = "function-style";
25
- const START_RETURN = /^return /;
26
- const RETURN_ONLY = /^return\s*(?:;\s*)?$/;
27
- const END_SEMICOLON = /;$/;
28
25
  const functionStyle = createEslintRule({
29
26
  name: RULE_NAME$7,
30
27
  meta: {
@@ -43,8 +40,6 @@ const functionStyle = createEslintRule({
43
40
  defaultOptions: [],
44
41
  create: (context) => {
45
42
  const sourceCode = context.getSourceCode();
46
- const text = sourceCode.getText();
47
- const getRawStatement = (statement) => `(${text.slice(statement.range[0], statement.range[1]).replace(START_RETURN, "").replace(END_SEMICOLON, "")})`;
48
43
  function getLoneReturnStatement(node) {
49
44
  const { body } = node;
50
45
  if (body.type !== types.AST_NODE_TYPES.BlockStatement) {
@@ -93,8 +88,8 @@ const functionStyle = createEslintRule({
93
88
  context.report({
94
89
  node,
95
90
  messageId: "declaration",
96
- fix: (fixer) => fixer.replaceTextRange(
97
- node.parent.parent.range,
91
+ fix: (fixer) => fixer.replaceText(
92
+ node.parent.parent,
98
93
  generateFunction("declaration", name, node)
99
94
  )
100
95
  });
@@ -111,26 +106,21 @@ const functionStyle = createEslintRule({
111
106
  }
112
107
  const statement = getLoneReturnStatement(node);
113
108
  const isExportDefault = node.parent?.type === types.AST_NODE_TYPES.ExportDefaultDeclaration;
114
- if (!statement || !node.id?.name && !isExportDefault || node.generator) {
115
- clearThisAccess();
116
- return;
117
- }
118
- const rawStatement = getRawStatement(statement);
119
- const statementWithoutBrackets = rawStatement.slice(1, -1);
120
- if (RETURN_ONLY.test(statementWithoutBrackets)) {
109
+ if (!statement?.argument || !node.id?.name && !isExportDefault || node.generator) {
121
110
  clearThisAccess();
122
111
  return;
123
112
  }
113
+ const returnVal = `(${sourceCode.getText(statement.argument)})`;
124
114
  context.report({
125
115
  node,
126
116
  messageId: "arrow",
127
- fix: (fixer) => fixer.replaceTextRange(
128
- node.range,
117
+ fix: (fixer) => fixer.replaceText(
118
+ node,
129
119
  generateFunction(
130
120
  "arrow",
131
121
  node.id?.name ?? null,
132
122
  node,
133
- getRawStatement(statement),
123
+ returnVal,
134
124
  !isExportDefault
135
125
  )
136
126
  )
@@ -144,31 +134,22 @@ const functionStyle = createEslintRule({
144
134
  }
145
135
  const { body, parent } = node;
146
136
  const statement = getLoneReturnStatement(node);
147
- if (statement) {
137
+ if (statement?.argument) {
138
+ const returnVal = `(${sourceCode.getText(statement.argument)})`;
148
139
  context.report({
149
140
  node,
150
141
  messageId: "arrow",
151
- fix: (fixer) => fixer.replaceTextRange(
152
- node.body.range,
153
- getRawStatement(statement)
154
- )
142
+ fix: (fixer) => fixer.replaceText(node.body, returnVal)
155
143
  });
156
- clearThisAccess();
157
- return;
158
- }
159
- if (parent?.id?.typeAnnotation) {
160
- clearThisAccess();
161
- return;
162
- }
163
- if (body.type === types.AST_NODE_TYPES.BlockStatement) {
144
+ } else if (body.type === types.AST_NODE_TYPES.BlockStatement && !parent?.id?.typeAnnotation) {
164
145
  const { body: blockBody } = body;
165
146
  if (blockBody.length > 0 && node.parent?.parent?.type === types.AST_NODE_TYPES.VariableDeclaration) {
166
147
  const { parent: grandParent } = node.parent;
167
148
  context.report({
168
149
  node: grandParent,
169
150
  messageId: "declaration",
170
- fix: (fixer) => fixer.replaceTextRange(
171
- grandParent.range,
151
+ fix: (fixer) => fixer.replaceText(
152
+ grandParent,
172
153
  generateFunction(
173
154
  "declaration",
174
155
  node.parent.id.name,
package/dist/index.mjs CHANGED
@@ -20,9 +20,6 @@ function getPreviousNode(node) {
20
20
  }
21
21
 
22
22
  const RULE_NAME$7 = "function-style";
23
- const START_RETURN = /^return /;
24
- const RETURN_ONLY = /^return\s*(?:;\s*)?$/;
25
- const END_SEMICOLON = /;$/;
26
23
  const functionStyle = createEslintRule({
27
24
  name: RULE_NAME$7,
28
25
  meta: {
@@ -41,8 +38,6 @@ const functionStyle = createEslintRule({
41
38
  defaultOptions: [],
42
39
  create: (context) => {
43
40
  const sourceCode = context.getSourceCode();
44
- const text = sourceCode.getText();
45
- const getRawStatement = (statement) => `(${text.slice(statement.range[0], statement.range[1]).replace(START_RETURN, "").replace(END_SEMICOLON, "")})`;
46
41
  function getLoneReturnStatement(node) {
47
42
  const { body } = node;
48
43
  if (body.type !== AST_NODE_TYPES.BlockStatement) {
@@ -91,8 +86,8 @@ const functionStyle = createEslintRule({
91
86
  context.report({
92
87
  node,
93
88
  messageId: "declaration",
94
- fix: (fixer) => fixer.replaceTextRange(
95
- node.parent.parent.range,
89
+ fix: (fixer) => fixer.replaceText(
90
+ node.parent.parent,
96
91
  generateFunction("declaration", name, node)
97
92
  )
98
93
  });
@@ -109,26 +104,21 @@ const functionStyle = createEslintRule({
109
104
  }
110
105
  const statement = getLoneReturnStatement(node);
111
106
  const isExportDefault = node.parent?.type === AST_NODE_TYPES.ExportDefaultDeclaration;
112
- if (!statement || !node.id?.name && !isExportDefault || node.generator) {
113
- clearThisAccess();
114
- return;
115
- }
116
- const rawStatement = getRawStatement(statement);
117
- const statementWithoutBrackets = rawStatement.slice(1, -1);
118
- if (RETURN_ONLY.test(statementWithoutBrackets)) {
107
+ if (!statement?.argument || !node.id?.name && !isExportDefault || node.generator) {
119
108
  clearThisAccess();
120
109
  return;
121
110
  }
111
+ const returnVal = `(${sourceCode.getText(statement.argument)})`;
122
112
  context.report({
123
113
  node,
124
114
  messageId: "arrow",
125
- fix: (fixer) => fixer.replaceTextRange(
126
- node.range,
115
+ fix: (fixer) => fixer.replaceText(
116
+ node,
127
117
  generateFunction(
128
118
  "arrow",
129
119
  node.id?.name ?? null,
130
120
  node,
131
- getRawStatement(statement),
121
+ returnVal,
132
122
  !isExportDefault
133
123
  )
134
124
  )
@@ -142,31 +132,22 @@ const functionStyle = createEslintRule({
142
132
  }
143
133
  const { body, parent } = node;
144
134
  const statement = getLoneReturnStatement(node);
145
- if (statement) {
135
+ if (statement?.argument) {
136
+ const returnVal = `(${sourceCode.getText(statement.argument)})`;
146
137
  context.report({
147
138
  node,
148
139
  messageId: "arrow",
149
- fix: (fixer) => fixer.replaceTextRange(
150
- node.body.range,
151
- getRawStatement(statement)
152
- )
140
+ fix: (fixer) => fixer.replaceText(node.body, returnVal)
153
141
  });
154
- clearThisAccess();
155
- return;
156
- }
157
- if (parent?.id?.typeAnnotation) {
158
- clearThisAccess();
159
- return;
160
- }
161
- if (body.type === AST_NODE_TYPES.BlockStatement) {
142
+ } else if (body.type === AST_NODE_TYPES.BlockStatement && !parent?.id?.typeAnnotation) {
162
143
  const { body: blockBody } = body;
163
144
  if (blockBody.length > 0 && node.parent?.parent?.type === AST_NODE_TYPES.VariableDeclaration) {
164
145
  const { parent: grandParent } = node.parent;
165
146
  context.report({
166
147
  node: grandParent,
167
148
  messageId: "declaration",
168
- fix: (fixer) => fixer.replaceTextRange(
169
- grandParent.range,
149
+ fix: (fixer) => fixer.replaceText(
150
+ grandParent,
170
151
  generateFunction(
171
152
  "declaration",
172
153
  node.parent.id.name,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@so1ve/eslint-plugin",
3
- "version": "1.0.0-alpha.6",
3
+ "version": "1.0.0-alpha.8",
4
4
  "author": "Ray <i@mk1.io> (https://github.com/so1ve/)",
5
5
  "keywords": [
6
6
  "eslint",