@@ -7,13 +7,9 @@ import {
77 CharacterClass ,
88 SpecialChar ,
99} from 'regexp-tree/ast' ;
10+ import { Chars } from '../constants' ;
1011import * 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' ;
1713
1814type Replace < ParentType extends AstClass > = (
1915 parentNode : AsExpression < ParentType > ,
@@ -59,73 +55,63 @@ const replacer: NodeReplacer = {
5955 } ,
6056} ;
6157
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 ) ) ;
7365const 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 ) ,
8668] ;
8769
70+ const optionsExtended = Chars . extended . map ( createSimpleChar ) ;
71+
8872function getMetaCharExpressions (
8973 metaChar : SpecialChar ,
9074 regExpFlags : string
9175) : CharacterClass [ 'expressions' ] {
9276 switch ( metaChar . value ) {
9377 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 ) ;
9584
9685 return [
9786 ...optionsAlpha ,
98- optionsDigit ,
87+ ... optionsDigit ,
9988 ...optionsWhitespaceNoBreak ,
10089 ...optionsOther ,
101- optionUnderscore ,
102- ...optionsExtendedAscii ,
103- ...dotAllOptions ,
90+ ...optionsExtended ,
91+ ...optionsDotAll ,
10492 ] ;
10593 }
10694 case '\\w' :
107- return [ ...optionsAlpha , optionsDigit , optionUnderscore ] ;
95+ return [ ...optionsAlpha , ... optionsDigit ] ;
10896 case '\\W' :
109- return [ ...optionsWhitespace , ...optionsOther , ...optionsExtendedAscii ] ;
97+ return [ ...optionsWhitespace , ...optionsOther , ...optionsExtended ] ;
11098 case '\\d' :
111- return [ optionsDigit ] ;
99+ return optionsDigit ;
112100 case '\\D' :
113101 return [
114102 ...optionsAlpha ,
115103 ...optionsWhitespace ,
116104 ...optionsOther ,
117- optionUnderscore ,
118- ...optionsExtendedAscii ,
105+ ...optionsExtended ,
119106 ] ;
120107 case '\\s' :
121108 return optionsWhitespace ;
122109 case '\\S' :
123110 return [
124111 ...optionsAlpha ,
125- optionsDigit ,
112+ ... optionsDigit ,
126113 ...optionsOther ,
127- optionUnderscore ,
128- ...optionsExtendedAscii ,
114+ ...optionsExtended ,
129115 ] ;
130116 default :
131117 return [ ] ;
0 commit comments