diff --git a/packages/midway-core/package.json b/packages/midway-core/package.json index 1585587e8fdf..53c8ff09248e 100644 --- a/packages/midway-core/package.json +++ b/packages/midway-core/package.json @@ -6,7 +6,7 @@ "typings": "dist/index.d.ts", "scripts": { "build": "npm run lint && midway-bin build -c", - "lint": "../../node_modules/.bin/tslint --format prose -c ../../tslint.json src/**/*.ts test/**/*.ts", + "lint": "../../node_modules/.bin/tslint --format prose -c ../../tslint.json --fix 'src/**/*.ts'", "test": "npm run lint && midway-bin clean && NODE_ENV=test midway-bin test --ts", "cov": "midway-bin clean && midway-bin cov --ts", "autod": "midway-bin autod" diff --git a/packages/midway-core/src/container.ts b/packages/midway-core/src/container.ts index 12742f08482d..60e087c00881 100644 --- a/packages/midway-core/src/container.ts +++ b/packages/midway-core/src/container.ts @@ -110,7 +110,7 @@ class LoggerResolver implements IManagedResolver { } resolve(managed: IManagedInstance): any { - const log: ManagedLogger = managed; + const log: ManagedLogger = managed as ManagedLogger; if (log.name) { return this.container.handlerMap.get(MidwayHandlerKey.LOGGER)(log.name); } @@ -151,7 +151,7 @@ class PluginResolver implements IManagedResolver { } resolve(managed: IManagedInstance): any { - const p = managed; + const p = managed as ManagedPlugin; return this.container.handlerMap.get(MidwayHandlerKey.PLUGIN)(p.name); } @@ -161,8 +161,8 @@ class PluginResolver implements IManagedResolver { } export class MidwayContainer extends Container implements IContainer { - controllersIds: Array = []; - middlewaresIds: Array = []; + controllersIds: string[] = []; + middlewaresIds: string[] = []; handlerMap: Map any>; // 仅仅用于兼容requestContainer的ctx ctx = {}; @@ -206,8 +206,8 @@ export class MidwayContainer extends Container implements IContainer { }) { const loadDirs = [].concat(opts.loadDir || []); - for (let dir of loadDirs) { - let fileResults = globby.sync(['**/**.ts', '**/**.js', '!**/**.d.ts'].concat(opts.pattern || []), { + for (const dir of loadDirs) { + const fileResults = globby.sync(['**/**.ts', '**/**.js', '!**/**.d.ts'].concat(opts.pattern || []), { cwd: dir, ignore: [ '**/node_modules/**', @@ -219,15 +219,15 @@ export class MidwayContainer extends Container implements IContainer { ].concat(opts.ignore || []), }); - for (let name of fileResults) { + for (const name of fileResults) { const file = path.join(dir, name); debug(`binding file => ${file}`); - let exports = require(file); + const exports = require(file); if (is.class(exports) || is.function(exports)) { this.bindClass(exports); } else { - for (let m in exports) { + for (const m in exports) { const module = exports[m]; if (is.class(module) || is.function(module)) { this.bindClass(module); @@ -238,9 +238,9 @@ export class MidwayContainer extends Container implements IContainer { } } - private bindClass(module) { + protected bindClass(module) { if (is.class(module)) { - let metaData = Reflect.getMetadata(TAGGED_CLS, module); + const metaData = Reflect.getMetadata(TAGGED_CLS, module) as TagClsMetadata; if (metaData) { this.bind(metaData.id, module); } else { @@ -248,7 +248,7 @@ export class MidwayContainer extends Container implements IContainer { this.bind(camelcase(module.name), module); } } else { - let info: { + const info: { id: ObjectIdentifier, provider: (context?: IApplicationContext) => any, scope?: Scope, @@ -283,8 +283,8 @@ export class MidwayContainer extends Container implements IContainer { } // lack of field if (constructorMetaData && constructorArgs) { - for (let idx in constructorMetaData) { - let index = parseInt(idx, 10); + for (const idx in constructorMetaData) { + const index = parseInt(idx, 10); const propertyMeta = constructorMetaData[index]; let result; @@ -345,7 +345,7 @@ export class MidwayContainer extends Container implements IContainer { * @param target * @returns {Array} */ - private getClzSetterProps(setterClzKey, target): Array { + private getClzSetterProps(setterClzKey, target): string[] { return Reflect.getMetadata(setterClzKey, target); } @@ -359,8 +359,8 @@ export class MidwayContainer extends Container implements IContainer { */ private defineGetterPropertyValue(setterProps, metadataKey, instance, getterHandler) { if (setterProps && getterHandler) { - for (let prop of setterProps) { - let propertyKey = Reflect.getMetadata(metadataKey, instance, prop); + for (const prop of setterProps) { + const propertyKey = Reflect.getMetadata(metadataKey, instance, prop); if (propertyKey) { Object.defineProperty(instance, prop, { get: () => getterHandler(propertyKey), @@ -380,7 +380,7 @@ export class MidwayContainer extends Container implements IContainer { super.registerCustomBinding(objectDefinition, target); // Override the default scope to request - let objDefOptions: ObjectDefinitionOptions = Reflect.getMetadata(OBJ_DEF_CLS, target); + const objDefOptions: ObjectDefinitionOptions = Reflect.getMetadata(OBJ_DEF_CLS, target); if (objDefOptions && !objDefOptions.scope) { debug(`register @scope to default value(request), id=${objectDefinition.id}`); objectDefinition.scope = ScopeEnum.Request; diff --git a/packages/midway-core/src/loader.ts b/packages/midway-core/src/loader.ts index 728518c7281b..a80a735848a3 100644 --- a/packages/midway-core/src/loader.ts +++ b/packages/midway-core/src/loader.ts @@ -57,7 +57,7 @@ export class MidwayLoader extends EggLoader { } const name = plugin.package || plugin.name; - let lookupDirs = []; + const lookupDirs = []; // 尝试在以下目录找到匹配的插件 // -> {APP_PATH}/node_modules @@ -144,7 +144,7 @@ export class MidwayLoader extends EggLoader { protected loadApplicationContext() { // this.app.options.container 测试用例编写方便点 - let containerConfig = this.config.container || this.app.options.container || {}; + const containerConfig = this.config.container || this.app.options.container || {}; // 在 super contructor 中会调用到getAppInfo,之后会被赋值 // 如果是typescript会加上 dist 或者 src 目录 this.applicationContext = new MidwayContainer(this.baseDir); @@ -196,7 +196,7 @@ export class MidwayLoader extends EggLoader { if (!self.pluginLoaded && isPluginName(prop) && !(prop in pluginContainerProps)) { // save to context when called app.xxx = xxx // now we can get plugin from context - debug(`pluginContext register [${prop}]`); + debug(`pluginContext register [${prop as string}]`); self.pluginContext.registerObject(prop, attributes.value); } return Object.defineProperty(target, prop, attributes); @@ -206,7 +206,7 @@ export class MidwayLoader extends EggLoader { this.getLoadUnits() .forEach(unit => { // 兼容旧插件加载方式 - let ret = this.loadFile(this.resolveModule(path.join(unit.path, fileName))); + const ret = this.loadFile(this.resolveModule(path.join(unit.path, fileName))); if (ret) { // midway 的插件会返回对象 debug(`pluginContext register [${unit.name}]`); diff --git a/packages/midway-core/src/loading.ts b/packages/midway-core/src/loading.ts index 5ca168293988..986879bfeef1 100644 --- a/packages/midway-core/src/loading.ts +++ b/packages/midway-core/src/loading.ts @@ -1,6 +1,7 @@ -/**! +/** * Midway Loading 文件加载 */ +/* tslint:disable:no-unused-expression */ const debug = require('debug')('midway:loading'); const is = require('is-type-of'); const globby = require('globby'); @@ -26,13 +27,13 @@ export function loading(files, options) { options = Object.assign({ call: true, - ignore: function(exports, file, dir) { + ignore(exports, file, dir) { return false; }, - resultHandler: function(result, file, dir, exports) { + resultHandler(result, file, dir, exports) { return result; }, - propertyHandler: function(properties, name, file) { + propertyHandler(properties, name, file) { return properties; } }, options); @@ -41,16 +42,16 @@ export function loading(files, options) { const into = is.object(options.into) && options.into; const flatten = !!options.flatten; - let results = []; - let loadDirs = [].concat(options.loadDirs); + const results = []; + const loadDirs = [].concat(options.loadDirs); loadDirs.forEach((dir) => { - let fileResults = globby.sync(files, {cwd: dir}); + const fileResults = globby.sync(files, {cwd: dir}); fileResults.forEach((name) => { const file = path.join(dir, name); debug(`LoadFiles => [${file}]: will load`); - let exports = require(file); + const exports = require(file); if (options.ignore(exports, file, dir)) { return; } @@ -67,7 +68,7 @@ export function loading(files, options) { const reg = /^[a-z][\.a-z0-9_-]*$/i; // 不支持 comma(,) let properties = name.replace(/\.js$/, '') .split('/') - .map( property => { + .map(property => { if (!reg.test(property)) { throw new Error(`${property} does not match ${reg} in ${name}`); } @@ -97,3 +98,4 @@ export function loading(files, options) { return results; } +/* tslint:enable:no-unused-expression */ diff --git a/packages/midway-core/src/requestContainer.ts b/packages/midway-core/src/requestContainer.ts index 130c971ded8d..c488eb13803f 100644 --- a/packages/midway-core/src/requestContainer.ts +++ b/packages/midway-core/src/requestContainer.ts @@ -72,11 +72,11 @@ export class MidwayRequestContainer extends MidwayContainer { definition.constructorArgs = [valueManagedIns]; } // create object from applicationContext definition for requestScope - return await this.resolverFactory.createAsync(definition, args); + return this.resolverFactory.createAsync(definition, args); } if (this.parent) { - return await this.parent.getAsync(identifier, args); + return this.parent.getAsync(identifier, args); } } } diff --git a/packages/midway-mock/package.json b/packages/midway-mock/package.json index 763c1a789464..5bd9811c767c 100644 --- a/packages/midway-mock/package.json +++ b/packages/midway-mock/package.json @@ -9,7 +9,7 @@ "typings": "dist/index.d.ts", "scripts": { "build": "npm run lint && midway-bin build -c", - "lint": "../../node_modules/.bin/tslint --format prose -c ../../tslint.json src/**/*.ts test/**/*.ts", + "lint": "../../node_modules/.bin/tslint --format prose -c ../../tslint.json --fix 'src/**/*.ts'", "test": "npm run lint && midway-bin clean && midway-bin test --ts", "cov": "midway-bin cov --ts", "ci": "midway-bin test", diff --git a/packages/midway-web/package.json b/packages/midway-web/package.json index 8b80ecfa039f..3ffe5bacf426 100644 --- a/packages/midway-web/package.json +++ b/packages/midway-web/package.json @@ -6,7 +6,7 @@ "typings": "dist/index.d.ts", "scripts": { "build": "npm run lint && midway-bin build -c", - "lint": "../../node_modules/.bin/tslint --format prose -c ../../tslint.json src/**/*.ts test/**/*.ts", + "lint": "../../node_modules/.bin/tslint --format prose -c ../../tslint.json --fix 'src/**/*.ts'", "test": "npm run lint && midway-bin clean && NODE_ENV=test midway-bin test --ts", "cov": "midway-bin cov --ts", "ci": "npm run test", diff --git a/packages/midway-web/src/baseController.ts b/packages/midway-web/src/baseController.ts index 953d06ddf31f..09d2da15b251 100644 --- a/packages/midway-web/src/baseController.ts +++ b/packages/midway-web/src/baseController.ts @@ -16,7 +16,7 @@ class Route { } export class BaseController { - routes: Array = []; + routes: Route[] = []; constructor() { this.init(); diff --git a/packages/midway-web/src/loading.ts b/packages/midway-web/src/loading.ts index ebfc1341c57e..5f11d636d515 100644 --- a/packages/midway-web/src/loading.ts +++ b/packages/midway-web/src/loading.ts @@ -1,4 +1,4 @@ -/**! +/** * Midway Loading 文件加载 */ const debug = require('debug')('midway:loading'); @@ -13,29 +13,29 @@ export function loading(files, options) { options = Object.assign({ call: true, - ignore: function(exports, file, dir) { + ignore(exports, file, dir) { return false; }, - resultHandler: function(result, file, dir, exports) { + resultHandler(result, file, dir, exports) { return result; }, - propertyHandler: function(properties, name, file) { + propertyHandler(properties, name, file) { return properties; } }, options); files = [].concat(files); - let results = []; - let loadDirs = [].concat(options.loadDirs); + const results = []; + const loadDirs = [].concat(options.loadDirs); loadDirs.forEach((dir) => { - let fileResults = globby.sync(files, {cwd: dir}); + const fileResults = globby.sync(files, {cwd: dir}); fileResults.forEach((name) => { const file = path.join(dir, name); debug(`LoadFiles => [${file}]: will load`); - let exports = require(file); + const exports = require(file); if (options.ignore(exports, file, dir)) { return; } diff --git a/packages/midway-web/src/midway.ts b/packages/midway-web/src/midway.ts index dad8876704e8..65c425bb20a3 100644 --- a/packages/midway-web/src/midway.ts +++ b/packages/midway-web/src/midway.ts @@ -6,9 +6,9 @@ import * as path from 'path'; const MIDWAY_PATH = path.dirname(__dirname); -class MidwayApplication extends (<{ +class MidwayApplication extends (Application as { new(...x) -}> Application) { +}) { get [Symbol.for('egg#loader')]() { return AppWorkerLoader; @@ -35,15 +35,15 @@ class MidwayApplication extends (<{ } getPluginContext() { - return (this.loader).pluginContext; + return (this.loader as AppWorkerLoader).pluginContext; } getApplicationContext() { - return (this.loader).applicationContext; + return (this.loader as AppWorkerLoader).applicationContext; } generateController(controllerMapping: string) { - return (this.loader).generateController(controllerMapping); + return (this.loader as AppWorkerLoader).generateController(controllerMapping); } /** @@ -90,9 +90,9 @@ class MidwayApplication extends (<{ } } -class MidwayAgent extends (<{ +class MidwayAgent extends (Agent as { new(...x) -}> Agent) { +}) { get [Symbol.for('egg#loader')]() { return AgentWorkerLoader; @@ -115,11 +115,11 @@ class MidwayAgent extends (<{ } getPluginContext() { - return (this.loader).pluginContext; + return (this.loader as AgentWorkerLoader).pluginContext; } getApplicationContext() { - return (this.loader).applicationContext; + return (this.loader as AgentWorkerLoader).applicationContext; } /** diff --git a/packages/midway-web/src/utils.ts b/packages/midway-web/src/utils.ts index f97210023bef..31760f101fab 100644 --- a/packages/midway-web/src/utils.ts +++ b/packages/midway-web/src/utils.ts @@ -3,7 +3,7 @@ import 'reflect-metadata'; export function attachMetaDataOnClass(clz, key, value) { // save method name on class let classMetaValue = Reflect.getMetadata(key, clz); - if(classMetaValue) { + if (classMetaValue) { classMetaValue = classMetaValue.concat(value); } else { classMetaValue = [value]; @@ -42,15 +42,15 @@ export function getMethodNames(obj) { const allOwnKeysOnPrototype = Object.getOwnPropertyNames(proto); // get methods from es6 class allOwnKeysOnPrototype.forEach(k => { - if(typeof obj[k] === 'function' && k !== 'constructor') { + if (typeof obj[k] === 'function' && k !== 'constructor') { result.push(k); } }); } - while(proto && proto !== Object.prototype); + while (proto && proto !== Object.prototype); // leave out those methods on Object's prototype return result.filter(k => { return ownKeysOnObjectPrototype.indexOf(k) === -1; }); -} \ No newline at end of file +} diff --git a/packages/midway/package.json b/packages/midway/package.json index 36baafa063c6..99f22fe6bc6e 100644 --- a/packages/midway/package.json +++ b/packages/midway/package.json @@ -6,7 +6,7 @@ "typings": "dist/index.d.ts", "scripts": { "build": "npm run lint && midway-bin build -c", - "lint": "../../node_modules/.bin/tslint --format prose -c ../../tslint.json src/**/*.ts test/**/*.ts", + "lint": "../../node_modules/.bin/tslint --format prose -c ../../tslint.json --fix 'src/**/*.ts'", "test": "midway-bin test --ts", "cov": "midway-bin cov --ts", "ci": "npm run test",