Skip to content

Commit

Permalink
fix(types): export selected types (#1881)
Browse files Browse the repository at this point in the history
Currently exports LaunchOptions, BrowserContextOptions, Cookie and their deps.
  • Loading branch information
dgozman committed Apr 21, 2020
1 parent 1935824 commit 948d51d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 19 deletions.
8 changes: 8 additions & 0 deletions utils/generate_types/exported.json
@@ -0,0 +1,8 @@
{
"BrowserTypeLaunchOptions": "LaunchOptions",
"BrowserContextCookies": "Cookie",
"BrowserNewContextOptions": "BrowserContextOptions",
"BrowserNewContextOptionsViewport": "ViewportSize",
"BrowserNewContextOptionsGeolocation": "Geolocation",
"BrowserNewContextOptionsHttpCredentials": "HTTPCredentials"
}
28 changes: 12 additions & 16 deletions utils/generate_types/index.js
Expand Up @@ -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} */
Expand Down Expand Up @@ -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);
Expand All @@ -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');
}
Expand Down Expand Up @@ -109,7 +112,7 @@ function classToString(classDesc) {
}

/**
* @param {string} type
* @param {string} type
*/
function argNameForType(type) {
if (type === 'void')
Expand Down Expand Up @@ -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}`))
Expand All @@ -206,7 +209,7 @@ function hasOwnMethod(classDesc, methodName) {
}

/**
* @param {Documentation.Class} classDesc
* @param {Documentation.Class} classDesc
*/
function parentClass(classDesc) {
if (!classDesc.extends)
Expand All @@ -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
Expand Down Expand Up @@ -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 = [];
Expand Down
35 changes: 32 additions & 3 deletions utils/generate_types/test/test.ts
Expand Up @@ -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'))!;
Expand Down Expand Up @@ -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<string, typeof cookie.name> = true;
const valueIsString: AssertType<string, typeof cookie.value> = true;
const pathIsString: AssertType<string, typeof cookie.path> = true;
const expiresIsNumber: AssertType<number, typeof cookie.expires> = true;
const httpOnlyIsBoolean: AssertType<boolean, typeof cookie.httpOnly> = true;
const secureIsBoolean: AssertType<boolean, typeof cookie.secure> = true;
const sameSiteIsEnum: AssertType<"Strict"|"Lax"|"None", typeof cookie.sameSite> = true;

const navResponse = await page.waitForNavigation({
timeout: 1000
});
Expand Down

0 comments on commit 948d51d

Please sign in to comment.