@@ -3,6 +3,20 @@ import ExecutionPathOptions from '../ExecutionPathOptions';
3
3
4
4
const functionOrClassDeclaration = / ^ (?: F u n c t i o n | C l a s s ) D e c l a r a t i o n / ;
5
5
6
+ function buildRegexWithSpaces ( re ) {
7
+ const spaceOrComment = "(?:" + [
8
+ / \s / . source , // Space
9
+ / \/ \/ .* [ \n \r ] / . source , // Single line comment
10
+ / \/ \* [ ^ ] * ?\* \/ / . source , // Multiline comment. There is [^] instead of . because it also matches \n
11
+ ] . join ( "|" ) + ")" ;
12
+ return new RegExp ( re . source . replace ( / \s | \\ s / g, spaceOrComment ) , re . flags ) ;
13
+ }
14
+
15
+ const sourceRE = {
16
+ exportDefault : buildRegexWithSpaces ( / ^ * e x p o r t + d e f a u l t * / ) ,
17
+ declarationHeader : buildRegexWithSpaces ( / ^ * e x p o r t + d e f a u l t + (?: (?: a s y n c + ) ? f u n c t i o n (?: * \* ) ? | c l a s s ) / ) ,
18
+ } ;
19
+
6
20
export default class ExportDefaultDeclaration extends Node {
7
21
bindNode ( ) {
8
22
if ( this . _declarationName ) {
@@ -37,7 +51,7 @@ export default class ExportDefaultDeclaration extends Node {
37
51
const statementStr = code . original . slice ( this . start , this . end ) ;
38
52
39
53
// paren workaround: find first non-whitespace character position after `export default`
40
- const declaration_start = this . start + statementStr . match ( / ^ \s * e x p o r t \s + d e f a u l t \s * / ) [ 0 ] . length ;
54
+ const declaration_start = this . start + statementStr . match ( sourceRE . exportDefault ) [ 0 ] . length ;
41
55
42
56
if ( functionOrClassDeclaration . test ( this . declaration . type ) ) {
43
57
if ( treeshakeable ) {
@@ -46,7 +60,7 @@ export default class ExportDefaultDeclaration extends Node {
46
60
47
61
// Add the id to anonymous declarations
48
62
if ( ! this . declaration . id ) {
49
- const id_insertPos = this . start + statementStr . match ( / ^ \s * e x p o r t \s + d e f a u l t \s * (?: f u n c t i o n | c l a s s ) / ) [ 0 ] . length ;
63
+ const id_insertPos = this . start + statementStr . match ( sourceRE . declarationHeader ) [ 0 ] . length ;
50
64
code . appendLeft ( id_insertPos , ` ${ name } ` ) ;
51
65
}
52
66
0 commit comments