11const VARIABLE_PATERN = '(?!\\d)[\\w_-][\\w\\d_-]*' ;
2+ const MAP_KEY_PATTERN = '[\\w-]+' ;
23const VALUE_PATERN = '[^;]+|"(?:[^"]+|(?:\\\\"|[^"])*)"' ;
3- const DECLARATION_PATTERN =
4- `\\$['"]?(${ VARIABLE_PATERN } )['"]?\\s*:\\s*(${ VALUE_PATERN } )(?:\\s*!(global|default)\\s*;|\\s*;(?![^\\{]*\\}))` ;
54
6- const MAP_DECLARATIOM_REGEX = / [ ' " ] ? ( (? ! \d ) [ \w _ - ] [ \w \d _ - ] * ) [ ' " ] ? \s * : \s * ( [ a - z \- ] + \( [ ^ \) ] + \) | [ ^ \) \( , \/ ] + | \( [ ^ \) ] + \) ) / gi;
5+ const MAP_DECLARATIOM_REGEX = / [ ' " ] ? ( [ \w _ - ] [ \w \d _ - ] * ) [ ' " ] ? \s * : \s * ( [ a - z \- ] + \( [ ^ \) ] + \) | [ ^ \) \( , \/ ] + | \( [ ^ \) ] + \) ) / gi;
76
87const QUOTES_PATTERN = / ^ ( [ ' " ] ) .* \1$ / ;
98const QUOTES_REPLACE = / ^ ( [ ' " ] ) | ( [ ' " ] ) $ / g;
@@ -77,7 +76,7 @@ export class Parser {
7776
7877
7978 private extractDeclarationsStructured ( content : string ) : [ any ] {
80- const matches = content . match ( new RegExp ( `${ DECLARATION_PATTERN } |${ SECTION_PATTERN } |${ END_SECTION_PATTERN } ` , 'g' ) ) ;
79+ const matches = content . match ( new RegExp ( `${ this . getDeclarationPattern ( ) } |${ SECTION_PATTERN } |${ END_SECTION_PATTERN } ` , 'g' ) ) ;
8180
8281 if ( ! matches ) {
8382 return [ ] as any ;
@@ -88,7 +87,7 @@ export class Parser {
8887
8988
9089 private extractDeclarations ( content : string ) : [ any ] {
91- const matches = content . match ( new RegExp ( DECLARATION_PATTERN , 'g' ) ) ;
90+ const matches = content . match ( new RegExp ( this . getDeclarationPattern ( ) , 'g' ) ) ;
9291
9392 if ( ! matches ) {
9493 return [ ] as any ;
@@ -108,10 +107,10 @@ export class Parser {
108107 }
109108
110109
111- private parseSingleDeclaration ( matchDeclaration : string ) : IDeclaration {
110+ private parseSingleDeclaration ( matchDeclaration : string , isMap : boolean = false ) : IDeclaration {
112111 let matches = matchDeclaration
113112 . replace ( / \s * ! ( d e f a u l t | g l o b a l ) \s * ; / , ';' )
114- . match ( new RegExp ( DECLARATION_PATTERN ) ) ;
113+ . match ( new RegExp ( this . getDeclarationPattern ( isMap ) ) ) ;
115114
116115 if ( ! matches ) {
117116 return ;
@@ -132,7 +131,8 @@ export class Parser {
132131 if ( map . length ) {
133132 parsedDeclaration . mapValue = map . map ( ( declaration ) => {
134133 const singleDeclaration = this . parseSingleDeclaration (
135- `$${ declaration } ;`
134+ `$${ declaration } ;` ,
135+ true
136136 ) ;
137137 this . parseMapDeclarations ( singleDeclaration ) ;
138138
@@ -148,4 +148,8 @@ export class Parser {
148148 private checkIsSectionEnd ( content : string ) : boolean {
149149 return ( new RegExp ( END_SECTION_PATTERN , 'gi' ) ) . test ( content ) ;
150150 }
151+
152+ private getDeclarationPattern ( isMap : boolean = false ) : string {
153+ return `\\$['"]?(${ isMap ? MAP_KEY_PATTERN : VARIABLE_PATERN } )['"]?\\s*:\\s*(${ VALUE_PATERN } )(?:\\s*!(global|default)\\s*;|\\s*;(?![^\\{]*\\}))` ;
154+ }
151155}
0 commit comments