Skip to content

Commit

Permalink
cmd/cdpgen: Update initialisms and package name linting
Browse files Browse the repository at this point in the history
  • Loading branch information
mafredri committed Sep 1, 2024
1 parent f0e0fc3 commit 1cf3654
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 77 deletions.
21 changes: 21 additions & 0 deletions cmd/cdpgen/lint/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

var (
reIDs = regexp.MustCompile("^(.*)Ids($|[A-Z].*$)")
reUUIDs = regexp.MustCompile("^(.*)Uuids($|[A-Z].*$)")
reURLs = regexp.MustCompile("^(.*)Urls($|[A-Z].*$)")
reIDRef = regexp.MustCompile("^(.*)Idref($|[A-Z].*$)")
)
Expand All @@ -31,10 +32,29 @@ func init() {
lint.SetInitialism("HSL", true)
lint.SetInitialism("CORS", true)
lint.SetInitialism("WASM", true)
lint.SetInitialism("JS", true)
lint.SetInitialism("JXL", true)
lint.SetInitialism("BR", true) // Brotli compression.
lint.SetInitialism("HWB", true) // Color format.
lint.SetInitialism("IME", true)
lint.SetInitialism("CM", true) // FedCm => FedCM.
lint.SetInitialism("OS", true) // Operating system.
lint.SetInitialism("COEP", true) // Cross-Origin-Embedder-Policy.
lint.SetInitialism("COOP", true) // Cross-Origin-Opener-Policy.
lint.SetInitialism("CORP", true) // Cross-Origin-Resource-Policy.
lint.SetInitialism("CVC", true) // Card verification code.
lint.SetInitialism("IDP", true) // Identity Provider.
lint.SetInitialism("CSP", true) // Content Security Policy.
lint.SetInitialism("DIP", true) // Document isolation policy.
lint.SetInitialism("RSSI", true)
lint.SetInitialism("CH", true)
lint.SetInitialism("DPR", true)
lint.SetInitialism("UA", true)
lint.SetInitialism("ECT", true)
lint.SetInitialism("RTT", true)
lint.SetInitialism("HID", true)
lint.SetInitialism("OTP", true)
lint.SetInitialism("XHR", true)
}

// Name returns a different name if it should be different.
Expand All @@ -46,6 +66,7 @@ func Name(name string) (should string) {
to string
}{
{re: reIDs, to: "IDs"},
{re: reUUIDs, to: "UUIDs"},
{re: reURLs, to: "URLs"},
{re: reIDRef, to: "IDRef"},
} {
Expand Down
32 changes: 19 additions & 13 deletions cmd/cdpgen/proto/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ import (
"log"
"strings"

"github.com/mafredri/cdp/cmd/cdpgen/lint"

"github.com/client9/misspell"
"golang.org/x/text/cases"
"golang.org/x/text/language"

"github.com/mafredri/cdp/cmd/cdpgen/lint"
)

var misspellReplacer = misspell.New()
var (
misspellReplacer = misspell.New()
titleCase = cases.Title(language.AmericanEnglish, cases.NoLower).String
)

func init() {
misspellReplacer.AddRuleList(misspell.DictAmerican)
Expand Down Expand Up @@ -43,12 +48,12 @@ type Domain struct {

// Name returns the domain name.
func (d Domain) Name() string {
return d.Domain
return lint.Name(titleCase(d.Domain))
}

// Type returns the domain type.
func (d Domain) Type() string {
return d.Domain
return lint.Name(titleCase(d.Domain))
}

// Desc returns the domain decription.
Expand All @@ -71,7 +76,7 @@ type Command struct {

// Name rturns the linted command name.
func (c Command) Name() string {
return lint.Name(strings.Title(c.NameName))
return lint.Name(titleCase(c.NameName))
}

// Desc returns a cleaned description.
Expand Down Expand Up @@ -183,7 +188,7 @@ type Event struct {

// Name returns the name of the event.
func (e Event) Name() string {
return lint.Name(strings.Title(e.NameName))
return lint.Name(titleCase(e.NameName))
}

// Desc returns the cleaned description.
Expand Down Expand Up @@ -214,7 +219,7 @@ func (e Enum) Name() string {
return strings.Replace(string(e), "-", "Negative", 1)
}
s := strings.Replace(string(e), "-", " ", -1)
s = strings.Title(s)
s = titleCase(s)
return lint.Name(strings.Replace(s, " ", "", -1))
}

Expand Down Expand Up @@ -245,7 +250,7 @@ func (at AnyType) ExportedName(d Domain) string {
if at.IDName != "" {
return at.Name(d)
}
return lint.Name(strings.Title(at.NameName))
return lint.Name(titleCase(at.NameName))
}

// Name returns a Go-ified name for the AnyType.
Expand All @@ -263,10 +268,11 @@ func (at AnyType) Recvr(d Domain) string {
}

func nameInDomain(d Domain, name, _ string) string {
name = lint.Name(strings.Title(name))
name = lint.Name(titleCase(name))
if name != d.Name() && strings.Index(name, d.Name()) == 0 {
name = strings.Replace(name, d.Name(), "", 1)
}
name = strings.TrimPrefix(name, "ing") // preload.Preloading -> ing -> ""
return name
}

Expand Down Expand Up @@ -349,8 +355,8 @@ func (at AnyType) refType(pkg string, d Domain) string {
if strings.ContainsRune(at.Ref, '.') {
s := strings.Split(at.Ref, ".")
prefix = strings.ToLower(s[0]) + "."
s[0] = lint.Name(strings.Title(s[0]))
s[1] = lint.Name(strings.Title(s[1]))
s[0] = lint.Name(titleCase(s[0]))
s[1] = lint.Name(titleCase(s[1]))

// Remove stutter, e.g. SecuritySecurityState.
if strings.Index(s[1], s[0]) == 0 || s[1] == s[0] {
Expand All @@ -362,7 +368,7 @@ func (at AnyType) refType(pkg string, d Domain) string {
prefix = ""
at.Ref = s[1]
}
name := prefix + strings.Title(lint.Name(s[1]))
name := prefix + titleCase(lint.Name(s[1]))

// Special cases for circular types.
switch {
Expand Down
22 changes: 11 additions & 11 deletions protocol/audits/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protocol/autofill/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions protocol/bluetoothemulation/command.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions protocol/bluetoothemulation/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protocol/browser/command.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions protocol/fedcm/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protocol/memory/command.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1cf3654

Please # to comment.