Skip to content

Commit

Permalink
WIP: enable eslint (#288)
Browse files Browse the repository at this point in the history
* enable eslint

* fix more eslint errors

* fix more eslint errors

* fix more eslint errors

* fix all eslint errors

* run lint and prettier in ci

* drop Node 14 support
  • Loading branch information
eugef authored Jan 16, 2024
1 parent fbd60b9 commit 8a2570b
Show file tree
Hide file tree
Showing 30 changed files with 4,417 additions and 5,094 deletions.
140 changes: 50 additions & 90 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,90 +1,50 @@
env:
node: true
es2017: true

parserOptions:
ecmaVersion: 2019

rules:
comma-dangle: 2
no-alert: 2
no-array-constructor: 2
no-caller: 2
no-catch-shadow: 2
no-control-regex: 2
no-debugger: 2
no-div-regex: 2
no-dupe-keys: 2
no-else-return: 2
no-empty: 2
no-empty-character-class: 2
no-eq-null: 2
no-eval: 2
no-ex-assign: 2
no-func-assign: 0
no-floating-decimal: 2
no-implied-eval: 2
no-with: 2
no-fallthrough: 2
no-unreachable: 2
no-undef: 2
no-undef-init: 2
no-unused-expressions: 2
no-octal: 2
no-octal-escape: 2
no-obj-calls: 2
no-multi-str: 2
no-new-wrappers: 2
no-new: 2
no-new-func: 2
no-native-reassign: 2
no-delete-var: 2
no-return-assign: 2
no-new-object: 2
no-label-var: 2
no-self-compare: 2
no-sync: 2
no-loop-func: 2
no-labels: 2
no-unused-vars:
- 1
- argsIgnorePattern: ^_
varsIgnorePattern: ^_
no-script-url: 2
no-proto: 2
no-iterator: 2
no-mixed-requires:
- 0
- false
no-extra-parens: 2
no-shadow: 2
no-use-before-define: 2
no-redeclare: 2
no-regex-spaces: 2
no-mixed-spaces-and-tabs: 2
no-underscore-dangle: 0

brace-style: 2
camelcase: 2
consistent-this:
- 2
- self
curly: 2
dot-notation: 2
eqeqeq: 2
new-cap: 2
new-parens: 2
quotes:
- 2
- single
semi: 2
strict:
- 2
- global
use-isnan: 2
valid-typeof: 2
wrap-iife: 2
indent:
- 1
- 4
- SwitchCase: 1
{
"extends": [
"airbnb-base",
"prettier"
],
"parser": "@typescript-eslint/parser",
"rules": {
"no-underscore-dangle": "off",
"no-console": "off",
"no-plusplus": "off",
"func-names": [
"warn",
"as-needed"
],
"prefer-destructuring": [
"error",
{
"object": true,
"array": false
}
],
"no-prototype-builtins": "warn",
"no-restricted-syntax": [
"error",
{
"selector": "LabeledStatement",
"message": "Labels are a form of GOTO; using them makes code confusing and hard to maintain and understand."
},
{
"selector": "WithStatement",
"message": "`with` is disallowed in strict mode because it makes code impossible to predict and optimize."
}
]
},
"overrides": [
{
"files": [
"test/**/*.js",
"test/**/*.ts"
],
"rules": {
"no-unused-expressions": "off"
},
"env": {
"mocha": true
}
}
],
"root": true
}
3 changes: 2 additions & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

strategy:
matrix:
node-version: [14.x, 16.x, 18.x, 20.x]
node-version: [16.x, 18.x, 20.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
Expand All @@ -28,4 +28,5 @@ jobs:
cache: 'npm'
- run: npm ci
- run: npm run build --if-present
- run: npm run check
- run: npm test
3 changes: 2 additions & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm run format
npm run check

18 changes: 8 additions & 10 deletions examples/express-route.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
'use strict';

var httpMocks = require('../lib/http-mock');
const httpMocks = require('../lib/http-mock');

// Suppose you have the following Express route:

// app.get('/user/:id', routeHandler);

// And you have created a function to handle that route's call:

var routeHandler = function (request, response) {
var id = request.params.id;
const routeHandler = function (request, response) {
const { id } = request.params;

console.log("We have a '%s' request for %s (ID: %d)", request.method, request.url, id);

var body = {
const body = {
name: 'Bob Dog',
age: 42,
email: '[email protected]'
Expand All @@ -29,20 +27,20 @@ var routeHandler = function (request, response) {
// In another file, you can easily test the routeHandler function
// with some code like this using the testing framework of your choice:

exports['routeHandler - Simple testing'] = function (test) {
var request = httpMocks.createRequest({
exports['routeHandler - Simple testing'] = function testing(test) {
const request = httpMocks.createRequest({
method: 'GET',
url: '/user/42',
params: {
id: 42
}
});

var response = httpMocks.createResponse();
const response = httpMocks.createResponse();

routeHandler(request, response);

var data = response._getJSONData();
const data = response._getJSONData();

test.equal('Bob Dog', data.name);
test.equal(42, data.age);
Expand Down
16 changes: 7 additions & 9 deletions examples/express-status-vs-json.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
'use strict';

var httpMocks = require('../lib/http-mock');
const httpMocks = require('../lib/http-mock');

// Suppose you have the following Express route:

// app.post('/users', routeHandler);

// And you have created a function to handle that route's call:

var routeHandler = function (request, response) {
const routeHandler = function (request, response) {
console.log("We have a '%s' request for %s", request.method, request.url);

var body = {
const body = {
name: 'Bob Dog',
age: 42,
email: '[email protected]'
Expand All @@ -25,17 +23,17 @@ var routeHandler = function (request, response) {
// In another file, you can easily test the routeHandler function
// with some code like this using the testing framework of your choice:

exports['routeHandler - Simple testing of status() vs json()'] = function (test) {
var request = httpMocks.createRequest({
exports['routeHandler - Simple testing of status() vs json()'] = function testing(test) {
const request = httpMocks.createRequest({
method: 'POST',
url: '/users'
});

var response = httpMocks.createResponse();
const response = httpMocks.createResponse();

routeHandler(request, response);

var data = response._getJSONData();
const data = response._getJSONData();

test.equal('Bob Dog', data.name);
test.equal(42, data.age);
Expand Down
51 changes: 12 additions & 39 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,25 @@
'use strict';
const gulp = require('gulp');
const mocha = require('gulp-mocha');
const istanbul = require('gulp-istanbul');

var gulp = require('gulp');
var mocha = require('gulp-mocha');
var istanbul = require('gulp-istanbul');
var eslint = require('gulp-eslint');

var files = {
const files = {
src: ['./lib/**/*.js'],
test: ['./test/**/*.spec.js', './*.js'],
test: ['./test/**/*.spec.js', './test/**/*.spec.ts'],
testTs: ['./test/**/*.spec.ts']
};
gulp.task('dot', () =>
gulp.src(files.test, { read: false }).pipe(mocha({ reporter: 'spec', require: 'ts-node/register' }))
);

gulp.task('lint', function () {
return gulp
.src(files.src.concat(files.test))
.pipe(
eslint({
// configFile: './.eslintrc',
useEslintrc: true
})
)
.pipe(eslint.format())
.pipe(eslint.failOnError());
});

gulp.task('dot', function () {
return gulp.src(files.test, { read: false }).pipe(mocha({ reporter: 'dot' }));
});

gulp.task('test', gulp.series('dot' /*, 'lint'*/));
gulp.task('test', gulp.series('dot'));

gulp.task('test:ts', function () {
return gulp.src(files.testTs, { read: false }).pipe(mocha({ reporter: 'dot', require: 'ts-node/register' }));
});

gulp.task('spec', function () {
return gulp.src(files.test, { read: false }).pipe(mocha({ reporter: 'spec' }));
});

gulp.task('spec:ts', function () {
return gulp.src(files.testTs, { read: false }).pipe(mocha({ reporter: 'spec' }));
});
gulp.task('spec', () => gulp.src(files.test, { read: false }).pipe(mocha({ reporter: 'spec' })));

gulp.task('coverage', function (done) {
gulp.task('coverage', (done) => {
gulp.src(files.src)
.pipe(istanbul())
.pipe(istanbul.hookRequire())
.on('finish', function () {
.on('finish', () => {
gulp.src(files.test)
.pipe(mocha({ reporter: 'dot' }))
.pipe(
Expand Down
Loading

0 comments on commit 8a2570b

Please sign in to comment.