Skip to content

Commit 948d51d

Browse files
authored
fix(types): export selected types (#1881)
Currently exports LaunchOptions, BrowserContextOptions, Cookie and their deps.
1 parent 1935824 commit 948d51d

File tree

3 files changed

+52
-19
lines changed

3 files changed

+52
-19
lines changed

utils/generate_types/exported.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"BrowserTypeLaunchOptions": "LaunchOptions",
3+
"BrowserContextCookies": "Cookie",
4+
"BrowserNewContextOptions": "BrowserContextOptions",
5+
"BrowserNewContextOptionsViewport": "ViewportSize",
6+
"BrowserNewContextOptionsGeolocation": "Geolocation",
7+
"BrowserNewContextOptionsHttpCredentials": "HTTPCredentials"
8+
}

utils/generate_types/index.js

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const Documentation = require('../doclint/check_public_api/Documentation');
2222
const PROJECT_DIR = path.join(__dirname, '..', '..');
2323
const fs = require('fs');
2424
const {parseOverrides} = require('./parseOverrides');
25+
const exported = require('./exported.json');
2526
const objectDefinitions = [];
2627
const handledMethods = new Set();
2728
/** @type {Documentation} */
@@ -64,12 +65,14 @@ let documentation;
6465
return classBody(docClassForName(className));
6566
});
6667
const classes = documentation.classesArray.filter(cls => !handledClasses.has(cls.name));
67-
const output = `// This file is generated by ${__filename.substring(path.join(__dirname, '..', '..').length)}
68+
let output = `// This file is generated by ${__filename.substring(path.join(__dirname, '..', '..').length)}
6869
${overrides}
6970
7071
${classes.map(classDesc => classToString(classDesc)).join('\n')}
7172
${objectDefinitionsToString()}
7273
`;
74+
for (const [key, value] of Object.entries(exported))
75+
output = output.replace(new RegExp('\\b' + key + '\\b', 'g'), value);
7376
fs.writeFileSync(path.join(typesDir, 'types.d.ts'), output, 'utf8');
7477
})().catch(e => {
7578
console.error(e);
@@ -81,7 +84,7 @@ function objectDefinitionsToString() {
8184
const parts = [];
8285
while ((definition = objectDefinitions.pop())) {
8386
const {name, properties} = definition;
84-
parts.push(`interface ${name} {`);
87+
parts.push(`${exported[name] ? 'export ' : ''}interface ${name} {`);
8588
parts.push(properties.map(member => `${memberJSDOC(member, ' ')}${nameForProperty(member)}${argsFromMember(member, name)}: ${typeToString(member.type, name, member.name)};`).join('\n\n'));
8689
parts.push('}\n');
8790
}
@@ -109,7 +112,7 @@ function classToString(classDesc) {
109112
}
110113

111114
/**
112-
* @param {string} type
115+
* @param {string} type
113116
*/
114117
function argNameForType(type) {
115118
if (type === 'void')
@@ -192,8 +195,8 @@ function classBody(classDesc) {
192195
}
193196

194197
/**
195-
* @param {Documentation.Class} classDesc
196-
* @param {string} methodName
198+
* @param {Documentation.Class} classDesc
199+
* @param {string} methodName
197200
*/
198201
function hasOwnMethod(classDesc, methodName) {
199202
if (handledMethods.has(`${classDesc.name}.${methodName}`))
@@ -206,7 +209,7 @@ function hasOwnMethod(classDesc, methodName) {
206209
}
207210

208211
/**
209-
* @param {Documentation.Class} classDesc
212+
* @param {Documentation.Class} classDesc
210213
*/
211214
function parentClass(classDesc) {
212215
if (!classDesc.extends)
@@ -221,13 +224,6 @@ function writeComment(comment, indent = '') {
221224
parts.push(indent + ' */');
222225
return parts.join('\n');
223226
}
224-
function writeComment2(comment, indent = '') {
225-
const parts = [];
226-
parts.push('/**');
227-
parts.push(...comment.split('\n').map(line => indent + ' * ' + line.replace(/\*\//g, '*\\/')));
228-
parts.push(indent + ' */\n' + indent);
229-
return parts.join('\n');
230-
}
231227

232228
/**
233229
* @param {Documentation.Type} type
@@ -373,9 +369,9 @@ function memberJSDOC(member, indent) {
373369
}
374370

375371
/**
376-
* @param {Documentation} mdDoc
377-
* @param {Documentation} jsDoc
378-
* @return {Documentation}
372+
* @param {Documentation} mdDoc
373+
* @param {Documentation} jsDoc
374+
* @return {Documentation}
379375
*/
380376
function mergeDocumentation(mdDoc, jsDoc) {
381377
const classes = [];

utils/generate_types/test/test.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,32 @@ playwright.chromium.launch().then(async browser => {
204204

205205
// Test v0.12 features
206206
(async () => {
207-
const browser = await playwright.chromium.launch({
207+
const launchOptions: playwright.LaunchOptions = {
208208
devtools: true,
209209
env: {
210210
JEST_TEST: true
211211
}
212-
});
213-
const page = await browser.newPage();
212+
};
213+
const browser = await playwright.chromium.launch(launchOptions);
214+
const viewport: playwright.ViewportSize = {
215+
width: 100,
216+
height: 200,
217+
};
218+
const geolocation: playwright.Geolocation = {
219+
latitude: 0,
220+
longitude: 0,
221+
accuracy: undefined,
222+
};
223+
const httpCredentials: playwright.HTTPCredentials = {
224+
username: 'foo',
225+
password: 'bar',
226+
};
227+
const contextOptions: playwright.BrowserContextOptions = {
228+
viewport,
229+
geolocation,
230+
httpCredentials,
231+
};
232+
const page = await browser.newPage(contextOptions);
214233
const button = (await page.$('#myButton'))!;
215234
const div = (await page.$('#myDiv'))!;
216235
const input = (await page.$('#myInput'))!;
@@ -246,6 +265,16 @@ playwright.chromium.launch().then(async browser => {
246265
const buttonText = await (await button.getProperty('textContent')).jsonValue();
247266
await page.context().clearCookies();
248267

268+
const cookies: playwright.Cookie[] = await page.context().cookies(['http://example.com']);
269+
const cookie = cookies[0];
270+
const nameIsString: AssertType<string, typeof cookie.name> = true;
271+
const valueIsString: AssertType<string, typeof cookie.value> = true;
272+
const pathIsString: AssertType<string, typeof cookie.path> = true;
273+
const expiresIsNumber: AssertType<number, typeof cookie.expires> = true;
274+
const httpOnlyIsBoolean: AssertType<boolean, typeof cookie.httpOnly> = true;
275+
const secureIsBoolean: AssertType<boolean, typeof cookie.secure> = true;
276+
const sameSiteIsEnum: AssertType<"Strict"|"Lax"|"None", typeof cookie.sameSite> = true;
277+
249278
const navResponse = await page.waitForNavigation({
250279
timeout: 1000
251280
});

0 commit comments

Comments
 (0)