From 0475ab0cc2aa85baf0c3e5c708b7442d7944f81d Mon Sep 17 00:00:00 2001 From: Alexander Pankratov Date: Wed, 17 Feb 2021 17:36:11 +0300 Subject: [PATCH] JsFunction type instead of callbacks --- src/Command/GenerateDocumentationCommand.php | 1 + src/Resources/Browser.php | 2 +- src/Resources/BrowserContext.php | 2 +- src/Resources/BrowserFetcher.php | 2 +- src/Resources/ExecutionContext.php | 4 ++-- src/Resources/Frame.php | 8 ++++---- src/Resources/JSHandle.php | 4 ++-- src/Resources/Page.php | 16 ++++++++-------- src/Resources/WebWorker.php | 4 ++-- src/doc-generator.ts | 15 +++++++++++---- 10 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/Command/GenerateDocumentationCommand.php b/src/Command/GenerateDocumentationCommand.php index a6d68ed..d0cf23b 100644 --- a/src/Command/GenerateDocumentationCommand.php +++ b/src/Command/GenerateDocumentationCommand.php @@ -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) { diff --git a/src/Resources/Browser.php b/src/Resources/Browser.php index ccd58a3..a067b65 100644 --- a/src/Resources/Browser.php +++ b/src/Resources/Browser.php @@ -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() diff --git a/src/Resources/BrowserContext.php b/src/Resources/BrowserContext.php index 8ba09d1..f8eb315 100644 --- a/src/Resources/BrowserContext.php +++ b/src/Resources/BrowserContext.php @@ -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) diff --git a/src/Resources/BrowserFetcher.php b/src/Resources/BrowserFetcher.php index eab2d87..4350f9c 100644 --- a/src/Resources/BrowserFetcher.php +++ b/src/Resources/BrowserFetcher.php @@ -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) diff --git a/src/Resources/ExecutionContext.php b/src/Resources/ExecutionContext.php index 0ac5ce7..4d2a8c0 100644 --- a/src/Resources/ExecutionContext.php +++ b/src/Resources/ExecutionContext.php @@ -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 diff --git a/src/Resources/Frame.php b/src/Resources/Frame.php index c9fb6ed..902cf88 100644 --- a/src/Resources/Frame.php +++ b/src/Resources/Frame.php @@ -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() @@ -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 diff --git a/src/Resources/JSHandle.php b/src/Resources/JSHandle.php index da229e9..4ef48d2 100644 --- a/src/Resources/JSHandle.php +++ b/src/Resources/JSHandle.php @@ -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() diff --git a/src/Resources/Page.php b/src/Resources/Page.php index 2959085..2739f90 100644 --- a/src/Resources/Page.php +++ b/src/Resources/Page.php @@ -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) @@ -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() @@ -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 = []) @@ -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 { diff --git a/src/Resources/WebWorker.php b/src/Resources/WebWorker.php index 2a0bb15..0732ec1 100644 --- a/src/Resources/WebWorker.php +++ b/src/Resources/WebWorker.php @@ -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 { diff --git a/src/doc-generator.ts b/src/doc-generator.ts index c82d7d9..e9ac02d 100644 --- a/src/doc-generator.ts +++ b/src/doc-generator.ts @@ -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; } @@ -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 { @@ -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') { @@ -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); }