Skip to content

Commit bb86eb8

Browse files
committed
feature: @putout/plugin-regexp: drop support of 🐊 < 39
1 parent 7c97b57 commit bb86eb8

File tree

21 files changed

+119
-149
lines changed

21 files changed

+119
-149
lines changed

packages/plugin-regexp/.putout.json

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"rules": {
3+
"regexp": "off"
4+
},
25
"match": {
36
"*.md": {
47
"regexp": "off"

packages/plugin-regexp/lib/apply-ends-with/index.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
'use strict';
1+
import {types} from 'putout';
22

3-
const {types} = require('putout');
43
const {isRegExpLiteral} = types;
54

6-
module.exports.report = () => `Use '.endsWith()' instead of '.test()'`;
5+
export const report = () => `Use '.endsWith()' instead of '.test()'`;
76

8-
module.exports.match = () => ({
7+
export const match = () => ({
98
'__a.test(__b)': ({__a}) => {
109
if (!isRegExpLiteral(__a))
1110
return false;
@@ -22,7 +21,7 @@ module.exports.match = () => ({
2221
},
2322
});
2423

25-
module.exports.replace = () => ({
24+
export const replace = () => ({
2625
'__a.test(__b)': ({__a}) => {
2726
const str = __a
2827
.raw

packages/plugin-regexp/lib/apply-ends-with/index.spec.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict';
1+
import {createTest} from '@putout/test';
2+
import * as optimize from './index.js';
23

3-
const {createTest} = require('@putout/test');
4-
const optimize = require('.');
5-
6-
const test = createTest(__dirname, {
4+
const test = createTest(import.meta.url, {
75
plugins: [
86
['regexp/apply-ends-with', optimize],
97
],

packages/plugin-regexp/lib/apply-literal-notation/index.js

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
'use strict';
2-
3-
const {types, operator} = require('putout');
1+
import {types, operator} from 'putout';
42

53
const {replaceWith} = operator;
64

@@ -9,19 +7,19 @@ const {
97
regExpLiteral,
108
} = types;
119

12-
const match = ({__a}) => isStringLiteral(__a);
10+
const check = ({__a}) => isStringLiteral(__a);
1311

14-
module.exports.report = () => `Use RegExp literal notation`;
12+
export const report = () => `Use RegExp literal notation`;
1513

16-
module.exports.match = () => ({
17-
'new RegExp(__a)': match,
18-
'new RegExp(__a, __b)': match,
14+
export const match = () => ({
15+
'new RegExp(__a)': check,
16+
'new RegExp(__a, __b)': check,
1917

20-
'RegExp(__a)': match,
21-
'RegExp(__a, __b)': match,
18+
'RegExp(__a)': check,
19+
'RegExp(__a, __b)': check,
2220
});
2321

24-
module.exports.replace = () => ({
22+
export const replace = () => ({
2523
'new RegExp(__a)': oneArgumentReplace,
2624
'new RegExp(__a, __b)': twoArgumentsReplace,
2725

packages/plugin-regexp/lib/apply-literal-notation/index.spec.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
'use strict';
1+
import {createTest} from '@putout/test';
2+
import * as removeUselessEscape from '@putout/plugin-remove-useless-escape';
3+
import * as applyLiteralNotation from './index.js';
4+
import * as removeUselessGroup from '../remove-useless-group/index.js';
25

3-
const {createTest} = require('@putout/test');
4-
5-
const removeUselessEscape = require('@putout/plugin-remove-useless-escape');
6-
const applyLiteralNotation = require('.');
7-
const removeUselessGroup = require('../remove-useless-group');
8-
9-
const test = createTest(__dirname, {
6+
const test = createTest(import.meta.url, {
107
plugins: [
118
['regexp/apply-literal-notation', applyLiteralNotation],
129
],

packages/plugin-regexp/lib/apply-starts-with/index.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
'use strict';
1+
import {types} from 'putout';
22

3-
const {types} = require('putout');
43
const {isRegExpLiteral} = types;
54

6-
module.exports.report = () => `Use '.startsWith()' instead of '.test()'`;
5+
export const report = () => `Use '.startsWith()' instead of '.test()'`;
76

8-
module.exports.match = () => ({
7+
export const match = () => ({
98
'__a.test(__b)': ({__a}) => {
109
if (!isRegExpLiteral(__a))
1110
return false;
@@ -19,7 +18,7 @@ module.exports.match = () => ({
1918
},
2019
});
2120

22-
module.exports.replace = () => ({
21+
export const replace = () => ({
2322
'__a.test(__b)': ({__a}) => {
2423
const str = __a
2524
.raw

packages/plugin-regexp/lib/apply-starts-with/index.spec.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict';
1+
import {createTest} from '@putout/test';
2+
import * as optimize from './index.js';
23

3-
const {createTest} = require('@putout/test');
4-
const optimize = require('.');
5-
6-
const test = createTest(__dirname, {
4+
const test = createTest(import.meta.url, {
75
plugins: [
86
['regexp/apply-starts-with', optimize],
97
],

packages/plugin-regexp/lib/convert-replace-to-replace-all/index.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
'use strict';
2-
3-
const {types, operator} = require('putout');
1+
import {types, operator} from 'putout';
42

53
const {stringLiteral} = types;
64

@@ -23,9 +21,9 @@ const decode = (a) => {
2321
.replaceAll('\\\\', '\\');
2422
};
2523

26-
module.exports.report = () => `Use 'replaceAll()' instead of 'replace()'`;
24+
export const report = () => `Use 'replaceAll()' instead of 'replace()'`;
2725

28-
module.exports.fix = ({path, pattern}) => {
26+
export const fix = ({path, pattern}) => {
2927
const regExpPath = path.get('arguments.0');
3028
const calleePath = path.get('callee.property');
3129

@@ -35,7 +33,7 @@ module.exports.fix = ({path, pattern}) => {
3533
return path;
3634
};
3735

38-
module.exports.traverse = ({push}) => ({
36+
export const traverse = ({push}) => ({
3937
'__a.replace(/__b/g, __c)': (path) => {
4038
const {__b} = getTemplateValues(path, '__a.replace(/__b/g, __c)');
4139

packages/plugin-regexp/lib/convert-replace-to-replace-all/index.spec.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
'use strict';
1+
import {createTest} from '@putout/test';
2+
import * as convertReplaceToReplaceAll from './index.js';
3+
import * as optimize from '../optimize/index.js';
24

3-
const {createTest} = require('@putout/test');
4-
5-
const convertReplaceToReplaceAll = require('.');
6-
const optimize = require('../optimize');
7-
8-
const test = createTest(__dirname, {
5+
const test = createTest(import.meta.url, {
96
plugins: [
107
['regexp/convert-replace-to-replace-all', convertReplaceToReplaceAll],
118
],
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1-
'use strict';
2-
3-
const {types, operator} = require('putout');
1+
import {types, operator} from 'putout';
42

53
const {replaceWith, isSimpleRegExp} = operator;
6-
74
const {stringLiteral} = types;
85

9-
const match = (flags) => ({__b}) => {
6+
export const report = () => 'String should be used instead of RegExp';
7+
8+
export const match = () => ({
9+
'__a.replace(/__b/, __c)': check(''),
10+
'__a.replaceAll(/__b/g, __c)': check('g'),
11+
});
12+
13+
export const replace = () => ({
14+
'__a.replace(/__b/, __c)': transform,
15+
'__a.replaceAll(/__b/g, __c)': transform,
16+
});
17+
18+
const check = (flags) => ({__b}) => {
1019
if (__b.flags === flags) {
1120
const {raw} = __b.extra;
1221
return isSimpleRegExp(raw);
@@ -15,23 +24,11 @@ const match = (flags) => ({__b}) => {
1524
return false;
1625
};
1726

18-
const replace = ({__b}, path) => {
27+
const transform = ({__b}, path) => {
1928
const {pattern} = __b;
2029
const regExpPath = path.get('arguments.0');
2130

2231
replaceWith(regExpPath, stringLiteral(pattern));
2332

2433
return path;
2534
};
26-
27-
module.exports.report = () => 'String should be used instead of RegExp';
28-
29-
module.exports.match = () => ({
30-
'__a.replace(/__b/, __c)': match(''),
31-
'__a.replaceAll(/__b/g, __c)': match('g'),
32-
});
33-
34-
module.exports.replace = () => ({
35-
'__a.replace(/__b/, __c)': replace,
36-
'__a.replaceAll(/__b/g, __c)': replace,
37-
});

packages/plugin-regexp/lib/convert-to-string/index.spec.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict';
1+
import {createTest} from '@putout/test';
2+
import * as convertToString from './index.js';
23

3-
const {createTest} = require('@putout/test');
4-
const convertToString = require('.');
5-
6-
const test = createTest(__dirname, {
4+
const test = createTest(import.meta.url, {
75
plugins: [
86
['regexp/convert-to-string', convertToString],
97
],

packages/plugin-regexp/lib/index.js

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
'use strict';
1+
import * as applyLiteralNotation from './apply-literal-notation/index.js';
2+
import * as applyStartsWith from './apply-starts-with/index.js';
3+
import * as applyEndsWith from './apply-ends-with/index.js';
4+
import * as optimize from './optimize/index.js';
5+
import * as convertToString from './convert-to-string/index.js';
6+
import * as convertReplaceToReplaceAll from './convert-replace-to-replace-all/index.js';
7+
import * as removeUselessGroup from './remove-useless-group/index.js';
8+
import * as removeUselessRegexp from './remove-useless-regexp/index.js';
29

3-
const applyLiteralNotation = require('./apply-literal-notation');
4-
const applyStartsWith = require('./apply-starts-with');
5-
const applyEndsWith = require('./apply-ends-with');
6-
const optimize = require('./optimize');
7-
const convertToString = require('./convert-to-string');
8-
const convertReplaceToReplaceAll = require('./convert-replace-to-replace-all');
9-
const removeUselessGroup = require('./remove-useless-group');
10-
const removeUselessRegexp = require('./remove-useless-regexp');
11-
12-
module.exports.rules = {
10+
export const rules = {
1311
'apply-literal-notation': applyLiteralNotation,
1412
'apply-starts-with': applyStartsWith,
1513
'apply-ends-with': applyEndsWith,

packages/plugin-regexp/lib/optimize/index.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
'use strict';
2-
3-
const tryCatch = require('try-catch');
4-
5-
const {optimize} = require('regexp-tree');
1+
import tryCatch from 'try-catch';
2+
import regexpTree from 'regexp-tree';
63

74
const cutSlashes = (a) => a
85
.split('/')
@@ -19,17 +16,25 @@ const options = {
1916
blacklist,
2017
};
2118

22-
module.exports.report = ({pattern, to}) => `RegExp /${pattern}/ can be optimized to /${to}/`;
19+
export const report = ({pattern, to}) => `RegExp /${pattern}/ can be optimized to /${to}/`;
2320

24-
module.exports.fix = ({path, to, flags}) => {
21+
export const fix = ({path, to, flags}) => {
2522
path.node.raw = `/${to}/${flags}`;
2623
path.node.pattern = to;
2724
};
2825

29-
module.exports.traverse = ({push}) => ({
26+
export const traverse = ({push}) => ({
3027
RegExpLiteral(path) {
3128
const {pattern, flags} = path.node;
32-
const [error, result] = tryCatch(optimize, RegExp(pattern, flags), whitelist, options);
29+
const [error, result] = tryCatch(
30+
regexpTree.optimize,
31+
RegExp(
32+
pattern,
33+
flags,
34+
),
35+
whitelist,
36+
options,
37+
);
3338

3439
if (error)
3540
return;

packages/plugin-regexp/lib/optimize/index.spec.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
'use strict';
1+
import {createTest} from '@putout/test';
2+
import * as optimize from './index.js';
3+
import * as convertReplaceToReplaceAll from '../convert-replace-to-replace-all/index.js';
24

3-
const {createTest} = require('@putout/test');
4-
const optimize = require('.');
5-
const convertReplaceToReplaceAll = require('../convert-replace-to-replace-all');
6-
7-
const test = createTest(__dirname, {
5+
const test = createTest(import.meta.url, {
86
plugins: [
97
['regexp/optimize', optimize],
108
],

packages/plugin-regexp/lib/remove-useless-group/index.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
'use strict';
2-
3-
const regexpTree = require('regexp-tree');
4-
const {compare} = require('putout').operator;
5-
const {
1+
import regexpTree from 'regexp-tree';
2+
import {operator} from 'putout';
3+
import {
64
isDisjunction,
75
isParentDisjunction,
8-
} = require('../types');
6+
} from '../types.js';
7+
8+
const {compare} = operator;
99

10-
module.exports.report = ({from, to}) => `Remove useless group from RegExp ${from}, use ${to}`;
10+
export const report = ({from, to}) => `Remove useless group from RegExp ${from}, use ${to}`;
1111

12-
module.exports.exclude = () => [
12+
export const exclude = () => [
1313
'__.match(__)',
1414
'__.split(__)',
1515
'__.exec(__)',
@@ -18,15 +18,15 @@ module.exports.exclude = () => [
1818
'const __a = /__b/',
1919
];
2020

21-
module.exports.fix = ({path, to}) => {
21+
export const fix = ({path, to}) => {
2222
const [, pattern] = to.split('/');
2323

2424
path.node.pattern = pattern;
2525
path.node.raw = to;
2626
path.node.extra.raw = to;
2727
};
2828

29-
module.exports.traverse = ({push}) => ({
29+
export const traverse = ({push}) => ({
3030
RegExpLiteral(path) {
3131
if (!includes(path))
3232
return;

packages/plugin-regexp/lib/remove-useless-group/index.spec.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
'use strict';
1+
import {createTest} from '@putout/test';
2+
import * as removeUselessGroup from './index.js';
3+
import * as applyLiteralNotation from '../apply-literal-notation/index.js';
4+
import * as optimize from '../optimize/index.js';
25

3-
const {createTest} = require('@putout/test');
4-
const removeUselessGroup = require('.');
5-
const applyLiteralNotation = require('../apply-literal-notation');
6-
7-
const optimize = require('../optimize');
8-
9-
const test = createTest(__dirname, {
6+
const test = createTest(import.meta.url, {
107
plugins: [
118
['regexp/remove-useless-group', removeUselessGroup],
129
],

0 commit comments

Comments
 (0)