diff --git a/lib/box/index.ts b/lib/box/index.ts index c09a8500ff..31a3df5e21 100644 --- a/lib/box/index.ts +++ b/lib/box/index.ts @@ -27,8 +27,6 @@ class Box extends EventEmitter { public File: any; public ignore: any[]; public source: any; - public emit: any; - public ctx: any; constructor(ctx: Hexo, base: string, options?: object) { super(); @@ -65,13 +63,13 @@ class Box extends EventEmitter { class _File extends File { public box: Box; - render(options?: any) { + render(options?: object) { return ctx.render.render({ path: this.source }, options); } - renderSync(options?: any) { + renderSync(options?: object) { return ctx.render.renderSync({ path: this.source }, options); diff --git a/lib/extend/renderer.ts b/lib/extend/renderer.ts index f55f92a5ec..701c5d0031 100644 --- a/lib/extend/renderer.ts +++ b/lib/extend/renderer.ts @@ -24,7 +24,7 @@ export interface StoreSyncFunction { // callback: (err: Error, value: string) => any ): any; output?: string; - compile?: (local: object) => string; + compile?: (local: object) => any; } export interface StoreFunction { ( @@ -37,7 +37,7 @@ export interface StoreFunction { callback: (err: Error, value: string) => any ): void; output?: string; - compile?: (local: object) => string; + compile?: (local: object) => any; disableNunjucks?: boolean; } diff --git a/lib/hexo/multi_config_path.ts b/lib/hexo/multi_config_path.ts index 84194400b6..978c04fca0 100644 --- a/lib/hexo/multi_config_path.ts +++ b/lib/hexo/multi_config_path.ts @@ -4,7 +4,7 @@ import yml from 'js-yaml'; import { deepMerge } from 'hexo-util'; import type Hexo from './index'; -export = (ctx: Hexo) => function multiConfigPath(base: string, configPaths: string, outputDir: any) { +export = (ctx: Hexo) => function multiConfigPath(base: string, configPaths: string, outputDir: string) { const { log } = ctx; const defaultPath = join(base, '_config.yml'); diff --git a/lib/hexo/post.ts b/lib/hexo/post.ts index e820b52955..34588bf76d 100644 --- a/lib/hexo/post.ts +++ b/lib/hexo/post.ts @@ -186,7 +186,7 @@ class PostRenderEscape { } } -const prepareFrontMatter = (data: object, jsonMode: boolean) => { +const prepareFrontMatter = (data: any, jsonMode: boolean) => { for (const [key, item] of Object.entries(data)) { if (moment.isMoment(item)) { data[key] = item.utc().format('YYYY-MM-DD HH:mm:ss'); @@ -232,19 +232,24 @@ interface Data { source?: string; } +interface PostData { + title?: string; + layout?: string; + slug?: string; + path?: string; + [prop: string]: any; +} + class Post { public context: Hexo; - public config: any; - public tag: any; - public separator: string; constructor(context: Hexo) { this.context = context; } - create(data: any, callback?: (...args: any[]) => any); - create(data: any, replace: boolean, callback?: (...args: any[]) => any); - create(data: any, replace: boolean | ((...args: any[]) => any), callback?: (...args: any[]) => any) { + create(data: PostData, callback?: (...args: any[]) => any); + create(data: PostData, replace: boolean, callback?: (...args: any[]) => any); + create(data: PostData, replace: boolean | ((...args: any[]) => any), callback?: (...args: any[]) => any) { if (!callback && typeof replace === 'function') { callback = replace; replace = false; @@ -288,7 +293,7 @@ class Post { }); } - _renderScaffold(data: any) { + _renderScaffold(data: PostData) { const { tag } = this.context.extend; let splitted; @@ -330,7 +335,7 @@ class Post { }); } - publish(data: any, replace: boolean, callback: (...args: any[]) => any) { + publish(data: PostData, replace: boolean, callback?: (...args: any[]) => any) { if (!callback && typeof replace === 'function') { callback = replace; replace = false; @@ -383,7 +388,7 @@ class Post { }).thenReturn(result).asCallback(callback); } - render(source: string, data: Data = {}, callback: (...args: any[]) => any) { + render(source: string, data: Data = {}, callback?: (...args: any[]) => any) { const ctx = this.context; const { config } = ctx; const { tag } = ctx.extend; diff --git a/lib/hexo/router.ts b/lib/hexo/router.ts index 9adfcece11..33edb61478 100644 --- a/lib/hexo/router.ts +++ b/lib/hexo/router.ts @@ -18,8 +18,6 @@ class RouteStream extends Readable { public _data: any; public _ended: boolean; public modified: any; - public push: any; - public emit: any; constructor(data: Data) { super({ objectMode: true }); diff --git a/lib/plugins/tag/code.ts b/lib/plugins/tag/code.ts index 4d70b68ce9..e56e2122c4 100644 --- a/lib/plugins/tag/code.ts +++ b/lib/plugins/tag/code.ts @@ -30,8 +30,8 @@ const rCaption = /\S[\S\s]*/; function parseArgs(args: string[]): HighlightOptions { const _else = []; const len = args.length; - let lang, language_attr, - line_number, line_threshold, wrap; + let lang: string, language_attr: boolean, + line_number: boolean, line_threshold: number, wrap: boolean; let firstLine = 1; const mark = []; for (let i = 0; i < len; i++) { @@ -123,7 +123,7 @@ export = (ctx: Hexo) => function codeTag(args: string[], content: string) { return `
${escapeHTML(content)}
`; } - let index; + let index: number; let enableHighlight = true; if ((index = args.findIndex(item => item.startsWith('highlight:'))) !== -1) { diff --git a/lib/plugins/tag/post_path.ts b/lib/plugins/tag/post_path.ts index 66c2b79704..fdc33eccab 100644 --- a/lib/plugins/tag/post_path.ts +++ b/lib/plugins/tag/post_path.ts @@ -9,7 +9,7 @@ import type Hexo from '../../hexo'; * {% post_path slug | title %} */ export = (ctx: Hexo) => { - return function postPathTag(args) { + return function postPathTag(args: any[]) { const slug = args.shift(); if (!slug) return; diff --git a/lib/theme/index.ts b/lib/theme/index.ts index 6553130a4e..aff5a74711 100644 --- a/lib/theme/index.ts +++ b/lib/theme/index.ts @@ -13,6 +13,7 @@ class Theme extends Box { public views: any; public i18n: I18n; public View: any; + public processors: any[]; constructor(ctx: Hexo, options?) { super(ctx, ctx.theme_dir, options); diff --git a/lib/theme/processors/config.ts b/lib/theme/processors/config.ts index 64f65e2c28..c37f3ed13a 100644 --- a/lib/theme/processors/config.ts +++ b/lib/theme/processors/config.ts @@ -1,13 +1,15 @@ import { Pattern } from 'hexo-util'; +import type { _File } from '../../box'; +import Theme from '..'; -function process(file) { +function process(file: _File) { if (file.type === 'delete') { - file.box.config = {}; + (file.box as Theme).config = {}; return; } return file.render().then(result => { - file.box.config = result; + (file.box as Theme).config = result; this.log.debug('Theme config loaded.'); }).catch(err => { this.log.error('Theme config load failed.'); diff --git a/lib/theme/processors/i18n.ts b/lib/theme/processors/i18n.ts index ce955eb8a4..af2db9be95 100644 --- a/lib/theme/processors/i18n.ts +++ b/lib/theme/processors/i18n.ts @@ -1,11 +1,13 @@ import { Pattern } from 'hexo-util'; import { extname } from 'path'; +import type { _File } from '../../box'; +import type Theme from '..'; -function process(file) { +function process(file: _File) { const { path } = file.params; const ext = extname(path); const name = path.substring(0, path.length - ext.length); - const { i18n } = file.box; + const { i18n } = (file.box as Theme); if (file.type === 'delete') { i18n.remove(name); diff --git a/lib/theme/processors/source.ts b/lib/theme/processors/source.ts index 40915fae47..ac8c21a1fe 100644 --- a/lib/theme/processors/source.ts +++ b/lib/theme/processors/source.ts @@ -1,7 +1,8 @@ import { Pattern } from 'hexo-util'; import * as common from '../../plugins/processor/common'; +import type { _File } from '../../box'; -function process(file) { +function process(file: _File) { const Asset = this.model('Asset'); const id = file.source.substring(this.base_dir.length).replace(/\\/g, '/'); const { path } = file.params; diff --git a/lib/theme/processors/view.ts b/lib/theme/processors/view.ts index c4011564e2..c127d02e7c 100644 --- a/lib/theme/processors/view.ts +++ b/lib/theme/processors/view.ts @@ -1,15 +1,17 @@ import { Pattern } from 'hexo-util'; +import type { _File } from '../../box'; +import type Theme from '..'; -function process(file) { +function process(file: _File) { const { path } = file.params; if (file.type === 'delete') { - file.box.removeView(path); + (file.box as Theme).removeView(path); return; } return file.read().then(result => { - file.box.setView(path, result); + (file.box as Theme).setView(path, result); }); } diff --git a/lib/theme/view.ts b/lib/theme/view.ts index 4b750a0960..af4c75be62 100644 --- a/lib/theme/view.ts +++ b/lib/theme/view.ts @@ -1,8 +1,10 @@ import { dirname, extname, join } from 'path'; import { parse as yfm } from 'hexo-front-matter'; import Promise from 'bluebird'; +import type Theme from '.'; +import type Render from '../hexo/render'; -const assignIn = (target, ...sources) => { +const assignIn = (target: any, ...sources: any[]) => { const length = sources.length; if (length < 1 || target == null) return target; @@ -23,14 +25,12 @@ class Options { class View { public path: string; public source: any; - public _theme: any; + public _theme: Theme; public data: any; public _compiled: any; public _compiledSync: any; public _helper: any; - public _render: any; - public layout: any; - public _content: any; + public _render: Render; constructor(path: string, data) { this.path = path; @@ -128,7 +128,7 @@ class View { text: this.data._content }; - function buildFilterArguments(result) { + function buildFilterArguments(result: any): [string, any, any] { const output = render.getOutput(ext) || ext; return [ `after_render:${output}`,