@putout/plugin-remove-useless-escape 1.4.0 → 2.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 +4 -4
- package/lib/remove-useless-escape.js +58 -1
- package/package.json +8 -8
package/README.md
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
# @putout/plugin-remove-useless-escape [![NPM version][NPMIMGURL]][NPMURL]
|
|
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
|
|
6
|
+
🐊[`Putout`](https://github.com/coderaiser/putout) plugin adds ability to find and remove useless escape.
|
|
9
7
|
|
|
10
8
|
## Install
|
|
11
9
|
|
|
@@ -29,6 +27,7 @@ npm i @putout/plugin-remove-useless-escape
|
|
|
29
27
|
const t = 'hello \"world\"';
|
|
30
28
|
const s1 = `hello \"world\"`;
|
|
31
29
|
const s = `hello \'world\'`;
|
|
30
|
+
const reg = /\w\:/g;
|
|
32
31
|
```
|
|
33
32
|
|
|
34
33
|
## ✅ Correct code Example
|
|
@@ -37,6 +36,7 @@ const s = `hello \'world\'`;
|
|
|
37
36
|
const t = 'hello "world"';
|
|
38
37
|
const s1 = `hello "world"`;
|
|
39
38
|
const s = `hello 'world'`;
|
|
39
|
+
const reg = /\w:/g;
|
|
40
40
|
```
|
|
41
41
|
|
|
42
42
|
## License
|
|
@@ -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
|
|
|
@@ -11,6 +19,22 @@ module.exports.fix = (path) => {
|
|
|
11
19
|
return;
|
|
12
20
|
}
|
|
13
21
|
|
|
22
|
+
if (path.isRegExpLiteral()) {
|
|
23
|
+
const {pattern, flags} = path.node;
|
|
24
|
+
const unescaped = unescapeRegExp(pattern);
|
|
25
|
+
|
|
26
|
+
const regExpNode = assign(RegExpLiteral(unescaped, flags), {
|
|
27
|
+
value: unescaped,
|
|
28
|
+
extra: {
|
|
29
|
+
raw: `/${unescaped}/`,
|
|
30
|
+
rawValue: unescaped,
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
replaceWith(path, regExpNode);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
14
38
|
for (const tmpl of path.node.quasis) {
|
|
15
39
|
const {raw} = tmpl.value;
|
|
16
40
|
tmpl.value.raw = unEscape(raw);
|
|
@@ -18,6 +42,12 @@ module.exports.fix = (path) => {
|
|
|
18
42
|
};
|
|
19
43
|
|
|
20
44
|
module.exports.traverse = ({push}) => ({
|
|
45
|
+
'RegExpLiteral'(path) {
|
|
46
|
+
const {raw} = path.node;
|
|
47
|
+
|
|
48
|
+
if (isEscapedRegExp(raw))
|
|
49
|
+
push(path);
|
|
50
|
+
},
|
|
21
51
|
'"__"'(path) {
|
|
22
52
|
const {raw} = path.node;
|
|
23
53
|
|
|
@@ -34,6 +64,9 @@ module.exports.traverse = ({push}) => ({
|
|
|
34
64
|
|
|
35
65
|
if (isEscaped(raw))
|
|
36
66
|
return push(path);
|
|
67
|
+
|
|
68
|
+
if (raw.includes('\\\''))
|
|
69
|
+
return push(path);
|
|
37
70
|
}
|
|
38
71
|
},
|
|
39
72
|
});
|
|
@@ -44,6 +77,7 @@ const match = (a) => a.match(emojiRegex()) || [];
|
|
|
44
77
|
const hasA = (a) => /\\\^/.test(a);
|
|
45
78
|
const hasDoubleQuote = (a) => createCheckRegExp('"').test(a);
|
|
46
79
|
const hasQuote = (a) => createCheckRegExp(`'`).test(a);
|
|
80
|
+
const hasComa = (a) => createCheckRegExp(',').test(a);
|
|
47
81
|
|
|
48
82
|
const hasEmoji = (a) => {
|
|
49
83
|
for (const emoji of match(a)) {
|
|
@@ -61,6 +95,9 @@ function isEscaped(raw) {
|
|
|
61
95
|
if (!raw.includes('\\'))
|
|
62
96
|
return false;
|
|
63
97
|
|
|
98
|
+
if (raw.includes('\\+') && !raw.includes('\\\\+'))
|
|
99
|
+
return true;
|
|
100
|
+
|
|
64
101
|
if (hasDoubleQuote(raw))
|
|
65
102
|
return true;
|
|
66
103
|
|
|
@@ -70,6 +107,9 @@ function isEscaped(raw) {
|
|
|
70
107
|
if (hasA(raw))
|
|
71
108
|
return true;
|
|
72
109
|
|
|
110
|
+
if (hasComa(raw))
|
|
111
|
+
return true;
|
|
112
|
+
|
|
73
113
|
return false;
|
|
74
114
|
}
|
|
75
115
|
|
|
@@ -78,8 +118,10 @@ const createEncodedRegExp = (a) => RegExp(`\\\\${a}`, 'g');
|
|
|
78
118
|
function unEscape(raw) {
|
|
79
119
|
raw = raw
|
|
80
120
|
.replace(/\\'/g, `'`)
|
|
121
|
+
.replace(/\\\+/g, '+')
|
|
81
122
|
.replace(createEncodedRegExp(`"`), '"')
|
|
82
|
-
.replace(/\\\^/g, '^')
|
|
123
|
+
.replace(/\\\^/g, '^')
|
|
124
|
+
.replace(/(\\),/g, ',');
|
|
83
125
|
|
|
84
126
|
for (const emoji of match(raw)) {
|
|
85
127
|
raw = raw.replace(createEncodedRegExp(emoji), emoji);
|
|
@@ -88,3 +130,18 @@ function unEscape(raw) {
|
|
|
88
130
|
return raw;
|
|
89
131
|
}
|
|
90
132
|
|
|
133
|
+
function unescapeRegExp(raw) {
|
|
134
|
+
return raw
|
|
135
|
+
.replace(/\\:/g, ':')
|
|
136
|
+
.replace(/\+\\\//g, '+/')
|
|
137
|
+
.replace(/(\\),/g, ',');
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const isRegExpColon = (a) => /\\:/.test(a) && !/\\\\:/.test(a);
|
|
141
|
+
const isRegExpSlash = (a) => /\+\\\//.test(a);
|
|
142
|
+
const isComa = (a) => /(\\),/.test(a);
|
|
143
|
+
|
|
144
|
+
function isEscapedRegExp(raw) {
|
|
145
|
+
return isRegExpColon(raw) || isRegExpSlash(raw) || isComa(raw);
|
|
146
|
+
}
|
|
147
|
+
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-remove-useless-escape",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.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": "
|
|
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": "^
|
|
39
|
-
"@putout/test": "^
|
|
38
|
+
"@putout/plugin-madrun": "^10.2.0",
|
|
39
|
+
"@putout/test": "^4.0.0",
|
|
40
40
|
"c8": "^7.5.0",
|
|
41
|
-
"eslint": "^
|
|
41
|
+
"eslint": "^8.0.1",
|
|
42
42
|
"eslint-plugin-node": "^11.0.0",
|
|
43
|
-
"eslint-plugin-putout": "^
|
|
43
|
+
"eslint-plugin-putout": "^12.0.0",
|
|
44
44
|
"madrun": "^8.0.1"
|
|
45
45
|
},
|
|
46
46
|
"peerDependencies": {
|
|
47
|
-
"putout": ">=
|
|
47
|
+
"putout": ">=22"
|
|
48
48
|
},
|
|
49
49
|
"license": "MIT",
|
|
50
50
|
"engines": {
|
|
51
|
-
"node": ">=
|
|
51
|
+
"node": ">=14"
|
|
52
52
|
},
|
|
53
53
|
"publishConfig": {
|
|
54
54
|
"access": "public"
|