Skip to content

Commit

Permalink
Refine code using eslint + adding travis.
Browse files Browse the repository at this point in the history
Refine code using eslint
Add eslint and travis

* streamline code for `for` statements

* apply no-var

* fixes for eslint

* refine more for and if

* revert `externalAppRuleParser` changes per review

* modify per review

* No "for..of" in "core-chrome-proxy.js" and "core-firefox-proxy.js" per review
  • Loading branch information
yfdyh000 authored and salarcode committed Sep 18, 2017
1 parent 13341a9 commit e766fc4
Show file tree
Hide file tree
Showing 14 changed files with 397 additions and 474 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.min.js
jsgrid.bootstrap.js
36 changes: 36 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"root": true,
"env": {
"browser": true,
"es6": true,
"webextensions": true
},
"extends": "eslint:recommended",
"globals": {
"$": false,
"browser": true,
"debug": false,
"environment": false,
"jQuery": false,
"jsGrid": false,
"localizeHtmlPage": false,
"messageBox": false,
"Noty": false,
"proxyImporter": false,
"proxyModeType": false,
"polyfill": false,
"ruleImporter": false,
"utils": false
},
"parserOptions": {
"ecmaVersion": 6
},
"rules": {
"no-console": 1,
"no-undef": 1,
"no-unused-vars": 1,
"no-extra-semi": 1,
"no-useless-escape": 1,
"no-var": 1
}
}
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
language: node_js
node_js: stable
sudo: false
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "SmartProxy",
"description": "SmartProxy is a Firefox/Chrome extension based on WebExtensions/Chrome Extensions.",
"devDependencies": {
"eslint": "^4.7.0"
},
"scripts": {
"lint": "eslint ./src",
"test": "npm run lint"
},
"license": "GPL-3.0"
}
28 changes: 14 additions & 14 deletions src/core-chrome-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with SmartProxy. If not, see <http://www.gnu.org/licenses/>.
*/
var chromeProxy = {
const chromeProxy = {
convertActiveProxyServer: function (activeProxyServer) {
const resultDirect = "DIRECT";

Expand Down Expand Up @@ -53,7 +53,7 @@ var chromeProxy = {

// matches all valid match patterns (except '<all_urls>')
// and extracts [ , scheme, host, path, ]
const matchPattern = (/^(?:(\*|http|https|file|ftp|app):\/\/([^\/]+|)\/?(.*))$/i);
const matchPattern = (/^(?:(\*|http|https|file|ftp|app):\/\/([^/]+|)\/?(.*))$/i);

if (pattern === '<all_urls>') {
//return (/^(?:https?|file|ftp|app):\/\//);
Expand All @@ -75,7 +75,7 @@ var chromeProxy = {
compileRules: function (proxyRulesList) {
if (!proxyRulesList || !proxyRulesList.length)
return [];
var result = [];
let result = [];

for (let i = 0; i < proxyRulesList.length; i++) {
let rule = proxyRulesList[i];
Expand All @@ -84,7 +84,7 @@ var chromeProxy = {

let regex = chromeProxy.matchPatternToRegExp(rule.pattern);
if (regex != null) {
var proxyResult = null;
let proxyResult = null;
if (rule.proxy) {
proxyResult = chromeProxy.convertActiveProxyServer(rule.proxy);
}
Expand All @@ -98,9 +98,9 @@ var chromeProxy = {
return result;
},
regexHostArrayToString: function (compiledRules) {
var compiledRulesAsStringArray = [];
for (var index = 0; index < compiledRules.length; index++) {
var rule = compiledRules[index];
let compiledRulesAsStringArray = [];
for (let index = 0; index < compiledRules.length; index++) {
let rule = compiledRules[index];

if (rule.proxy) {
compiledRulesAsStringArray.push(`{regex:${rule.regex.toString()},proxy:"${rule.proxy}"}`);
Expand All @@ -111,14 +111,14 @@ var chromeProxy = {
return compiledRulesAsStringArray;
},
generateChromePacScript: function (proxyInitData) {
var proxyRules = proxyInitData.proxyRules;
var proxyMode = proxyInitData.proxyMode;
var resultActiveProxy = chromeProxy.convertActiveProxyServer(proxyInitData.activeProxyServer);
let proxyRules = proxyInitData.proxyRules;
let proxyMode = proxyInitData.proxyMode;
let resultActiveProxy = chromeProxy.convertActiveProxyServer(proxyInitData.activeProxyServer);

var compiledRules = chromeProxy.compileRules(proxyRules);
var compiledRulesAsString = chromeProxy.regexHostArrayToString(compiledRules).join(",");
let compiledRules = chromeProxy.compileRules(proxyRules);
let compiledRulesAsString = chromeProxy.regexHostArrayToString(compiledRules).join(",");

var pacTemplateString = `var proxyMode = "${proxyMode}";
let pacTemplateString = `var proxyMode = "${proxyMode}";
var compiledRules = [${compiledRulesAsString}];
var hasActiveProxyServer = ${((proxyInitData.activeProxyServer) ? "true" : "false")};
const proxyModeType = {
Expand Down Expand Up @@ -155,7 +155,7 @@ function FindProxyForURL(url, host) {
if (rule.regex.test(url)) {
if (rule.proxy)
// this rule has its own proxy setted
// this rule has its own proxy setup
return rule.proxy;
return resultActiveProxy;
}
Expand Down
26 changes: 13 additions & 13 deletions src/core-firefox-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@
* You should have received a copy of the GNU General Public License
* along with SmartProxy. If not, see <http://www.gnu.org/licenses/>.
*/
var proxyMode = "1";
var compiledRules = [];
var activeProxyServer = null;
let proxyMode = "1";
let compiledRules = [];
let activeProxyServer = null;
const proxyModeType = {
direct: "1",
smartProxy: "2",
always: "3",
systemProxy: "4"
};
var resultActiveProxy = "DIRECT";
let resultActiveProxy = "DIRECT";
const resultDirect = "DIRECT";
const resultSystem = "SYSTEM";

//-----------------------------
// Subset of polyfill api for proxy, since it doesn't have access to 'core-polyfill.js'
//-----------------------------
var environment = {
let environment = {
chrome: false
};

Expand All @@ -40,7 +40,7 @@ if (typeof browser === "undefined") {
environment.chrome = true;
}

var polyfill = {
const polyfill = {
lastError: function () {
if (environment.chrome) {
// chrome.extension.lastError Deprecated since Chrome 58
Expand All @@ -56,7 +56,7 @@ var polyfill = {
message,
options,
function (response) {
var error = polyfill.lastError();
let error = polyfill.lastError();
if (error) {
if (fail) fail(error);
} else {
Expand Down Expand Up @@ -87,7 +87,7 @@ var polyfill = {
function handleMessages(message, sender, sendResponse) {

if (typeof (message) == "object") {
var command = message["command"];
let command = message["command"];

if (command == "proxyModeChanged" &&
message["proxyMode"] != null) {
Expand All @@ -100,15 +100,15 @@ var polyfill = {
} else if (command == "activeProxyServerChanged" &&
message["activeProxyServer"] != null) {

var newActiveProxyServer = message["activeProxyServer"];
let newActiveProxyServer = message["activeProxyServer"];

activeProxyServer = newActiveProxyServer;
resultActiveProxy = convertActiveProxyServer(activeProxyServer);

} else if (command == "proxyRulesChanged" &&
message["proxyRules"] != null) {

var newProxyRules = message["proxyRules"];
let newProxyRules = message["proxyRules"];

compiledRules = compileRules(newProxyRules);
}
Expand Down Expand Up @@ -138,7 +138,7 @@ var polyfill = {
function compileRules(proxyRules) {
if (!proxyRules || !proxyRules.length)
return [];
var result = [];
let result = [];

for (let i = 0; i < proxyRules.length; i++) {
let rule = proxyRules[i];
Expand All @@ -147,7 +147,7 @@ var polyfill = {

let regex = matchPatternToRegExp(rule.pattern);
if (regex != null) {
var proxyResult = null;
let proxyResult = null;
if (rule.proxy) {
proxyResult = convertActiveProxyServer(rule.proxy);
}
Expand Down Expand Up @@ -251,7 +251,7 @@ function FindProxyForURL(url, host) {

if (rule.regex.test(url)) {
if (rule.proxy)
// this rule has its own proxy setted
// this rule has its own proxy setup
return rule.proxy;
return resultActiveProxy;
}
Expand Down
Loading

0 comments on commit e766fc4

Please sign in to comment.