eslint - rules updated

This commit is contained in:
Sebastian Seedorf
2020-11-19 22:22:14 +01:00
parent b79c2f96cd
commit 5c31dc285a
13 changed files with 100 additions and 83 deletions

View File

@@ -5,38 +5,48 @@ module.exports = {
plugins: [ plugins: [
'@typescript-eslint', '@typescript-eslint',
'no-null', 'no-null',
'promise',
], ],
extends: [ extends: [
'eslint:recommended', 'eslint:recommended',
"plugin:@typescript-eslint/eslint-recommended", 'plugin:@typescript-eslint/eslint-recommended',
"plugin:@typescript-eslint/recommended", 'plugin:@typescript-eslint/recommended',
'plugin:promise/recommended',
], ],
rules: { rules: {
"no-console": "error", 'max-len': ['error', {'code': 128}],
"max-len": ["error", {"code": 128}], 'no-process-env': 'error',
"no-process-env": "error", 'no-process-exit': 'error',
"no-process-exit": "error", 'no-null/no-null': 'error',
"no-null/no-null": "error", 'no-useless-return': 'error',
"no-useless-return": "error", 'prefer-arrow-callback': 'warn',
"prefer-arrow-callback": "warn", 'consistent-return': 'error',
"object-curly-spacing": "error", '@typescript-eslint/explicit-function-return-type': [
"consistent-return": "error", 'error', {
"@typescript-eslint/explicit-function-return-type": [ 'allowExpressions': true,
"error", {
"allowExpressions": true,
}, },
], ],
"no-void": "error", 'no-void': 'error',
"comma-spacing": "error", 'comma-spacing': 'error',
"comma-dangle": ["error", "always-multiline"], 'comma-dangle': ['error', 'always-multiline'],
"comma-style": "error", 'comma-style': 'error',
"semi": "error", 'semi': 'error',
"no-implicit-coercion": "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", 'no-restricted-imports': ['error',
"assert", "buffer", "child_process", "cluster", "crypto", "dgram", "dns", "domain", "events", "freelist", 'assert', 'buffer', 'child_process', 'cluster', 'crypto', 'dgram', 'dns', 'domain', 'events', 'freelist',
"fs", "http", "https", "module", "net", "os", "path", "punycode", "querystring", "readline", "repl", 'fs', 'http', 'https', 'module', 'net', 'os', 'path', 'punycode', 'querystring', 'readline', 'repl',
"smalloc", "stream", "string_decoder", "sys", "timers", "tls", "tracing", "tty", "url", "util", "vm", "zlib", 'smalloc', 'stream', 'string_decoder', 'sys', 'timers', 'tls', 'tracing', 'tty', 'url', 'util', 'vm', 'zlib',
], ],
'no-console': ['error', {allow: ['warn', 'error']}],
}, },
}; };

View File

@@ -2,6 +2,6 @@ import {getUserInfo} from './utils/utils';
export async function getUserName(): Promise<string> { export async function getUserName(): Promise<string> {
const info = await getUserInfo(); const info = await getUserInfo();
return info?.name ?? "No name found!"; return info?.name ?? 'No name found!';
} }

View File

@@ -6,7 +6,7 @@ import {getUserName} from './SomeModule';
function updateUserName(name: string): void { function updateUserName(name: string): void {
const node = document.createElement('span'); const node = document.createElement('span');
node.innerText = `This user name is fetched with Javascript: ${name}`; 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));

View File

