Skip to content

Commit c6cc733

Browse files
committed
refactor: move pin label validation from Chip to NormalComponent base class
1 parent 21c4a85 commit c6cc733

2 files changed

Lines changed: 38 additions & 35 deletions

File tree

lib/components/base-components/NormalComponent/NormalComponent.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import { Trace } from "lib/components/primitive-components/Trace/Trace"
4949
import { NormalComponent__getMinimumFlexContainerSize } from "./NormalComponent__getMinimumFlexContainerSize"
5050
import { NormalComponent__repositionOnPcb } from "./NormalComponent__repositionOnPcb"
5151
import { NormalComponent_doInitialSourceDesignRuleChecks } from "./NormalComponent_doInitialSourceDesignRuleChecks"
52+
import { filterPinLabels } from "lib/utils/filterPinLabels"
5253

5354
const debug = Debug("tscircuit:core")
5455

@@ -96,6 +97,8 @@ export class NormalComponent<
9697
pcb_missing_footprint_error_id?: string
9798
_hasStartedFootprintUrlLoad = false
9899

100+
private _invalidPinLabelMessages: string[] = []
101+
99102
/**
100103
* Override this property for component defaults
101104
*/
@@ -115,7 +118,20 @@ export class NormalComponent<
115118
}
116119

117120
constructor(props: z.input<ZodProps>) {
118-
super(props)
121+
const filteredProps = { ...props }
122+
let invalidPinLabelsMessages: string[] = []
123+
124+
// Apply invalid pin label filtering for object-based pinLabels only
125+
// Array-based pinLabels (used by PinHeader) are left unfiltered
126+
if (filteredProps.pinLabels && !Array.isArray(filteredProps.pinLabels)) {
127+
const { validPinLabels, invalidPinLabelsMessages: messages } =
128+
filterPinLabels(filteredProps.pinLabels)
129+
filteredProps.pinLabels = validPinLabels
130+
invalidPinLabelsMessages = messages
131+
}
132+
133+
super(filteredProps)
134+
this._invalidPinLabelMessages = invalidPinLabelsMessages
119135
this._addChildrenFromStringFootprint()
120136
this.initPorts()
121137
}
@@ -425,6 +441,26 @@ export class NormalComponent<
425441
if (this.root?.schematicDisabled) return
426442
const { db } = this.root!
427443

444+
// Insert warnings for invalid pin labels
445+
if (this._invalidPinLabelMessages?.length && this.root?.db) {
446+
for (const message of this._invalidPinLabelMessages) {
447+
let property_name = "pinLabels"
448+
const match = message.match(
449+
/^Invalid pin label:\s*([^=]+)=\s*'([^']+)'/,
450+
)
451+
if (match) {
452+
const label = match[2]
453+
property_name = `pinLabels['${label}']`
454+
}
455+
this.root.db.source_property_ignored_warning.insert({
456+
source_component_id: this.source_component_id!,
457+
property_name,
458+
message,
459+
error_type: "source_property_ignored_warning",
460+
})
461+
}
462+
}
463+
428464
const { schematicSymbolName } = this.config
429465

430466
if (schematicSymbolName) {

lib/components/normal-components/Chip.ts

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,16 @@ import { NormalComponent } from "lib/components/base-components/NormalComponent"
33
import { type SchematicBoxDimensions } from "lib/utils/schematic/getAllDimensionsForSchematicBox"
44
import { Trace } from "lib/components/primitive-components/Trace/Trace"
55
import { Port } from "lib/components/primitive-components/Port"
6-
import { filterPinLabels } from "lib/utils/filterPinLabels"
76
import type { z } from "zod"
87

98
export class Chip<PinLabels extends string = never> extends NormalComponent<
109
typeof chipProps,
1110
PinLabels
1211
> {
1312
schematicBoxDimensions: SchematicBoxDimensions | null = null
14-
private _invalidPinLabelMessages: string[] = []
1513

1614
constructor(props: z.input<typeof chipProps>) {
17-
const filteredProps = { ...props }
18-
let invalidPinLabelsMessages: string[] = []
19-
20-
if (filteredProps.pinLabels) {
21-
const { validPinLabels, invalidPinLabelsMessages: messages } =
22-
filterPinLabels(filteredProps.pinLabels)
23-
filteredProps.pinLabels = validPinLabels
24-
invalidPinLabelsMessages = messages
25-
}
26-
27-
// super needs to run before we can assign to `this`
28-
super(filteredProps)
29-
this._invalidPinLabelMessages = invalidPinLabelsMessages
15+
super(props)
3016
}
3117

3218
get config() {
@@ -90,25 +76,6 @@ export class Chip<PinLabels extends string = never> extends NormalComponent<
9076
// Early return if noSchematicRepresentation is true
9177
if (props?.noSchematicRepresentation === true) return
9278

93-
if (this._invalidPinLabelMessages?.length && this.root?.db) {
94-
for (const message of this._invalidPinLabelMessages) {
95-
let property_name = "pinLabels"
96-
const match = message.match(
97-
/^Invalid pin label:\s*([^=]+)=\s*'([^']+)'/,
98-
)
99-
if (match) {
100-
const label = match[2]
101-
property_name = `pinLabels['${label}']`
102-
}
103-
this.root.db.source_property_ignored_warning.insert({
104-
source_component_id: this.source_component_id!,
105-
property_name,
106-
message,
107-
error_type: "source_property_ignored_warning",
108-
})
109-
}
110-
}
111-
11279
// Continue with normal schematic rendering
11380
super.doInitialSchematicComponentRender()
11481
}

0 commit comments

Comments
 (0)