Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): export selected types #1881

Merged
merged 1 commit into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions utils/generate_types/exported.json
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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