Skip to content
This repository has been archived by the owner on Jan 1, 2023. It is now read-only.

Commit

Permalink
JsFunction type instead of callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
xtrime-ru committed Feb 17, 2021
1 parent aadb786 commit 0475ab0
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 25 deletions.
1 change: 1 addition & 0 deletions src/Command/GenerateDocumentationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

// Handle the specific Puppeteer class
$classDocumentation = array_replace_recursive($documentation['Puppeteer'], $documentation['PuppeteerNode']);
unset($documentation['Puppeteer'], $documentation['PuppeteerNode']);
if ($classDocumentation !== null) {
$phpDoc = self::generatePhpDocWithDocumentation($classDocumentation);
if ($phpDoc !== null) {
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/Browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @method Page newPage()
* @method Target[] targets()
* @method Target target()
* @method Target waitForTarget(JSHandle $predicate, array $options = [])
* @method Target waitForTarget(\Nesk\Rialto\Data\JsFunction $predicate, array $options = [])
* @method Page[] pages()
* @method string version()
* @method string userAgent()
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/BrowserContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* @method Target[] targets()
* @method Target waitForTarget(JSHandle $predicate, array $options = [])
* @method Target waitForTarget(\Nesk\Rialto\Data\JsFunction $predicate, array $options = [])
* @method Page[] pages()
* @method bool isIncognito()
* @method void overridePermissions(string $origin, string[] $permissions)
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/BrowserFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @method mixed product()
* @method string host()
* @method bool canDownload(string $revision)
* @method mixed download(string $revision, JSHandle $progressCallback = null)
* @method mixed download(string $revision, \Nesk\Rialto\Data\JsFunction $progressCallback = null)
* @method string[] localRevisions()
* @method void remove(string $revision)
* @method mixed revisionInfo(string $revision)
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/ExecutionContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

/**
* @method Frame|null frame()
* @method mixed evaluate(JSHandle|string $pageFunction, mixed ...$args)
* @method JSHandle|ElementHandle evaluateHandle(JSHandle|string $pageFunction, int|float|string|bool|null|array|JSHandle ...$args)
* @method mixed evaluate(\Nesk\Rialto\Data\JsFunction|string $pageFunction, mixed ...$args)
* @method JSHandle|ElementHandle evaluateHandle(\Nesk\Rialto\Data\JsFunction|string $pageFunction, int|float|string|bool|null|array|JSHandle ...$args)
* @method JSHandle queryObjects(JSHandle $prototypeHandle)
*/
class ExecutionContext extends BasicResource
Expand Down
8 changes: 4 additions & 4 deletions src/Resources/Frame.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
* @method HTTPResponse|null goto(string $url, array $options = [])
* @method HTTPResponse|null waitForNavigation(array $options = [])
* @method ExecutionContext executionContext()
* @method mixed evaluateHandle(JSHandle|string $pageFunction, int|float|string|bool|null|array|JSHandle ...$args)
* @method mixed evaluate(mixed $pageFunction, int|float|string|bool|null|array|JSHandle ...$args)
* @method mixed evaluateHandle(\Nesk\Rialto\Data\JsFunction|string $pageFunction, int|float|string|bool|null|array|JSHandle ...$args)
* @method mixed evaluate(\Nesk\Rialto\Data\JsFunction $pageFunction, int|float|string|bool|null|array|JSHandle ...$args)
* @method string content()
* @method void setContent(string $html, array $options = [])
* @method string name()
Expand All @@ -27,11 +27,11 @@
* @method string[] select(string $selector, string ...$values)
* @method void tap(string $selector)
* @method void type(string $selector, string $text, array $options = [])
* @method JSHandle|null waitFor(string|float|JSHandle $selectorOrFunctionOrTimeout, array|string[]|mixed[] $options = null, int|float|string|bool|null|array|JSHandle ...$args)
* @method JSHandle|null waitFor(string|float|\Nesk\Rialto\Data\JsFunction $selectorOrFunctionOrTimeout, array|string[]|mixed[] $options = null, int|float|string|bool|null|array|JSHandle ...$args)
* @method void waitForTimeout(float $milliseconds)
* @method ElementHandle|null waitForSelector(string $selector, array $options = [])
* @method ElementHandle|null waitForXPath(string $xpath, array $options = [])
* @method JSHandle waitForFunction(JSHandle|string $pageFunction, array $options = [], int|float|string|bool|null|array|JSHandle ...$args)
* @method JSHandle waitForFunction(\Nesk\Rialto\Data\JsFunction|string $pageFunction, array $options = [], int|float|string|bool|null|array|JSHandle ...$args)
* @method string title()
*/
class Frame extends BasicResource
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/JSHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

/**
* @method ExecutionContext executionContext()
* @method mixed evaluate(mixed|string $pageFunction, int|float|string|bool|null|array|JSHandle ...$args)
* @method JSHandle|ElementHandle evaluateHandle(JSHandle|string $pageFunction, int|float|string|bool|null|array|JSHandle ...$args)
* @method mixed evaluate(\Nesk\Rialto\Data\JsFunction $pageFunction, int|float|string|bool|null|array|JSHandle ...$args)
* @method JSHandle|ElementHandle evaluateHandle(\Nesk\Rialto\Data\JsFunction|string $pageFunction, int|float|string|bool|null|array|JSHandle ...$args)
* @method JSHandle|null getProperty(string $propertyName)
* @method array|string[]|JSHandle[] getProperties()
* @method array|string[]|mixed[] jsonValue()
Expand Down
16 changes: 8 additions & 8 deletions src/Resources/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
* @method void setOfflineMode(bool $enabled)
* @method void setDefaultNavigationTimeout(float $timeout)
* @method void setDefaultTimeout(float $timeout)
* @method mixed evaluateHandle(JSHandle|string $pageFunction, int|float|string|bool|null|array|JSHandle ...$args)
* @method mixed evaluateHandle(\Nesk\Rialto\Data\JsFunction|string $pageFunction, int|float|string|bool|null|array|JSHandle ...$args)
* @method JSHandle queryObjects(JSHandle $prototypeHandle)
* @method mixed[] cookies(string ...$urls)
* @method void deleteCookie(mixed ...$cookies)
* @method void setCookie(mixed ...$cookies)
* @method ElementHandle addScriptTag(array $options)
* @method ElementHandle addStyleTag(array $options)
* @method void exposeFunction(string $name, JSHandle $puppeteerFunction)
* @method void exposeFunction(string $name, \Nesk\Rialto\Data\JsFunction $puppeteerFunction)
* @method void authenticate(mixed $credentials)
* @method void setExtraHTTPHeaders(array|string[]|string[] $headers)
* @method void setUserAgent(string $userAgent)
Expand All @@ -43,8 +43,8 @@
* @method HTTPResponse goto(string $url, array $options = [])
* @method HTTPResponse|null reload(array $options = [])
* @method HTTPResponse|null waitForNavigation(array $options = [])
* @method HTTPRequest waitForRequest(string|JSHandle $urlOrPredicate, array $options = [])
* @method HTTPResponse waitForResponse(string|JSHandle $urlOrPredicate, array $options = [])
* @method HTTPRequest waitForRequest(string|\Nesk\Rialto\Data\JsFunction $urlOrPredicate, array $options = [])
* @method HTTPResponse waitForResponse(string|\Nesk\Rialto\Data\JsFunction $urlOrPredicate, array $options = [])
* @method HTTPResponse|null goBack(array $options = [])
* @method HTTPResponse|null goForward(array $options = [])
* @method void bringToFront()
Expand All @@ -58,8 +58,8 @@
* @method void emulateVisionDeficiency(mixed $type = null)
* @method void setViewport(mixed $viewport)
* @method mixed|null viewport()
* @method mixed evaluate(mixed $pageFunction, int|float|string|bool|null|array|JSHandle ...$args)
* @method void evaluateOnNewDocument(JSHandle|string $pageFunction, mixed ...$args)
* @method mixed evaluate(\Nesk\Rialto\Data\JsFunction $pageFunction, int|float|string|bool|null|array|JSHandle ...$args)
* @method void evaluateOnNewDocument(\Nesk\Rialto\Data\JsFunction|string $pageFunction, mixed ...$args)
* @method void setCacheEnabled(bool $enabled = null)
* @method mixed|string|null screenshot(array $options = [])
* @method mixed pdf(array $options = [])
Expand All @@ -72,11 +72,11 @@
* @method string[] select(string $selector, string ...$values)
* @method void tap(string $selector)
* @method void type(string $selector, string $text, array $options = [])
* @method JSHandle waitFor(string|float|JSHandle $selectorOrFunctionOrTimeout, array $options = [], int|float|string|bool|null|array|JSHandle ...$args)
* @method JSHandle waitFor(string|float|\Nesk\Rialto\Data\JsFunction $selectorOrFunctionOrTimeout, array $options = [], int|float|string|bool|null|array|JSHandle ...$args)
* @method void waitForTimeout(float $milliseconds)
* @method ElementHandle|null waitForSelector(string $selector, array $options = [])
* @method ElementHandle|null waitForXPath(string $xpath, array $options = [])
* @method JSHandle waitForFunction(JSHandle|string $pageFunction, array $options = [], int|float|string|bool|null|array|JSHandle ...$args)
* @method JSHandle waitForFunction(\Nesk\Rialto\Data\JsFunction|string $pageFunction, array $options = [], int|float|string|bool|null|array|JSHandle ...$args)
*/
class Page extends EventEmitter
{
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/WebWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
/**
* @method string url()
* @method ExecutionContext executionContext()
* @method mixed evaluate(JSHandle|string $pageFunction, mixed ...$args)
* @method JSHandle evaluateHandle(JSHandle|string $pageFunction, int|float|string|bool|null|array|JSHandle ...$args)
* @method mixed evaluate(\Nesk\Rialto\Data\JsFunction|string $pageFunction, mixed ...$args)
* @method JSHandle evaluateHandle(\Nesk\Rialto\Data\JsFunction|string $pageFunction, int|float|string|bool|null|array|JSHandle ...$args)
*/
class WebWorker extends BasicResource
{
Expand Down
15 changes: 11 additions & 4 deletions src/doc-generator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as ts from 'typescript';
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const callbackClass = '\\Nesk\\Rialto\\Data\\JsFunction';

type ObjectMemberAsJson = { [key: string]: string; }

Expand Down Expand Up @@ -170,7 +171,7 @@ class PhpDocumentationFormatter implements DocumentationFormatter {
}

formatAnonymousFunction(parameters: string, returnType: string): string {
return `JSHandle`;
return callbackClass;
}

formatFunction(name: string, parameters: string, returnType: string): string {
Expand Down Expand Up @@ -246,11 +247,11 @@ class PhpDocumentationFormatter implements DocumentationFormatter {

// Types ending with "Fn" are always callables or strings
if (type.endsWith('Fn')) {
return this.formatUnion(['JSHandle', 'string']);
return this.formatUnion([callbackClass, 'string']);
}

if (type === 'Function') {
return 'JSHandle';
return callbackClass;
}

if (type === 'PuppeteerLifeCycleEvent') {
Expand Down Expand Up @@ -440,9 +441,15 @@ class DocumentationGenerator {

private getParameterDeclarationAsString(node: ts.ParameterDeclaration): string {
const name = this.getNamedDeclarationAsString(node);
const type = this.getTypeNodeAsString(node.type);
let type = this.getTypeNodeAsString(node.type);
const isVariadic = node.dotDotDotToken !== undefined;
const isOptional = node.questionToken !== undefined;

//fix missing argument type in evaluate* methods.
if (name.includes('Function') && type.includes('mixed')) {
type = this.formatter.formatTypeReference('Function');
}

return this.formatter.formatParameter(name, type, isVariadic, isOptional);
}

Expand Down

0 comments on commit 0475ab0

Please sign in to comment.