@@ -587,7 +587,9 @@ function UpdateRefCount(key, path_switch) {
587587 const start = info_key [ key ] . start
588588 info_key [ key ] . value [ start ] = 1
589589 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+ )
591593}
592594
593595/**
@@ -690,7 +692,7 @@ function UpdateSwitchCases(key, path_switch, nodes, queue) {
690692 )
691693 delete nodes [ value ]
692694 } else {
693- console . error ( `Missing case ${ value } in switch ${ key } ` )
695+ console . error ( `Missing Case ${ value } in Switch ${ key } ` )
694696 }
695697 }
696698 for ( let value in nodes ) {
@@ -741,7 +743,8 @@ function FlattenSwitch(ast) {
741743 }
742744 body = choice . node . consequent [ 0 ] . body
743745 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 } ` )
745748 continue
746749 }
747750 if ( mp [ c ] . length > 1 ) {
@@ -917,40 +920,50 @@ function MergeSwitch(ast) {
917920 } )
918921}
919922
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+ } ,
933938}
934939
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+ } ,
952958}
953959
960+ /**
961+ * Split the AssignmentExpressions. For example:
962+ *
963+ * - In the test of IfStatement
964+ * - In the VariableDeclaration
965+ * - Nested Expression (Assignment...)
966+ */
954967function MoveAssignment ( ast ) {
955968 // post order traversal
956969 let visitor = {
@@ -1183,11 +1196,11 @@ export default function (code) {
11831196 // Flatten nested switch
11841197 FlattenSwitch ( ast )
11851198 // Convert some for to while
1186- FlattenFor ( ast )
1199+ traverse ( ast , ConvertFor )
11871200 // After the conversion, we should split some expressions,
11881201 // to help get constant test results in the if statement.
11891202 // The Variable Declaration list must be splitted first
1190- SplitVarDef ( ast )
1203+ traverse ( ast , SplitVarDef )
11911204 // Then, the assignment should be splitted
11921205 MoveAssignment ( ast )
11931206 // Merge switch case
0 commit comments