@putout/plugin-remove-useless-escape 2.0.0 → 3.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 +3 -3
- package/lib/remove-useless-escape.js +26 -12
- package/package.json +9 -8
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
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
5
|
|
|
6
|
-
🐊[
|
|
6
|
+
🐊[**Putout**](https://github.com/coderaiser/putout) plugin adds ability to find and remove useless escape.
|
|
7
7
|
|
|
8
8
|
## Install
|
|
9
9
|
|
|
@@ -21,7 +21,7 @@ npm i @putout/plugin-remove-useless-escape
|
|
|
21
21
|
}
|
|
22
22
|
```
|
|
23
23
|
|
|
24
|
-
## ❌
|
|
24
|
+
## ❌ Example of incorrect code
|
|
25
25
|
|
|
26
26
|
```js
|
|
27
27
|
const t = 'hello \"world\"';
|
|
@@ -30,7 +30,7 @@ const s = `hello \'world\'`;
|
|
|
30
30
|
const reg = /\w\:/g;
|
|
31
31
|
```
|
|
32
32
|
|
|
33
|
-
## ✅
|
|
33
|
+
## ✅ Example of correct code
|
|
34
34
|
|
|
35
35
|
```js
|
|
36
36
|
const t = 'hello "world"';
|
|
@@ -22,11 +22,13 @@ module.exports.fix = (path) => {
|
|
|
22
22
|
if (path.isRegExpLiteral()) {
|
|
23
23
|
const {pattern, flags} = path.node;
|
|
24
24
|
const unescaped = unescapeRegExp(pattern);
|
|
25
|
+
const raw = `/${unescaped}/`;
|
|
25
26
|
|
|
26
27
|
const regExpNode = assign(RegExpLiteral(unescaped, flags), {
|
|
27
28
|
value: unescaped,
|
|
29
|
+
raw,
|
|
28
30
|
extra: {
|
|
29
|
-
raw
|
|
31
|
+
raw,
|
|
30
32
|
rawValue: unescaped,
|
|
31
33
|
},
|
|
32
34
|
});
|
|
@@ -45,8 +47,9 @@ module.exports.traverse = ({push}) => ({
|
|
|
45
47
|
'RegExpLiteral'(path) {
|
|
46
48
|
const {raw} = path.node;
|
|
47
49
|
|
|
48
|
-
if (isEscapedRegExp(raw))
|
|
50
|
+
if (isEscapedRegExp(raw)) {
|
|
49
51
|
push(path);
|
|
52
|
+
}
|
|
50
53
|
},
|
|
51
54
|
'"__"'(path) {
|
|
52
55
|
const {raw} = path.node;
|
|
@@ -95,6 +98,12 @@ function isEscaped(raw) {
|
|
|
95
98
|
if (!raw.includes('\\'))
|
|
96
99
|
return false;
|
|
97
100
|
|
|
101
|
+
if (/\\\\/g.test(raw))
|
|
102
|
+
return false;
|
|
103
|
+
|
|
104
|
+
if (/\\\//g.test(raw))
|
|
105
|
+
return true;
|
|
106
|
+
|
|
98
107
|
if (raw.includes('\\+') && !raw.includes('\\\\+'))
|
|
99
108
|
return true;
|
|
100
109
|
|
|
@@ -117,11 +126,12 @@ const createEncodedRegExp = (a) => RegExp(`\\\\${a}`, 'g');
|
|
|
117
126
|
|
|
118
127
|
function unEscape(raw) {
|
|
119
128
|
raw = raw
|
|
120
|
-
.
|
|
121
|
-
.
|
|
129
|
+
.replaceAll('\\\'', `'`)
|
|
130
|
+
.replaceAll('\\/', '/')
|
|
131
|
+
.replaceAll('\\+', '+')
|
|
122
132
|
.replace(createEncodedRegExp(`"`), '"')
|
|
123
|
-
.
|
|
124
|
-
.replace(/(\\)
|
|
133
|
+
.replaceAll('\\^', '^')
|
|
134
|
+
.replace(/(\\),/, ',');
|
|
125
135
|
|
|
126
136
|
for (const emoji of match(raw)) {
|
|
127
137
|
raw = raw.replace(createEncodedRegExp(emoji), emoji);
|
|
@@ -132,16 +142,20 @@ function unEscape(raw) {
|
|
|
132
142
|
|
|
133
143
|
function unescapeRegExp(raw) {
|
|
134
144
|
return raw
|
|
135
|
-
.
|
|
136
|
-
.
|
|
137
|
-
.
|
|
145
|
+
.replaceAll('\\:', ':')
|
|
146
|
+
.replaceAll('\\+\\/', '+/')
|
|
147
|
+
.replaceAll('\\,', ',');
|
|
138
148
|
}
|
|
139
149
|
|
|
140
|
-
const
|
|
141
|
-
const
|
|
142
|
-
const isComa = (
|
|
150
|
+
const is = (a) => (b) => b.includes(`\\${a}`) && !b.includes(`\\\\${a}`);
|
|
151
|
+
const isRegExpColon = is(':');
|
|
152
|
+
const isComa = is(',');
|
|
153
|
+
const isRegExpSlash = (a) => a.includes('\\\\\\\\');
|
|
143
154
|
|
|
144
155
|
function isEscapedRegExp(raw) {
|
|
156
|
+
if (raw.includes('\\/'))
|
|
157
|
+
return false;
|
|
158
|
+
|
|
145
159
|
return isRegExpColon(raw) || isRegExpSlash(raw) || isComa(raw);
|
|
146
160
|
}
|
|
147
161
|
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-remove-useless-escape",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.1.0",
|
|
4
|
+
"type": "commonjs",
|
|
4
5
|
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
|
|
5
6
|
"description": "putout plugin adds ability to find and remove useless escape",
|
|
6
|
-
"homepage": "https://github.com/coderaiser/putout/tree/master/packages/plugin-remove-useless-escape",
|
|
7
|
+
"homepage": "https://github.com/coderaiser/putout/tree/master/packages/plugin-remove-useless-escape#readme",
|
|
7
8
|
"main": "lib/remove-useless-escape.js",
|
|
8
9
|
"release": false,
|
|
9
10
|
"tag": false,
|
|
@@ -35,20 +36,20 @@
|
|
|
35
36
|
"escape"
|
|
36
37
|
],
|
|
37
38
|
"devDependencies": {
|
|
38
|
-
"@putout/plugin-madrun": "
|
|
39
|
-
"@putout/test": "^
|
|
39
|
+
"@putout/plugin-madrun": "*",
|
|
40
|
+
"@putout/test": "^5.0.0",
|
|
40
41
|
"c8": "^7.5.0",
|
|
41
42
|
"eslint": "^8.0.1",
|
|
42
43
|
"eslint-plugin-node": "^11.0.0",
|
|
43
|
-
"eslint-plugin-putout": "^
|
|
44
|
-
"madrun": "^
|
|
44
|
+
"eslint-plugin-putout": "^14.0.0",
|
|
45
|
+
"madrun": "^9.0.0"
|
|
45
46
|
},
|
|
46
47
|
"peerDependencies": {
|
|
47
|
-
"putout": ">=
|
|
48
|
+
"putout": ">=25"
|
|
48
49
|
},
|
|
49
50
|
"license": "MIT",
|
|
50
51
|
"engines": {
|
|
51
|
-
"node": ">=
|
|
52
|
+
"node": ">=16"
|
|
52
53
|
},
|
|
53
54
|
"publishConfig": {
|
|
54
55
|
"access": "public"
|