Introduce Eslint to validate style

This patch introduces eslint and fixes multiple minor code
style issues.
This commit is contained in:
Andrey Lushnikov 2017-06-11 01:32:59 -07:00
parent 2b50d8cc32
commit 1f51384918
14 changed files with 165 additions and 33 deletions

2
.eslintignore Normal file
View File

@ -0,0 +1,2 @@
third_party/*
examples/*

124
.eslintrc.js Normal file
View File

@ -0,0 +1,124 @@
module.exports = {
"root": true,
"env": {
"node": true,
"es6": true
},
"parserOptions": {
"ecmaVersion": 8
},
/**
* ESLint rules
*
* All available rules: http://eslint.org/docs/rules/
*
* Rules take the following form:
* "rule-name", [severity, { opts }]
* Severity: 2 == error, 1 == warning, 0 == off.
*/
"rules": {
/**
* Enforced rules
*/
// syntax preferences
"quotes": [2, "single", {
"avoidEscape": true,
"allowTemplateLiterals": true
}],
"semi": 2,
"no-extra-semi": 2,
"comma-style": [2, "last"],
"wrap-iife": [2, "inside"],
"spaced-comment": [2, "always", {
"markers": ["*"]
}],
"eqeqeq": [2],
"arrow-body-style": [2, "as-needed"],
"accessor-pairs": [2, {
"getWithoutSet": false,
"setWithoutGet": false
}],
"curly": [2, "multi-or-nest", "consistent"],
"new-parens": 2,
"func-call-spacing": 2,
"arrow-parens": [2, "as-needed"],
// anti-patterns
"no-with": 2,
"no-multi-str": 2,
"no-caller": 2,
"no-implied-eval": 2,
"no-labels": 2,
"no-new-object": 2,
"no-octal-escape": 2,
"no-self-compare": 2,
"no-shadow-restricted-names": 2,
"no-cond-assign": 2,
"no-debugger": 2,
"no-dupe-keys": 2,
"no-duplicate-case": 2,
"no-empty-character-class": 2,
"no-unreachable": 2,
"no-unsafe-negation": 2,
"radix": 2,
"valid-typeof": 2,
"no-unused-vars": [2, { "args": "none", "vars": "local" }],
// es2015 features
"require-yield": 2,
"template-curly-spacing": [2, "never"],
// spacing details
"space-infix-ops": 2,
"space-in-parens": [2, "never"],
"space-before-function-paren": [2, "never"],
"no-whitespace-before-property": 2,
"keyword-spacing": [2, {
"overrides": {
"if": {"after": true},
"else": {"after": true},
"for": {"after": true},
"while": {"after": true},
"do": {"after": true},
"switch": {"after": true},
"return": {"after": true}
}
}],
"arrow-spacing": [2, {
"after": true,
"before": true
}],
// file whitespace
"no-multiple-empty-lines": [2, {"max": 2}],
"no-mixed-spaces-and-tabs": 2,
"no-trailing-spaces": 2,
"linebreak-style": [ 2, "unix" ],
/**
* Disabled, aspirational rules
*/
"indent": [0, 4, { "SwitchCase": 1, "CallExpression": {"arguments": 2}, "MemberExpression": 2 }],
// brace-style is disabled, as eslint cannot enforce 1tbs as default, but allman for functions
"brace-style": [0, "allman", { "allowSingleLine": true }],
// key-spacing is disabled, as some objects use value-aligned spacing, some not.
"key-spacing": [0, {
"beforeColon": false,
"afterColon": true,
"align": "value"
}],
// quote-props is diabled, as property quoting styles are too varied to enforce.
"quote-props": [0, "as-needed"],
// no-implicit-globals will prevent accidental globals
"no-implicit-globals": [0]
}
};

View File

@ -16,7 +16,6 @@
var Downloader = require('./utils/ChromiumDownloader'); var Downloader = require('./utils/ChromiumDownloader');
var revision = require('./package').puppeteer.chromium_revision; var revision = require('./package').puppeteer.chromium_revision;
var fs = require('fs');
var ProgressBar = require('progress'); var ProgressBar = require('progress');
// Do nothing if the revision is already downloaded. // Do nothing if the revision is already downloaded.

16
lib/.eslintrc.js Normal file
View File

@ -0,0 +1,16 @@
module.exports = {
"extends": "../.eslintrc.js",
/**
* ESLint rules
*
* All available rules: http://eslint.org/docs/rules/
*
* Rules take the following form:
* "rule-name", [severity, { opts }]
* Severity: 2 == error, 1 == warning, 0 == off.
*/
"rules": {
"no-console": [2, { "allow": ["warn", "error", "assert", "timeStamp", "time", "timeEnd"] }],
"no-debugger": 0,
}
};

View File

@ -130,13 +130,12 @@ function waitForChromeResponsive(remoteDebuggingPort) {
path: '/json/list' path: '/json/list'
}; };
var probeTimeout = 100; var probeTimeout = 100;
var probeAttempt = 1;
sendRequest(); sendRequest();
return promise; return promise;
function sendRequest() { function sendRequest() {
var req = http.request(options, res => { var req = http.request(options, res => {
fulfill() fulfill();
}); });
req.on('error', e => setTimeout(sendRequest, probeTimeout)); req.on('error', e => setTimeout(sendRequest, probeTimeout));
req.end(); req.end();

View File

@ -208,10 +208,8 @@ class Page extends EventEmitter {
_handleException(exceptionDetails) { _handleException(exceptionDetails) {
var stack = []; var stack = [];
if (exceptionDetails.stackTrace) { if (exceptionDetails.stackTrace)
stack = exceptionDetails.stackTrace.callFrames.map(cf => cf.url); stack = exceptionDetails.stackTrace.callFrames.map(cf => cf.url);
}
var stackTrace = exceptionDetails.stackTrace;
this.emit(Page.Events.Exception, exceptionDetails.exception.description, stack); this.emit(Page.Events.Exception, exceptionDetails.exception.description, stack);
} }
@ -545,7 +543,7 @@ function convertPrintParameterToInches(parameter) {
throw new Error('printToPDF Cannot handle parameter type: ' + (typeof parameter)); throw new Error('printToPDF Cannot handle parameter type: ' + (typeof parameter));
} }
return pixels / 96; return pixels / 96;
}; }
Page.Events = { Page.Events = {
Alert: 'alert', Alert: 'alert',

View File

@ -87,4 +87,4 @@ var helpers = module.exports = {
code += `\n//# sourceURL=${sourceURL}`; code += `\n//# sourceURL=${sourceURL}`;
return code; return code;
} }
} };

