@putout/plugin-remove-useless-escape 1.7.0 → 3.0.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 -7
- 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,10 +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}/`;
|
|
26
|
+
|
|
25
27
|
const regExpNode = assign(RegExpLiteral(unescaped, flags), {
|
|
26
28
|
value: unescaped,
|
|
29
|
+
raw,
|
|
27
30
|
extra: {
|
|
28
|
-
raw
|
|
31
|
+
raw,
|
|
29
32
|
rawValue: unescaped,
|
|
30
33
|
},
|
|
31
34
|
});
|
|
@@ -44,8 +47,9 @@ module.exports.traverse = ({push}) => ({
|
|
|
44
47
|
'RegExpLiteral'(path) {
|
|
45
48
|
const {raw} = path.node;
|
|
46
49
|
|
|
47
|
-
if (isEscapedRegExp(raw))
|
|
50
|
+
if (isEscapedRegExp(raw)) {
|
|
48
51
|
push(path);
|
|
52
|
+
}
|
|
49
53
|
},
|
|
50
54
|
'"__"'(path) {
|
|
51
55
|
const {raw} = path.node;
|
|
@@ -76,6 +80,7 @@ const match = (a) => a.match(emojiRegex()) || [];
|
|
|
76
80
|
const hasA = (a) => /\\\^/.test(a);
|
|
77
81
|
const hasDoubleQuote = (a) => createCheckRegExp('"').test(a);
|
|
78
82
|
const hasQuote = (a) => createCheckRegExp(`'`).test(a);
|
|
83
|
+
const hasComa = (a) => createCheckRegExp(',').test(a);
|
|
79
84
|
|
|
80
85
|
const hasEmoji = (a) => {
|
|
81
86
|
for (const emoji of match(a)) {
|
|
@@ -93,6 +98,9 @@ function isEscaped(raw) {
|
|
|
93
98
|
if (!raw.includes('\\'))
|
|
94
99
|
return false;
|
|
95
100
|
|
|
101
|
+
if (/\\\//g.test(raw) && !/\\\\\//g.test(raw))
|
|
102
|
+
return true;
|
|
103
|
+
|
|
96
104
|
if (raw.includes('\\+') && !raw.includes('\\\\+'))
|
|
97
105
|
return true;
|
|
98
106
|
|
|
@@ -105,6 +113,9 @@ function isEscaped(raw) {
|
|
|
105
113
|
if (hasA(raw))
|
|
106
114
|
return true;
|
|
107
115
|
|
|
116
|
+
if (hasComa(raw))
|
|
117
|
+
return true;
|
|
118
|
+
|
|
108
119
|
return false;
|
|
109
120
|
}
|
|
110
121
|
|
|
@@ -113,9 +124,11 @@ const createEncodedRegExp = (a) => RegExp(`\\\\${a}`, 'g');
|
|
|
113
124
|
function unEscape(raw) {
|
|
114
125
|
raw = raw
|
|
115
126
|
.replace(/\\'/g, `'`)
|
|
127
|
+
.replace(/\\\//g, '/')
|
|
116
128
|
.replace(/\\\+/g, '+')
|
|
117
129
|
.replace(createEncodedRegExp(`"`), '"')
|
|
118
|
-
.replace(/\\\^/g, '^')
|
|
130
|
+
.replace(/\\\^/g, '^')
|
|
131
|
+
.replace(/(\\),/, ',');
|
|
119
132
|
|
|
120
133
|
for (const emoji of match(raw)) {
|
|
121
134
|
raw = raw.replace(createEncodedRegExp(emoji), emoji);
|
|
@@ -127,13 +140,19 @@ function unEscape(raw) {
|
|
|
127
140
|
function unescapeRegExp(raw) {
|
|
128
141
|
return raw
|
|
129
142
|
.replace(/\\:/g, ':')
|
|
130
|
-
.replace(/\+\\\//g, '+/')
|
|
143
|
+
.replace(/\+\\\//g, '+/')
|
|
144
|
+
.replace(/\\,/g, ',');
|
|
131
145
|
}
|
|
132
146
|
|
|
133
|
-
const
|
|
134
|
-
const
|
|
147
|
+
const is = (a) => (b) => b.includes(`\\${a}`) && !b.includes(`\\\\${a}`);
|
|
148
|
+
const isRegExpColon = is(':');
|
|
149
|
+
const isComa = is(',');
|
|
150
|
+
const isRegExpSlash = (a) => a.includes('\\\\\\\\');
|
|
135
151
|
|
|
136
152
|
function isEscapedRegExp(raw) {
|
|
137
|
-
|
|
153
|
+
if (raw.includes('\\/'))
|
|
154
|
+
return false;
|
|
155
|
+
|
|
156
|
+
return isRegExpColon(raw) || isRegExpSlash(raw) || isComa(raw);
|
|
138
157
|
}
|
|
139
158
|
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-remove-useless-escape",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.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"
|