From 948d51d52ceb58a14916f6e5ea67991dcd8b3891 Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Mon, 20 Apr 2020 17:30:57 -0700 Subject: [PATCH] fix(types): export selected types (#1881) Currently exports LaunchOptions, BrowserContextOptions, Cookie and their deps. --- utils/generate_types/exported.json | 8 +++++++ utils/generate_types/index.js | 28 ++++++++++-------------- utils/generate_types/test/test.ts | 35 +++++++++++++++++++++++++++--- 3 files changed, 52 insertions(+), 19 deletions(-) create mode 100644 utils/generate_types/exported.json diff --git a/utils/generate_types/exported.json b/utils/generate_types/exported.json new file mode 100644 index 0000000000..20cc2f0f58 --- /dev/null +++ b/utils/generate_types/exported.json @@ -0,0 +1,8 @@ +{ + "BrowserTypeLaunchOptions": "LaunchOptions", + "BrowserContextCookies": "Cookie", + "BrowserNewContextOptions": "BrowserContextOptions", + "BrowserNewContextOptionsViewport": "ViewportSize", + "BrowserNewContextOptionsGeolocation": "Geolocation", + "BrowserNewContextOptionsHttpCredentials": "HTTPCredentials" +} diff --git a/utils/generate_types/index.js b/utils/generate_types/index.js index 07e28d9880..3327c10adc 100644 --- a/utils/generate_types/index.js +++ b/utils/generate_types/index.js @@ -22,6 +22,7 @@ const Documentation = require('../doclint/check_public_api/Documentation'); const PROJECT_DIR = path.join(__dirname, '..', '..'); const fs = require('fs'); const {parseOverrides} = require('./parseOverrides'); +const exported = require('./exported.json'); const objectDefinitions = []; const handledMethods = new Set(); /** @type {Documentation} */ @@ -64,12 +65,14 @@ let documentation; return classBody(docClassForName(className)); }); const classes = documentation.classesArray.filter(cls => !handledClasses.has(cls.name)); - const output = `// This file is generated by ${__filename.substring(path.join(__dirname, '..', '..').length)} + let output = `// This file is generated by ${__filename.substring(path.join(__dirname, '..', '..').length)} ${overrides} ${classes.map(classDesc => classToString(classDesc)).join('\n')} ${objectDefinitionsToString()} `; + for (const [key, value] of Object.entries(exported)) + output = output.replace(new RegExp('\\b' + key + '\\b', 'g'), value); fs.writeFileSync(path.join(typesDir, 'types.d.ts'), output, 'utf8'); })().catch(e => { console.error(e); @@ -81,7 +84,7 @@ function objectDefinitionsToString() { const parts = []; while ((definition = objectDefinitions.pop())) { const {name, properties} = definition; - parts.push(`interface ${name} {`); + parts.push(`${exported[name] ? 'export ' : ''}interface ${name} {`); parts.push(properties.map(member => `${memberJSDOC(member, ' ')}${nameForProperty(member)}${argsFromMember(member, name)}: ${typeToString(member.type, name, member.name)};`).join('\n\n')); parts.push('}\n'); } @@ -109,7 +112,7 @@ function classToString(classDesc) { } /** - * @param {string} type + * @param {string} type */ function argNameForType(type) { if (type === 'void') @@ -192,8 +195,8 @@ function classBody(classDesc) { } /** - * @param {Documentation.Class} classDesc - * @param {string} methodName + * @param {Documentation.Class} classDesc + * @param {string} methodName */ function hasOwnMethod(classDesc, methodName) { if (handledMethods.has(`${classDesc.name}.${methodName}`)) @@ -206,7 +209,7 @@ function hasOwnMethod(classDesc, methodName) { } /** - * @param {Documentation.Class} classDesc + * @param {Documentation.Class} classDesc */ function parentClass(classDesc) { if (!classDesc.extends) @@ -221,13 +224,6 @@ function writeComment(comment, indent = '') { parts.push(indent + ' */'); return parts.join('\n'); } -function writeComment2(comment, indent = '') { - const parts = []; - parts.push('/**'); - parts.push(...comment.split('\n').map(line => indent + ' * ' + line.replace(/\*\//g, '*\\/'))); - parts.push(indent + ' */\n' + indent); - return parts.join('\n'); -} /** * @param {Documentation.Type} type @@ -373,9 +369,9 @@ function memberJSDOC(member, indent) { } /** - * @param {Documentation} mdDoc - * @param {Documentation} jsDoc - * @return {Documentation} + * @param {Documentation} mdDoc + * @param {Documentation} jsDoc + * @return {Documentation} */ function mergeDocumentation(mdDoc, jsDoc) { const classes = []; diff --git a/utils/generate_types/test/test.ts b/utils/generate_types/test/test.ts index 0578035682..9432b4393c 100644 --- a/utils/generate_types/test/test.ts +++ b/utils/generate_types/test/test.ts @@ -204,13 +204,32 @@ playwright.chromium.launch().then(async browser => { // Test v0.12 features (async () => { - const browser = await playwright.chromium.launch({ + const launchOptions: playwright.LaunchOptions = { devtools: true, env: { JEST_TEST: true } - }); - const page = await browser.newPage(); + }; + const browser = await playwright.chromium.launch(launchOptions); + const viewport: playwright.ViewportSize = { + width: 100, + height: 200, + }; + const geolocation: playwright.Geolocation = { + latitude: 0, + longitude: 0, + accuracy: undefined, + }; + const httpCredentials: playwright.HTTPCredentials = { + username: 'foo', + password: 'bar', + }; + const contextOptions: playwright.BrowserContextOptions = { + viewport, + geolocation, + httpCredentials, + }; + const page = await browser.newPage(contextOptions); const button = (await page.$('#myButton'))!; const div = (await page.$('#myDiv'))!; const input = (await page.$('#myInput'))!; @@ -246,6 +265,16 @@ playwright.chromium.launch().then(async browser => { const buttonText = await (await button.getProperty('textContent')).jsonValue(); await page.context().clearCookies(); + const cookies: playwright.Cookie[] = await page.context().cookies(['http://example.com']); + const cookie = cookies[0]; + const nameIsString: AssertType = true; + const valueIsString: AssertType = true; + const pathIsString: AssertType = true; + const expiresIsNumber: AssertType = true; + const httpOnlyIsBoolean: AssertType = true; + const secureIsBoolean: AssertType = true; + const sameSiteIsEnum: AssertType<"Strict"|"Lax"|"None", typeof cookie.sameSite> = true; + const navResponse = await page.waitForNavigation({ timeout: 1000 });