Skip to content

Commit

Permalink
Nudge forward style checks a la eslint-config-standard (#1082)
Browse files Browse the repository at this point in the history
Because I despise nitpicking stuff like indentation and spacing in pull request comments, I'd like to nudge forward our automated style checking, at least for new files being added.

I don't want to totally rewrite server.js just to get automated style checking… the blame tracking is just too useful. So let's it's just take care of that when we start splitting it out.

More discussion in #948.
  • Loading branch information
paulmelnikow authored Oct 2, 2017
1 parent 820b72c commit f271b82
Show file tree
Hide file tree
Showing 34 changed files with 337 additions and 279 deletions.
15 changes: 15 additions & 0 deletions .eslintrc-preferred.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
rules:
# From eslint-config-standard.
arrow-spacing: ["error", { "before": true, "after": true }]
brace-style: ["error", "1tbs", { "allowSingleLine": true }]
camelcase: ["error", { "properties": "never" }]
func-style: ["error", "declaration", { "allowArrowFunctions": true }]
indent: ["error", 2, { "SwitchCase": 1 }]
key-spacing: ["error", { "beforeColon": false, "afterColon": true }]
no-trailing-spaces: "error"
quotes: ["error", "single", { "avoidEscape": true, "allowTemplateLiterals": true }]

# Shields additions.
no-var: "error"
prefer-const: "error"
strict: "error"
1 change: 1 addition & 0 deletions lib/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extends: '../.eslintrc-preferred.yml'
28 changes: 15 additions & 13 deletions lib/analytics.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
'use strict';

const fs = require('fs');

// We can either use a process-wide object regularly saved to a JSON file,
// or a Redis equivalent (for multi-process / when the filesystem is unreliable.
var redis;
var useRedis = false;
let redis;
let useRedis = false;
if (process.env.REDISTOGO_URL) {
var redisToGo = require('url').parse(process.env.REDISTOGO_URL);
const redisToGo = require('url').parse(process.env.REDISTOGO_URL);
redis = require('redis').createClient(redisToGo.port, redisToGo.hostname);
redis.auth(redisToGo.auth.split(':')[1]);
useRedis = true;
}

var analytics = {};
let analytics = {};

var analyticsAutoSaveFileName = process.env.SHIELDS_ANALYTICS_FILE || './analytics.json';
var analyticsAutoSavePeriod = 10000;
const analyticsAutoSaveFileName = process.env.SHIELDS_ANALYTICS_FILE || './analytics.json';
const analyticsAutoSavePeriod = 10000;
setInterval(function analyticsAutoSave() {
if (useRedis) {
redis.set(analyticsAutoSaveFileName, JSON.stringify(analytics));
Expand All @@ -24,7 +26,7 @@ setInterval(function analyticsAutoSave() {
}, analyticsAutoSavePeriod);

function defaultAnalytics() {
var analytics = Object.create(null);
const analytics = Object.create(null);
// In case something happens on the 36th.
analytics.vendorMonthly = new Array(36);
resetMonthlyAnalytics(analytics.vendorMonthly);
Expand All @@ -43,7 +45,7 @@ function defaultAnalytics() {

// Auto-load analytics.
function analyticsAutoLoad() {
var defaultAnalyticsObject = defaultAnalytics();
const defaultAnalyticsObject = defaultAnalytics();
if (useRedis) {
redis.get(analyticsAutoSaveFileName, function(err, value) {
if (err == null && value != null) {
Expand All @@ -52,7 +54,7 @@ function analyticsAutoLoad() {
try {
analytics = JSON.parse(value);
// Extend analytics with a new value.
for (var key in defaultAnalyticsObject) {
for (const key in defaultAnalyticsObject) {
if (!(key in analytics)) {
analytics[key] = defaultAnalyticsObject[key];
}
Expand All @@ -70,7 +72,7 @@ function analyticsAutoLoad() {
try {
analytics = JSON.parse(fs.readFileSync(analyticsAutoSaveFileName));
// Extend analytics with a new value.
for (var key in defaultAnalyticsObject) {
for (const key in defaultAnalyticsObject) {
if (!(key in analytics)) {
analytics[key] = defaultAnalyticsObject[key];
}
Expand All @@ -85,15 +87,15 @@ function analyticsAutoLoad() {
}
}

var lastDay = (new Date()).getDate();
let lastDay = (new Date()).getDate();
function resetMonthlyAnalytics(monthlyAnalytics) {
for (var i = 0; i < monthlyAnalytics.length; i++) {
for (let i = 0; i < monthlyAnalytics.length; i++) {
monthlyAnalytics[i] = 0;
}
}
function incrMonthlyAnalytics(monthlyAnalytics) {
try {
var currentDay = (new Date()).getDate();
const currentDay = (new Date()).getDate();
// If we changed month, reset empty days.
while (lastDay !== currentDay) {
// Assumption: at least a hit a month.
Expand Down
2 changes: 2 additions & 0 deletions lib/badge-data.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

const logos = require('./load-logos')();

function toArray(val) {
Expand Down
2 changes: 2 additions & 0 deletions lib/badge-data.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

const assert = require('assert');
const {
isDataUri,
Expand Down
55 changes: 29 additions & 26 deletions lib/badge.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
var fs = require('fs');
var path = require('path');
var SVGO = require('svgo');
var dot = require('dot');
var measureTextWidth = require('./measure-text.js');
'use strict';

const fs = require('fs');
const path = require('path');
const SVGO = require('svgo');
const dot = require('dot');
const measureTextWidth = require('./measure-text.js');

// cache templates.
var templates = {};
var templateFiles = fs.readdirSync(path.join(__dirname, '..', 'templates'));
const templates = {};
const templateFiles = fs.readdirSync(path.join(__dirname, '..', 'templates'));
dot.templateSettings.strip = false; // Do not strip whitespace.
templateFiles.forEach(function(filename) {
if (filename[0] === '.') { return; }
var templateData = fs.readFileSync(
const templateData = fs.readFileSync(
path.join(__dirname, '..', 'templates', filename)).toString();
var extension = path.extname(filename).slice(1);
var style = filename.slice(0, -(('-template.' + extension).length));
const extension = path.extname(filename).slice(1);
const style = filename.slice(0, -(('-template.' + extension).length));
// Compile the template. Necessary to always have a working template.
templates[style + '-' + extension] = dot.template(templateData);
if (extension === 'svg') {
// Substitute dot code.
var mapping = new Map();
var mappingIndex = 1;
var untemplatedSvg = templateData.replace(/{{.*?}}/g, function(match) {
const mapping = new Map();
let mappingIndex = 1;
const untemplatedSvg = templateData.replace(/{{.*?}}/g, function(match) {
// Weird substitution that currently works for all templates.
var mapKey = '99999990' + mappingIndex + '.1';
const mapKey = '99999990' + mappingIndex + '.1';
mappingIndex++;
mapping.set(mapKey, match);
return mapKey;
Expand All @@ -35,10 +37,10 @@ templateFiles.forEach(function(filename) {
return;
}
// Substitute dot code back.
var svg = object.data;
var unmappedKeys = [];
let svg = object.data;
const unmappedKeys = [];
mapping.forEach(function(value, key) {
var keySubstituted = false;
let keySubstituted = false;
svg = svg.replace(RegExp(key, 'g'), function() {
keySubstituted = true;
return value;
Expand Down Expand Up @@ -76,14 +78,14 @@ function addEscapers(data) {
data.capitalize = capitalize;
}

var colorscheme = require(path.join(__dirname, 'colorscheme.json'));
const colorscheme = require(path.join(__dirname, 'colorscheme.json'));

function compressSvg(string, callback) {
var svgo = new SVGO();
const svgo = new SVGO();
svgo.optimize(string, callback);
}

var cssColor = /^#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})$/;
const cssColor = /^#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})$/;

function makeImage(data, cb) {
if (data.format !== 'json') {
Expand All @@ -93,7 +95,7 @@ function makeImage(data, cb) {
data.template = 'flat';
}
if (data.colorscheme) {
var pickedColorscheme = colorscheme[data.colorscheme];
let pickedColorscheme = colorscheme[data.colorscheme];
if (!pickedColorscheme) {
pickedColorscheme = colorscheme.red;
}
Expand All @@ -113,8 +115,8 @@ function makeImage(data, cb) {
data.logoPadding = 0;
}

var textWidth1 = (measureTextWidth(data.text[0])|0);
var textWidth2 = (measureTextWidth(data.text[1])|0);
let textWidth1 = (measureTextWidth(data.text[0])|0);
let textWidth2 = (measureTextWidth(data.text[1])|0);
// Increase chances of pixel grid alignment.
if (textWidth1 % 2 === 0) { textWidth1++; }
if (textWidth2 % 2 === 0) { textWidth2++; }
Expand All @@ -125,15 +127,16 @@ function makeImage(data, cb) {
if (data.links === undefined) {
data.links = ['', ''];
} else {
for (var i = 0; i < data.links.length; i++) {
for (let i = 0; i < data.links.length; i++) {
data.links[i] = escapeXml(data.links[i]);
}
}

var template = templates[data.template + '-' + data.format];
const template = templates[data.template + '-' + data.format];
addEscapers(data);
let result;
try {
var result = template(data);
result = template(data);
} catch(e) {
cb('', e);
return;
Expand Down
2 changes: 2 additions & 0 deletions lib/badge.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

const assert = require('assert');
const badge = require('./badge');
const isSvg = require('is-svg');
Expand Down
2 changes: 1 addition & 1 deletion lib/color-formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
'use strict';

function versionFormatter(version) {
var first = version[0];
let first = version[0];
if (first === 'v') {
first = version[1];
} else if (/^[0-9]/.test(version)) {
Expand Down
Loading

0 comments on commit f271b82

Please sign in to comment.