@@ -587,7 +587,9 @@ function UpdateRefCount(key, path_switch) {
587
587
const start = info_key [ key ] . start
588
588
info_key [ key ] . value [ start ] = 1
589
589
path_switch . traverse ( visitor_value , { name : key } )
590
- console . info ( `Key: ${ key } Size: ${ Object . keys ( info_key [ key ] . value ) . length } ` )
590
+ console . info (
591
+ `Switch: ${ key } Size: ${ Object . keys ( info_key [ key ] . value ) . length } `
592
+ )
591
593
}
592
594
593
595
/**
@@ -690,7 +692,7 @@ function UpdateSwitchCases(key, path_switch, nodes, queue) {
690
692
)
691
693
delete nodes [ value ]
692
694
} else {
693
- console . error ( `Missing case ${ value } in switch ${ key } ` )
695
+ console . error ( `Missing Case ${ value } in Switch ${ key } ` )
694
696
}
695
697
}
696
698
for ( let value in nodes ) {
@@ -741,7 +743,8 @@ function FlattenSwitch(ast) {
741
743
}
742
744
body = choice . node . consequent [ 0 ] . body
743
745
if ( ! ( c in mp ) ) {
744
- console . warn ( `drop key ${ key } :${ c } ` )
746
+ // This case is not referenced
747
+ console . warn ( `Drop Case ${ c } in Switch ${ key } ` )
745
748
continue
746
749
}
747
750
if ( mp [ c ] . length > 1 ) {
@@ -917,40 +920,50 @@ function MergeSwitch(ast) {
917
920
} )
918
921
}
919
922
920
- function FlattenFor ( ast ) {
921
- traverse ( ast , {
922
- ForStatement ( path ) {
923
- let { init, test, update, body } = path . node
924
- if ( ! update || generator ( update ) . code . indexOf ( '++' ) == - 1 ) {
925
- return
926
- }
927
- body . body . push ( t . expressionStatement ( update ) )
928
- path . insertBefore ( init )
929
- const repl = t . whileStatement ( test , body )
930
- path . replaceWith ( repl )
931
- } ,
932
- } )
923
+ /**
924
+ * In this scenario, some ForStatements are used to decode a string.
925
+ * We can convert these codes to WhileStatement for further processing.
926
+ */
927
+ const ConvertFor = {
928
+ ForStatement ( path ) {
929
+ let { init, test, update, body } = path . node
930
+ if ( ! update || generator ( update ) . code . indexOf ( '++' ) == - 1 ) {
931
+ return
932
+ }
933
+ body . body . push ( t . expressionStatement ( update ) )
934
+ path . insertBefore ( init )
935
+ const repl = t . whileStatement ( test , body )
936
+ path . replaceWith ( repl )
937
+ } ,
933
938
}
934
939
935
- function SplitVarDef ( ast ) {
936
- traverse ( ast , {
937
- VariableDeclaration ( path ) {
938
- if ( t . isForStatement ( path . parent ) ) {
939
- return
940
- }
941
- const kind = path . node . kind
942
- const list = path . node . declarations
943
- if ( list . length == 1 ) {
944
- return
945
- }
946
- for ( let item of list ) {
947
- path . insertBefore ( t . variableDeclaration ( kind , [ item ] ) )
948
- }
949
- path . remove ( )
950
- } ,
951
- } )
940
+ /**
941
+ * Split the variable declarator. (Cannot be performed before `CollectVars`)
942
+ */
943
+ const SplitVarDef = {
944
+ VariableDeclaration ( path ) {
945
+ if ( t . isForStatement ( path . parent ) ) {
946
+ return
947
+ }
948
+ const kind = path . node . kind
949
+ const list = path . node . declarations
950
+ if ( list . length == 1 ) {
951
+ return
952
+ }
953
+ for ( let item of list ) {
954
+ path . insertBefore ( t . variableDeclaration ( kind , [ item ] ) )
955
+ }
956
+ path . remove ( )
957
+ } ,
952
958
}
953
959
960
+ /**
961
+ * Split the AssignmentExpressions. For example:
962
+ *
963
+ * - In the test of IfStatement
964
+ * - In the VariableDeclaration
965
+ * - Nested Expression (Assignment...)
966
+ */
954
967
function MoveAssignment ( ast ) {
955
968
// post order traversal
956
969
let visitor = {
@@ -1183,11 +1196,11 @@ export default function (code) {
1183
1196
// Flatten nested switch
1184
1197
FlattenSwitch ( ast )
1185
1198
// Convert some for to while
1186
- FlattenFor ( ast )
1199
+ traverse ( ast , ConvertFor )
1187
1200
// After the conversion, we should split some expressions,
1188
1201
// to help get constant test results in the if statement.
1189
1202
// The Variable Declaration list must be splitted first
1190
- SplitVarDef ( ast )
1203
+ traverse ( ast , SplitVarDef )
1191
1204
// Then, the assignment should be splitted
1192
1205
MoveAssignment ( ast )
1193
1206
// Merge switch case
0 commit comments