Skip to content

Commit

Permalink
feat(tools): add eslint/prettier (#151)
Browse files Browse the repository at this point in the history
* feat(tools): add eslint/prettier

* fix: es2021

Co-authored-by: Oliver Eyton-Williams <[email protected]>

* fix: ignore the boilerplate test files

* chore: lint american-british-translator

* chore: lint anonymous-message-board

* chore: lint chart-the-stock-market

* chore: lint exercise-tracker

* chore: lint file-metadata-microservice

* chore: lint forum-proxy

* chore: lint image-search-abstraction-layer

* chore: lint issue-tracker

* chore: lint manage-a-book-trading-club

* chore: lint metric-imperial-converter

* chore: lint p2p-video-chat-application

* chore: lint personal-library

* chore: lint request-header-parser-microservice

* chore: lint secure-real-time-multiplayer-game

* chore: lint stock-price-checker-proxy

* chore: lint stock-price-checker

* chore: lint sudoku-solver

* chore: lint timestamp-microservice

* chore: lint twitch-proxy

* chore: lint voting-app

* chore: lint weather-proxy

* fix: modules for specific test file

* feat: enable linting

* chore: move configs around

Co-authored-by: Oliver Eyton-Williams <[email protected]>

* chore: lint new react errors

Co-authored-by: Oliver Eyton-Williams <[email protected]>
  • Loading branch information
Naomi Carrigan and ojeytonwilliams authored Feb 4, 2022
1 parent a05aedd commit 6acdac9
Show file tree
Hide file tree
Showing 56 changed files with 1,489 additions and 139 deletions.
6 changes: 6 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Ignore minified libraries
apps/voting-app/public/*.min.js
# Ignore test runner files?
apps/**/assertion-analyser.js
apps/**/test-runner.js
apps/**/fcctesting.js
42 changes: 42 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"env": {
"es2021": true,
"browser": true,
"mocha": true,
"node": true
},
"globals": {
"Promise": true,
"window": true,
"$": true,
"ga": true,
"jQuery": true,
"router": true
},
"extends": ["eslint:recommended", "prettier"],
"overrides": [
{
"files": ["apps/secure-real-time-multiplayer-game/tests/1_unit-tests.js"],
"parserOptions": {
"sourceType": "module"
}
},
{
"files": ["**/*.jsx"],
"settings": {
"react": {
"version": "15.7.0"
}
},
"rules": {
"react/no-string-refs": "off"
},
"extends": ["plugin:react/recommended"],
"parserOptions": {
"ecmaFeatures": {
"jsx": true
}
}
}
]
}
3 changes: 3 additions & 0 deletions .github/workflows/node-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@ jobs:
- name: Install Dependencies
run: npm ci

- name: Lint Files
run: npm run lint

- name: Run Tests
run: npm run test-local-or-ci
9 changes: 9 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"endOfLine": "lf",
"semi": true,
"singleQuote": true,
"jsxSingleQuote": true,
"tabWidth": 2,
"trailingComma": "none",
"arrowParens": "avoid"
}
10 changes: 5 additions & 5 deletions apps/american-british-translator/components/translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ const britishOnly = require('./british-only.js')

