From 5c31dc285a6d4768f0b3d6f37836bcd582212352 Mon Sep 17 00:00:00 2001 From: Sebastian Seedorf Date: Thu, 19 Nov 2020 22:22:14 +0100 Subject: [PATCH] eslint - rules updated --- public/js-source/.eslintrc.js | 58 +++++++++++++++++------------ public/js-source/src/SomeModule.ts | 2 +- public/js-source/src/index.ts | 4 +- public/js-source/src/utils/utils.ts | 2 +- public/js-source/test/SomeModule.ts | 18 ++++----- public/js-source/test/index.ts | 14 +++---- public/js-source/test/utils.ts | 9 +++-- src/.eslintrc.js | 52 ++++++++++++++------------ src/app.ts | 12 +++--- src/healthcheck.ts | 2 +- src/index.ts | 2 +- src/routes/api/user.ts | 2 +- src/routes/index.ts | 6 +-- 13 files changed, 100 insertions(+), 83 deletions(-) diff --git a/public/js-source/.eslintrc.js b/public/js-source/.eslintrc.js index e19549d..3144434 100644 --- a/public/js-source/.eslintrc.js +++ b/public/js-source/.eslintrc.js @@ -5,38 +5,48 @@ module.exports = { plugins: [ '@typescript-eslint', 'no-null', + 'promise', ], extends: [ 'eslint:recommended', - "plugin:@typescript-eslint/eslint-recommended", - "plugin:@typescript-eslint/recommended", + 'plugin:@typescript-eslint/eslint-recommended', + 'plugin:@typescript-eslint/recommended', + 'plugin:promise/recommended', ], rules: { - "no-console": "error", - "max-len": ["error", {"code": 128}], - "no-process-env": "error", - "no-process-exit": "error", - "no-null/no-null": "error", - "no-useless-return": "error", - "prefer-arrow-callback": "warn", - "object-curly-spacing": "error", - "consistent-return": "error", - "@typescript-eslint/explicit-function-return-type": [ - "error", { - "allowExpressions": true, + 'max-len': ['error', {'code': 128}], + 'no-process-env': 'error', + 'no-process-exit': 'error', + 'no-null/no-null': 'error', + 'no-useless-return': 'error', + 'prefer-arrow-callback': 'warn', + 'consistent-return': 'error', + '@typescript-eslint/explicit-function-return-type': [ + 'error', { + 'allowExpressions': true, }, ], - "no-void": "error", - "comma-spacing": "error", - "comma-dangle": ["error", "always-multiline"], - "comma-style": "error", - "semi": "error", - "no-implicit-coercion": "error", + 'no-void': 'error', + 'comma-spacing': 'error', + 'comma-dangle': ['error', 'always-multiline'], + 'comma-style': 'error', + 'semi': 'error', + 'no-implicit-coercion': 'error', + 'quotes': ['error', 'single', 'avoid-escape'], + 'keyword-spacing': 'error', + 'semi-spacing': 'error', + 'arrow-spacing': 'error', + 'object-curly-spacing': 'error', + 'array-bracket-spacing': 'error', + 'key-spacing': 'error', + 'block-spacing': 'error', + 'promise/always-return': 'off', - "no-restricted-imports": ["error", - "assert", "buffer", "child_process", "cluster", "crypto", "dgram", "dns", "domain", "events", "freelist", - "fs", "http", "https", "module", "net", "os", "path", "punycode", "querystring", "readline", "repl", - "smalloc", "stream", "string_decoder", "sys", "timers", "tls", "tracing", "tty", "url", "util", "vm", "zlib", + 'no-restricted-imports': ['error', + 'assert', 'buffer', 'child_process', 'cluster', 'crypto', 'dgram', 'dns', 'domain', 'events', 'freelist', + 'fs', 'http', 'https', 'module', 'net', 'os', 'path', 'punycode', 'querystring', 'readline', 'repl', + 'smalloc', 'stream', 'string_decoder', 'sys', 'timers', 'tls', 'tracing', 'tty', 'url', 'util', 'vm', 'zlib', ], + 'no-console': ['error', {allow: ['warn', 'error']}], }, }; diff --git a/public/js-source/src/SomeModule.ts b/public/js-source/src/SomeModule.ts index 48fdee2..0b371f4 100644 --- a/public/js-source/src/SomeModule.ts +++ b/public/js-source/src/SomeModule.ts @@ -2,6 +2,6 @@ import {getUserInfo} from './utils/utils'; export async function getUserName(): Promise { const info = await getUserInfo(); - return info?.name ?? "No name found!"; + return info?.name ?? 'No name found!'; } diff --git a/public/js-source/src/index.ts b/public/js-source/src/index.ts index 79bd6c9..b962a92 100644 --- a/public/js-source/src/index.ts +++ b/public/js-source/src/index.ts @@ -6,7 +6,7 @@ import {getUserName} from './SomeModule'; function updateUserName(name: string): void { const node = document.createElement('span'); node.innerText = `This user name is fetched with Javascript: ${name}`; - document.getElementsByTagName("body")[0].appendChild(node); + document.getElementsByTagName('body')[0].appendChild(node); } -getUserName().then(updateUserName); +getUserName().then(updateUserName).catch((err) => console.error(err)); diff --git a/public/js-source/src/utils/utils.ts b/public/js-source/src/utils/utils.ts index 28ac479..90b6955 100644 --- a/public/js-source/src/utils/utils.ts +++ b/public/js-source/src/utils/utils.ts @@ -22,7 +22,7 @@ export function resetConfig(): void { export async function getUserInfo(): Promise|undefined> { const config = await getConfig(); - const res = await fetch(config.EXTERNAL_BASE_URL + "/api/user"); + const res = await fetch(config.EXTERNAL_BASE_URL + '/api/user'); return res.json(); } diff --git a/public/js-source/test/SomeModule.ts b/public/js-source/test/SomeModule.ts index 39ca108..8f040a6 100644 --- a/public/js-source/test/SomeModule.ts +++ b/public/js-source/test/SomeModule.ts @@ -7,7 +7,7 @@ import {getUserName} from '../src/SomeModule'; import {setConfig, UserInfo} from '../src/utils/utils'; describe('frontend:SomeModule', () => { - const CONFIG = {EXTERNAL_BASE_URL: "http://demo.url"}; + const CONFIG = {EXTERNAL_BASE_URL: 'http://demo.url'}; before(() => { setConfig(CONFIG); fetchMock.config.overwriteRoutes = true; @@ -15,14 +15,14 @@ describe('frontend:SomeModule', () => { it('should return username', async () => { const tests: [Partial, string][] = [ [ - {name: "John Doe"}, - "John Doe", + {name: 'John Doe'}, + 'John Doe', ], [ - {name: "John Doe", email: "some.mail@example.com"}, - "John Doe", + {name: 'John Doe', email: 'some.mail@example.com'}, + 'John Doe', ], [ - {name: "", email: "some.mail@example.com"}, - "", + {name: '', email: 'some.mail@example.com'}, + '', ], ]; for (const [USER_INFO, RESULT] of tests) { @@ -35,13 +35,13 @@ describe('frontend:SomeModule', () => { } }); it('should return default string', async () => { - const RESULT = "No name found!"; + const RESULT = 'No name found!'; const tests: (Partial|unknown)[] = [ // eslint-disable-next-line no-null/no-null {name: null}, // eslint-disable-next-line no-null/no-null null, - {email: "some.mail@example.com"}, + {email: 'some.mail@example.com'}, {name: undefined}, {}, ]; diff --git a/public/js-source/test/index.ts b/public/js-source/test/index.ts index b0628ab..36107f6 100644 --- a/public/js-source/test/index.ts +++ b/public/js-source/test/index.ts @@ -7,13 +7,13 @@ import {JSDOM} from 'jsdom'; // @ts-ignore import * as rewire from 'rewire'; -describe("frontend:index", () => { - const updateUserName = rewire("../src/index").__get__("updateUserName") as (name: string) => void; +describe('frontend:index', () => { + const updateUserName = rewire('../src/index').__get__('updateUserName') as (name: string) => void; beforeEach(() => { const dom = new JSDOM( ` - + @@ -27,13 +27,13 @@ describe("frontend:index", () => { global.document = dom.window.document; }); - it("updateUserName", (done) => { - const NAME = "Patrick Star"; - const RESULT = "This user name is fetched with Javascript: Patrick Star"; + it('updateUserName', (done) => { + const NAME = 'Patrick Star'; + const RESULT = 'This user name is fetched with Javascript: Patrick Star'; updateUserName(NAME); // give the browser a chance to update the DOM setTimeout(() => { - const span = document.getElementsByTagName("span"); + const span = document.getElementsByTagName('span'); expect(span).to.not.be.null; expect(span.length).to.be.greaterThan(0); expect(span[0].innerText).to.deep.equal(RESULT); diff --git a/public/js-source/test/utils.ts b/public/js-source/test/utils.ts index cc522c2..4f8796e 100644 --- a/public/js-source/test/utils.ts +++ b/public/js-source/test/utils.ts @@ -1,3 +1,4 @@ +/* eslint-disable promise/catch-or-return,promise/no-callback-in-promise */ import {setConfig, getConfig, resetConfig, getUserInfo, UserInfo} from '../src/utils/utils'; import {Resolvable, WaitForSync} from '../src/utils/resolvable'; import {describe, it} from 'mocha'; @@ -7,7 +8,7 @@ import {expect} from 'chai'; import * as fetchMock from 'fetch-mock'; describe('frontend:utils - setConfig/getConfig', () => { - const CONFIG = {EXTERNAL_BASE_URL: "http://demo.url"}; + const CONFIG = {EXTERNAL_BASE_URL: 'http://demo.url'}; afterEach(() => resetConfig()); it('should return config (afterwards)', async () => { @@ -25,9 +26,9 @@ describe('frontend:utils - setConfig/getConfig', () => { }); describe('frontend:utils - getUserInfo', () => { - const CONFIG = {EXTERNAL_BASE_URL: "http://demo.url"}; + const CONFIG = {EXTERNAL_BASE_URL: 'http://demo.url'}; const USER_INFO: Partial = { - name: "John Doe", + name: 'John Doe', }; beforeEach(() => setConfig(CONFIG)); @@ -46,7 +47,7 @@ describe('frontend:utils - getUserInfo', () => { describe('frontend:utils - resolvable', () => { const DATA = 5; - const ERROR = new Error("Custom error!"); + const ERROR = new Error('Custom error!'); it('waitForSync should return data (afterwards)', async () => { const resolvable = new WaitForSync(); diff --git a/src/.eslintrc.js b/src/.eslintrc.js index d9f4df8..d262676 100644 --- a/src/.eslintrc.js +++ b/src/.eslintrc.js @@ -9,32 +9,38 @@ module.exports = { ], extends: [ 'eslint:recommended', - "plugin:@typescript-eslint/eslint-recommended", - "plugin:@typescript-eslint/recommended", - "plugin:promise/recommended", + 'plugin:@typescript-eslint/eslint-recommended', + 'plugin:@typescript-eslint/recommended', + 'plugin:promise/recommended', ], rules: { - "no-console": "error", - "max-len": ["error", {"code": 128}], - "no-process-env": "error", - "no-process-exit": "error", - "no-null/no-null": "error", - "no-useless-return": "error", - "prefer-arrow-callback": "warn", - "object-curly-spacing": "error", - "consistent-return": "error", - "@typescript-eslint/explicit-function-return-type": [ - "error", { - "allowExpressions": true, + 'no-console': 'error', + 'max-len': ['error', {'code': 128}], + 'no-process-env': 'error', + 'no-process-exit': 'error', + 'no-null/no-null': 'error', + 'no-useless-return': 'error', + 'prefer-arrow-callback': 'warn', + 'consistent-return': 'error', + '@typescript-eslint/explicit-function-return-type': [ + 'error', { + 'allowExpressions': true, }, ], - "no-void": "error", - "comma-spacing": "error", - "comma-dangle": ["error", "always-multiline"], - "comma-style": "error", - "semi": "error", - "no-implicit-coercion": "error", - - "promise/always-return": "off", + 'no-void': 'error', + 'comma-spacing': 'error', + 'comma-dangle': ['error', 'always-multiline'], + 'comma-style': 'error', + 'semi': 'error', + 'no-implicit-coercion': 'error', + 'quotes': ['error', 'single', 'avoid-escape'], + 'keyword-spacing': 'error', + 'semi-spacing': 'error', + 'arrow-spacing': 'error', + 'object-curly-spacing': 'error', + 'array-bracket-spacing': 'error', + 'key-spacing': 'error', + 'block-spacing': 'error', + 'promise/always-return': 'off', }, }; diff --git a/src/app.ts b/src/app.ts index d4cc446..83bc06e 100644 --- a/src/app.ts +++ b/src/app.ts @@ -11,11 +11,11 @@ export const app = express(); // Permissions PermManager - .grant("user") - .execute("read").on("userinfo") - .grant("coe_bs") - .extend("user") - .execute("write").on("userinfo"); + .grant('user') + .execute('read').on('userinfo') + .grant('coe_bs') + .extend('user') + .execute('write').on('userinfo'); // view engine setup app.set('views', path.join(__dirname, '../views')); @@ -44,7 +44,7 @@ router.use(AutoReloader.router); //router.use(Session.getRouter()); // static config -router.use("/js/polyfill.js", Polyfill.getRouter('./public/js/bundle.js')); +router.use('/js/polyfill.js', Polyfill.getRouter('./public/js/bundle.js')); router.use(sassMiddleware({ src: path.join(__dirname, '../public'), dest: path.join(__dirname, '../public'), diff --git a/src/healthcheck.ts b/src/healthcheck.ts index f7006d1..e102810 100644 --- a/src/healthcheck.ts +++ b/src/healthcheck.ts @@ -1,5 +1,5 @@ /* eslint-disable no-process-exit,no-console */ -import * as http from "http"; +import * as http from 'http'; import {DefaultConfig, urlJoin} from 'pkg-express-utils'; const options = { diff --git a/src/index.ts b/src/index.ts index 760c133..b0726c2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,9 +9,9 @@ import {DefaultConfig, Logger} from 'pkg-express-utils'; app.set('port', DefaultConfig.PORT); const server = http.createServer(app); -app.listen(DefaultConfig.PORT); server.on('error', onError); server.on('listening', onListening); +server.listen(DefaultConfig.PORT); function onError(error: HttpError): void { if (error.syscall !== 'listen') { diff --git a/src/routes/api/user.ts b/src/routes/api/user.ts index 156f330..7e15f67 100644 --- a/src/routes/api/user.ts +++ b/src/routes/api/user.ts @@ -4,7 +4,7 @@ import {PermManager} from 'pkg-express-utils'; const userRouter = express.Router(); /* GET users listing. */ -userRouter.get('/', PermManager.getRouter("userinfo", {action: "read"}), async (req, res) => { +userRouter.get('/', PermManager.getRouter('userinfo', {action: 'read'}), async (req, res) => { res.json(await req.getUserInfo() || {}); }); diff --git a/src/routes/index.ts b/src/routes/index.ts index eec512c..baec620 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -5,12 +5,12 @@ import healthRouter from './healthcheck'; const router = express.Router(); export default router; -router.use("/api/user", userRouter); -router.use("/health", healthRouter); +router.use('/api/user', userRouter); +router.use('/health', healthRouter); /* GET home page. */ router.get('/', async (req, res) => { - const email = (await req.getUserInfo())?.email ?? "No email found!"; + const email = (await req.getUserInfo())?.email ?? 'No email found!'; res.render('index', {title: 'Express', email}); });