Прогнал линтером для автоматических доделок, но оставлять его не стал - слишком много ошибок

This commit is contained in:
Nikita Dezzpil Orlov 2021-09-29 18:01:08 +03:00
parent ce3a192cbd
commit c6db8bc2fe
14 changed files with 118 additions and 8324 deletions

View File

@ -1,6 +0,0 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="TsLint" enabled="true" level="WARNING" enabled_by_default="true" />
</profile>
</component>

View File

@ -14,7 +14,7 @@ export default {
bail: 1,
verbose: true,
testSequencer: '<rootDir>/src/tests/conf/sequencer.js',
testTimeout: 60000,
testTimeout: 60_000,
coverageProvider: 'v8',
coverageReporters: ['json', 'lcov', 'text', 'clover', 'teamcity'],

8204
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -8,8 +8,7 @@
],
"scripts": {
"test": "node --no-warnings node_modules/.bin/jest --runInBand --forceExit",
"prepare": "npm run lint && npm test && npm run build",
"lint": "xo --space=4",
"prepare": "npm test && npm run build",
"build": "tsc -b -v"
},
"repository": {
@ -25,11 +24,9 @@
"license": "Apache-2.0",
"devDependencies": {
"@jest/test-sequencer": "^27.2.3",
"@types/bson": "^4.2.0",
"@types/chai": "^4.2.22",
"@types/jest": "^27.0.2",
"@types/node": "^14.17.19",
"@types/rfdc": "^1.2.0",
"chai": "^4.3.4",
"jasmine": "^3.9.0",
"jasmine-fail-fast": "^2.0.1",
@ -40,8 +37,7 @@
"rfdc": "^1.3.0",
"ts-jest": "^27.0.5",
"ts-node": "^10.2.1",
"typescript": "^4.4.3",
"xo": "^0.44.0"
"typescript": "^4.4.3"
},
"dependencies": {
"mongodb": "^4.1.2",

View File

@ -1,33 +1,32 @@
import rfdc from 'rfdc'
interface ValuesObject {
[key: string]: any
}
type ValuesObject = Record<string, any>
export abstract class Data {
[key: string]: any
static assign(vo: Data, values: ValuesObject): Data {
for (let key of Object.getOwnPropertyNames(values)) {
for (const key of Object.getOwnPropertyNames(values)) {
vo[key] = values[key]
}
return vo
}
[key: string]: any
set(key: string, value: any) {
this[key] = value
}
fromObject(obj: ValuesObject): void {
for (let key of Object.getOwnPropertyNames(obj)) {
fromObject(object: ValuesObject): void {
for (const key of Object.getOwnPropertyNames(object)) {
if (key === '_id') continue
this[key] = obj[key]
this[key] = object[key]
}
}
toObject(exclude: string[] = []): object {
const obj = {} as ValuesObject
for (let key of Object.getOwnPropertyNames(this)) {
toObject(exclude: string[] = []): Record<string, any> {
const object = {} as ValuesObject
for (const key of Object.getOwnPropertyNames(this)) {
if (key.startsWith('__')) {
continue
}
@ -36,14 +35,15 @@ export abstract class Data {
const type = typeof this[key]
if (type === 'object') {
obj[key] = rfdc()(this[key])
} else if (['function', 'undefined'].indexOf(type) >= 0) {
object[key] = rfdc()(this[key])
} else if (['function', 'undefined'].includes(type)) {
// ...
} else {
obj[key] = this[key]
object[key] = this[key]
}
}
return obj
return object
}
abstract uniqKey(): string

View File

@ -2,9 +2,7 @@ import { Data } from './data'
import { Repo } from './repo'
import { Storage } from './storage'
export type EntityConstructor<T extends Data> = {
new (data?: T): Entity<T>
}
export type EntityConstructor<T extends Data> = new (data?: T) => Entity<T>
export class ErrEntityHasNoUniqKeyValue extends Error {}
@ -13,7 +11,7 @@ export abstract class Entity<T extends Data> {
abstract _getVO(): T
protected _data: T
private __id: string = '' // storage inner id
private __id = '' // Storage inner id
get data(): T {
return this._data

View File

@ -8,8 +8,8 @@ export abstract class Repo<T extends Entity<any>> {
protected _storage: Storage
protected _entity: T
protected _limit: number = 0
protected _offset: number = 0
protected _limit = 0
protected _offset = 0
/**
* Возвращает объект соотв. сущности, например new App()
@ -21,8 +21,7 @@ export abstract class Repo<T extends Entity<any>> {
*/
abstract Name(): string
// @ts-ignore
_transformer: (obj: any) => any
_transformer: (object: any) => any
constructor(storage: Storage) {
this._storage = storage
@ -31,10 +30,10 @@ export abstract class Repo<T extends Entity<any>> {
}
resetTransformer() {
this._transformer = function (obj: any): any {
this._transformer = function (object: any): any {
const entity = this.Entity()
entity.data.fromObject(obj)
entity._id = obj._id
entity.data.fromObject(object)
entity._id = object._id
return entity
}
}
@ -58,7 +57,8 @@ export abstract class Repo<T extends Entity<any>> {
`found few (${entryList.length}) entries in ${this.Name()} for id ${id}`
)
}
if (entryList.length == 0) {
if (entryList.length === 0) {
throw new ErrEntityNotFound(`not found entry in [${this.Name()}] for id [${id}]`)
}
@ -69,27 +69,31 @@ export abstract class Repo<T extends Entity<any>> {
*
* @protected
*/
async _findByParams(params: {}, limit?: number, order?: {}): Promise<T[]> {
const cursor = await this._storage.find(this.Name(), params)
async _findByParams(
parameters: Record<string, unknown>,
limit?: number,
order?: Record<string, unknown>
): Promise<T[]> {
const cursor = await this._storage.find(this.Name(), parameters)
if (limit && limit > 0) await cursor.limit(limit)
if (order) await cursor.sort(order)
const data = await cursor.toArray()
const list = []
for (let item of data) {
for (const item of data) {
list.push(this._transformer(item))
}
return list
}
async findMany(params: object, order?: object): Promise<T[]> {
const cursor = await this._storage.find(this.Name(), params)
async findMany(parameters: Record<string, unknown>, order?: Record<string, unknown>): Promise<T[]> {
const cursor = await this._storage.find(this.Name(), parameters)
if (this._offset > 0) await cursor.skip(this._offset)
if (this._limit > 0) await cursor.limit(this._limit)
if (order) await cursor.sort(order)
const data = await cursor.toArray()
const list = []
for (let item of data) {
for (const item of data) {
list.push(this._transformer(item))
}
@ -102,7 +106,7 @@ export abstract class Repo<T extends Entity<any>> {
return this
}
async count(query?: object): Promise<number> {
async count(query?: Record<string, unknown>): Promise<number> {
return this._storage.count(this.Name(), query)
}

View File

@ -1,6 +1,6 @@
export interface StorageCursor {
limit(num: number): StorageCursor
sort(params: {}): StorageCursor
limit(number_: number): StorageCursor
sort(parameters: Record<string, unknown>): StorageCursor
skip(offset: number): StorageCursor
toArray(): Promise<any[]>
}
@ -15,18 +15,18 @@ export abstract class Storage {
this._dsn = dsn
}
abstract find(name: string, query: Object): Promise<StorageCursor>
abstract find(name: string, query: Record<string, unknown>): Promise<StorageCursor>
abstract save(name: string, uniqKey: string, data: Object): Promise<string>
abstract save(name: string, uniqKey: string, data: Record<string, unknown>): Promise<string>
abstract createSession(): StorageSession
abstract count(name: string, query?: object): Promise<number>
abstract count(name: string, query?: Record<string, unknown>): Promise<number>
abstract remove(collectionName: string, uniqKeyName: string, uniqKey: string): Promise<boolean>
}
export abstract class StorageSession {
abstract start(options?: {}): Promise<void>
abstract commit(fn: () => any, options?: {}): Promise<void>
abstract start(options?: Record<string, unknown>): Promise<void>
abstract commit(fn: () => any, options?: Record<string, unknown>): Promise<void>
}

View File

@ -1,9 +1,9 @@
import { Storage, StorageCursor, StorageSession } from '../storage'
import { ClientSession, MongoClient, TransactionOptions } from 'mongodb'
import { Storage, StorageCursor, StorageSession } from '../storage'
import { ErrEntityHasNoUniqKeyValue } from '../entity'
export class MongoStorage extends Storage {
private _client: MongoClient
private readonly _client: MongoClient
_session: ClientSession
constructor(dsn: string) {
@ -20,19 +20,19 @@ export class MongoStorage extends Storage {
await this._client.connect()
}
async find(collectionName: string, query: object): Promise<StorageCursor> {
async find(collectionName: string, query: Record<string, unknown>): Promise<StorageCursor> {
await this._connect()
const coll = await this._client.db().collection(collectionName)
return coll.find(query)
}
async count(collectionName: string, query: object): Promise<number> {
async count(collectionName: string, query: Record<string, unknown>): Promise<number> {
await this._connect()
const coll = await this._client.db().collection(collectionName)
return coll.countDocuments(query)
}
async save(collectionName: string, uniqKey: string, data: object): Promise<string> {
async save(collectionName: string, uniqKey: string, data: Record<string, unknown>): Promise<string> {
await this._connect()
const id = data[uniqKey]
const coll = await this._client.db().collection(collectionName)
@ -42,14 +42,14 @@ export class MongoStorage extends Storage {
if (result.lastErrorObject) {
if (result.lastErrorObject.updatedExisting) {
return result.value._id
} else {
return result.lastErrorObject.upserted
}
} else {
throw new TypeError(`can not save data to ${collectionName} with result ${result}`)
return result.lastErrorObject.upserted
}
throw new TypeError(`can not save data to ${collectionName} with result ${result}`)
} else {
// нельзя сохранить сущность без значения уникального ключа
// Нельзя сохранить сущность без значения уникального ключа
// const result = await coll.insertOne(data)
// return result.insertedId._id.toHexString()
throw new ErrEntityHasNoUniqKeyValue()
@ -96,8 +96,8 @@ export class MongoStorageSession extends StorageSession {
async commit(fn, options?: TransactionOptions) {
try {
await this._session.withTransaction(fn, options)
} catch (e) {
throw e
} catch (error) {
throw error
}
await this._session.endSession()

View File

@ -1,13 +1,13 @@
const Sequencer = require('@jest/test-sequencer').default;
const Sequencer = require('@jest/test-sequencer').default
class CustomSequencer extends Sequencer {
sort(tests) {
// Test structure information
// https://github.com/facebook/jest/blob/6b8b1404a1d9254e7d5d90a8934087a9c9899dab/packages/jest-runner/src/types.ts#L17-L21
const copyTests = Array.from(tests);
const copyTests = Array.from(tests)
// @ts-ignore
return copyTests.sort((testA, testB) => (testA.path > testB.path ? 1 : -1));
return copyTests.sort((testA, testB) => (testA.path > testB.path ? 1 : -1))
}
}
module.exports = CustomSequencer;
module.exports = CustomSequencer

View File

@ -1,10 +1,10 @@
const failFast = require("jasmine-fail-fast");
const failFast = require('jasmine-fail-fast')
// Добавляем нормальный fail fast
// https://github.com/facebook/jest/issues/2867
//if (process.argv.includes("--bail")) {
// @ts-ignore
const jasmineEnv = jasmine.getEnv();
jasmineEnv.addReporter(failFast.init());
//}
// if (process.argv.includes("--bail")) {
// @ts-expect-error
const jasmineEnv = jasmine.getEnv()
jasmineEnv.addReporter(failFast.init())
// }

View File

@ -6,30 +6,30 @@ export class ItemData extends Data {
return 'a'
}
a: number = 0
a = 0
b: number
c: { d: number; e?: number } = { d: 1 }
__f: number = 0
__f = 0
sum() {
return this.a + this.c.d + this.c.e
}
}
describe('Data', function () {
describe('#toObject', function () {
it('should return object', function () {
describe('Data', () => {
describe('#toObject', () => {
it('should return object', () => {
const data = new ItemData()
data.a = 1
data.c = { d: 2, e: undefined }
const obj = data.toObject()
const object = data.toObject()
assert.strictEqual('a' in obj, true)
assert.strictEqual('b' in obj, false)
assert.strictEqual('c' in obj, true)
assert.strictEqual('d' in obj['c'], true)
assert.strictEqual('e' in obj['c'], true)
assert.strictEqual('__f' in obj, false)
assert.strictEqual('sum' in obj, false)
assert.strictEqual('a' in object, true)
assert.strictEqual('b' in object, false)
assert.strictEqual('c' in object, true)
assert.strictEqual('d' in object['c'], true)
assert.strictEqual('e' in object['c'], true)
assert.strictEqual('__f' in object, false)
assert.strictEqual('sum' in object, false)
})
})
describe('#fromObject', () => {
@ -37,10 +37,10 @@ describe('Data', function () {
const dataOrig = new ItemData()
dataOrig.a = 1
dataOrig.c = { d: 2, e: undefined }
const obj = dataOrig.toObject()
const object = dataOrig.toObject()
const dataRecovered = new ItemData()
dataRecovered.fromObject(obj)
dataRecovered.fromObject(object)
assert.notStrictEqual(dataOrig, dataRecovered)
})

View File

@ -1,10 +1,10 @@
import { ItemData } from './data.test'
import assert from 'assert'
import { Entity } from '../app/entity'
import { Storage } from '../app/storage'
import { Repo } from '../app/repo'
import { MapStorage } from './storage.test'
import { Data } from '../app/data'
import assert from 'assert'
import { MapStorage } from './storage.test'
import { ItemData } from './data.test'
class Item extends Entity<ItemData> {
_getVO(): ItemData {
@ -16,6 +16,7 @@ class Item extends Entity<ItemData> {
getA(): number {
return this._data.a
}
getC(): { d: number; e?: number } {
return this._data.c
}
@ -29,13 +30,14 @@ class ItemRepository extends Repo<Item> {
Name() {
return 'items'
}
Entity() {
return new Item()
}
}
describe('ItemRepository', function () {
describe('map storage save', function () {
describe('ItemRepository', () => {
describe('map storage save', () => {
const storage = new MapStorage('')
const repo = new ItemRepository(storage)

View File

@ -9,16 +9,16 @@ export class MapCursor implements StorageCursor {
this._map = map
}
limit(num: number): StorageCursor {
this._limit = num
limit(number_: number): StorageCursor {
this._limit = number_
return this
}
sort(params: {}): StorageCursor {
sort(parameters: Record<string, unknown>): StorageCursor {
return this
}
toArray(): Promise<any[]> {
async toArray(): Promise<any[]> {
return Promise.resolve(Array.from(this._map.values()))
}
@ -37,50 +37,52 @@ export class MapStorage extends Storage {
this._mapPool = {}
}
async find(name: string, query: Object): Promise<StorageCursor> {
async find(name: string, query: Record<string, unknown>): Promise<StorageCursor> {
if (!(name in this._mapPool)) {
return new MapCursor(new Map())
}
const map: Map<any, any> = new Map()
for (let [_, elem] of this._mapPool[name]) {
nextParam: for (let [key, val] of Object.entries(query)) {
if (key in elem) {
for (let symbol of ['=', '>', '<', '!']) {
if ((val + '').startsWith(symbol)) {
if (eval(elem[key] + val + '')) {
map.set(_, elem)
break nextParam
}
for (const [_, element] of this._mapPool[name]) {
nextParam: for (const [key, value] of Object.entries(query)) {
if (key in element) {
for (const symbol of ['=', '>', '<', '!']) {
if (String(value).startsWith(symbol) && eval(String(element[key] + value))) {
map.set(_, element)
break nextParam
}
}
if (elem[key] == val) {
map.set(_, elem)
if (element[key] == value) {
map.set(_, element)
}
}
}
}
return new MapCursor(map)
}
count(name: string, query: object): Promise<number> {
async count(name: string, query: Record<string, unknown>): Promise<number> {
return Promise.resolve(0)
}
remove(collectionName: string, uniqKeyName: string, uniqKey: string): Promise<boolean> {
async remove(collectionName: string, uniqKeyName: string, uniqKey: string): Promise<boolean> {
return Promise.resolve(true)
}
async save(name: string, idKey: string, data: Object): Promise<string> {
async save(name: string, idKey: string, data: Record<string, unknown>): Promise<string> {
if (!(name in this._mapPool)) {
this._mapPool[name] = new Map()
this._mapPoolId[name] = {}
}
const id = data[idKey] + ''
const id = String(data[idKey])
this._mapPool[name].set(id, data)
if (!(id in this._mapPoolId[name])) {
this._mapPoolId[name][id] = Date.now()
}
return Promise.resolve(this._mapPoolId[name][id])
}
@ -90,17 +92,17 @@ export class MapStorage extends Storage {
}
class MapStorageSession extends StorageSession {
async commit(fn, options?: {}): Promise<any> {
async commit(fn, options?: Record<string, unknown>): Promise<any> {
return Promise.resolve(undefined)
}
async start(options?: {}): Promise<any> {
async start(options?: Record<string, unknown>): Promise<any> {
return Promise.resolve(undefined)
}
}
describe('Storage', function () {
describe('#save', function () {
describe('Storage', () => {
describe('#save', () => {
it('should return inner id', async () => {
const storage = new MapStorage('')