Skip to content

Commit

Permalink
#3 Add linting
Browse files Browse the repository at this point in the history
  • Loading branch information
gidztech committed Mar 6, 2018
1 parent 872cddb commit 8131db7
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 19 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"description": "Convenience functions for the Puppeteer",
"main": "lib/index.js",
"scripts": {
"build": "tsc",
"build": "npm run lint && tsc",
"test": "npm run build && jest /tests",
"postinstall": "node tests/setup.js"
"lint": "tslint -t codeFrame 'src/**/*.ts'"
"postinstall": "node tests/setup.js",
"lint": "tslint --project tsconfig.json -t codeFrame 'src/**/*.ts'"
},
"repository": {
"type": "git",
Expand Down
3 changes: 2 additions & 1 deletion src/api/miscellaneous.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
*
* This file represents the miscellaneous API. It exposes functions that don't fit under a particular category.
*
**/
*/

import {serializeFunctionWithArgs} from '../external/serialization-utils';
import {Page} from "puppeteer";

Expand Down
2 changes: 1 addition & 1 deletion src/api/retrieval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* This file represents the retrieval API. It exposes convenience functions for getting properties/values/state from the UI.
*
**/
*/

import {Page} from "puppeteer";

Expand Down
19 changes: 12 additions & 7 deletions src/api/waits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* This file represents the waits API. It exposes useful polling functions for particular resources or selectors
*
**/
*/

import {serializeFunctionWithArgs} from '../external/serialization-utils';
import {Request, Page} from "puppeteer";
Expand All @@ -26,7 +26,7 @@ const pollFor = ({checkFn, interval, timeout, timeoutMsg} : {checkFn: () => Prom

const isSuccessfulResponse = (request: Request) : boolean => {
const response = request.response && request.response();
return response && (response.status() === 200 || response.status() === 304);
return response && (response.status() === 200 || response.status() === 304) || false;
};

export function init (puppeteerPage: Page, requests, defaultTimeout) : object {
Expand All @@ -35,7 +35,7 @@ export function init (puppeteerPage: Page, requests, defaultTimeout) : object {
* Wait for a resource request to be responded to
* @param {string} resource - The URL of the resource (or a substring of it)
* @param {number] [timeout=defaultTimeout] - Timeout for the check
*/
*/
waitForResource (resource: string, timeout : number = defaultTimeout) {
return new Promise((resolve, reject) => {

Expand All @@ -49,7 +49,7 @@ export function init (puppeteerPage: Page, requests, defaultTimeout) : object {
} else {
pollFor({
checkFn: async () => {
return await resourceRequestHasResponded();
return resourceRequestHasResponded();
},
interval: 100,
timeout: timeout,
Expand All @@ -62,7 +62,7 @@ export function init (puppeteerPage: Page, requests, defaultTimeout) : object {
* Wait for a specific number of web fonts to be loaded and ready on the page
* @param {number} count - The number of web fonts to expect
* @param {number] [timeout=defaultTimeout] - Timeout for the check
*/
*/
async waitForLoadedWebFontCountToBe(count: number, timeout : number = defaultTimeout) {
let hasInjectedWebFontsAllLoadedFunction = false;

Expand All @@ -82,8 +82,13 @@ export function init (puppeteerPage: Page, requests, defaultTimeout) : object {
});
} else {
await puppeteerPage.evaluate(() => {
(async function() {
window.__webFontsAllLoaded = await document.fonts.ready;
return (async function() {
try {
window.__webFontsAllLoaded = await document.fonts.ready;
} catch (e) {
return false;
}

})();
});

Expand Down
16 changes: 10 additions & 6 deletions src/external/serialization-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Copyright (C) 2011 James Roe <[email protected]>
* Copyright (C) 2011 execjosh, http://execjosh.blogspot.com
* Copyright (C) 2012 James M. Greene <[email protected]>
**/
*/

// Source: https://github.com/ariya/phantomjs/blob/master/src/modules/webpage.js#L205
const detectType = (value: any) => {
Expand All @@ -33,7 +33,11 @@ const detectType = (value: any) => {

// Source: https://github.com/ariya/phantomjs/blob/master/src/modules/webpage.js#L167
const quoteString = (str: string) => {
let c, i, l = str.length, o = '"';
let c;
let i;
let l = str.length;
let o = '"';

for (i = 0; i < l; i += 1) {
c = str.charAt(i);
if (c >= ' ') {
Expand Down Expand Up @@ -80,14 +84,14 @@ export function serializeFunctionWithArgs(fn: any, ...args: any[]) {
let argType = detectType(arg);

switch (argType) {
case 'object': //< for type "object"
case 'array': //< for type "array"
case 'object': // < for type "object"
case 'array': // < for type "array"
str += JSON.stringify(arg) + ',';
break;
case 'date': //< for type "date"
case 'date': // < for type "date"
str += 'new Date(' + JSON.stringify(arg) + '),';
break;
case 'string': //< for type "string"
case 'string': // < for type "string"
str += quoteString(arg) + ',';
break;
default: // for types: "null", "number", "function", "regexp", "undefined"
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"lib": ["es2015", "es2016", "es2017", "dom"],
"outDir": "lib",
"sourceMap": true,
"target": "es2015"
"target": "es2015",
"strictNullChecks": true
},
"include": [
"src",
Expand Down

0 comments on commit 8131db7

Please sign in to comment.