@putout/plugin-putout 20.1.0 → 20.3.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.
package/README.md CHANGED
@@ -62,6 +62,7 @@ npm i @putout/plugin-putout -D
62
62
  - ✅ [replace-operate-with-operator](#replace-operate-with-operator);
63
63
  - ✅ [replace-test-message](#replace-test-message);
64
64
  - ✅ [shorten-imports](#shorten-imports);
65
+ - ✅ [simplify-replace-template](#simplify-replace-template);
65
66
 
66
67
  ## Config
67
68
 
@@ -113,7 +114,8 @@ npm i @putout/plugin-putout -D
113
114
  "putout/includer": "on",
114
115
  "putout/move-require-on-top-level": "on",
115
116
  "putout/replace-test-message": "on",
116
- "putout/remove-unused-get-properties-argument": "on"
117
+ "putout/remove-unused-get-properties-argument": "on",
118
+ "putout/simplify-replace-template": "on"
117
119
  }
118
120
  }
119
121
  ```
@@ -1236,6 +1238,26 @@ const {
1236
1238
  } = getProperties(__jsonPath, ['parser', 'rules', 'extends']);
1237
1239
  ```
1238
1240
 
1241
+ ## simplify-replace-template
1242
+
1243
+ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/a012fc12f4d38038da022f7c8f379fe2/c65bf97c42650aa31c5481849bc934323df892a6).
1244
+
1245
+ ### ❌ Example of incorrect code
1246
+
1247
+ ```js
1248
+ module.exports.replace = () => ({
1249
+ 'if (__a) {__b} else {__c}': () => 'if (__a) __b; else __c',
1250
+ });
1251
+ ```
1252
+
1253
+ ### ✅ Example of correct code
1254
+
1255
+ ```js
1256
+ module.exports.replace = () => ({
1257
+ 'if (__a) {__b} else {__c}': 'if (__a) __b; else __c',
1258
+ });
1259
+ ```
1260
+
1239
1261
  ## License
1240
1262
 
1241
1263
  MIT
@@ -2,18 +2,20 @@
2
2
 
3
3
  const putout = require('putout');
4
4
  const tryCatch = require('try-catch');
5
- const noop = () => {};
6
5
 
6
+ const noop = () => {};
7
7
  const {types, operator} = putout;
8
- const {replaceWith} = operator;
9
8
 
10
9
  const {
11
10
  ArrayPattern,
12
11
  BlockStatement,
13
12
  ObjectExpression,
14
13
  ObjectPattern,
14
+ Identifier,
15
15
  } = types;
16
16
 
17
+ const {replaceWith} = operator;
18
+
17
19
  module.exports = (rootPath, key) => {
18
20
  const getVar = createVarStore(rootPath);
19
21
 
@@ -64,8 +66,17 @@ module.exports = (rootPath, key) => {
64
66
  if (name === '__object')
65
67
  return objectify(path);
66
68
 
67
- if (name === '__body')
69
+ if (name === '__body') {
70
+ if (path.parentPath.isClassProperty()) {
71
+ const key = Identifier(getVar());
72
+
73
+ replaceWith(path, key);
74
+
75
+ return;
76
+ }
77
+
68
78
  replaceWith(path, BlockStatement([]));
79
+ }
69
80
  },
70
81
  }],
71
82
  ],
package/lib/index.js CHANGED
@@ -49,6 +49,7 @@ const convertProgressToTrackFile = require('./convert-progress-to-track-file');
49
49
  const addAwaitToProgress = require('./add-await-to-progress');
50
50
  const applyForOfToTrackFile = require('./apply-for-of-to-track-file');
51
51
  const removeUnusedGetPropertiesArgument = require('./remove-unused-get-properties-argument');
52
+ const simplifyReplaceTemplate = require('./simplify-replace-template');
52
53
 
53
54
  module.exports.rules = {
54
55
  'apply-processors-destructuring': applyProcessorsDestructuring,
@@ -100,4 +101,5 @@ module.exports.rules = {
100
101
  'add-await-to-progress': addAwaitToProgress,
101
102
  'apply-for-of-to-track-file': applyForOfToTrackFile,
102
103
  'remove-unused-get-properties-argument': removeUnusedGetPropertiesArgument,
104
+ 'simplify-replace-template': simplifyReplaceTemplate,
103
105
  };
@@ -0,0 +1,28 @@
1
+ 'use strict';
2
+
3
+ const {operator} = require('putout');
4
+ const {compareAny, replaceWith} = operator;
5
+
6
+ const parentNodesList = [
7
+ 'module.exports.replace = __',
8
+ 'export const replace = __',
9
+ ];
10
+
11
+ module.exports.report = () => `Simplify replce template`;
12
+
13
+ module.exports.fix = (path) => {
14
+ const {body} = path.node;
15
+ replaceWith(path, body);
16
+ };
17
+
18
+ module.exports.include = () => [
19
+ '() => "__a"',
20
+ ];
21
+
22
+ module.exports.filter = (path) => {
23
+ return path.find(isReplace);
24
+ };
25
+
26
+ function isReplace(path) {
27
+ return compareAny(path, parentNodesList);
28
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-putout",
3
- "version": "20.1.0",
3
+ "version": "20.3.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "🐊Putout plugin helps with plugins development",