@@ -22,7 +22,7 @@ export function resetConfig(): void {
export async function getUserInfo(): Promise<Partial<UserInfo>|undefined> { export async function getUserInfo(): Promise<Partial<UserInfo>|undefined> {
const config = await getConfig(); 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(); return res.json();
} }

View File

@@ -7,7 +7,7 @@ import {getUserName} from '../src/SomeModule';
import {setConfig, UserInfo} from '../src/utils/utils'; import {setConfig, UserInfo} from '../src/utils/utils';
describe('frontend:SomeModule', () => { describe('frontend:SomeModule', () => {
const CONFIG = {EXTERNAL_BASE_URL: "http://demo.url"}; const CONFIG = {EXTERNAL_BASE_URL: 'http://demo.url'};
before(() => { before(() => {
setConfig(CONFIG); setConfig(CONFIG);
fetchMock.config.overwriteRoutes = true; fetchMock.config.overwriteRoutes = true;
@@ -15,14 +15,14 @@ describe('frontend:SomeModule', () => {
it('should return username', async () => { it('should return username', async () => {
const tests: [Partial<UserInfo>, string][] = [ const tests: [Partial<UserInfo>, string][] = [
[ [
{name: "John Doe"}, {name: 'John Doe'},
"John Doe", 'John Doe',
], [ ], [
{name: "John Doe", email: "some.mail@example.com"}, {name: 'John Doe', email: 'some.mail@example.com'},
"John Doe", 'John Doe',
], [ ], [
{name: "", email: "some.mail@example.com"}, {name: '', email: 'some.mail@example.com'},
"", '',
], ],
]; ];
for (const [USER_INFO, RESULT] of tests) { for (const [USER_INFO, RESULT] of tests) {
@@ -35,13 +35,13 @@ describe('frontend:SomeModule', () => {
} }
}); });
it('should return default string', async () => { it('should return default string', async () => {
const RESULT = "No name found!"; const RESULT = 'No name found!';
const tests: (Partial<UserInfo>|unknown)[] = [ const tests: (Partial<UserInfo>|unknown)[] = [
// eslint-disable-next-line no-null/no-null // eslint-disable-next-line no-null/no-null
{name: null}, {name: null},
// eslint-disable-next-line no-null/no-null // eslint-disable-next-line no-null/no-null
null, null,
{email: "some.mail@example.com"}, {email: 'some.mail@example.com'},
{name: undefined}, {name: undefined},
{}, {},
]; ];

View File

@@ -7,13 +7,13 @@ import {JSDOM} from 'jsdom';
// @ts-ignore // @ts-ignore
import * as rewire from 'rewire'; import * as rewire from 'rewire';
describe("frontend:index", () => { describe('frontend:index', () => {
const updateUserName = rewire("../src/index").__get__("updateUserName") as (name: string) => void; const updateUserName = rewire('../src/index').__get__('updateUserName') as (name: string) => void;
beforeEach(() => { beforeEach(() => {
const dom = new JSDOM( const dom = new JSDOM(
` `
<html> <html lang="en">
<body> <body>
</body> </body>
</html> </html>
@@ -27,13 +27,13 @@ describe("frontend:index", () => {
global.document = dom.window.document; global.document = dom.window.document;
}); });
it("updateUserName", (done) => { it('updateUserName', (done) => {
const NAME = "Patrick Star"; const NAME = 'Patrick Star';
const RESULT = "This user name is fetched with Javascript: Patrick Star"; const RESULT = 'This user name is fetched with Javascript: Patrick Star';
updateUserName(NAME); updateUserName(NAME);
// give the browser a chance to update the DOM // give the browser a chance to update the DOM
setTimeout(() => { setTimeout(() => {
const span = document.getElementsByTagName("span"); const span = document.getElementsByTagName('span');
expect(span).to.not.be.null; expect(span).to.not.be.null;
expect(span.length).to.be.greaterThan(0); expect(span.length).to.be.greaterThan(0);
expect(span[0].innerText).to.deep.equal(RESULT); expect(span[0].innerText).to.deep.equal(RESULT);

View File

@@ -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 {setConfig, getConfig, resetConfig, getUserInfo, UserInfo} from '../src/utils/utils';
import {Resolvable, WaitForSync} from '../src/utils/resolvable'; import {Resolvable, WaitForSync} from '../src/utils/resolvable';
import {describe, it} from 'mocha'; import {describe, it} from 'mocha';
@@ -7,7 +8,7 @@ import {expect} from 'chai';
import * as fetchMock from 'fetch-mock'; import * as fetchMock from 'fetch-mock';
describe('frontend:utils - setConfig/getConfig', () => { describe('frontend:utils - setConfig/getConfig', () => {
const CONFIG = {EXTERNAL_BASE_URL: "http://demo.url"}; const CONFIG = {EXTERNAL_BASE_URL: 'http://demo.url'};
afterEach(() => resetConfig()); afterEach(() => resetConfig());
it('should return config (afterwards)', async () => { it('should return config (afterwards)', async () => {
@@ -25,9 +26,9 @@ describe('frontend:utils - setConfig/getConfig', () => {
}); });
describe('frontend:utils - getUserInfo', () => { describe('frontend:utils - getUserInfo', () => {
const CONFIG = {EXTERNAL_BASE_URL: "http://demo.url"}; const CONFIG = {EXTERNAL_BASE_URL: 'http://demo.url'};
const USER_INFO: Partial<UserInfo> = { const USER_INFO: Partial<UserInfo> = {
name: "John Doe", name: 'John Doe',
}; };
beforeEach(() => setConfig(CONFIG)); beforeEach(() => setConfig(CONFIG));
@@ -46,7 +47,7 @@ describe('frontend:utils - getUserInfo', () => {
describe('frontend:utils - resolvable', () => { describe('frontend:utils - resolvable', () => {
const DATA = 5; const DATA = 5;
const ERROR = new Error("Custom error!"); const ERROR = new Error('Custom error!');
it('waitForSync should return data (afterwards)', async () => { it('waitForSync should return data (afterwards)', async () => {
const resolvable = new WaitForSync<number>(); const resolvable = new WaitForSync<number>();

View File

@@ -9,32 +9,38 @@ module.exports = {
], ],
extends: [ extends: [
'eslint:recommended', 'eslint:recommended',
"plugin:@typescript-eslint/eslint-recommended", 'plugin:@typescript-eslint/eslint-recommended',
"plugin:@typescript-eslint/recommended", 'plugin:@typescript-eslint/recommended',
"plugin:promise/recommended", 'plugin:promise/recommended',
], ],
rules: { rules: {
"no-console": "error", 'no-console': 'error',
"max-len": ["error", {"code": 128}], 'max-len': ['error', {'code': 128}],
"no-process-env": "error", 'no-process-env': 'error',
"no-process-exit": "error", 'no-process-exit': 'error',
"no-null/no-null": "error", 'no-null/no-null': 'error',
"no-useless-return": "error", 'no-useless-return': 'error',
"prefer-arrow-callback": "warn", 'prefer-arrow-callback': 'warn',
"object-curly-spacing": "error", 'consistent-return': 'error',
"consistent-return": "error", '@typescript-eslint/explicit-function-return-type': [
"@typescript-eslint/explicit-function-return-type": [ 'error', {
"error", { 'allowExpressions': true,
"allowExpressions": true,
}, },
], ],
"no-void": "error", 'no-void': 'error',
"comma-spacing": "error", 'comma-spacing': 'error',
"comma-dangle": ["error", "always-multiline"], 'comma-dangle': ['error', 'always-multiline'],
"comma-style": "error", 'comma-style': 'error',
"semi": "error", 'semi': 'error',
"no-implicit-coercion": "error", 'no-implicit-coercion': 'error',
'quotes': ['error', 'single', 'avoid-escape'],
"promise/always-return": "off", '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',
}, },
}; };

View File

@@ -11,11 +11,11 @@ export const app = express();
// Permissions // Permissions
PermManager PermManager
.grant("user") .grant('user')
.execute("read").on("userinfo") .execute('read').on('userinfo')
.grant("coe_bs") .grant('coe_bs')
.extend("user") .extend('user')
.execute("write").on("userinfo"); .execute('write').on('userinfo');
// view engine setup // view engine setup
app.set('views', path.join(__dirname, '../views')); app.set('views', path.join(__dirname, '../views'));
@@ -44,7 +44,7 @@ router.use(AutoReloader.router);
//router.use(Session.getRouter()); //router.use(Session.getRouter());
// static config // 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({ router.use(sassMiddleware({
src: path.join(__dirname, '../public'), src: path.join(__dirname, '../public'),
dest: path.join(__dirname, '../public'), dest: path.join(__dirname, '../public'),

View File

@@ -1,5 +1,5 @@
/* eslint-disable no-process-exit,no-console */ /* eslint-disable no-process-exit,no-console */
import * as http from "http"; import * as http from 'http';
import {DefaultConfig, urlJoin} from 'pkg-express-utils'; import {DefaultConfig, urlJoin} from 'pkg-express-utils';
const options = { const options = {

View File

@@ -9,9 +9,9 @@ import {DefaultConfig, Logger} from 'pkg-express-utils';
app.set('port', DefaultConfig.PORT); app.set('port', DefaultConfig.PORT);
const server = http.createServer(app); const server = http.createServer(app);
app.listen(DefaultConfig.PORT);
server.on('error', onError); server.on('error', onError);
server.on('listening', onListening); server.on('listening', onListening);
server.listen(DefaultConfig.PORT);
function onError(error: HttpError): void { function onError(error: HttpError): void {
if (error.syscall !== 'listen') { if (error.syscall !== 'listen') {

View File

@@ -4,7 +4,7 @@ import {PermManager} from 'pkg-express-utils';
const userRouter = express.Router(); const userRouter = express.Router();
/* GET users listing. */ /* 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() || {}); res.json(await req.getUserInfo() || {});
}); });

View File

@@ -5,12 +5,12 @@ import healthRouter from './healthcheck';
const router = express.Router(); const router = express.Router();
export default router; export default router;
router.use("/api/user", userRouter); router.use('/api/user', userRouter);
router.use("/health", healthRouter); router.use('/health', healthRouter);
/* GET home page. */ /* GET home page. */
router.get('/', async (req, res) => { 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}); res.render('index', {title: 'Express', email});
}); });