class Translator {

static replaceCurry(word, replacement, highlight, adjustCase = false) {
static replaceCurry(_, replacement, highlight, adjustCase = false) {
if(adjustCase) {
replacement = replacement.replace(/^([a-z])/ig, letter => letter.toUpperCase());
}
return (word) => {
return () => {
if(highlight) {
return `<span class="highlight">${replacement}</span>`.replace(/\s/g,"\0");
} else {
Expand All @@ -35,14 +35,14 @@ class Translator {

// Title Replacement
for([american, british] of Object.entries(americanToBritishTitles) ) {
american = american.replace('.', '\.');
american = american.replace('.', '\\.');
input = input.replace(new RegExp(`\\b${american}`,'gi'),
Translator.replaceCurry(american,british,highlight, true));
}

// Time Replacement, colon to period replacement
if(highlight) {
input = input.replace(/(\d{1,2}):(\d{1,2})/gi, '<span class="highlight">\$1.\$2</span>');
input = input.replace(/(\d{1,2}):(\d{1,2})/gi, '<span class="highlight">$1.$2</span>');
} else {
input = input.replace(/(?<=\d{1,2}):(?=\d{1,2})/gi, '.');
}
Expand Down Expand Up @@ -76,7 +76,7 @@ class Translator {

// Time Replacement, period to colon replacement
if(highlight) {
input = input.replace(/(\d{1,2}).(\d{1,2})/gi, '<span class="highlight">\$1:\$2</span>');
input = input.replace(/(\d{1,2}).(\d{1,2})/gi, '<span class="highlight">$1:$2</span>');
} else {
input = input.replace(/(?<=\d{1,2})\.(?=\d{1,2})/gi, ':');
}
Expand Down
2 changes: 1 addition & 1 deletion apps/american-british-translator/routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = function (app) {
app.route('/api/translate')
.post((req, res) => {
let text = req.body.text;
if(!req.body.hasOwnProperty('text') || !req.body.locale) {
if(!("text" in req.body) || !req.body.locale) {
return res.json({ error: 'Required field(s) missing' });
}

Expand Down
3 changes: 1 addition & 2 deletions apps/american-british-translator/server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const expect = require('chai').expect;
const cors = require('cors');

const fccTestingRoutes = require('./routes/fcctesting.js');
Expand Down Expand Up @@ -29,7 +28,7 @@ fccTestingRoutes(app);
userRoutes(app);

// 404 Not Found Middleware
app.use(function(req, res, next) {
app.use(function(req, res) {
res.status(404)
.type('text')
.send('Not Found');
Expand Down
2 changes: 0 additions & 2 deletions apps/american-british-translator/tests/2_functional-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ const server = require('../server.js');

chai.use(chaiHttp);

let Translator = require('../components/translator.js');

suite('Functional Tests', () => {

suite('"POST" to /api/translate', () => {
Expand Down
4 changes: 2 additions & 2 deletions apps/anonymous-message-board/controllers/replyHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function ReplyHandler() {
$set: { bumped_on: now },
$push: { replies: reply },
},
(err, doc) => {}
() => {}
);
}
);
Expand All @@ -73,7 +73,7 @@ function ReplyHandler() {
"replies._id": new ObjectId(req.body.reply_id),
},
{ $set: { "replies.$.reported": true } },
(err, doc) => {}
() => {}
);
}
);
Expand Down
2 changes: 1 addition & 1 deletion apps/anonymous-message-board/controllers/threadHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function ThreadHandler() {
collection.findOneAndUpdate(
{ _id: new ObjectId(req.body.report_id) },
{ $set: { reported: true } },
(err, doc) => {}
() => {}
);
}
);
Expand Down
1 change: 0 additions & 1 deletion apps/anonymous-message-board/routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

'use strict';

const expect = require('chai').expect;
const ThreadHandler = require('../controllers/threadHandler.js');
const ReplyHandler = require('../controllers/replyHandler.js');

Expand Down
3 changes: 1 addition & 2 deletions apps/anonymous-message-board/server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const expect = require('chai').expect;
const cors = require('cors');
const helmet = require('helmet');

Expand Down Expand Up @@ -46,7 +45,7 @@ fccTestingRoutes(app);
apiRoutes(app);

// 404 Not Found middleware
app.use(function(req, res, next) {
app.use(function(req, res) {
res.status(404)
.type('text')
.send('Not Found');
Expand Down
7 changes: 6 additions & 1 deletion apps/build-a-pinterest-clone/client/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ var Nav = require('./navbar.jsx');

var appUrl = window.location.origin;

/**
* TODO: I assume at some point we'll bump the React version.
* When we do, we'll need to update this.
*/
// eslint-disable-next-line react/no-deprecated
var App = React.createClass({
componentDidMount: function () {
var self = this;
Expand Down Expand Up @@ -83,7 +88,7 @@ var App = React.createClass({
deletePic: function(index) {
var self = this;
var id = this.state.pics[index]._id;
Ajax.delete(appUrl + '/api/pics/' + id, {}, function(err, d){
Ajax.delete(appUrl + '/api/pics/' + id, {}, function(){
var pics = self.state.pics;
pics.splice(index,1);
self.setState({pics: pics});
Expand Down
6 changes: 6 additions & 0 deletions apps/build-a-pinterest-clone/client/navbar.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
var React = require('react');

/**
* TODO: I assume at some point we'll bump the React version.
* When we do, we'll need to update this.
*/
// eslint-disable-next-line react/no-deprecated
module.exports = React.createClass({
submit: function(e) {
e.preventDefault();
Expand All @@ -13,6 +18,7 @@ module.exports = React.createClass({
var hideIfLoggedOut = this.props.loggedIn ? '' : ' hide';
var hideIfLoggedIn = this.props.loggedIn ? ' hide' : '';
var dOL = this.props.setPageDisabled;
var myPics;
var all = myPics = '';
switch (this.props.page) {
case 'all' :
Expand Down
6 changes: 5 additions & 1 deletion apps/build-a-pinterest-clone/client/pic.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
var React = require('react');
var appUrl = window.location.origin;

/**
* TODO: I assume at some point we'll bump the React version.
* When we do, we'll need to update this.
*/
// eslint-disable-next-line react/no-deprecated
module.exports = React.createClass({
render: function() {
var likeClass = this.props.liked ? "liked btn btn-default btn-sm" : 'like btn btn-default btn-sm';
Expand Down
4 changes: 2 additions & 2 deletions apps/chart-the-stock-market/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ io.on('connection', function(socket) {
emitStockData();

socket.on('newStock', function(stock) {
if(!currentStockData.hasOwnProperty(stock.symbol) && Object.keys(currentStockData).length < 4) {
if(!(stock.symbol in currentStockData) && Object.keys(currentStockData).length < 4) {
const requestUrl = `https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=${stock.symbol}&apikey=${process.env.APIKEY}`;

https.get(requestUrl, res => {
Expand Down Expand Up @@ -60,7 +60,7 @@ io.on('connection', function(socket) {
socket.on('deleteStock', function(stock) {
console.log('delete' + stock);
console.log(stock.symbol);
if(currentStockData.hasOwnProperty(stock.symbol)) {
if(stock.symbol in currentStockData) {
delete currentStockData[stock.symbol];

emitStockData();
Expand Down
1 change: 1 addition & 0 deletions apps/chart-the-stock-market/public/stocks.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global io, Chart */
var stockData = {};

/////////// socket connection ////////////
Expand Down
2 changes: 1 addition & 1 deletion apps/exercise-tracker/routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const Exercises = require('../models/exercises');

const router = require('express').Router();

router.get('/users', (req, res, next) => {
router.get('/users', (req, res) => {
Users.find({}, (err, data) => {
res.json(data)
})
Expand Down
2 changes: 1 addition & 1 deletion apps/exercise-tracker/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ app.use((req, res, next) => {
})

// Error Handling middleware
app.use((err, req, res, next) => {
app.use((err, req, res) => {
let errCode, errMessage

if (err.errors) {
Expand Down
2 changes: 1 addition & 1 deletion apps/file-metadata-microservice/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ app.post('/api/fileanalyse',upload.single('upfile'), function(req, res){


// 404-NOT FOUND Middleware
app.use(function(req, res, next){
app.use(function(req, res){
res.status(404);
res.type('txt').send('Not found');
});
Expand Down
4 changes: 2 additions & 2 deletions apps/forum-proxy/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ app.get('/latest', (req, res, next) => {

app.use((req, res) => res.status(404).send('not found'));

app.use((err, req, res, next) => res.status(500).json(err));
app.use((err, req, res) => res.status(500).json(err));

const portNum = process.env.PORT || 3000;

const listener = app.listen(portNum, function() {
app.listen(portNum, function() {
console.log(`Your app is listening on port ${portNum}`);
});

Expand Down
2 changes: 1 addition & 1 deletion apps/image-search-abstraction-layer/public/imageSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function enableInputs() {
page.removeAttribute('disabled');
size.removeAttribute('disabled');
updateURL();
} else if(selected = "recent") {
} else if(selected == "recent") {
query.setAttribute('disabled', "true");
page.setAttribute('disabled', "true");
size.setAttribute('disabled', "true");
Expand Down
2 changes: 1 addition & 1 deletion apps/issue-tracker/models/issue.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const IssueSchema = new mongoose.Schema({
// Hide project and __v fields from JSON
// Ref: https://stackoverflow.com/a/17063594/1420506
IssueSchema.set('toJSON', {
transform: function(doc, ret, options) {
transform: function(doc, ret) {
delete ret.project;
delete ret.__v;
return ret;
Expand Down
11 changes: 4 additions & 7 deletions apps/issue-tracker/routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@

'use strict';

const expect = require('chai').expect;
const MongoClient = require('mongodb');
const ObjectId = require('mongodb').ObjectID;
const IssueModel = require('../models/issue').IssueModel;

const mongoose = require('mongoose');
const connection = mongoose.connect(process.env.DB, {
mongoose.connect(process.env.DB, {
useNewUrlParser: true,
useUnifiedTopology: true
});
Expand Down Expand Up @@ -46,7 +43,7 @@ module.exports = function (app) {
// Build Query
let query = { project: project }; // We always filter by project
field_list.forEach(field => {
if(req.query.hasOwnProperty(field)) {
if(field in req.query) {
query[field] = req.query[field];
}
})
Expand All @@ -61,7 +58,7 @@ module.exports = function (app) {

// check required fields
let missing_fields = ['issue_title', 'issue_text', 'created_by']
.filter( field => !req.body.hasOwnProperty(field))
.filter( field => !(field in req.body))
.join(',');

if(missing_fields) {
Expand Down Expand Up @@ -102,7 +99,7 @@ module.exports = function (app) {
let update = {};
let count = 0;
updatable_fields.forEach(field => {
if(req.body.hasOwnProperty(field)) {
if(field in req.body) {
update[field] = req.body[field];
count++;
}
Expand Down
3 changes: 1 addition & 2 deletions apps/issue-tracker/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const expect = require('chai').expect;
const cors = require('cors');

const apiRoutes = require('./routes/api.js');
Expand Down Expand Up @@ -40,7 +39,7 @@ fccTestingRoutes(app);
apiRoutes(app);

//404 Not Found Middleware
app.use(function(req, res, next) {
app.use(function(req, res) {
res.status(404)
.type('text')
.send('Not Found');
Expand Down
8 changes: 0 additions & 8 deletions apps/manage-a-book-trading-club/.eslintrc.json

This file was deleted.

Loading

0 comments on commit 6acdac9

Please sign in to comment.