View File

@ -121,13 +121,13 @@ module.exports.create = function(context, scriptPath) {
*/ */
loadModule: function(moduleSource, filename) { loadModule: function(moduleSource, filename) {
var code = [ var code = [
"(function(require, exports, module) {\n", '(function(require, exports, module) {\n',
moduleSource, moduleSource,
"\n}.call({},", '\n}.call({},',
"require.cache['" + filename + "']._getRequire(),", 'require.cache[\'' + filename + '\']._getRequire(),',
"require.cache['" + filename + "'].exports,", 'require.cache[\'' + filename + '\'].exports,',
"require.cache['" + filename + "']", 'require.cache[\'' + filename + '\']',
"));" '));'
].join(''); ].join('');
vm.runInContext(code, context, { vm.runInContext(code, context, {
filename: filename, filename: filename,
@ -136,4 +136,4 @@ module.exports.create = function(context, scriptPath) {
}, },
}; };
return phantom; return phantom;
} };

View File

@ -29,7 +29,7 @@ class System {
this.stdin = new StandardInput(process.stdin); this.stdin = new StandardInput(process.stdin);
this.stdout = new StandardOutput(process.stdout); this.stdout = new StandardOutput(process.stdout);
this.stderr = new StandardOutput(process.stderr); this.stderr = new StandardOutput(process.stderr);
this.platform = "phantomjs"; this.platform = 'phantomjs';
this.pid = process.pid; this.pid = process.pid;
this.isSSLSupported = false; this.isSSLSupported = false;
this.os = { this.os = {

View File

@ -18,10 +18,7 @@ var await = require('./utilities').await;
var EventEmitter = require('events'); var EventEmitter = require('events');
var fs = require('fs'); var fs = require('fs');
var path = require('path'); var path = require('path');
var mime = require('mime');
var PageEvents = require('../lib/Page').Events; var PageEvents = require('../lib/Page').Events;
var ScreenshotTypes = require('../lib/Page').ScreenshotTypes;
var noop = function() { }; var noop = function() { };
@ -42,9 +39,7 @@ class WebPage {
if (options.viewportSize) if (options.viewportSize)
await(this._page.setSize(options.viewportSize)); await(this._page.setSize(options.viewportSize));
await(this._page.setInPageCallback('callPhantom', (...args) => { await(this._page.setInPageCallback('callPhantom', (...args) => this.onCallback.apply(null, args)));
return this.onCallback.apply(null, args);
}));
this.clipRect = options.clipRect || {left: 0, top: 0, width: 0, height: 0}; this.clipRect = options.clipRect || {left: 0, top: 0, width: 0, height: 0};
this.onCallback = null; this.onCallback = null;

View File

@ -22,7 +22,7 @@ class WebServer {
this._server = http.createServer(); this._server = http.createServer();
this.objectName = 'WebServer'; this.objectName = 'WebServer';
this.listenOnPort = this.listen; this.listenOnPort = this.listen;
this.newRequest = function(req, res) { } this.newRequest = function(req, res) { };
Object.defineProperty(this, 'port', { Object.defineProperty(this, 'port', {
get: () => { get: () => {
if (!this._server.listening) if (!this._server.listening)
@ -64,10 +64,10 @@ class WebServer {
}); });
} }
res.header = res.getHeader; res.header = res.getHeader;
res.setHeaders = (headers) => { res.setHeaders = headers => {
for (var key in headers) for (var key in headers)
res.setHeader(key, headers[key]); res.setHeader(key, headers[key]);
} };
Object.defineProperty(res, 'statusCode', { Object.defineProperty(res, 'statusCode', {
enumerable: true, enumerable: true,
configurable: true, configurable: true,

View File

@ -44,7 +44,7 @@ module.exports = {
context.phantom = Phantom.create(context, scriptPath); context.phantom = Phantom.create(context, scriptPath);
context.console = console; context.console = console;
context.window = context; context.window = context;
context.WebPage = (options) => new WebPage(browser, scriptPath, options); context.WebPage = options => new WebPage(browser, scriptPath, options);
vm.createContext(context); vm.createContext(context);
@ -67,5 +67,5 @@ module.exports = {
})(nativeExports); })(nativeExports);
return context; return context;
} }
} };

View File

@ -29,5 +29,4 @@ module.exports = {
throw error; throw error;
return result; return result;
} }
} };

View File

@ -20,10 +20,10 @@ var https = require('https');
var OMAHA_PROXY = 'https://omahaproxy.appspot.com/all.json'; var OMAHA_PROXY = 'https://omahaproxy.appspot.com/all.json';
var colors = { var colors = {
reset: "\x1b[0m", reset: '\x1b[0m',
red: "\x1b[31m", red: '\x1b[31m',
green: "\x1b[32m", green: '\x1b[32m',
yellow: "\x1b[33m" yellow: '\x1b[33m'
}; };
class Table { class Table {