Skip to content

Commit

Permalink
Merge pull request #244 from tscircuit/format
Browse files Browse the repository at this point in the history
Add formatbot
  • Loading branch information
seveibar authored Nov 7, 2024
2 parents 0701423 + 5dc7c46 commit 11eb337
Show file tree
Hide file tree
Showing 18 changed files with 182 additions and 76 deletions.
63 changes: 63 additions & 0 deletions .github/workflows/formatbot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: Format PR

on:
pull_request:
types: [opened, synchronize, reopened, ready_for_review]

jobs:
format:
name: Format code
runs-on: ubuntu-latest
if: github.event.pull_request.draft == false

steps:
- name: Determine if fork
id: check_fork
run: |
if [ "${{ github.event.pull_request.head.repo.full_name }}" = "${{ github.repository }}" ]; then
echo "is_fork=false" >> $GITHUB_OUTPUT
else
echo "is_fork=true" >> $GITHUB_OUTPUT
fi
- name: Checkout code
uses: actions/checkout@v3
with:
token: ${{ steps.check_fork.outputs.is_fork == 'true' && secrets.GITHUB_TOKEN || secrets.TSCIRCUIT_BOT_GITHUB_TOKEN }}

- name: Setup bun
uses: oven-sh/setup-bun@v1
with:
bun-version: latest

- name: Get @biomejs/biome version
id: get-biome-version
run: echo "BIOME_VERSION=$(node -p "require('./package.json').devDependencies['@biomejs/biome']")" >> $GITHUB_OUTPUT

- name: Install @biomejs/biome
run: bun install @biomejs/biome@${{ steps.get-biome-version.outputs.BIOME_VERSION }}

- name: Run Formatter and autofix
if: steps.check_fork.outputs.is_fork == 'false'
run: npx @biomejs/biome format . --write

- name: Format Check (cannot autofix against forks)
if: steps.check_fork.outputs.is_fork == 'true'
run: npx @biomejs/biome format .

- name: Restore lock files
if: steps.check_fork.outputs.is_fork == 'false'
run: |
git checkout -- *lock.json || true
git checkout -- *.lock || true
git checkout -- *.lockb || true
- name: Commit changes
if: steps.check_fork.outputs.is_fork == 'false'
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: "formatbot: Automatically format code"
branch: ${{ github.head_ref }}
commit_user_name: tscircuitbot
commit_user_email: [email protected]
commit_author: tscircuitbot <[email protected]>
16 changes: 14 additions & 2 deletions lib/components/normal-components/Capacitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,20 @@ export class Capacitor extends NormalComponent<
}

initPorts() {
this.add(new Port({ name: "pin1", pinNumber: 1, aliases: ["anode", "pos", "left"] }))
this.add(new Port({ name: "pin2", pinNumber: 2, aliases: ["cathode", "neg", "right"] }))
this.add(
new Port({
name: "pin1",
pinNumber: 1,
aliases: ["anode", "pos", "left"],
}),
)
this.add(
new Port({
name: "pin2",
pinNumber: 2,
aliases: ["cathode", "neg", "right"],
}),
)
}

