Skip to content

Commit 1130c58

Browse files
authored
chore: add comments to generated interfaces (#75)
1 parent 6c31896 commit 1130c58

File tree

6 files changed

+931
-20
lines changed

6 files changed

+931
-20
lines changed

generated_interfaces.go

Lines changed: 880 additions & 1 deletion
Large diffs are not rendered by default.

page.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ func (p *pageImpl) Hover(selector string, options ...PageHoverOptions) error {
437437
return p.mainFrame.Hover(selector, options...)
438438
}
439439

440-
func (p *pageImpl) Isclosed() bool {
440+
func (p *pageImpl) IsClosed() bool {
441441
return p.isClosed
442442
}
443443

scripts/data/interfaces.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@
737737
"selector string, options ...PageInnerTextOptions",
738738
"(string, error)"
739739
],
740-
"Isclosed": [
740+
"IsClosed": [
741741
null,
742742
"bool"
743743
],

scripts/generate-interfaces.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/usr/bin/env node
2+
const { transformMethodNamesToGo, getAPIDocs } = require("./helpers")
23

34
const interfaceData = require("./data/interfaces.json")
5+
const api = getAPIDocs()
46

57
const transformInputParameters = (input) => {
68
if (!input) return ""
@@ -14,14 +16,45 @@ const transformReturnParameters = (input) => {
1416
return `(${input})`
1517
}
1618

19+
const writeComment = (comment) => {
20+
const lines = comment.split("\n")
21+
let inExample = false
22+
let lastWasBlank = true
23+
const out = []
24+
for (const line of lines) {
25+
if (!line.trim()) {
26+
lastWasBlank = true
27+
continue
28+
}
29+
if (line.trim() === "```js")
30+
inExample = true
31+
if (!inExample) {
32+
if (lastWasBlank)
33+
lastWasBlank = false
34+
out.push(line.trim())
35+
}
36+
if (line.trim() === "```")
37+
inExample = false
38+
}
39+
40+
for (const line of out)
41+
console.log(`// ${line}`)
42+
}
43+
1744
console.log("package playwright")
1845

1946
for (const [className, methods] of Object.entries(interfaceData)) {
47+
if (api[className])
48+
writeComment(api[className].comment)
2049
console.log(`type ${className} interface {`)
2150
for (const [funcName, funcData] of Object.entries(methods)) {
2251
if (funcData.length === 0) {
2352
console.log(funcName)
2453
} else {
54+
const apiFunc = api[className] && Object.entries(api[className].methods).find(([item]) => transformMethodNamesToGo(item) === funcName)
55+
if (apiFunc && apiFunc[1].comment)
56+
writeComment(apiFunc[1].comment)
57+
2558
const [inputTypes, returnTypes] = funcData
2659
console.log(`${funcName}(${transformInputParameters(inputTypes)}) ${transformReturnParameters(returnTypes)}`)
2760
}

scripts/helpers.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,20 @@ const getAPIDocs = () => {
3232
}).toString())
3333
}
3434

35-
const transformMethodNamesToGo = (funcName) => funcName
36-
.replace("$$eval", "evalOnSelectorAll")
37-
.replace("$eval", "evalOnSelector")
38-
.replace("$$", "querySelectorAll")
39-
.replace("$", "querySelector")
40-
.replace("pdf", "PDF")
41-
.replace("url", "URL")
42-
.replace("json", "JSON")
35+
const transformMethodNamesToGo = (funcName) => {
36+
const standardised = funcName
37+
.replace("$$eval", "evalOnSelectorAll")
38+
.replace("$eval", "evalOnSelector")
39+
.replace("$$", "querySelectorAll")
40+
.replace("$", "querySelector")
41+
.replace("pdf", "PDF")
42+
.replace("url", "URL")
43+
.replace("json", "JSON")
44+
45+
return standardised[0].toUpperCase() + standardised.slice(1)
46+
}
4347

4448
module.exports = {
4549
getAPIDocs,
46-
transformMethodNamesToGo
50+
transformMethodNamesToGo,
4751
}

scripts/validate-interfaces.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ const shouldIgnoreClass = ([k]) =>
1717
!k.startsWith("Firefox") &&
1818
!k.startsWith("WebKit")
1919

20-
const transformFunctionName = (v) => {
21-
v = transformMethodNamesToGo(v)
22-
return v[0].toUpperCase() + v.slice(1)
23-
}
24-
2520
const allowedMissing = [
2621
"BrowserType.Connect",
2722
"BrowserType.LaunchServer",
@@ -31,8 +26,8 @@ const allowedMissing = [
3126
const missingFunctions = []
3227

3328
for (const [className, classData] of Object.entries(api).filter(shouldIgnoreClass)) {
34-
for (const [funcName, funcData] of Object.entries(classData.methods)) {
35-
const goFuncName = transformFunctionName(funcName)
29+
for (const funcName in classData.methods) {
30+
const goFuncName = transformMethodNamesToGo(funcName)
3631
const functionSignature = `${className}.${goFuncName}`;
3732
if (!interfaceData[className] || !interfaceData[className][goFuncName] && !allowedMissing.includes(functionSignature)) {
3833
missingFunctions.push(functionSignature)
@@ -42,6 +37,6 @@ for (const [className, classData] of Object.entries(api).filter(shouldIgnoreClas
4237

4338
if (missingFunctions.length > 0) {
4439
console.log("Missing API interface functions:")
45-
console.log(missingFunctions.map(item => `- ${item}`).join("\n"))
40+
console.log(missingFunctions.map(item => `- [ ] ${item}`).join("\n"))
4641
process.exit(1)
4742
}

0 commit comments

Comments
 (0)