@putout/plugin-remove-useless-escape 1.3.0 → 1.7.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,9 +1,7 @@
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
6
  `putout` plugin adds ability to find and remove useless escape.
9
7
 
@@ -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,34 +19,56 @@ 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
+ const regExpNode = assign(RegExpLiteral(unescaped, flags), {
26
+ value: unescaped,
27
+ extra: {
28
+ raw: `/${unescaped}/`,
29
+ rawValue: unescaped,
30
+ },
31
+ });
32
+
33
+ replaceWith(path, regExpNode);
34
+ return;
35
+ }
36
+
14
37
  for (const tmpl of path.node.quasis) {
15
38
  const {raw} = tmpl.value;
16
39
  tmpl.value.raw = unEscape(raw);
17
40
  }
18
41
  };
19
42
 
20
- module.exports.traverse = ({push}) => {
21
- return {
22
- '"__"'(path) {
23
- const {raw} = path.node;
43
+ module.exports.traverse = ({push}) => ({
44
+ 'RegExpLiteral'(path) {
45
+ const {raw} = path.node;
46
+
47
+ if (isEscapedRegExp(raw))
48
+ push(path);
49
+ },
50
+ '"__"'(path) {
51
+ const {raw} = path.node;
52
+
53
+ if (isEscaped(raw))
54
+ push(path);
55
+ },
56
+
57
+ '`__`'(path) {
58
+ for (const tmpl of path.node.quasis) {
59
+ const {raw} = tmpl.value;
60
+
61
+ if (hasQuote(raw))
62
+ return push(path);
24
63
 
25
64
  if (isEscaped(raw))
26
- push(path);
27
- },
28
-
29
- '`__`'(path) {
30
- for (const tmpl of path.node.quasis) {
31
- const {raw} = tmpl.value;
32
-
33
- if (hasQuote(raw))
34
- return push(path);
35
-
36
- if (isEscaped(raw))
37
- return push(path);
38
- }
39
- },
40
- };
41
- };
65
+ return push(path);
66
+
67
+ if (raw.includes('\\\''))
68
+ return push(path);
69
+ }
70
+ },
71
+ });
42
72
 
43
73
  const createCheckRegExp = (a) => RegExp(`^((?!\\\\).)*\\\\${a}.`);
44
74
 
@@ -63,6 +93,9 @@ function isEscaped(raw) {
63
93
  if (!raw.includes('\\'))
64
94
  return false;
65
95
 
96
+ if (raw.includes('\\+') && !raw.includes('\\\\+'))
97
+ return true;
98
+
66
99
  if (hasDoubleQuote(raw))
67
100
  return true;
68
101
 
@@ -80,6 +113,7 @@ const createEncodedRegExp = (a) => RegExp(`\\\\${a}`, 'g');
80
113
  function unEscape(raw) {
81
114
  raw = raw
82
115
  .replace(/\\'/g, `'`)
116
+ .replace(/\\\+/g, '+')
83
117
  .replace(createEncodedRegExp(`"`), '"')
84
118
  .replace(/\\\^/g, '^');
85
119
 
@@ -90,3 +124,16 @@ function unEscape(raw) {
90
124
  return raw;
91
125
  }
92
126
 
127
+ function unescapeRegExp(raw) {
128
+ return raw
129
+ .replace(/\\:/g, ':')
130
+ .replace(/\+\\\//g, '+/');
131
+ }
132
+
133
+ const isRegExpColon = (a) => /\\:/.test(a) && !/\\\\:/.test(a);
134
+ const isRegExpSlash = (a) => /\+\\\//.test(a);
135
+
136
+ function isEscapedRegExp(raw) {
137
+ return isRegExpColon(raw) || isRegExpSlash(raw);
138
+ }
139
+
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@putout/plugin-remove-useless-escape",
3
- "version": "1.3.0",
3
+ "version": "1.7.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,12 +35,12 @@
35
35
  "escape"
36
36
  ],
37
37
  "devDependencies": {
38
- "@putout/plugin-madrun": "^6.1.2",
38
+ "@putout/plugin-madrun": "^10.2.0",
39
39
  "@putout/test": "^3.0.0",
40
40
  "c8": "^7.5.0",
41
- "eslint": "^7.0.0",
41
+ "eslint": "^8.0.1",
42
42
  "eslint-plugin-node": "^11.0.0",
43
- "eslint-plugin-putout": "^7.0.0",
43
+ "eslint-plugin-putout": "^11.0.0",
44
44
  "madrun": "^8.0.1"
45
45
  },
46
46
  "peerDependencies": {