fix(types): export selected types (#1881)

Currently exports LaunchOptions, BrowserContextOptions, Cookie and their deps.
This commit is contained in:
Dmitry Gozman 2020-04-20 17:30:57 -07:00 committed by GitHub
parent 193582445d
commit 948d51d52c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 19 deletions

View File

@ -0,0 +1,8 @@
{
"BrowserTypeLaunchOptions": "LaunchOptions",
"BrowserContextCookies": "Cookie",
"BrowserNewContextOptions": "BrowserContextOptions",
"BrowserNewContextOptionsViewport": "ViewportSize",
"BrowserNewContextOptionsGeolocation": "Geolocation",
"BrowserNewContextOptionsHttpCredentials": "HTTPCredentials"
}

View File

@ -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 = [];

View File

@ -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<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
});