mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
chore(typescript): migrate src/Dialog (#5639)
This PR changes `src/Dialog.js` to `src/Dialog.ts` and rewrites accordingly. Most of the changes are straight forward; the only interesting one from a TS point of view is the `DialogType` enum. I expose it again as `Dialog.Type` to avoid a breaking change. This PR also exposed some bugs with our ESLint TypeScript settings and applying the overrides, so I fixed those too. I also updated our DocLint tool to work on TS source files over JS lib files if they exist. This is the minimal change to keep the existing doc system working as we're working on moving away from this system longer term.
This commit is contained in:
parent
a9f6a266b9
commit
3e4c8c9d0d
11
.eslintrc.js
11
.eslintrc.js
@ -102,12 +102,17 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
"overrides": [
|
"overrides": [
|
||||||
{
|
{
|
||||||
// apply TypeScript linting to the TS files in src/
|
"files": ["*.ts"],
|
||||||
"files": ["src/*.ts"],
|
|
||||||
"extends": [
|
"extends": [
|
||||||
'plugin:@typescript-eslint/eslint-recommended',
|
'plugin:@typescript-eslint/eslint-recommended',
|
||||||
'plugin:@typescript-eslint/recommended',
|
'plugin:@typescript-eslint/recommended',
|
||||||
]
|
],
|
||||||
|
"rules": {
|
||||||
|
"no-unused-vars": 0,
|
||||||
|
"@typescript-eslint/no-unused-vars": 2,
|
||||||
|
"semi": 0,
|
||||||
|
"@typescript-eslint/semi": 2
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
|
@ -14,48 +14,46 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const {assert} = require('./helper');
|
import helpers = require('./helper');
|
||||||
|
|
||||||
|
const {assert} = helpers;
|
||||||
|
|
||||||
|
enum DialogType {
|
||||||
|
Alert = 'alert',
|
||||||
|
BeforeUnload = 'beforeunload',
|
||||||
|
Confirm = 'confirm',
|
||||||
|
Prompt = 'prompt'
|
||||||
|
}
|
||||||
|
|
||||||
class Dialog {
|
class Dialog {
|
||||||
/**
|
static Type = DialogType;
|
||||||
* @param {!Puppeteer.CDPSession} client
|
|
||||||
* @param {string} type
|
private _client: Puppeteer.CDPSession;
|
||||||
* @param {string} message
|
private _type: DialogType;
|
||||||
* @param {(string|undefined)} defaultValue
|
private _message: string;
|
||||||
*/
|
private _defaultValue: string;
|
||||||
constructor(client, type, message, defaultValue = '') {
|
private _handled = false;
|
||||||
|
|
||||||
|
constructor(client: Puppeteer.CDPSession, type: DialogType, message: string, defaultValue = '') {
|
||||||
this._client = client;
|
this._client = client;
|
||||||
this._type = type;
|
this._type = type;
|
||||||
this._message = message;
|
this._message = message;
|
||||||
this._handled = false;
|
|
||||||
this._defaultValue = defaultValue;
|
this._defaultValue = defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
type(): DialogType {
|
||||||
* @return {string}
|
|
||||||
*/
|
|
||||||
type() {
|
|
||||||
return this._type;
|
return this._type;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
message(): string {
|
||||||
* @return {string}
|
|
||||||
*/
|
|
||||||
message() {
|
|
||||||
return this._message;
|
return this._message;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
defaultValue(): string {
|
||||||
* @return {string}
|
|
||||||
*/
|
|
||||||
defaultValue() {
|
|
||||||
return this._defaultValue;
|
return this._defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
async accept(promptText?: string): Promise<void> {
|
||||||
* @param {string=} promptText
|
|
||||||
*/
|
|
||||||
async accept(promptText) {
|
|
||||||
assert(!this._handled, 'Cannot accept dialog which is already handled!');
|
assert(!this._handled, 'Cannot accept dialog which is already handled!');
|
||||||
this._handled = true;
|
this._handled = true;
|
||||||
await this._client.send('Page.handleJavaScriptDialog', {
|
await this._client.send('Page.handleJavaScriptDialog', {
|
||||||
@ -64,7 +62,7 @@ class Dialog {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async dismiss() {
|
async dismiss(): Promise<void> {
|
||||||
assert(!this._handled, 'Cannot dismiss dialog which is already handled!');
|
assert(!this._handled, 'Cannot dismiss dialog which is already handled!');
|
||||||
this._handled = true;
|
this._handled = true;
|
||||||
await this._client.send('Page.handleJavaScriptDialog', {
|
await this._client.send('Page.handleJavaScriptDialog', {
|
||||||
@ -73,11 +71,4 @@ class Dialog {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Dialog.Type = {
|
export = {Dialog};
|
||||||
Alert: 'alert',
|
|
||||||
BeforeUnload: 'beforeunload',
|
|
||||||
Confirm: 'confirm',
|
|
||||||
Prompt: 'prompt'
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = {Dialog};
|
|
@ -3,6 +3,7 @@ const path = require('path');
|
|||||||
const Documentation = require('./Documentation');
|
const Documentation = require('./Documentation');
|
||||||
module.exports = checkSources;
|
module.exports = checkSources;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {!Array<!import('../Source')>} sources
|
* @param {!Array<!import('../Source')>} sources
|
||||||
*/
|
*/
|
||||||
@ -30,7 +31,23 @@ function checkSources(sources) {
|
|||||||
const classes = [];
|
const classes = [];
|
||||||
/** @type {!Map<string, string>} */
|
/** @type {!Map<string, string>} */
|
||||||
const inheritance = new Map();
|
const inheritance = new Map();
|
||||||
sourceFiles.filter(x => !x.fileName.includes('node_modules')).map(x => visit(x));
|
|
||||||
|
const sourceFilesNoNodeModules = sourceFiles.filter(x => !x.fileName.includes('node_modules'));
|
||||||
|
const sourceFileNamesSet = new Set(sourceFilesNoNodeModules.map(x => x.fileName));
|
||||||
|
sourceFilesNoNodeModules.map(x => {
|
||||||
|
if (x.fileName.includes('/lib/')) {
|
||||||
|
const potentialTSSource = x.fileName.replace('lib', 'src').replace('.js', '.ts');
|
||||||
|
if (sourceFileNamesSet.has(potentialTSSource)) {
|
||||||
|
/* Not going to visit this file because we have the TypeScript src code
|
||||||
|
* which we'll use instead.
|
||||||
|
*/
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
visit(x);
|
||||||
|
});
|
||||||
|
|
||||||
const errors = [];
|
const errors = [];
|
||||||
const documentation = new Documentation(recreateClassesWithInheritance(classes, inheritance));
|
const documentation = new Documentation(recreateClassesWithInheritance(classes, inheritance));
|
||||||
|
|
||||||
|
@ -49,8 +49,13 @@ async function run() {
|
|||||||
const browser = await puppeteer.launch();
|
const browser = await puppeteer.launch();
|
||||||
const page = await browser.newPage();
|
const page = await browser.newPage();
|
||||||
const checkPublicAPI = require('./check_public_api');
|
const checkPublicAPI = require('./check_public_api');
|
||||||
|
const tsSources = await Source.readdir(path.join(PROJECT_DIR, 'src'), 'ts');
|
||||||
|
|
||||||
|
const tsSourcesNoDefinitions = tsSources.filter(source => !source.filePath().endsWith('.d.ts'));
|
||||||
|
|
||||||
const jsSources = await Source.readdir(path.join(PROJECT_DIR, 'lib'));
|
const jsSources = await Source.readdir(path.join(PROJECT_DIR, 'lib'));
|
||||||
messages.push(...await checkPublicAPI(page, mdSources, jsSources));
|
const allSrcCode = [...jsSources, ...tsSourcesNoDefinitions];
|
||||||
|
messages.push(...await checkPublicAPI(page, mdSources, allSrcCode));
|
||||||
|
|
||||||
await browser.close();
|
await browser.close();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user