Skip to content

Commit 3c7729d

Browse files
authored
refactor: migrate typescript (#5417)
1 parent 7ef26ad commit 3c7729d

File tree

174 files changed

+1719
-1692
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

174 files changed

+1719
-1692
lines changed

.mocharc.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@ reporter: spec
33
ui: bdd
44
full-trace: true
55
exit: true
6-
parallel: true

lib/extend/generator.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ import type { NodeJSLikeCallback } from '../types';
33

44
interface BaseObj {
55
path: string;
6-
data: any;
7-
layout?: string;
6+
data?: any;
7+
layout?: string | string[];
88
}
99
type ReturnType = BaseObj | BaseObj[];
1010
type GeneratorReturnType = ReturnType | Promise<ReturnType>;
1111

1212
interface GeneratorFunction {
13-
(locals: object, callback?: NodeJSLikeCallback<any>): GeneratorReturnType;
13+
(locals: any, callback?: NodeJSLikeCallback<any>): GeneratorReturnType;
1414
}
1515

1616
type StoreFunctionReturn = Promise<ReturnType>;
1717

1818
interface StoreFunction {
19-
(locals: object): StoreFunctionReturn;
19+
(locals: any): StoreFunctionReturn;
2020
}
2121

2222
interface Store {

lib/hexo/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ class Hexo extends EventEmitter {
660660
});
661661
}
662662

