@putout/plugin-remove-useless-escape 1.5.0 → 2.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.
package/README.md CHANGED
@@ -1,11 +1,9 @@
1
- # @putout/plugin-remove-useless-escape [![NPM version][NPMIMGURL]][NPMURL] [![Dependency Status][DependencyStatusIMGURL]][DependencyStatusURL]
1
+ # @putout/plugin-remove-useless-escape [![NPM version][NPMIMGURL]][NPMURL]
2
2
 
3
3
  [NPMIMGURL]: https://img.shields.io/npm/v/@putout/plugin-remove-useless-escape.svg?style=flat&longCache=true
4
4
  [NPMURL]: https://npmjs.org/package/@putout/plugin-remove-useless-escape"npm"
5
- [DependencyStatusURL]: https://david-dm.org/coderaiser/putout?path=packages/plugin-remove-useless-escape
6
- [DependencyStatusIMGURL]: https://david-dm.org/coderaiser/putout.svg?path=packages/plugin-remove-useless-escape
7
5
 
8
- `putout` plugin adds ability to find and remove useless escape.
6
+ 🐊[`Putout`](https://github.com/coderaiser/putout) plugin adds ability to find and remove useless escape.
9
7
 
10
8
  ## Install
11
9
 
@@ -1,6 +1,14 @@
1
1
  'use strict';
2
2
 
3
3
  const emojiRegex = require('emoji-regex');
4
+ const {
5
+ types,
6
+ operator,
7
+ } = require('putout');
8
+
9
+ const {replaceWith} = operator;
10
+ const {RegExpLiteral} = types;
11
+ const {assign} = Object;
4
12
 
5
13
  module.exports.report = () => 'Unnecessary escape character';
6
14
 
@@ -12,11 +20,20 @@ module.exports.fix = (path) => {
12
20
  }
13
21
 
14
22
  if (path.isRegExpLiteral()) {
15
- const {pattern} = path.node;
23
+ const {pattern, flags} = path.node;
16
24
  const unescaped = unescapeRegExp(pattern);
17
- path.node.pattern = unescaped;
18
- path.node.raw = `/${unescaped}/`;
19
- path.node.extra.raw = `/${unescaped}/`;
25
+ const raw = `/${unescaped}/`;
26
+
27
+ const regExpNode = assign(RegExpLiteral(unescaped, flags), {
28
+ value: unescaped,
29
+ raw,
30
+ extra: {
31
+ raw,
32
+ rawValue: unescaped,
33
+ },
34
+ });
35
+
36
+ replaceWith(path, regExpNode);
20
37
  return;
21
38
  }
22
39
 
@@ -49,6 +66,9 @@ module.exports.traverse = ({push}) => ({
49
66
 
50
67
  if (isEscaped(raw))
51
68
  return push(path);
69
+
70
+ if (raw.includes('\\\''))
71
+ return push(path);
52
72
  }
53
73
  },
54
74
  });
@@ -59,6 +79,7 @@ const match = (a) => a.match(emojiRegex()) || [];
59
79
  const hasA = (a) => /\\\^/.test(a);
60
80
  const hasDoubleQuote = (a) => createCheckRegExp('"').test(a);
61
81
  const hasQuote = (a) => createCheckRegExp(`'`).test(a);
82
+ const hasComa = (a) => createCheckRegExp(',').test(a);
62
83
 
63
84
  const hasEmoji = (a) => {
64
85
  for (const emoji of match(a)) {
@@ -76,6 +97,9 @@ function isEscaped(raw) {
76
97
  if (!raw.includes('\\'))
77
98
  return false;
78
99
 
100
+ if (raw.includes('\\+') && !raw.includes('\\\\+'))
101
+ return true;
102
+
79
103
  if (hasDoubleQuote(raw))
80
104
  return true;
81
105
 
@@ -85,6 +109,9 @@ function isEscaped(raw) {
85
109
  if (hasA(raw))
86
110
  return true;
87
111
 
112
+ if (hasComa(raw))
113
+ return true;
114
+
88
115
  return false;
89
116
  }
90
117
 
@@ -93,8 +120,10 @@ const createEncodedRegExp = (a) => RegExp(`\\\\${a}`, 'g');
93
120
  function unEscape(raw) {
94
121
  raw = raw
95
122
  .replace(/\\'/g, `'`)
123
+ .replace(/\\\+/g, '+')
96
124
  .replace(createEncodedRegExp(`"`), '"')
97
- .replace(/\\\^/g, '^');
125
+ .replace(/\\\^/g, '^')
126
+ .replace(/(\\),/, ',');
98
127
 
99
128
  for (const emoji of match(raw)) {
100
129
  raw = raw.replace(createEncodedRegExp(emoji), emoji);
@@ -104,13 +133,18 @@ function unEscape(raw) {
104
133
  }
105
134
 
106
135
  function unescapeRegExp(raw) {
107
- return raw.replace(/\\:/g, ':');
136
+ return raw
137
+ .replace(/\\:/g, ':')
138
+ .replace(/\+\\\//g, '+/')
139
+ .replace(/\\,/g, ',');
108
140
  }
109
141
 
142
+ const is = (a) => (b) => b.includes(`\\${a}`) && !b.includes(`\\\\${a}`);
143
+ const isRegExpColon = is(':');
144
+ const isComa = is(',');
145
+ const isRegExpSlash = (a) => a.includes('\\\\\\\\');
146
+
110
147
  function isEscapedRegExp(raw) {
111
- const includes = /\\:/.test(raw);
112
- const notIncludes = !/\\\\:/.test(raw);
113
-
114
- return includes && notIncludes;
148
+ return isRegExpColon(raw) || isRegExpSlash(raw) || isComa(raw);
115
149
  }
116
150
 
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@putout/plugin-remove-useless-escape",
3
- "version": "1.5.0",
3
+ "version": "2.1.0",
4
4
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
5
5
  "description": "putout plugin adds ability to find and remove useless escape",
6
- "homepage": "http://github.com/coderaiser/putout",
6
+ "homepage": "https://github.com/coderaiser/putout/tree/master/packages/plugin-remove-useless-escape",
7
7
  "main": "lib/remove-useless-escape.js",
8
8
  "release": false,
9
9
  "tag": false,
@@ -35,20 +35,20 @@
35
35
  "escape"
36
36
  ],
37
37
  "devDependencies": {
38
- "@putout/plugin-madrun": "^10.2.0",
39
- "@putout/test": "^3.0.0",
38
+ "@putout/plugin-madrun": "*",
39
+ "@putout/test": "^4.0.0",
40
40
  "c8": "^7.5.0",
41
- "eslint": "^8.0.0-beta.0",
41
+ "eslint": "^8.0.1",
42
42
  "eslint-plugin-node": "^11.0.0",
43
- "eslint-plugin-putout": "^10.0.0",
43
+ "eslint-plugin-putout": "^12.0.0",
44
44
  "madrun": "^8.0.1"
45
45
  },
46
46
  "peerDependencies": {
47
- "putout": ">=6.2"
47
+ "putout": ">=22"
48
48
  },
49
49
  "license": "MIT",
50
50
  "engines": {
51
- "node": ">=8.3.0"
51
+ "node": ">=14"
52
52
  },
53
53
  "publishConfig": {
54
54
  "access": "public"