Skip to content

Commit ba4a71c

Browse files
committed
add comments
1 parent 92db378 commit ba4a71c

File tree

1 file changed

+48
-35
lines changed

1 file changed

+48
-35
lines changed

src/plugin/awsc.js

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,9 @@ function UpdateRefCount(key, path_switch) {
585585
const start = info_key[key].start
586586
info_key[key].value[start] = 1
587587
path_switch.traverse(visitor_value, { name: key })
588-
console.info(`Key: ${key} Size: ${Object.keys(info_key[key].value).length}`)
588+
console.info(
589+
`Switch: ${key} Size: ${Object.keys(info_key[key].value).length}`
590+
)
589591
}
590592

591593
/**
@@ -688,7 +690,7 @@ function UpdateSwitchCases(key, path_switch, nodes, queue) {
688690
)
689691
delete nodes[value]
690692
} else {
691-
console.error(`Missing case ${value} in switch ${key}`)
693+
console.error(`Missing Case ${value} in Switch ${key}`)
692694
}
693695
}
694696
for (let value in nodes) {
@@ -739,7 +741,8 @@ function FlattenSwitch(ast) {
739741
}
740742
body = choice.node.consequent[0].body
741743
if (!(c in mp)) {
742-
console.warn(`drop key ${key}:${c}`)
744+
// This case is not referenced
745+
console.warn(`Drop Case ${c} in Switch ${key}`)
743746
continue
744747
}
745748
if (mp[c].length > 1) {
@@ -915,40 +918,50 @@ function MergeSwitch(ast) {
915918
})
916919
}
917920

918-
function FlattenFor(ast) {
919-
traverse(ast, {
920-
ForStatement(path) {
921-
let { init, test, update, body } = path.node
922-
if (!update || generator(update).code.indexOf('++') == -1) {
923-
return
924-
}
925-
body.body.push(t.expressionStatement(update))
926-
path.insertBefore(init)
927-
const repl = t.whileStatement(test, body)
928-
path.replaceWith(repl)
929-
},
930-
})
921+
/**
922+
* In this scenario, some ForStatements are used to decode a string.
923+
* We can convert these codes to WhileStatement for further processing.
924+
*/
925+
const ConvertFor = {
926+
ForStatement(path) {
927+
let { init, test, update, body } = path.node
928+
if (!update || generator(update).code.indexOf('++') == -1) {
929+
return
930+
}
931+
body.body.push(t.expressionStatement(update))
932+
path.insertBefore(init)
933+
const repl = t.whileStatement(test, body)
934+
path.replaceWith(repl)
935+
},
931936
}
932937

933-
function SplitVarDef(ast) {
934-
traverse(ast, {
935-
VariableDeclaration(path) {
936-
if (t.isForStatement(path.parent)) {
937-
return
938-
}
939-
const kind = path.node.kind
940-
const list = path.node.declarations
941-
if (list.length == 1) {
942-
return
943-
}
944-
for (let item of list) {
945-
path.insertBefore(t.variableDeclaration(kind, [item]))
946-
}
947-
path.remove()
948-
},
949-
})
938+
/**
939+
* Split the variable declarator. (Cannot be performed before `CollectVars`)
940+
*/
941+
const SplitVarDef = {
942+
VariableDeclaration(path) {
943+
if (t.isForStatement(path.parent)) {
944+
return
945+
}
946+
const kind = path.node.kind
947+
const list = path.node.declarations
948+
if (list.length == 1) {
949+
return
950+
}
951+
for (let item of list) {
952+
path.insertBefore(t.variableDeclaration(kind, [item]))
953+
}
954+
path.remove()
955+
},
950956
}
951957

958+
/**
959+
* Split the AssignmentExpressions. For example:
960+
*
961+
* - In the test of IfStatement
962+
* - In the VariableDeclaration
963+
* - Nested Expression (Assignment...)
964+
*/
952965
function MoveAssignment(ast) {
953966
// post order traversal
954967
let visitor = {
@@ -1181,11 +1194,11 @@ module.exports = function (code) {
11811194
// Flatten nested switch
11821195
FlattenSwitch(ast)
11831196
// Convert some for to while
1184-
FlattenFor(ast)
1197+
traverse(ast, ConvertFor)
11851198
// After the conversion, we should split some expressions,
11861199
// to help get constant test results in the if statement.
11871200
// The Variable Declaration list must be splitted first
1188-
SplitVarDef(ast)
1201+
traverse(ast, SplitVarDef)
11891202
// Then, the assignment should be splitted
11901203
MoveAssignment(ast)
11911204
// Merge switch case

0 commit comments

Comments
 (0)