Commit 6b932e3
committed
Ensure exhaustive switch statements
PR #162 revealed that missing node type handlers in switch statements
cause silent data loss - CDATA content was being dropped because
CharDataNode wasn't handled. This PR adds defensive programming to
prevent this entire class of bug.
Changes:
- Added exhaustive switch coverage for all xmlquery.NodeType values
- Added exhaustive switch coverage for xml.Token and html.TokenType
- Added exhaustive switch coverage for json.Token and json.Delim
- Added default cases with panic() to all exhaustive switches
- Added explicit documented defaults to intentionally non-exhaustive switches
This follows the Go stdlib pattern (runtime/panic.go, go/types) of using
panic("unreachable") for "should be impossible" states. If future xmlquery
versions add new node types, or if we overlook a handler, tests will fail
immediately rather than silently corrupting output.
All switches now explicitly handle defaults:
- 8 exhaustive switches panic on unknown values
- 7 non-exhaustive switches have documented intentional behavior
Added TestExhaustiveNodeTypeHandling to verify all node types are
processed without panicking. All existing tests pass.
This is a non-breaking change - no API modifications, only defensive
additions to switch statements.
Related: #1621 parent ec5a59a commit 6b932e3
File tree
4 files changed
+76
-1
lines changed- internal/utils
4 files changed
+76
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
| 64 | + | |
| 65 | + | |
64 | 66 | | |
65 | 67 | | |
66 | 68 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
39 | 40 | | |
40 | 41 | | |
41 | 42 | | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
42 | 48 | | |
43 | 49 | | |
44 | 50 | | |
| |||
53 | 59 | | |
54 | 60 | | |
55 | 61 | | |
56 | | - | |
| 62 | + | |
| 63 | + | |
57 | 64 | | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
58 | 69 | | |
59 | 70 | | |
60 | 71 | | |
| |||
79 | 90 | | |
80 | 91 | | |
81 | 92 | | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
82 | 98 | | |
83 | 99 | | |
84 | 100 | | |
| |||
103 | 119 | | |
104 | 120 | | |
105 | 121 | | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
106 | 127 | | |
107 | 128 | | |
108 | 129 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
190 | 190 | | |
191 | 191 | | |
192 | 192 | | |
| 193 | + | |
| 194 | + | |
193 | 195 | | |
194 | 196 | | |
195 | 197 | | |
| |||
403 | 405 | | |
404 | 406 | | |
405 | 407 | | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
406 | 411 | | |
407 | 412 | | |
408 | 413 | | |
| |||
469 | 474 | | |
470 | 475 | | |
471 | 476 | | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
472 | 480 | | |
473 | 481 | | |
474 | 482 | | |
| |||
485 | 493 | | |
486 | 494 | | |
487 | 495 | | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
488 | 499 | | |
489 | 500 | | |
490 | 501 | | |
| |||
494 | 505 | | |
495 | 506 | | |
496 | 507 | | |
| 508 | + | |
| 509 | + | |
497 | 510 | | |
498 | 511 | | |
499 | 512 | | |
| |||
0 commit comments