File tree Expand file tree Collapse file tree 2 files changed +43
-39
lines changed
packages/plugin-regexp/lib/remove-useless-regexp Expand file tree Collapse file tree 2 files changed +43
-39
lines changed Original file line number Diff line number Diff line change 1- import regexpTree from 'regexp-tree' ;
21import {
32 template ,
43 types ,
54 operator ,
65} from 'putout' ;
7- import { isAlternative , isChar } from '../types .js' ;
6+ import * as regexp from './regexp .js' ;
87
9- const { replaceWith} = operator ;
8+ const {
9+ replaceWith,
10+ transformRegExp,
11+ } = operator ;
1012const { stringLiteral} = types ;
1113
14+ const cut = ( a ) => a . slice ( START , END ) ;
15+ const wrap = ( a ) => `/${ a } /` ;
16+ const START = 1 ;
17+ const END = - 1 ;
18+
1219export const report = ( ) => `Remove useless RegExp, use strict equal operator instead` ;
1320
1421const build = template ( `A === B` ) ;
@@ -33,42 +40,15 @@ export const traverse = ({push}) => ({
3340
3441 const [ arg ] = path . node . arguments ;
3542 const to = cut ( str ) ;
36- const is = isOnlyChars ( wrap ( to ) ) ;
43+ const [ , places ] = transformRegExp ( wrap ( to ) , regexp ) ;
44+
45+ if ( ! places . length )
46+ return ;
3747
38- if ( is )
39- push ( {
40- path,
41- arg,
42- to,
43- } ) ;
48+ push ( {
49+ path,
50+ arg,
51+ to,
52+ } ) ;
4453 } ,
4554} ) ;
46-
47- const not = ( f ) => ( ...a ) => ! f ( ...a ) ;
48- const START = 1 ;
49- const END = - 1 ;
50- const cut = ( a ) => a . slice ( START , END ) ;
51- const wrap = ( a ) => `/${ a } /` ;
52-
53- function isOnlyChars ( str ) {
54- const ast = regexpTree . parse ( str ) ;
55- let is = false ;
56-
57- regexpTree . traverse ( ast , {
58- RegExp ( { node} ) {
59- const { body} = node ;
60-
61- if ( isChar ( body ) && ! body . value . includes ( '\\' ) ) {
62- is = true ;
63- return ;
64- }
65-
66- if ( isAlternative ( body ) ) {
67- is = ! body . expressions . filter ( not ( isChar ) ) . length ;
68- return ;
69- }
70- } ,
71- } ) ;
72-
73- return is ;
74- }
Original file line number Diff line number Diff line change 1+ import { isAlternative , isChar } from '../types.js' ;
2+
3+ const not = ( f ) => ( ...a ) => ! f ( ...a ) ;
4+
5+ export const report = ( ) => `Remove useless RegExp, use strict equal operator instead` ;
6+ export const fix = ( ) => { } ;
7+ export const traverse = ( { push} ) => ( {
8+ RegExp ( path ) {
9+ const { node} = path ;
10+ const { body} = node ;
11+
12+ if ( isChar ( body ) && ! body . value . includes ( '\\' ) ) {
13+ push ( path ) ;
14+ return ;
15+ }
16+
17+ if ( isAlternative ( body ) ) {
18+ if ( ! body . expressions . filter ( not ( isChar ) ) . length )
19+ push ( path ) ;
20+
21+ return ;
22+ }
23+ } ,
24+ } ) ;
You can’t perform that action at this time.
0 commit comments