doInitialCreateNetsFromProps() {
Expand Down
12 changes: 10 additions & 2 deletions lib/components/normal-components/Diode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,18 @@ export class Diode extends NormalComponent<

initPorts() {
this.add(
new Port({ name: "pin1", pinNumber: 1, aliases: ["anode", "pos", "left"] }),
new Port({
name: "pin1",
pinNumber: 1,
aliases: ["anode", "pos", "left"],
}),
)
this.add(
new Port({ name: "pin2", pinNumber: 2, aliases: ["cathode", "neg", "right"] }),
new Port({
name: "pin2",
pinNumber: 2,
aliases: ["cathode", "neg", "right"],
}),
)
}

Expand Down
6 changes: 5 additions & 1 deletion lib/components/normal-components/Inductor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { inductorProps } from "@tscircuit/props"
import type { SourceSimpleInductor } from "circuit-json"
import { FTYPE, type BaseSymbolName,type PassivePorts } from "lib/utils/constants"
import {
FTYPE,
type BaseSymbolName,
type PassivePorts,
} from "lib/utils/constants"
import { NormalComponent } from "../base-components/NormalComponent"
import { Port } from "../primitive-components/Port"

Expand Down
19 changes: 16 additions & 3 deletions lib/components/normal-components/Led.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,28 @@ export class Led extends NormalComponent<
get config() {
return {
componentName: "Led",
schematicSymbolName: this.props.symbolName ?? "led_horz" as BaseSymbolName,
schematicSymbolName:
this.props.symbolName ?? ("led_horz" as BaseSymbolName),
zodProps: ledProps,
sourceFtype: "simple_diode" as Ftype,
}
}

initPorts() {
this.add(new Port({ name: "pin1", pinNumber: 1, aliases: ["anode", "pos", "left"] }))
this.add(new Port({ name: "pin2", pinNumber: 2, aliases: ["cathode", "neg", "right"] }))
this.add(
new Port({
name: "pin1",
pinNumber: 1,
aliases: ["anode", "pos", "left"],
}),
)
this.add(
new Port({
name: "pin2",
pinNumber: 2,
aliases: ["cathode", "neg", "right"],
}),
)
}

pos = this.portMap.pin1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ test("chip with cadModel positionOffset", () => {
positionOffset: { x: 1, y: 2, z: 3 },
}}
/>
</board>
</board>,
)

project.render()
Expand Down
8 changes: 7 additions & 1 deletion tests/components/normal-components/inductor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ test("<inductor /> component", async () => {

circuit.add(
<board width="12mm" height="10mm">
<inductor name="U1" inductance="10" footprint="axial_p0.3in" pcbX={0} pcbY={0} />
<inductor
name="U1"
inductance="10"
footprint="axial_p0.3in"
pcbX={0}
pcbY={0}
/>
</board>,
)

Expand Down
18 changes: 9 additions & 9 deletions tests/components/normal-components/power-source.test.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { expect, it } from "bun:test";
import { getTestFixture } from "tests/fixtures/get-test-fixture";
import { expect, it } from "bun:test"
import { getTestFixture } from "tests/fixtures/get-test-fixture"

it("should render a power source", async () => {
const { project } = getTestFixture();
const { project } = getTestFixture()

project.add(
<board width="10mm" height="10mm">
<powersource name="pwr" voltage={5} schX={2} schY={3} pcbX={0} pcbY={0} />
</board>
);
</board>,
)

project.render();
expect(project.db.schematic_component.list()).toHaveLength(1);
expect(project.db.schematic_port.list()).toHaveLength(2);
});
project.render()
expect(project.db.schematic_component.list()).toHaveLength(1)
expect(project.db.schematic_port.list()).toHaveLength(2)
})
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
],
"edit_events": [],
"manual_trace_hints": []
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test("Manual edits prop usage", () => {

project.add(
<board width="10mm" height="10mm" manualEdits={manualEdits}>
<resistor name="R1" resistance="100" footprint="0402"/>
<resistor name="R1" resistance="100" footprint="0402" />
</board>,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ test("manual trace hints correctly change trace routes", async () => {
expect(traceRoute.map((p) => ("layer" in p ? p.layer : ""))).toContain(
"bottom",
)

expect(circuit.selectAll("tracehint").length).toBe(1)

expect(circuit.db.pcb_trace_hint.list().length).toBe(1)

expect(circuit.db.pcb_trace_hint.list()[0].pcb_port_id).toBeTruthy()

expect(circuit).toMatchPcbSnapshot(import.meta.path)
})
16 changes: 8 additions & 8 deletions tests/components/primitive-components/net-alias.test.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { expect, it } from "bun:test";
import { getTestFixture } from "tests/fixtures/get-test-fixture";
import { expect, it } from "bun:test"
import { getTestFixture } from "tests/fixtures/get-test-fixture"

it("should render a net alias", async () => {
const { project } = getTestFixture();
const { project } = getTestFixture()

project.add(
<board width="10mm" height="10mm">
<netalias net="net1" schX="1mm" schY="1mm" />
</board>
);
</board>,
)

project.render();
project.render()

expect(project.db.schematic_net_label.list()).toHaveLength(1);
});
expect(project.db.schematic_net_label.list()).toHaveLength(1)
})
30 changes: 15 additions & 15 deletions tests/components/primitive-components/silkscreen-rect.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { expect, test } from "bun:test";
import { getTestFixture } from "tests/fixtures/get-test-fixture";
import { expect, test } from "bun:test"
import { getTestFixture } from "tests/fixtures/get-test-fixture"

