fix: correct parseInt radix parameters and use parseFloat for dimensions#576
Open
claw-explorer wants to merge 1 commit intotscircuit:mainfrom
Open
fix: correct parseInt radix parameters and use parseFloat for dimensions#576claw-explorer wants to merge 1 commit intotscircuit:mainfrom
claw-explorer wants to merge 1 commit intotscircuit:mainfrom
Conversation
Three footprint generators used the wrong radix in Number.parseInt(),
causing string-based pin count parsing to silently return NaN:
- sot323.ts: parseInt(match[1]!, 3) → parseInt(match[1]!, 10)
Radix 3 only accepts digits 0-2, so '3' returns NaN
- sot23w.ts: parseInt(match[1]!, 3) → parseInt(match[1]!, 10)
Same base-3 issue
- sot343.ts: parseInt(match[1]!, 4) → parseInt(match[1]!, 10)
Radix 4 only accepts digits 0-3, so '4' returns NaN
The second argument to parseInt() was accidentally set to the default
pin count instead of the radix (10). This meant parsing any pin count
from the string always failed, falling back to defaults.
Also fixes parseInt → parseFloat for silkscreen ref text positioning
in sot23, sot23w, and sot323, where parseInt('2.50mm') truncated to
2 instead of using the correct 2.5mm value.
Adds explicit radix 10 to parseInt calls in to220, to220f, and bga
for consistency and to follow best practices.
All 389 tests pass (4 new regression tests added).
Comment on lines
+1
to
+41
| import { test, expect } from "bun:test" | ||
| import { fp } from "src/footprinter" | ||
|
|
||
| test("sot323 string parsing correctly reads num_pins", () => { | ||
| // Before fix: parseInt(match[1]!, 3) used base-3 radix, | ||
| // so digit "3" and above returned NaN | ||
| const params = fp.string("sot323").params() | ||
| expect(params.fn).toBe("sot323") | ||
| }) | ||
|
|
||
| test("sot343 string parsing correctly reads num_pins", () => { | ||
| // Before fix: parseInt(match[1]!, 4) used base-4 radix, | ||
| // so digit "4" and above returned NaN | ||
| const params = fp.string("sot343").params() | ||
| expect(params.fn).toBe("sot343") | ||
| const circuitJson = fp.string("sot343").circuitJson() | ||
| const smtpads = circuitJson.filter((e) => e.type === "pcb_smtpad") | ||
| expect(smtpads.length).toBe(4) | ||
| }) | ||
|
|
||
| test("sot23w generates 3 pads by default", () => { | ||
| const circuitJson = fp.string("sot23w").circuitJson() | ||
| const smtpads = circuitJson.filter((e) => e.type === "pcb_smtpad") | ||
| expect(smtpads.length).toBe(3) | ||
| }) | ||
|
|
||
| test("sot23 silkscreen ref uses parseFloat for height positioning", () => { | ||
| // Before fix: parseInt("2.50mm") returned 2, truncating the decimal | ||
| // After fix: parseFloat("2.50mm") returns 2.5 | ||
| const circuitJson = fp.string("sot23").circuitJson() | ||
| const silkscreenText = circuitJson.find( | ||
| (e) => e.type === "pcb_silkscreen_text", | ||
| ) | ||
| expect(silkscreenText).toBeDefined() | ||
| // With parseFloat, the Y position should use the full decimal value | ||
| // not just the integer part | ||
| if (silkscreenText && "anchor_position" in silkscreenText) { | ||
| // Y should be based on parseFloat("2.50mm") = 2.5, not parseInt("2.50mm") = 2 | ||
| expect(silkscreenText.anchor_position.y).not.toBe(2) | ||
| } | ||
| }) |
Contributor
There was a problem hiding this comment.
This test file contains 4 test() functions, which violates the rule that a *.test.ts file may have AT MOST one test(...). After that, the user should split into multiple, numbered files. The file should be split into separate files like parseint-radix1.test.ts, parseint-radix2.test.ts, parseint-radix3.test.ts, and parseint-radix4.test.ts, with each file containing only one test function.
Spotted by Graphite (based on custom rule: Custom rule)
Is this helpful? React 👍 or 👎 to let us know.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes latent bugs in three footprint generators where
Number.parseInt()was called with the default pin count as the radix instead of base 10, causing string-based pin count parsing to silently returnNaN.The Bug
parseInt Radix Fixes
sot323.tsparseInt(match[1]!, 3)parseInt(match[1]!, 10)sot23w.tsparseInt(match[1]!, 3)parseInt(match[1]!, 10)sot343.tsparseInt(match[1]!, 4)parseInt(match[1]!, 10)to220.tsparseInt(...)parseInt(..., 10)to220f.tsparseInt(...)parseInt(..., 10)bga.tsparseInt(m[2]!)parseInt(m[2]!, 10)parseInt → parseFloat Fixes
sot23.tsparseInt(parameters.h)parseFloat(parameters.h)parseInt('2.50mm')→ 2 (truncated);parseFloat→ 2.5 (correct)sot23w.tsparseInt(parameters.h)parseFloat(parameters.h)sot323.tsparseInt(parameters.h)parseFloat(parameters.h)Test Plan
tests/parseint-radix.test.tsvalidates:bun run format— no formatting changes needed