-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathpreprocessSelector.ts
More file actions
55 lines (53 loc) · 2.08 KB
/
Copy pathpreprocessSelector.ts
File metadata and controls
55 lines (53 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import type { PrimitiveComponent } from "./PrimitiveComponent"
const buildPlusMinusNetErrorMessage = (
selector: string,
component?: PrimitiveComponent,
) => {
const netName = selector.split("net.")[1]?.split(/[ >]/)[0] ?? selector
const componentName = component?.componentName ?? "Unknown component"
return (
`Net names cannot contain "+" or "-" (component "${componentName}" received "${netName}" via "${selector}"). ` +
`Try using underscores instead, e.g. VCC_P`
)
}
export const preprocessSelector = (
selector: string,
component?: PrimitiveComponent,
) => {
if (/net\.[^\s>]*\./.test(selector)) {
throw new Error(
'Net names cannot contain a period, try using "sel.net..." to autocomplete with conventional net names, e.g. V3_3',
)
}
if (/net\.[^\s>]*[+-]/.test(selector)) {
throw new Error(buildPlusMinusNetErrorMessage(selector, component))
}
if (/net\.[0-9]/.test(selector)) {
const match = selector.match(/net\.([^ >]+)/)
const netName = match ? match[1] : ""
throw new Error(
`Net name "${netName}" cannot start with a number, try using a prefix like "VBUS1"`,
)
}
return (
selector
.replace(/ pin(?=[\d.])/g, " port")
.replace(/ subcircuit\./g, " group[isSubcircuit=true]")
.replace(/([^ ])\>([^ ])/g, "$1 > $2")
.replace(
/(^|[ >])(?!pin\.)(?!port\.)(?!net\.)([A-Z][A-Za-z0-9_-]*)\.([A-Za-z0-9_+-]+)/g,
(_, sep, name, pin) => {
const pinPart = /^\d+$/.test(pin) ? `pin${pin}` : pin
return `${sep}.${name} > .${pinPart}`
},
)
// Escape "+" inside class/pin identifiers (e.g. a pin labelled "PUL+").
// css-select otherwise treats "+" as an adjacent-sibling combinator, so
// ".PUL+" would parse as ".PUL" followed by a combinator and never match a
// port literally named "PUL+". A leading "+" (e.g. ".+INA") already parses
// as part of the identifier, so we only escape "+" that follows an
// identifier character. Net names containing "+" are rejected above.
.replace(/(?<=[A-Za-z0-9_])\+/g, "\\+")
.trim()
)
}