test("SilkscreenRect rendering", () => {
const { project } = getTestFixture();
const { project } = getTestFixture()
project.add(
<board width="10mm" height="10mm">
<silkscreenrect
Expand All @@ -12,18 +12,18 @@ test("SilkscreenRect rendering", () => {
height={"1.5mm"}
layer="bottom"
/>
</board>
);
project.render();
</board>,
)
project.render()

const silkscreenRects = project.db.pcb_silkscreen_rect.list();
const silkscreenRects = project.db.pcb_silkscreen_rect.list()

expect(silkscreenRects.length).toBe(1);
expect(silkscreenRects[0].center.x).toBe(2);
expect(silkscreenRects[0].center.y).toBe(3);
expect(silkscreenRects[0].width).toBe(2);
expect(silkscreenRects[0].height).toBe(1.5);
expect(silkscreenRects[0].layer).toBe("bottom");
expect(silkscreenRects.length).toBe(1)
expect(silkscreenRects[0].center.x).toBe(2)
expect(silkscreenRects[0].center.y).toBe(3)
expect(silkscreenRects[0].width).toBe(2)
expect(silkscreenRects[0].height).toBe(1.5)
expect(silkscreenRects[0].layer).toBe("bottom")

expect(project).toMatchPcbSnapshot(import.meta.path);
});
expect(project).toMatchPcbSnapshot(import.meta.path)
})
8 changes: 4 additions & 4 deletions tests/components/primitive-components/silkscreenpath.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ test("SilkscreenPath rendering", () => {
route={[
{ x: "0mm", y: "0mm" },
{ x: "5mm", y: "5mm" },
{ x: "10mm", y: "0mm" }
{ x: "10mm", y: "0mm" },
]}
strokeWidth="0.2mm"
layer="top"
/>
</board>
</board>,
)

project.render()
Expand All @@ -25,11 +25,11 @@ test("SilkscreenPath rendering", () => {
expect(silkscreenPaths.length).toBe(1)
expect(silkscreenPaths[0].layer).toBe("top")
expect(silkscreenPaths[0].stroke_width).toBe(0.2)

expect(silkscreenPaths[0].route).toEqual([
{ x: 0, y: 0 },
{ x: 5, y: 5 },
{ x: 10, y: 0 }
{ x: 10, y: 0 },
])

expect(silkscreenPaths[0].pcb_silkscreen_path_id).toBeTruthy()
Expand Down
22 changes: 11 additions & 11 deletions tests/components/primitive-components/via.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { expect, it } from "bun:test";
import type { Via } from "lib/components";
import { getTestFixture } from "tests/fixtures/get-test-fixture";
import { expect, it } from "bun:test"
import type { Via } from "lib/components"
import { getTestFixture } from "tests/fixtures/get-test-fixture"

it("should create a Via component with correct properties", () => {
const { project } = getTestFixture();
const { project } = getTestFixture()

project.add(
<board width="10mm" height="10mm">
Expand All @@ -15,15 +15,15 @@ it("should create a Via component with correct properties", () => {
fromLayer="top"
toLayer="bottom"
/>
</board>
);
</board>,
)

project.render();
project.render()

expect(project).toMatchPcbSnapshot(import.meta.path)

const via = project.selectOne("via") as Via;
const via = project.selectOne("via") as Via

expect(via).not.toBeNull();
expect(via.props.pcbX).toBe("0mm");
});
expect(via).not.toBeNull()
expect(via.props.pcbX).toBe("0mm")
})
2 changes: 1 addition & 1 deletion tests/examples/bug-high-port-number.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ test("bug high port number", async () => {
width={10}
height={10}
// @ts-ignore
// _schDebugObjectsEnabled
// _schDebugObjectsEnabled
>
<chip
name="U1"
Expand Down
Loading

0 comments on commit 11eb337

Please sign in to comment.