fix(preprocessor): don't print line when a parent level is unmet
With no defined dependencies and the following code to preprocess:
```
// #!if defined(FOO)
FOO is defined
// #!if defined(BAR)
FOO and BAR are defined
// #!else
FOO is defined but BAR isn't
// #!endif
// #!endif
```
The preprocessor would output this:
```
FOO is defined but BAR isn't
```
But the output should be empty.
This CL fixes this bug and adds a test to prevent regressions.
Change-Id: Id90b246ac75e51dffb5020ac081cc4616a6a6964
diff --git a/tools/preprocessor/preprocessor.go b/tools/preprocessor/preprocessor.go
index 85472a9..e6d5403 100644
--- a/tools/preprocessor/preprocessor.go
+++ b/tools/preprocessor/preprocessor.go
@@ -17,6 +17,12 @@
Satisfied bool
// true if currently processing the if branch. false if processing the else branch.
ProcessingIfBranch bool
+ // true if some parent level indicates that this line should not be printed.
+ ParentsBlockPrint bool
+}
+
+func (levelStatus *LevelStatus) shouldPrint() bool {
+ return !levelStatus.ParentsBlockPrint && ((levelStatus.ProcessingIfBranch && levelStatus.Satisfied) || (!levelStatus.ProcessingIfBranch && !levelStatus.Satisfied))
}
type preprocessor struct {
@@ -75,6 +81,7 @@
p.stack = append(p.stack, &LevelStatus{
Satisfied: query.Satisfies(p.definedDependencies),
ProcessingIfBranch: true,
+ ParentsBlockPrint: len(p.stack) > 0 && !p.stack[len(p.stack)-1].shouldPrint(),
})
return false, nil
}
@@ -104,6 +111,6 @@
return true, nil
}
lastLevelStatus := p.stack[len(p.stack)-1]
- shouldPrintLine := (lastLevelStatus.ProcessingIfBranch && lastLevelStatus.Satisfied) || (!lastLevelStatus.ProcessingIfBranch && !lastLevelStatus.Satisfied)
+ shouldPrintLine := lastLevelStatus.shouldPrint()
return shouldPrintLine, nil
}
diff --git a/tools/preprocessor/preprocessor_test.go b/tools/preprocessor/preprocessor_test.go
index 84848b0..40bc017 100644
--- a/tools/preprocessor/preprocessor_test.go
+++ b/tools/preprocessor/preprocessor_test.go
@@ -60,6 +60,10 @@
name: "Complex",
testDataFolder: "complex",
},
+ {
+ name: "NestedIfElse",
+ testDataFolder: "nested_if_else",
+ },
}
for _, test := range testSuites {
diff --git a/tools/preprocessor/test_data/nested_if_else/defined_dependencies.txt b/tools/preprocessor/test_data/nested_if_else/defined_dependencies.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tools/preprocessor/test_data/nested_if_else/defined_dependencies.txt
diff --git a/tools/preprocessor/test_data/nested_if_else/expected.txt b/tools/preprocessor/test_data/nested_if_else/expected.txt
new file mode 100644
index 0000000..5a92183
--- /dev/null
+++ b/tools/preprocessor/test_data/nested_if_else/expected.txt
@@ -0,0 +1,6 @@
+// This test should not output anything since ENABLE_KILL_SWITCH_MECHANISM is
+// not a defined dependency.
+//
+// Due to a regression, previous versions output the else branch of the
+// defined(PRODUCTION) if-else statement.
+//
diff --git a/tools/preprocessor/test_data/nested_if_else/source.txt b/tools/preprocessor/test_data/nested_if_else/source.txt
new file mode 100644
index 0000000..d2599f1
--- /dev/null
+++ b/tools/preprocessor/test_data/nested_if_else/source.txt
@@ -0,0 +1,34 @@
+// This test should not output anything since ENABLE_KILL_SWITCH_MECHANISM is
+// not a defined dependency.
+//
+// Due to a regression, previous versions output the else branch of the
+// defined(PRODUCTION) if-else statement.
+//
+// #!if defined(ENABLE_KILL_SWITCH_MECHANISM)
+const killSwitchMechanism = new KillSwitchMechanism(syncStorageAreaRepository);
+killSwitchMechanism.updateBadge();
+
+let KILL_SWITCH_UPDATE_PERIOD_IN_MINUTES: number;
+// #!if defined(PRODUCTION)
+KILL_SWITCH_UPDATE_PERIOD_IN_MINUTES = 30;
+// #!else
+KILL_SWITCH_UPDATE_PERIOD_IN_MINUTES = 1;
+// #!endif
+
+chrome.alarms.get('updateKillSwitchStatus', (alarm) => {
+ if (!alarm)
+ chrome.alarms.create('updateKillSwitchStatus', {
+ periodInMinutes: KILL_SWITCH_UPDATE_PERIOD_IN_MINUTES,
+ });
+});
+
+chrome.alarms.onAlarm.addListener((alarm) => {
+ if (alarm.name === 'updateKillSwitchStatus')
+ killSwitchMechanism.updateKillSwitchStatus();
+});
+
+// When the profile is first started, update the kill switch status.
+chrome.runtime.onStartup.addListener(() => {
+ killSwitchMechanism.updateKillSwitchStatus();
+});
+// #!endif