@@ -988,20 +988,16 @@ class Parser extends Tapable {
988
988
this . hooks . export . call ( statement ) ;
989
989
}
990
990
if ( statement . declaration ) {
991
- if ( / E x p r e s s i o n $ / . test ( statement . declaration . type ) ) {
992
- throw new Error ( "Doesn't occur?" ) ;
993
- } else {
994
- if ( ! this . hooks . exportDeclaration . call ( statement , statement . declaration ) ) {
995
- const originalDefinitions = this . scope . definitions ;
996
- const tracker = new TrackingSet ( this . scope . definitions ) ;
997
- this . scope . definitions = tracker ;
998
- this . prewalkStatement ( statement . declaration ) ;
999
- const newDefs = Array . from ( tracker . getAddedItems ( ) ) ;
1000
- this . scope . definitions = originalDefinitions ;
1001
- for ( let index = newDefs . length - 1 ; index >= 0 ; index -- ) {
1002
- const def = newDefs [ index ] ;
1003
- this . hooks . exportSpecifier . call ( statement , def , def , index ) ;
1004
- }
991
+ if ( ! this . hooks . exportDeclaration . call ( statement , statement . declaration ) ) {
992
+ const originalDefinitions = this . scope . definitions ;
993
+ const tracker = new TrackingSet ( this . scope . definitions ) ;
994
+ this . scope . definitions = tracker ;
995
+ this . prewalkStatement ( statement . declaration ) ;
996
+ const newDefs = Array . from ( tracker . getAddedItems ( ) ) ;
997
+ this . scope . definitions = originalDefinitions ;
998
+ for ( let index = newDefs . length - 1 ; index >= 0 ; index -- ) {
999
+ const def = newDefs [ index ] ;
1000
+ this . hooks . exportSpecifier . call ( statement , def , def , index ) ;
1005
1001
}
1006
1002
}
1007
1003
}
@@ -1030,7 +1026,7 @@ class Parser extends Tapable {
1030
1026
}
1031
1027
1032
1028
prewalkExportDefaultDeclaration ( statement ) {
1033
- if ( / D e c l a r a t i o n $ / . test ( statement . declaration . type ) ) {
1029
+ if ( statement . declaration . id ) {
1034
1030
const originalDefinitions = this . scope . definitions ;
1035
1031
const tracker = new TrackingSet ( this . scope . definitions ) ;
1036
1032
this . scope . definitions = tracker ;
@@ -1046,12 +1042,21 @@ class Parser extends Tapable {
1046
1042
1047
1043
walkExportDefaultDeclaration ( statement ) {
1048
1044
this . hooks . export . call ( statement ) ;
1049
- if ( / D e c l a r a t i o n $ / . test ( statement . declaration . type ) ) {
1045
+ if ( statement . declaration . id ) {
1050
1046
if ( ! this . hooks . exportDeclaration . call ( statement , statement . declaration ) ) {
1051
1047
this . walkStatement ( statement . declaration ) ;
1052
1048
}
1053
1049
} else {
1054
- this . walkExpression ( statement . declaration ) ;
1050
+ // Acorn parses `export default function() {}` as `FunctionDeclaration` and
1051
+ // `export default class {}` as `ClassDeclaration`, both with `id = null`.
1052
+ // These nodes must be treated as expressions.
1053
+ if ( statement . declaration . type === "FunctionDeclaration" ) {
1054
+ this . walkFunctionDeclaration ( statement . declaration ) ;
1055
+ } else if ( statement . declaration . type === "ClassDeclaration" ) {
1056
+ this . walkClassDeclaration ( statement . declaration ) ;
1057
+ } else {
1058
+ this . walkExpression ( statement . declaration ) ;
1059
+ }
1055
1060
if ( ! this . hooks . exportExpression . call ( statement , statement . declaration ) ) {
1056
1061
this . hooks . exportSpecifier . call ( statement , statement . declaration , "default" ) ;
1057
1062
}
0 commit comments