Skip to content

Commit

Permalink
Introduce Eslint to validate style
Browse files Browse the repository at this point in the history
This patch introduces eslint and fixes multiple minor code
style issues.
  • Loading branch information
aslushnikov committed Jun 11, 2017
1 parent 2b50d8c commit 1f51384
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 33 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
third_party/*
examples/*
124 changes: 124 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -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]
}
};
1 change: 0 additions & 1 deletion install.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

var Downloader = require('./utils/ChromiumDownloader');
var revision = require('./package').puppeteer.chromium_revision;
var fs = require('fs');
var ProgressBar = require('progress');

// Do nothing if the revision is already downloaded.
Expand Down
16 changes: 16 additions & 0 deletions lib/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -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,
}
};
3 changes: 1 addition & 2 deletions lib/Browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,12 @@ function waitForChromeResponsive(remoteDebuggingPort) {
path: '/json/list'
};
var probeTimeout = 100;
var probeAttempt = 1;
sendRequest();
return promise;

function sendRequest() {
var req = http.request(options, res => {
fulfill()
fulfill();
});
req.on('error', e => setTimeout(sendRequest, probeTimeout));
req.end();
Expand Down
6 changes: 2 additions & 4 deletions lib/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,8 @@ class Page extends EventEmitter {

_handleException(exceptionDetails) {
var stack = [];
if (exceptionDetails.stackTrace) {
if (exceptionDetails.stackTrace)
stack = exceptionDetails.stackTrace.callFrames.map(cf => cf.url);
}
var stackTrace = exceptionDetails.stackTrace;
this.emit(Page.Events.Exception, exceptionDetails.exception.description, stack);
}

Expand Down Expand Up @@ -545,7 +543,7 @@ function convertPrintParameterToInches(parameter) {
throw new Error('printToPDF Cannot handle parameter type: ' + (typeof parameter));
}
return pixels / 96;
};
}

Page.Events = {
Alert: 'alert',
Expand Down
2 changes: 1 addition & 1 deletion lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@ var helpers = module.exports = {
code += `\n//# sourceURL=${sourceURL}`;
return code;
}
}
};
14 changes: 7 additions & 7 deletions phantomjs/Phantom.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@ module.exports.create = function(context, scriptPath) {
*/
loadModule: function(moduleSource, filename) {
var code = [
"(function(require, exports, module) {\n",
'(function(require, exports, module) {\n',
moduleSource,
"\n}.call({},",
"require.cache['" + filename + "']._getRequire(),",
"require.cache['" + filename + "'].exports,",
"require.cache['" + filename + "']",
"));"
'\n}.call({},',
'require.cache[\'' + filename + '\']._getRequire(),',
'require.cache[\'' + filename + '\'].exports,',
'require.cache[\'' + filename + '\']',
'));'
].join('');
vm.runInContext(code, context, {
filename: filename,
Expand All @@ -136,4 +136,4 @@ module.exports.create = function(context, scriptPath) {
},
};
return phantom;
}
};
2 changes: 1 addition & 1 deletion phantomjs/System.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class System {
this.stdin = new StandardInput(process.stdin);
this.stdout = new StandardOutput(process.stdout);
this.stderr = new StandardOutput(process.stderr);
this.platform = "phantomjs";
this.platform = 'phantomjs';
this.pid = process.pid;
this.isSSLSupported = false;
this.os = {
Expand Down
7 changes: 1 addition & 6 deletions phantomjs/WebPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ var await = require('./utilities').await;
var EventEmitter = require('events');
var fs = require('fs');
var path = require('path');
var mime = require('mime');

var PageEvents = require('../lib/Page').Events;
var ScreenshotTypes = require('../lib/Page').ScreenshotTypes;

var noop = function() { };

Expand All @@ -42,9 +39,7 @@ class WebPage {
if (options.viewportSize)
await(this._page.setSize(options.viewportSize));

await(this._page.setInPageCallback('callPhantom', (...args) => {
return this.onCallback.apply(null, args);
}));
await(this._page.setInPageCallback('callPhantom', (...args) => this.onCallback.apply(null, args)));

this.clipRect = options.clipRect || {left: 0, top: 0, width: 0, height: 0};
this.onCallback = null;
Expand Down
6 changes: 3 additions & 3 deletions phantomjs/WebServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class WebServer {
this._server = http.createServer();
this.objectName = 'WebServer';
this.listenOnPort = this.listen;
this.newRequest = function(req, res) { }
this.newRequest = function(req, res) { };
Object.defineProperty(this, 'port', {
get: () => {
if (!this._server.listening)
Expand Down Expand Up @@ -64,10 +64,10 @@ class WebServer {
});
}
res.header = res.getHeader;
res.setHeaders = (headers) => {
res.setHeaders = headers => {
for (var key in headers)
res.setHeader(key, headers[key]);
}
};
Object.defineProperty(res, 'statusCode', {
enumerable: true,
configurable: true,
Expand Down
4 changes: 2 additions & 2 deletions phantomjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module.exports = {
context.phantom = Phantom.create(context, scriptPath);
context.console = console;
context.window = context;
context.WebPage = (options) => new WebPage(browser, scriptPath, options);
context.WebPage = options => new WebPage(browser, scriptPath, options);

vm.createContext(context);

Expand All @@ -67,5 +67,5 @@ module.exports = {
})(nativeExports);
return context;
}
}
};

3 changes: 1 addition & 2 deletions phantomjs/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,4 @@ module.exports = {
throw error;
return result;
}
}

};
8 changes: 4 additions & 4 deletions utils/check_availability.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ var https = require('https');
var OMAHA_PROXY = 'https://omahaproxy.appspot.com/all.json';

var colors = {
reset: "\x1b[0m",
red: "\x1b[31m",
green: "\x1b[32m",
yellow: "\x1b[33m"
reset: '\x1b[0m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m'
};

class Table {
Expand Down

0 comments on commit 1f51384

Please sign in to comment.