@putout/plugin-conditions 7.3.1 → 8.1.0

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.
@@ -1,8 +1,6 @@
1
- 'use strict';
1
+ export const report = () => `Add return statement`;
2
2
 
3
- module.exports.report = () => `Add return statement`;
4
-
5
- module.exports.replace = () => ({
3
+ export const replace = () => ({
6
4
  'if (__a) false': 'if (__a) return false',
7
5
  'if (__a) true': 'if (__a) return true',
8
6
  });
@@ -1,12 +1,12 @@
1
- 'use strict';
1
+ import {operator} from 'putout';
2
2
 
3
- module.exports.report = ({leftPath, rightPath}) => {
3
+ const {replaceWith} = operator;
4
+
5
+ export const report = ({leftPath, rightPath}) => {
4
6
  return `Swap '${leftPath.toString()}' with '${rightPath}'`;
5
7
  };
6
8
 
7
- const {replaceWith} = require('putout').operator;
8
-
9
- module.exports.fix = ({path, leftPath, rightPath, operator}) => {
9
+ export const fix = ({path, leftPath, rightPath, operator}) => {
10
10
  const leftNode = leftPath.node;
11
11
  const rightNode = rightPath.node;
12
12
 
@@ -16,7 +16,7 @@ module.exports.fix = ({path, leftPath, rightPath, operator}) => {
16
16
  path.node.operator = convertOperator(operator);
17
17
  };
18
18
 
19
- module.exports.traverse = ({push}) => ({
19
+ export const traverse = ({push}) => ({
20
20
  BinaryExpression: (path) => {
21
21
  const {operator} = path.node;
22
22
 
@@ -1,6 +1,5 @@
1
- 'use strict';
1
+ import {types, operator} from 'putout';
2
2
 
3
- const {types, operator} = require('putout');
4
3
  const {replaceWith} = operator;
5
4
  const {
6
5
  isBlockStatement,
@@ -8,9 +7,9 @@ const {
8
7
  blockStatement,
9
8
  } = types;
10
9
 
11
- module.exports.report = () => `Use consistent blocks`;
10
+ export const report = () => `Use consistent blocks`;
12
11
 
13
- module.exports.fix = (path) => {
12
+ export const fix = (path) => {
14
13
  const paths = getAllNodes(path);
15
14
 
16
15
  if (isAllBlocks(paths))
@@ -51,16 +50,19 @@ function isAllBlocks(paths) {
51
50
  return false;
52
51
  }
53
52
 
54
- module.exports.include = () => [
53
+ export const include = () => [
55
54
  'IfStatement',
56
55
  ];
57
56
 
58
- module.exports.filter = (path) => {
57
+ export const filter = (path) => {
59
58
  const {consequent, alternate} = path.node;
60
59
 
61
60
  if (!alternate && !consequent.body?.length)
62
61
  return false;
63
62
 
63
+ if (isBlockStatement(consequent) && !consequent.body.length)
64
+ return false;
65
+
64
66
  if (path === path.parentPath.get('alternate'))
65
67
  return false;
66
68
 
@@ -1,11 +1,10 @@
1
- 'use strict';
1
+ import {operator} from 'putout';
2
2
 
3
- const {operator} = require('putout');
4
3
  const {remove, replaceWith} = operator;
5
4
 
6
- module.exports.report = () => 'Avoid empty statement in if condition';
5
+ export const report = () => 'Avoid empty statement in if condition';
7
6
 
8
- module.exports.filter = (path) => {
7
+ export const filter = (path) => {
9
8
  const nextPath = path.getNextSibling();
10
9
 
11
10
  if (!nextPath.node)
@@ -14,11 +13,11 @@ module.exports.filter = (path) => {
14
13
  return path.get('consequent').isEmptyStatement();
15
14
  };
16
15
 
17
- module.exports.include = () => [
16
+ export const include = () => [
18
17
  'IfStatement',
19
18
  ];
20
19
 
21
- module.exports.fix = (path) => {
20
+ export const fix = (path) => {
22
21
  const nextPath = path.getNextSibling();
23
22
  const consequentPath = path.get('consequent');
24
23
 
@@ -1,12 +1,11 @@
1
- 'use strict';
1
+ import {types} from 'putout';
2
2
 
3
- const {types} = require('putout');
4
3
  const {
5
4
  isBinaryExpression,
6
5
  isJSXExpressionContainer,
7
6
  } = types;
8
7
 
9
- module.exports.createAvoidInAssertions = (value) => ({
8
+ export const createAvoidInAssertions = (value) => ({
10
9
  report: createReport(value),
11
10
  match: createMatch(value),
12
11
  replace: createReplace(value),
@@ -1,7 +1,5 @@
1
- 'use strict';
1
+ export const report = () => `Use 'condition' instead of 'arrow function'`;
2
2
 
3
- module.exports.report = () => `Use 'condition' instead of 'arrow function'`;
4
-
5
- module.exports.replace = () => ({
3
+ export const replace = () => ({
6
4
  'if (__a => __b) __c': 'if (__a >= __b) __c',
7
5
  });
@@ -1,18 +1,16 @@
1
- 'use strict';
2
-
3
- const {types, operator} = require('putout');
1
+ import {types, operator} from 'putout';
4
2
 
5
3
  const {replaceWith, compute} = operator;
6
4
 
7
5
  const {isIdentifier, booleanLiteral} = types;
8
6
 
9
- module.exports.report = () => 'Avoid constant conditions';
7
+ export const report = () => 'Avoid constant conditions';
10
8
 
11
- module.exports.fix = ({path, value}) => {
9
+ export const fix = ({path, value}) => {
12
10
  replaceWith(path, booleanLiteral(value));
13
11
  };
14
12
 
15
- module.exports.traverse = ({push}) => ({
13
+ export const traverse = ({push}) => ({
16
14
  BinaryExpression(path) {
17
15
  const {
18
16
  left,
@@ -1,13 +1,11 @@
1
- 'use strict';
1
+ export const report = () => `Use strict equal ('===') instead of equal ('==')`;
2
2
 
3
- module.exports.report = () => `Use strict equal ('===') instead of equal ('==')`;
4
-
5
- module.exports.exclude = () => [
3
+ export const exclude = () => [
6
4
  '__ == null',
7
5
  '__ != null',
8
6
  ];
9
7
 
10
- module.exports.replace = () => ({
8
+ export const replace = () => ({
11
9
  '__a == __b': '__a === __b',
12
10
  '__a != __b': '__a !== __b',
13
11
  });
@@ -1,8 +1,6 @@
1
- 'use strict';
1
+ export const report = () => 'Avoid useless conditions';
2
2
 
3
- module.exports.report = () => 'Avoid useless conditions';
4
-
5
- module.exports.match = () => ({
3
+ export const match = () => ({
6
4
  'if (__a) __b': (vars, path) => {
7
5
  const testPath = path.get('test');
8
6
  const {confident} = testPath.evaluate();
@@ -11,7 +9,7 @@ module.exports.match = () => ({
11
9
  },
12
10
  });
13
11
 
14
- module.exports.replace = () => ({
12
+ export const replace = () => ({
15
13
  'if (__a) __b': (vars, path) => {
16
14
  const testPath = path.get('test');
17
15
  const {value} = testPath.evaluate();
package/lib/index.js CHANGED
@@ -1,25 +1,23 @@
1
- 'use strict';
1
+ import * as applyConsistentBlocks from './apply-consistent-blocks/index.js';
2
+ import * as applyComparisonOrder from './apply-comparison-order/index.js';
3
+ import * as applyIf from './apply-if/index.js';
4
+ import * as evaluate from './evaluate/index.js';
5
+ import * as convertComparisonToBoolean from './convert-comparison-to-boolean/index.js';
6
+ import * as convertEqualToStrictEqual from './convert-equal-to-strict-equal/index.js';
7
+ import * as mergeIfStatements from './merge-if-statements/index.js';
8
+ import * as removeBoolean from './remove-boolean/index.js';
9
+ import removeZero from './remove-zero/index.js';
10
+ import * as removeUselessElse from './remove-useless-else/index.js';
11
+ import * as simplify from './simplify/index.js';
12
+ import * as removeSameValuesCondition from './remove-same-values-condition/index.js';
13
+ import * as addReturn from './add-return/index.js';
14
+ import * as convertArrowToCondition from './convert-arrow-to-condition/index.js';
15
+ import * as reverseCondition from './reverse-condition/index.js';
16
+ import * as wrapWithBlock from './wrap-with-block/index.js';
17
+ import * as removeUselessLoopCondition from './remove-useless-loop-condition/index.js';
18
+ import * as mergeIfWithElse from './merge-if-with-else/index.js';
2
19
 
3
- const applyConsistentBlocks = require('./apply-consistent-blocks');
4
- const applyComparisonOrder = require('./apply-comparison-order');
5
- const applyIf = require('./apply-if');
6
- const evaluate = require('./evaluate');
7
- const convertComparisonToBoolean = require('./convert-comparison-to-boolean');
8
- const convertEqualToStrictEqual = require('./convert-equal-to-strict-equal');
9
- const mergeIfStatements = require('./merge-if-statements');
10
- const removeBoolean = require('./remove-boolean');
11
- const removeZero = require('./remove-zero');
12
- const removeUselessElse = require('./remove-useless-else');
13
- const simplify = require('./simplify');
14
- const removeSameValuesCondition = require('./remove-same-values-condition');
15
- const addReturn = require('./add-return');
16
- const convertArrowToCondition = require('./convert-arrow-to-condition');
17
- const reverseCondition = require('./reverse-condition');
18
- const wrapWithBlock = require('./wrap-with-block');
19
- const removeUselessLoopCondition = require('./remove-useless-loop-condition');
20
- const mergeIfWithElse = require('./merge-if-with-else');
21
-
22
- module.exports.rules = {
20
+ export const rules = {
23
21
  'apply-comparison-order': applyComparisonOrder,
24
22
  'apply-consistent-blocks': applyConsistentBlocks,
25
23
  'apply-if': applyIf,
@@ -1,13 +1,11 @@
1
- 'use strict';
2
-
3
- const {types, operator} = require('putout');
1
+ import {types, operator} from 'putout';
4
2
 
5
3
  const {replaceWith} = operator;
6
4
  const {logicalExpression} = types;
7
5
 
8
- module.exports.report = () => `Merge 'if' statements`;
6
+ export const report = () => `Merge 'if' statements`;
9
7
 
10
- module.exports.fix = ({path, consequentPath}) => {
8
+ export const fix = ({path, consequentPath}) => {
11
9
  const testPath = path.get('test');
12
10
  const left = testPath.node;
13
11
  const right = consequentPath.node.test;
@@ -37,7 +35,7 @@ const getConsequent = (path) => {
37
35
  return null;
38
36
  };
39
37
 
40
- module.exports.traverse = ({push}) => ({
38
+ export const traverse = ({push}) => ({
41
39
  'if (__) __': onIfStatement({
42
40
  push,
43
41
  }),
@@ -1,20 +1,18 @@
1
- 'use strict';
2
-
3
- const {types, operator} = require('putout');
1
+ import {types, operator} from 'putout';
4
2
 
5
3
  const {compare} = operator;
6
4
  const {logicalExpression} = types;
7
5
 
8
- module.exports.report = () => `Merge 'if' with 'else' when the body is the same`;
6
+ export const report = () => `Merge 'if' with 'else' when the body is the same`;
9
7
 
10
- module.exports.fix = (path) => {
8
+ export const fix = (path) => {
11
9
  const {test: testFirst, alternate} = path.node;
12
10
  const {test: testSecond} = alternate;
13
11
 
14
12
  path.node.test = logicalExpression('||', testFirst, testSecond);
15
13
  delete path.node.alternate;
16
14
  };
17
- module.exports.traverse = ({push}) => ({
15
+ export const traverse = ({push}) => ({
18
16
  IfStatement: (path) => {
19
17
  if (!path.node.alternate)
20
18
  return;
@@ -1,8 +1,6 @@
1
- 'use strict';
1
+ export const report = () => 'Avoid boolean in assertions';
2
2
 
3
- module.exports.report = () => 'Avoid boolean in assertions';
4
-
5
- module.exports.replace = () => ({
3
+ export const replace = () => ({
6
4
  'return __a === true': 'return Boolean(__a)',
7
5
  'return __a == true': 'return Boolean(__a)',
8
6
  'const __a = __b === true': 'const __a = __b',
@@ -1,7 +1,5 @@
1
- 'use strict';
2
-
3
- const {runInNewContext} = require('node:vm');
4
- const {types, operator} = require('putout');
1
+ import {runInNewContext} from 'node:vm';
2
+ import {types, operator} from 'putout';
5
3
 
6
4
  const {
7
5
  replaceWith,
@@ -11,9 +9,9 @@ const {
11
9
 
12
10
  const {isIdentifier} = types;
13
11
 
14
- module.exports.report = () => 'Avoid constant conditions';
12
+ export const report = () => 'Avoid constant conditions';
15
13
 
16
- module.exports.fix = ({path, result}) => {
14
+ export const fix = ({path, result}) => {
17
15
  const {alternate, consequent} = path.node;
18
16
 
19
17
  const {body = [consequent]} = consequent;
@@ -27,7 +25,7 @@ module.exports.fix = ({path, result}) => {
27
25
  replaceWith(path, alternate);
28
26
  };
29
27
 
30
- module.exports.traverse = ({push, generate}) => ({
28
+ export const traverse = ({push, generate}) => ({
31
29
  IfStatement(path) {
32
30
  const testPath = path.get('test');
33
31
 
@@ -1,20 +1,19 @@
1
- 'use strict';
1
+ import {operator} from 'putout';
2
2
 
3
- const {operator} = require('putout');
4
3
  const {
5
4
  getTemplateValues,
6
5
  compare,
7
6
  traverse,
8
7
  } = operator;
9
8
 
10
- module.exports.report = () => `Avoid condition with the same value`;
9
+ export const report = () => `Avoid condition with the same value`;
11
10
 
12
- module.exports.match = () => ({
11
+ export const match = () => ({
13
12
  'if (__a === __b) __c': check('if (__a !== __b) __c'),
14
13
  'if (__a !== __b) __c': check('if (__a === __b) __c'),
15
14
  });
16
15
 
17
- module.exports.replace = () => ({
16
+ export const replace = () => ({
18
17
  'if (__a === __b) __c': '__c',
19
18
  'if (__a !== __b) __c': '__c',
20
19
  });
@@ -1,30 +1,38 @@
1
- 'use strict';
1
+ import {types} from 'putout';
2
2
 
3
3
  const {
4
4
  isReturnStatement,
5
5
  isBlockStatement,
6
6
  isContinueStatement,
7
7
  isBreakStatement,
8
- } = require('putout').types;
8
+ } = types;
9
9
 
10
- module.exports.report = () => `Avoid useless 'else'`;
10
+ export const report = () => `Avoid useless 'else'`;
11
11
 
12
- module.exports.match = () => ({
12
+ export const match = () => ({
13
13
  'if (__a) __b; else __c': ({__b}) => {
14
14
  if (!isBlockStatement(__b))
15
15
  return isReturnLike(__b);
16
16
 
17
+ if (!__b.body.length)
18
+ return true;
19
+
17
20
  const latest = __b.body.at(-1);
18
21
 
19
22
  return isReturnLike(latest);
20
23
  },
21
24
  });
22
25
 
23
- module.exports.replace = () => ({
24
- 'if (__a) __b; else __c': `{
25
- if (__a) __b;
26
- __c;
27
- }`,
26
+ export const replace = () => ({
27
+ 'if (__a) __b; else __c': ({__b}) => {
28
+ if (isBlockStatement(__b) && !__b.body.length)
29
+ return 'if (!__a) __c';
30
+
31
+ return `{
32
+ if (__a) __b;
33
+ __c;
34
+ }`;
35
+ },
28
36
  });
29
37
 
30
38
  function isReturnLike(node) {
@@ -1,6 +1,5 @@
1
- 'use strict';
1
+ import {operator} from 'putout';
2
2
 
3
- const {operator} = require('putout');
4
3
  const {
5
4
  getTemplateValues,
6
5
  compare,
@@ -8,9 +7,9 @@ const {
8
7
 
9
8
  const LOOP = 'while (__c = __d) __body';
10
9
 
11
- module.exports.report = () => `Avoid useless loop condition`;
10
+ export const report = () => `Avoid useless loop condition`;
12
11
 
13
- module.exports.match = () => ({
12
+ export const match = () => ({
14
13
  'if (!__a) __b': ({__a}, path) => {
15
14
  const {parentPath} = path.parentPath;
16
15
 
@@ -23,6 +22,6 @@ module.exports.match = () => ({
23
22
  },
24
23
  });
25
24
 
26
- module.exports.replace = () => ({
25
+ export const replace = () => ({
27
26
  'if (!__a) __b': '',
28
27
  });
@@ -1,5 +1,3 @@
1
- 'use strict';
1
+ import {createAvoidInAssertions} from '../avoid-in-assertions.js';
2
2
 
3
- const {createAvoidInAssertions} = require('../avoid-in-assertions');
4
-
5
- module.exports = createAvoidInAssertions(0);
3
+ export default createAvoidInAssertions(0);
@@ -1,8 +1,6 @@
1
- 'use strict';
1
+ export const report = () => `Avoid useless '!'`;
2
2
 
3
- module.exports.report = () => `Avoid useless '!'`;
4
-
5
- module.exports.replace = () => ({
3
+ export const replace = () => ({
6
4
  '!(__a > __b)': '__a <= __b',
7
5
  '!(__a !== __b && __c === __d)': '__a === __b || __c !== __d',
8
6
  '!(__a !== __b || __c !== __d)': '__a === __b && __c === __d',
@@ -1,8 +1,6 @@
1
- 'use strict';
1
+ export const report = () => 'Avoid useless conditions';
2
2
 
3
- module.exports.report = () => 'Avoid useless conditions';
4
-
5
- module.exports.replace = () => ({
3
+ export const replace = () => ({
6
4
  'if (__a?.__b) {__a.__b(__args)}': '__a?.__b(__args)',
7
5
  'if (__a?.__b) __a.__b(__args)': '__a?.__b(__args)',
8
6
  'if (__a) __b; else __b': '__b',
@@ -1,13 +1,11 @@
1
- 'use strict';
1
+ export const report = () => `Lexical declaration cannot appear in single-statement-context`;
2
2
 
3
- module.exports.report = () => `Lexical declaration cannot appear in single-statement-context`;
4
-
5
- module.exports.match = () => ({
3
+ export const match = () => ({
6
4
  'const __a = __b': (vars, path) => {
7
5
  return path.parentPath.isIfStatement();
8
6
  },
9
7
  });
10
8
 
11
- module.exports.replace = () => ({
9
+ export const replace = () => ({
12
10
  'const __a = __b': '{const __a = __b}',
13
11
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@putout/plugin-conditions",
3
- "version": "7.3.1",
4
- "type": "commonjs",
3
+ "version": "8.1.0",
4
+ "type": "module",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "🐊Putout plugin adds support of conditions transformations",
7
7
  "homepage": "https://github.com/coderaiser/putout/tree/master/packages/plugin-conditions#readme",
@@ -32,23 +32,23 @@
32
32
  "conditions"
33
33
  ],
34
34
  "devDependencies": {
35
- "@putout/eslint-flat": "^2.0.0",
35
+ "@putout/eslint-flat": "^3.0.0",
36
36
  "@putout/plugin-for-of": "*",
37
- "@putout/test": "^12.0.0",
37
+ "@putout/test": "^13.0.0",
38
38
  "c8": "^10.0.0",
39
39
  "eslint": "^9.0.0",
40
40
  "eslint-plugin-n": "^17.0.0",
41
- "eslint-plugin-putout": "^25.0.1",
42
- "madrun": "^10.0.0",
41
+ "eslint-plugin-putout": "^27.0.0",
42
+ "madrun": "^11.0.0",
43
43
  "montag": "^1.2.1",
44
44
  "nodemon": "^3.0.1"
45
45
  },
46
46
  "peerDependencies": {
47
- "putout": ">=38"
47
+ "putout": ">=40"
48
48
  },
49
49
  "license": "MIT",
50
50
  "engines": {
51
- "node": ">=18"
51
+ "node": ">=20"
52
52
  },
53
53
  "publishConfig": {
54
54
  "access": "public"