Skip to content

Commit

Permalink
chore: add comments to generated interfaces (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt authored Dec 29, 2020
1 parent 6c31896 commit 1130c58
Show file tree
Hide file tree
Showing 6 changed files with 931 additions and 20 deletions.
881 changes: 880 additions & 1 deletion generated_interfaces.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion page.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ func (p *pageImpl) Hover(selector string, options ...PageHoverOptions) error {
return p.mainFrame.Hover(selector, options...)
}

func (p *pageImpl) Isclosed() bool {
func (p *pageImpl) IsClosed() bool {
return p.isClosed
}

Expand Down
2 changes: 1 addition & 1 deletion scripts/data/interfaces.json
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@
"selector string, options ...PageInnerTextOptions",
"(string, error)"
],
"Isclosed": [
"IsClosed": [
null,
"bool"
],
Expand Down
33 changes: 33 additions & 0 deletions scripts/generate-interfaces.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env node
const { transformMethodNamesToGo, getAPIDocs } = require("./helpers")

const interfaceData = require("./data/interfaces.json")
const api = getAPIDocs()

const transformInputParameters = (input) => {
if (!input) return ""
Expand All @@ -14,14 +16,45 @@ const transformReturnParameters = (input) => {
return `(${input})`
}

const writeComment = (comment) => {
const lines = comment.split("\n")
let inExample = false
let lastWasBlank = true
const out = []
for (const line of lines) {
if (!line.trim()) {
lastWasBlank = true
continue
}
if (line.trim() === "```js")
inExample = true
if (!inExample) {
if (lastWasBlank)
lastWasBlank = false
out.push(line.trim())
}
if (line.trim() === "```")
inExample = false
}

for (const line of out)
console.log(`// ${line}`)
}

console.log("package playwright")

for (const [className, methods] of Object.entries(interfaceData)) {
if (api[className])
writeComment(api[className].comment)
console.log(`type ${className} interface {`)
for (const [funcName, funcData] of Object.entries(methods)) {
if (funcData.length === 0) {
console.log(funcName)
} else {
const apiFunc = api[className] && Object.entries(api[className].methods).find(([item]) => transformMethodNamesToGo(item) === funcName)
if (apiFunc && apiFunc[1].comment)
writeComment(apiFunc[1].comment)

const [inputTypes, returnTypes] = funcData
console.log(`${funcName}(${transformInputParameters(inputTypes)}) ${transformReturnParameters(returnTypes)}`)
}
Expand Down
22 changes: 13 additions & 9 deletions scripts/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,20 @@ const getAPIDocs = () => {
}).toString())
}

const transformMethodNamesToGo = (funcName) => funcName
.replace("$$eval", "evalOnSelectorAll")
.replace("$eval", "evalOnSelector")
.replace("$$", "querySelectorAll")
.replace("$", "querySelector")
.replace("pdf", "PDF")
.replace("url", "URL")
.replace("json", "JSON")
const transformMethodNamesToGo = (funcName) => {
const standardised = funcName
.replace("$$eval", "evalOnSelectorAll")
.replace("$eval", "evalOnSelector")
.replace("$$", "querySelectorAll")
.replace("$", "querySelector")
.replace("pdf", "PDF")
.replace("url", "URL")
.replace("json", "JSON")

return standardised[0].toUpperCase() + standardised.slice(1)
}

module.exports = {
getAPIDocs,
transformMethodNamesToGo
transformMethodNamesToGo,
}
11 changes: 3 additions & 8 deletions scripts/validate-interfaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ const shouldIgnoreClass = ([k]) =>
!k.startsWith("Firefox") &&
!k.startsWith("WebKit")

const transformFunctionName = (v) => {
v = transformMethodNamesToGo(v)
return v[0].toUpperCase() + v.slice(1)
}

const allowedMissing = [
"BrowserType.Connect",
"BrowserType.LaunchServer",
Expand All @@ -31,8 +26,8 @@ const allowedMissing = [
const missingFunctions = []

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

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

0 comments on commit 1130c58

Please sign in to comment.