@so1ve/eslint-plugin 0.78.0 → 0.78.1

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
@@ -27,7 +27,7 @@ const functionStyle = createEslintRule({
27
27
  create: (context) => {
28
28
  const sourceCode = context.getSourceCode();
29
29
  const text = sourceCode.getText();
30
- const getStatementRaw = (statement) => text.slice(statement.range[0], statement.range[1]).replace(START_RETURN, "").replace(END_SEMICOLON, "");
30
+ const getStatementRaw = (statement) => `(${text.slice(statement.range[0], statement.range[1]).replace(START_RETURN, "").replace(END_SEMICOLON, "")})`;
31
31
  function getLoneReturnStatement(node) {
32
32
  const { body } = node;
33
33
  if (body.type === types.AST_NODE_TYPES.BlockStatement) {
@@ -40,13 +40,14 @@ const functionStyle = createEslintRule({
40
40
  }
41
41
  }
42
42
  }
43
- function generateFunction(type, name, node, rawStatement) {
43
+ function generateFunction(type, name, node, rawStatement, asVariable = true) {
44
44
  const async = node.async ? "async " : "";
45
45
  const generics = node.typeParameters ? sourceCode.getText(node.typeParameters) : "";
46
46
  const params = node.params.map((param) => sourceCode.getText(param)).join(", ");
47
47
  const returnType = node.returnType ? sourceCode.getText(node.returnType) : "";
48
48
  const body = sourceCode.getText(node.body);
49
- return type === "arrow" ? `const ${name} = ${async}${generics}(${params})${returnType} => ${rawStatement};` : `${async}function ${name}${generics}(${params})${returnType} ${body}`;
49
+ const variableDeclaration = asVariable ? `const ${name} = ` : "";
50
+ return type === "arrow" ? `${variableDeclaration}${async}${generics}(${params})${returnType} => ${rawStatement};` : `${async}function ${name}${generics}(${params})${returnType} ${body}`;
50
51
  }
51
52
  return {
52
53
  FunctionExpression(node) {
@@ -68,6 +69,7 @@ const functionStyle = createEslintRule({
68
69
  if (!statement || !node.id?.name || node.generator) {
69
70
  return;
70
71
  }
72
+ const asVariable = node.parent?.type !== types.AST_NODE_TYPES.ExportDefaultDeclaration;
71
73
  context.report({
72
74
  node,
73
75
  messageId: "arrow",
@@ -77,7 +79,8 @@ const functionStyle = createEslintRule({
77
79
  "arrow",
78
80
  node.id.name,
79
81
  node,
80
- getStatementRaw(statement)
82
+ getStatementRaw(statement),
83
+ asVariable
81
84
  )
82
85
  )
83
86
  });
@@ -244,7 +247,7 @@ const noNegatedEqual = createEslintRule({
244
247
  meta: {
245
248
  type: "problem",
246
249
  docs: {
247
- description: "Disallow negated eqeqeq.",
250
+ description: "Disallow negated equal sign.",
248
251
  recommended: "error"
249
252
  },
250
253
  fixable: "code",
@@ -256,17 +259,18 @@ const noNegatedEqual = createEslintRule({
256
259
  defaultOptions: [],
257
260
  create: (context) => ({
258
261
  BinaryExpression(node) {
259
- const { parent, left, right } = node;
262
+ const { parent, left, right, operator } = node;
260
263
  if (!parent) {
261
264
  return;
262
265
  }
263
- if (["==", "==="].includes(node.operator) && parent.type === types.AST_NODE_TYPES.UnaryExpression && parent.operator === "!") {
266
+ if (["==", "==="].includes(node.operator) && parent.type === types.AST_NODE_TYPES.UnaryExpression && // Is this necessary?
267
+ parent.operator === "!") {
264
268
  context.report({
265
269
  node,
266
270
  messageId: "noNegatedEqual",
267
271
  *fix(fixer) {
268
272
  const operatorRange = [left.range[1], right.range[0]];
269
- const fixedOperator = node.operator === "==" ? "!=" : "!==";
273
+ const fixedOperator = operator === "==" ? "!=" : "!==";
270
274
  yield fixer.replaceTextRange(operatorRange, fixedOperator);
271
275
  yield fixer.removeRange([parent.range[0], parent.range[0] + 1]);
272
276
  }
package/dist/index.mjs CHANGED
@@ -25,7 +25,7 @@ const functionStyle = createEslintRule({
25
25
  create: (context) => {
26
26
  const sourceCode = context.getSourceCode();
27
27
  const text = sourceCode.getText();
28
- const getStatementRaw = (statement) => text.slice(statement.range[0], statement.range[1]).replace(START_RETURN, "").replace(END_SEMICOLON, "");
28
+ const getStatementRaw = (statement) => `(${text.slice(statement.range[0], statement.range[1]).replace(START_RETURN, "").replace(END_SEMICOLON, "")})`;
29
29
  function getLoneReturnStatement(node) {
30
30
  const { body } = node;
31
31
  if (body.type === AST_NODE_TYPES.BlockStatement) {
@@ -38,13 +38,14 @@ const functionStyle = createEslintRule({
38
38
  }
39
39
  }
40
40
  }
41
- function generateFunction(type, name, node, rawStatement) {
41
+ function generateFunction(type, name, node, rawStatement, asVariable = true) {
42
42
  const async = node.async ? "async " : "";
43
43
  const generics = node.typeParameters ? sourceCode.getText(node.typeParameters) : "";
44
44
  const params = node.params.map((param) => sourceCode.getText(param)).join(", ");
45
45
  const returnType = node.returnType ? sourceCode.getText(node.returnType) : "";
46
46
  const body = sourceCode.getText(node.body);
47
- return type === "arrow" ? `const ${name} = ${async}${generics}(${params})${returnType} => ${rawStatement};` : `${async}function ${name}${generics}(${params})${returnType} ${body}`;
47
+ const variableDeclaration = asVariable ? `const ${name} = ` : "";
48
+ return type === "arrow" ? `${variableDeclaration}${async}${generics}(${params})${returnType} => ${rawStatement};` : `${async}function ${name}${generics}(${params})${returnType} ${body}`;
48
49
  }
49
50
  return {
50
51
  FunctionExpression(node) {
@@ -66,6 +67,7 @@ const functionStyle = createEslintRule({
66
67
  if (!statement || !node.id?.name || node.generator) {
67
68
  return;
68
69
  }
70
+ const asVariable = node.parent?.type !== AST_NODE_TYPES.ExportDefaultDeclaration;
69
71
  context.report({
70
72
  node,
71
73
  messageId: "arrow",
@@ -75,7 +77,8 @@ const functionStyle = createEslintRule({
75
77
  "arrow",
76
78
  node.id.name,
77
79
  node,
78
- getStatementRaw(statement)
80
+ getStatementRaw(statement),
81
+ asVariable
79
82
  )
80
83
  )
81
84
  });
@@ -242,7 +245,7 @@ const noNegatedEqual = createEslintRule({
242
245
  meta: {
243
246
  type: "problem",
244
247
  docs: {
245
- description: "Disallow negated eqeqeq.",
248
+ description: "Disallow negated equal sign.",
246
249
  recommended: "error"
247
250
  },
248
251
  fixable: "code",
@@ -254,17 +257,18 @@ const noNegatedEqual = createEslintRule({
254
257
  defaultOptions: [],
255
258
  create: (context) => ({
256
259
  BinaryExpression(node) {
257
- const { parent, left, right } = node;
260
+ const { parent, left, right, operator } = node;
258
261
  if (!parent) {
259
262
  return;
260
263
  }
261
- if (["==", "==="].includes(node.operator) && parent.type === AST_NODE_TYPES.UnaryExpression && parent.operator === "!") {
264
+ if (["==", "==="].includes(node.operator) && parent.type === AST_NODE_TYPES.UnaryExpression && // Is this necessary?
265
+ parent.operator === "!") {
262
266
  context.report({
263
267
  node,
264
268
  messageId: "noNegatedEqual",
265
269
  *fix(fixer) {
266
270
  const operatorRange = [left.range[1], right.range[0]];
267
- const fixedOperator = node.operator === "==" ? "!=" : "!==";
271
+ const fixedOperator = operator === "==" ? "!=" : "!==";
268
272
  yield fixer.replaceTextRange(operatorRange, fixedOperator);
269
273
  yield fixer.removeRange([parent.range[0], parent.range[0] + 1]);
270
274
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@so1ve/eslint-plugin",
3
- "version": "0.78.0",
3
+ "version": "0.78.1",
4
4
  "author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
5
5
  "contributors": [
6
6
  {