@@ -7,13 +7,9 @@ import {
7
7
CharacterClass ,
8
8
SpecialChar ,
9
9
} from 'regexp-tree/ast' ;
10
+ import { Chars } from '../constants' ;
10
11
import * as Guards from '../types/regexp-tree-guards' ;
11
- import {
12
- createClassRange ,
13
- createEscapedSimpleChar ,
14
- createSimpleChar ,
15
- createSimpleChars ,
16
- } from './utils' ;
12
+ import { createEscapedSimpleChar , createSimpleChar } from './utils' ;
17
13
18
14
type Replace < ParentType extends AstClass > = (
19
15
parentNode : AsExpression < ParentType > ,
@@ -59,73 +55,63 @@ const replacer: NodeReplacer = {
59
55
} ,
60
56
} ;
61
57
62
- const optionsAlpha = [ createClassRange ( 'a' , 'z' ) , createClassRange ( 'A' , 'Z' ) ] ;
63
- const optionsDigit = createClassRange ( '0' , '9' ) ;
64
- const optionUnderscore = createEscapedSimpleChar ( '_' ) ;
65
- const optionsWhitespaceNoBreak = [
66
- ...createSimpleChars ( ' \t' ) ,
67
- createSimpleChar ( String . fromCharCode ( 160 ) ) , //
68
- ] ;
69
- const optionsWhitespace = [
70
- ...optionsWhitespaceNoBreak ,
71
- ...createSimpleChars ( '\r\n' ) ,
72
- ] ;
58
+ const optionsAlpha = Chars . basicAlpha . map ( createSimpleChar ) ;
59
+ const optionsDigit = Chars . digits . map ( createSimpleChar ) ;
60
+
61
+ const optionsWhitespace = Chars . whitespace . map ( createSimpleChar ) ;
62
+
63
+ const needEscape = [ ']' , '-' , '\\' ] ;
64
+ const noEscape = Chars . basicSpecial . filter ( c => ! needEscape . includes ( c ) ) ;
73
65
const optionsOther = [
74
- ...createSimpleChars ( '~`!@#$%^&*()=+<,>.?/[{}|:;"\'' ) ,
75
- createEscapedSimpleChar ( ']' ) ,
76
- createEscapedSimpleChar ( '-' ) ,
77
- createEscapedSimpleChar ( '\\' ) ,
78
- ] ;
79
- const optionsNewLine = createSimpleChar ( '\n' ) ;
80
- const optionsExtendedAscii = [
81
- ...createSimpleChars ( 'àáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ' ) ,
82
- ...createSimpleChars ( 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞß' ) ,
83
- ...createSimpleChars ( '¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿' ) ,
84
- ...createSimpleChars ( '€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ×÷' ) ,
85
- createSimpleChar ( String . fromCharCode ( 173 ) ) , // ­
66
+ ...noEscape . map ( createSimpleChar ) ,
67
+ ...needEscape . map ( createEscapedSimpleChar ) ,
86
68
] ;
87
69
70
+ const optionsExtended = Chars . extended . map ( createSimpleChar ) ;
71
+
88
72
function getMetaCharExpressions (
89
73
metaChar : SpecialChar ,
90
74
regExpFlags : string
91
75
) : CharacterClass [ 'expressions' ] {
92
76
switch ( metaChar . value ) {
93
77
case '.' : {
94
- const dotAllOptions = regExpFlags . includes ( 's' ) ? [ optionsNewLine ] : [ ] ;
78
+ const optionsNewLine = createSimpleChar ( '\n' ) ;
79
+ const optionsDotAll = regExpFlags . includes ( 's' ) ? [ optionsNewLine ] : [ ] ;
80
+ const whitespaceNoBreaks = Chars . whitespace . filter (
81
+ c => ! '\r\n' . includes ( c )
82
+ ) ;
83
+ const optionsWhitespaceNoBreak = whitespaceNoBreaks . map ( createSimpleChar ) ;
95
84
96
85
return [
97
86
...optionsAlpha ,
98
- optionsDigit ,
87
+ ... optionsDigit ,
99
88
...optionsWhitespaceNoBreak ,
100
89
...optionsOther ,
101
- optionUnderscore ,
102
- ...optionsExtendedAscii ,
103
- ...dotAllOptions ,
90
+ ...optionsExtended ,
91
+ ...optionsDotAll ,
104
92
] ;
105
93
}
106
94
case '\\w' :
107
- return [ ...optionsAlpha , optionsDigit , optionUnderscore ] ;
95
+ return [ ...optionsAlpha , ... optionsDigit ] ;
108
96
case '\\W' :
109
- return [ ...optionsWhitespace , ...optionsOther , ...optionsExtendedAscii ] ;
97
+ return [ ...optionsWhitespace , ...optionsOther , ...optionsExtended ] ;
110
98
case '\\d' :
111
- return [ optionsDigit ] ;
99
+ return optionsDigit ;
112
100
case '\\D' :
113
101
return [
114
102
...optionsAlpha ,
115
103
...optionsWhitespace ,
116
104
...optionsOther ,
117
- optionUnderscore ,
118
- ...optionsExtendedAscii ,
105
+ ...optionsExtended ,
119
106
] ;
120
107
case '\\s' :
121
108
return optionsWhitespace ;
122
109
case '\\S' :
123
110
return [
124
111
...optionsAlpha ,
125
- optionsDigit ,
112
+ ... optionsDigit ,
126
113
...optionsOther ,
127
- optionUnderscore ,
128
- ...optionsExtendedAscii ,
114
+ ...optionsExtended ,
129
115
] ;
130
116
default :
131
117
return [ ] ;
0 commit comments