663-
exit(err?: Error): Promise<void> {
663+
exit(err?: any): Promise<void> {
664664
if (err) {
665665
this.log.fatal(
666666
{ err },
@@ -674,11 +674,11 @@ class Hexo extends EventEmitter {
674674
});
675675
}
676676

677-
execFilter(type: string, data: any, options) {
677+
execFilter(type: string, data: any, options?) {
678678
return this.extend.filter.exec(type, data, options);
679679
}
680680

681-
execFilterSync(type: string, data: any, options) {
681+
execFilterSync(type: string, data: any, options?) {
682682
return this.extend.filter.execSync(type, data, options);
683683
}
684684
}

lib/hexo/multi_config_path.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import yml from 'js-yaml';
44
import { deepMerge } from 'hexo-util';
55
import type Hexo from './index';
66

7-
export = (ctx: Hexo) => function multiConfigPath(base: string, configPaths: string, outputDir: string): string {
7+
export = (ctx: Hexo) => function multiConfigPath(base: string, configPaths?: string, outputDir?: string): string {
88
const { log } = ctx;
99
const defaultPath = join(base, '_config.yml');
1010

lib/hexo/post.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,9 @@ interface Result {
231231
}
232232

233233
interface PostData {
234-
title?: string;
234+
title?: string | number;
235235
layout?: string;
236-
slug?: string;
236+
slug?: string | number;
237237
path?: string;
238238
[prop: string]: any;
239239
}
@@ -333,7 +333,10 @@ class Post {
333333
});
334334
}
335335

336-
publish(data: PostData, replace: boolean, callback?: NodeJSLikeCallback<Result>) {
336+
publish(data: PostData, replace?: boolean);
337+
publish(data: PostData, callback?: NodeJSLikeCallback<Result>);
338+
publish(data: PostData, replace: boolean, callback?: NodeJSLikeCallback<Result>);
339+
publish(data: PostData, replace?: boolean | NodeJSLikeCallback<Result>, callback?: NodeJSLikeCallback<Result>) {
337340
if (!callback && typeof replace === 'function') {
338341
callback = replace;
339342
replace = false;
@@ -366,7 +369,7 @@ class Post {
366369
data.content = data._content;
367370
data._content = undefined;
368371

369-
return this.create(data, replace);
372+
return this.create(data, replace as boolean);
370373
}).then(post => {
371374
result.path = post.path;
372375
result.content = post.content;

lib/hexo/render.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ class Render {
5656
return this.getRenderer(ext, true);
5757
}
5858

59-
render(data: StoreFunctionData, options?: { highlight?: boolean; }, callback?: NodeJSLikeCallback<any>): Promise<any> {
59+
render(data: StoreFunctionData, callback?: NodeJSLikeCallback<any>): Promise<any>;
60+
render(data: StoreFunctionData, options: any, callback?: NodeJSLikeCallback<any>): Promise<any>;
61+
render(data: StoreFunctionData, options?: any | NodeJSLikeCallback<any>, callback?: NodeJSLikeCallback<any>): Promise<any> {
6062
if (!callback && typeof options === 'function') {
6163
callback = options;
6264
options = {};

lib/hexo/router.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class RouteStream extends Readable {
8484
}
8585
}
8686

87-
const _format = (path: string): string => {
87+
const _format = (path?: string): string => {
8888
path = path || '';
8989
if (typeof path !== 'string') throw new TypeError('path must be a string!');
9090

@@ -118,7 +118,7 @@ class Router extends EventEmitter {
118118
return Object.keys(routes).filter(key => routes[key]);
119119
}
120120

121-
format(path: string): string {
121+
format(path?: string): string {
122122
return _format(path);
123123
}
124124

lib/models/types/moment.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class SchemaTypeMoment extends warehouse.SchemaType<moment.Moment> {
1616
super(name, options);
1717
}
1818

19-
cast(value, data) {
19+
cast(value?, data?) {
2020
value = super.cast(value, data);
2121
if (value == null) return value;
2222

@@ -29,7 +29,7 @@ class SchemaTypeMoment extends warehouse.SchemaType<moment.Moment> {
2929
return value;
3030
}
3131

32-
validate(value, data) {
32+
validate(value, data?) {
3333
value = super.validate(value, data);
3434
if (value == null) return value;
3535

@@ -42,11 +42,11 @@ class SchemaTypeMoment extends warehouse.SchemaType<moment.Moment> {
4242
return value;
4343
}
4444

45-
match(value, query, data) {
45+
match(value, query, data?) {
4646
return value ? value.valueOf() === query.valueOf() : false;
4747
}
4848

49-
compare(a, b) {
49+
compare(a?, b?) {
5050
if (a) {
5151
if (b) return a - b;
5252
return 1;
@@ -56,33 +56,33 @@ class SchemaTypeMoment extends warehouse.SchemaType<moment.Moment> {
5656
return 0;
5757
}
5858

59-
parse(value) {
59+
parse(value?) {
6060
if (value) return toMoment(value);
6161
}
6262

63-
value(value, data) {
63+
value(value?, data?) {
6464
// FIXME: Same as above. Also a dirty hack.
6565
return value ? value._d.toISOString() : value;
6666
}
6767

68-
q$day(value, query, data) {
68+
q$day(value, query, data?) {
6969
return value ? value.date() === query : false;
7070
}
7171

72-
q$month(value, query, data) {
72+
q$month(value, query, data?) {
7373
return value ? value.month() === query : false;
7474
}
7575

76-
q$year(value, query, data) {
76+
q$year(value, query, data?) {
7777
return value ? value.year() === query : false;
7878
}
7979

80-
u$inc(value, update, data) {
80+
u$inc(value, update, data?) {
8181
if (!value) return value;
8282
return value.add(update);
8383
}
8484

85-
u$dec(value, update, data) {
85+
u$dec(value, update, data?) {
8686
if (!value) return value;
8787
return value.subtract(update);
8888
}

lib/plugins/filter/new_post_path.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const reservedKeys = {
1818
hash: true
1919
};
2020

21-
function newPostPathFilter(this: Hexo, data: PostSchema = {}, replace: boolean): Promise<string> {
21+
function newPostPathFilter(this: Hexo, data: PostSchema = {}, replace?: boolean): Promise<string> {
2222
const sourceDir = this.source_dir;
2323
const draftDir = join(sourceDir, '_drafts');
2424
const postDir = join(sourceDir, '_posts');

lib/plugins/helper/date.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function getMoment(date: moment.MomentInput | moment.Moment, lang: string, timez
1717
return date;
1818
}
1919

20-
function toISOString(date: string | number | Date | moment.Moment) {
20+
function toISOString(date?: string | number | Date | moment.Moment) {
2121
if (date == null) {
2222
return new Date().toISOString();
2323
}
@@ -29,19 +29,19 @@ function toISOString(date: string | number | Date | moment.Moment) {
2929
return new Date(date as (string | number)).toISOString();
3030
}
3131

32-
function dateHelper(this: LocalsType, date: moment.Moment | moment.MomentInput, format?: string) {
32+
function dateHelper(this: LocalsType, date?: moment.Moment | moment.MomentInput, format?: string) {
3333
const { config } = this;
3434
const moment = getMoment(date, getLanguage(this), config.timezone);
3535
return moment.format(format || config.date_format);
3636
}
3737

38-
function timeHelper(this: LocalsType, date: moment.Moment | moment.MomentInput, format?: string) {
38+
function timeHelper(this: LocalsType, date?: moment.Moment | moment.MomentInput, format?: string) {
3939
const { config } = this;
4040
const moment = getMoment(date, getLanguage(this), config.timezone);
4141
return moment.format(format || config.time_format);
4242
}
4343

44-
function fullDateHelper(this: LocalsType, date: moment.Moment | moment.MomentInput, format: string) {
44+
function fullDateHelper(this: LocalsType, date?: moment.Moment | moment.MomentInput, format?: string) {
4545
if (format) {
4646
const moment = getMoment(date, getLanguage(this), this.config.timezone);
4747
return moment.format(format);
@@ -50,13 +50,13 @@ function fullDateHelper(this: LocalsType, date: moment.Moment | moment.MomentInp
5050
return `${this.date(date)} ${this.time(date)}`;
5151
}
5252

53-
function relativeDateHelper(this: LocalsType, date: moment.Moment | moment.MomentInput) {
53+
function relativeDateHelper(this: LocalsType, date?: moment.Moment | moment.MomentInput) {
5454
const { config } = this;
5555
const moment = getMoment(date, getLanguage(this), config.timezone);
5656
return moment.fromNow();
5757
}
5858

59-
function timeTagHelper(this: LocalsType, date: string | number | Date | moment.Moment, format: string) {
59+
function timeTagHelper(this: LocalsType, date?: string | number | Date | moment.Moment, format?: string) {
6060
return `<time datetime="${toISOString(date)}">${this.date(date, format)}</time>`;
6161
}
6262

@@ -72,7 +72,7 @@ function getLanguage(ctx: LocalsType) {
7272
*
7373
* Moment defined locales: https://github.com/moment/moment/tree/master/locale
7474
*/
75-
function _toMomentLocale(lang: string) {
75+
function _toMomentLocale(lang?: string) {
7676
if (lang === undefined) {
7777
return undefined;
7878
}

lib/plugins/helper/debug.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { inspect } from 'util';
22

33
// this format object as string, resolves circular reference
4-
function inspectObject(object: any, options: boolean) {
4+
function inspectObject(object: any, options?: any) {
55
return inspect(object, options);
66
}
77

lib/plugins/helper/feed_tag.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ const feedFn = (str = '') => {
99

1010
interface Options {
1111
title?: string;
12-
type?: string;
12+
type?: string | null;
1313
}
1414

15-
function makeFeedTag(this: LocalsType, path: string, options: Options = {}, configFeed?: any, configTitle?: string) {
15+
function makeFeedTag(this: LocalsType, path?: string, options: Options = {}, configFeed?: any, configTitle?: string) {
1616
const title = options.title || configTitle;
1717

1818
if (path) {
@@ -47,7 +47,7 @@ function makeFeedTag(this: LocalsType, path: string, options: Options = {}, conf
4747
return '';
4848
}
4949

50-
function feedTagHelper(this: LocalsType, path: string, options: Options = {}) {
50+
function feedTagHelper(this: LocalsType, path?: string, options: Options = {}) {
5151
const { config } = this;
5252
return moize.deep(makeFeedTag.bind(this))(path, options, (config as any).feed, config.title);
5353
}

lib/plugins/helper/full_url_for.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
import { full_url_for } from 'hexo-util';
33
import type { LocalsType } from '../../types';
44

5-
export = function(this: LocalsType, path: string) {
5+
export = function(this: LocalsType, path?: string) {
66
return full_url_for.call(this, path);
77
}

lib/plugins/helper/image_tag.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { LocalsType } from '../../types';
33

44
interface Options {
55
src?: string;
6+
alt?: string;
67
class?: string | string[];
78
}
89

lib/plugins/helper/link_to.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { htmlTag, url_for } from 'hexo-util';
22
import type { LocalsType } from '../../types';
33

44
interface Options {
5+
id?: string;
56
href?: string;
67
title?: string;
78
external?: boolean | null;
@@ -20,7 +21,7 @@ interface Attrs {
2021
[key: string]: string | boolean | null | undefined;
2122
}
2223

23-
function linkToHelper(this: LocalsType, path: string, text: string, options: Options | boolean = {}) {
24+
function linkToHelper(this: LocalsType, path: string, text?: string, options: Options | boolean = {}) {
2425
if (typeof options === 'boolean') options = {external: options};
2526

2627
if (!text) text = path.replace(/^https?:\/\/|\/$/g, '');

lib/plugins/helper/list_archives.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { url_for } from 'hexo-util';
55
interface Options {
66
format?: string;
77
type?: string;
8-
style?: string;
8+
style?: string | false;
99
transform?: (name: string) => string;
1010
separator?: string;
1111
show_count?: boolean;

lib/plugins/helper/list_categories.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { CategorySchema, LocalsType } from '../../types';
33
import type Query from 'warehouse/dist/query';
44

55
interface Options {
6-
style?: string;
6+
style?: string | false;
77
class?: string;
88
depth?: number | string;
99
orderby?: string;
@@ -13,11 +13,9 @@ interface Options {
1313
transform?: (name: string) => string;
1414
separator?: string;
1515
suffix?: string;
16-
children_indicator?: boolean;
16+
children_indicator?: string | boolean;
1717
}
1818

19-
function listCategoriesHelper(this: LocalsType, options?: Options): string;
20-
function listCategoriesHelper(this: LocalsType, categories: Query<CategorySchema>, options?: Options): string;
2119
function listCategoriesHelper(this: LocalsType, categories?: Query<CategorySchema> | Options, options?: Options) {
2220
if (!options && (!categories || !Object.prototype.hasOwnProperty.call(categories, 'length'))) {
2321
options = categories as Options;

lib/plugins/helper/list_posts.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { LocalsType, PostSchema } from '../../types';
33
import type Query from 'warehouse/dist/query';
44

55
interface Options {
6-
style?: string;
6+
style?: string | false;
77
class?: string;
88
amount?: number;
99
orderby?: string;
@@ -12,8 +12,6 @@ interface Options {
1212
separator?: string;
1313
}
1414

15-
function listPostsHelper(this: LocalsType, options?: Options): string;
16-
function listPostsHelper(this: LocalsType, posts: Query<PostSchema>, options?: Options): string;
1715
function listPostsHelper(this: LocalsType, posts?: Query<PostSchema> | Options, options?: Options) {
1816
if (!options && (!posts || !Object.prototype.hasOwnProperty.call(posts, 'length'))) {
1917
options = posts as Options;

0 commit comments

Comments
 (0)