Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrate typescript #5417

Merged
merged 6 commits into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .mocharc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ reporter: spec
ui: bdd
full-trace: true
exit: true
parallel: true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why parallel was disabled?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried keeping parallel, but then tests containing async on my computer throw exceptions, and I don't know why
image
image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parallel makes it easier to make tests timeout in my experience

8 changes: 4 additions & 4 deletions lib/extend/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import type { NodeJSLikeCallback } from '../types';

interface BaseObj {
path: string;
data: any;
layout?: string;
data?: any;
layout?: string | string[];
}
type ReturnType = BaseObj | BaseObj[];
type GeneratorReturnType = ReturnType | Promise<ReturnType>;

interface GeneratorFunction {
(locals: object, callback?: NodeJSLikeCallback<any>): GeneratorReturnType;
(locals: any, callback?: NodeJSLikeCallback<any>): GeneratorReturnType;
}

type StoreFunctionReturn = Promise<ReturnType>;

interface StoreFunction {
(locals: object): StoreFunctionReturn;
(locals: any): StoreFunctionReturn;
}

interface Store {
Expand Down
6 changes: 3 additions & 3 deletions lib/hexo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ class Hexo extends EventEmitter {
});
}

exit(err?: Error): Promise<void> {
exit(err?: any): Promise<void> {
if (err) {
this.log.fatal(
{ err },
Expand All @@ -674,11 +674,11 @@ class Hexo extends EventEmitter {
});
}

execFilter(type: string, data: any, options) {
execFilter(type: string, data: any, options?) {
return this.extend.filter.exec(type, data, options);
}

execFilterSync(type: string, data: any, options) {
execFilterSync(type: string, data: any, options?) {
return this.extend.filter.execSync(type, data, options);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/hexo/multi_config_path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: string): string {
export = (ctx: Hexo) => function multiConfigPath(base: string, configPaths?: string, outputDir?: string): string {
const { log } = ctx;
const defaultPath = join(base, '_config.yml');

Expand Down
11 changes: 7 additions & 4 deletions lib/hexo/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,9 @@ interface Result {
}

interface PostData {
title?: string;
title?: string | number;
layout?: string;
slug?: string;
slug?: string | number;
path?: string;
[prop: string]: any;
}
Expand Down Expand Up @@ -333,7 +333,10 @@ class Post {
});
}

publish(data: PostData, replace: boolean, callback?: NodeJSLikeCallback<Result>) {
publish(data: PostData, replace?: boolean);
publish(data: PostData, callback?: NodeJSLikeCallback<Result>);
publish(data: PostData, replace: boolean, callback?: NodeJSLikeCallback<Result>);
publish(data: PostData, replace?: boolean | NodeJSLikeCallback<Result>, callback?: NodeJSLikeCallback<Result>) {
if (!callback && typeof replace === 'function') {
callback = replace;
replace = false;
Expand Down Expand Up @@ -366,7 +369,7 @@ class Post {
data.content = data._content;
data._content = undefined;

return this.create(data, replace);
return this.create(data, replace as boolean);
}).then(post => {
result.path = post.path;
result.content = post.content;
Expand Down
4 changes: 3 additions & 1 deletion lib/hexo/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ class Render {
return this.getRenderer(ext, true);
}

render(data: StoreFunctionData, options?: { highlight?: boolean; }, callback?: NodeJSLikeCallback<any>): Promise<any> {
render(data: StoreFunctionData, callback?: NodeJSLikeCallback<any>): Promise<any>;
render(data: StoreFunctionData, options: any, callback?: NodeJSLikeCallback<any>): Promise<any>;
render(data: StoreFunctionData, options?: any | NodeJSLikeCallback<any>, callback?: NodeJSLikeCallback<any>): Promise<any> {
D-Sketon marked this conversation as resolved.
Show resolved Hide resolved
if (!callback && typeof options === 'function') {
callback = options;
options = {};
Expand Down
4 changes: 2 additions & 2 deletions lib/hexo/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class RouteStream extends Readable {
}
}

const _format = (path: string): string => {
const _format = (path?: string): string => {
path = path || '';
if (typeof path !== 'string') throw new TypeError('path must be a string!');

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

format(path: string): string {
format(path?: string): string {
return _format(path);
}

Expand Down
22 changes: 11 additions & 11 deletions lib/models/types/moment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
super(name, options);
}

cast(value, data) {
cast(value?, data?) {
value = super.cast(value, data);
if (value == null) return value;

Expand All @@ -29,7 +29,7 @@
return value;
}

validate(value, data) {
validate(value, data?) {
value = super.validate(value, data);
if (value == null) return value;

Expand All @@ -42,11 +42,11 @@
return value;
}

match(value, query, data) {
match(value, query, data?) {

Check warning on line 45 in lib/models/types/moment.ts

View workflow job for this annotation

GitHub Actions / linter

'data' is defined but never used
return value ? value.valueOf() === query.valueOf() : false;
}

compare(a, b) {
compare(a?, b?) {
if (a) {
if (b) return a - b;
return 1;
Expand All @@ -56,33 +56,33 @@
return 0;
}

parse(value) {
parse(value?) {
if (value) return toMoment(value);
}

value(value, data) {
value(value?, data?) {
// FIXME: Same as above. Also a dirty hack.
return value ? value._d.toISOString() : value;
}

q$day(value, query, data) {
q$day(value, query, data?) {
return value ? value.date() === query : false;
}

q$month(value, query, data) {
q$month(value, query, data?) {
return value ? value.month() === query : false;
}

q$year(value, query, data) {
q$year(value, query, data?) {
return value ? value.year() === query : false;
}

u$inc(value, update, data) {
u$inc(value, update, data?) {
if (!value) return value;
return value.add(update);
}

u$dec(value, update, data) {
u$dec(value, update, data?) {
if (!value) return value;
return value.subtract(update);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/filter/new_post_path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const reservedKeys = {
hash: true
};

function newPostPathFilter(this: Hexo, data: PostSchema = {}, replace: boolean): Promise<string> {
function newPostPathFilter(this: Hexo, data: PostSchema = {}, replace?: boolean): Promise<string> {
const sourceDir = this.source_dir;
const draftDir = join(sourceDir, '_drafts');
const postDir = join(sourceDir, '_posts');
Expand Down
14 changes: 7 additions & 7 deletions lib/plugins/helper/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function getMoment(date: moment.MomentInput | moment.Moment, lang: string, timez
return date;
}

function toISOString(date: string | number | Date | moment.Moment) {
function toISOString(date?: string | number | Date | moment.Moment) {
if (date == null) {
return new Date().toISOString();
}
Expand All @@ -29,19 +29,19 @@ function toISOString(date: string | number | Date | moment.Moment) {
return new Date(date as (string | number)).toISOString();
}

function dateHelper(this: LocalsType, date: moment.Moment | moment.MomentInput, format?: string) {
function dateHelper(this: LocalsType, date?: moment.Moment | moment.MomentInput, format?: string) {
const { config } = this;
const moment = getMoment(date, getLanguage(this), config.timezone);
return moment.format(format || config.date_format);
}

function timeHelper(this: LocalsType, date: moment.Moment | moment.MomentInput, format?: string) {
function timeHelper(this: LocalsType, date?: moment.Moment | moment.MomentInput, format?: string) {
const { config } = this;
const moment = getMoment(date, getLanguage(this), config.timezone);
return moment.format(format || config.time_format);
}

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

function relativeDateHelper(this: LocalsType, date: moment.Moment | moment.MomentInput) {
function relativeDateHelper(this: LocalsType, date?: moment.Moment | moment.MomentInput) {
const { config } = this;
const moment = getMoment(date, getLanguage(this), config.timezone);
return moment.fromNow();
}

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

Expand All @@ -72,7 +72,7 @@ function getLanguage(ctx: LocalsType) {
*
* Moment defined locales: https://github.com/moment/moment/tree/master/locale
*/
function _toMomentLocale(lang: string) {
function _toMomentLocale(lang?: string) {
if (lang === undefined) {
return undefined;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/helper/debug.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { inspect } from 'util';

// this format object as string, resolves circular reference
function inspectObject(object: any, options: boolean) {
function inspectObject(object: any, options?: any) {
return inspect(object, options);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/plugins/helper/feed_tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ const feedFn = (str = '') => {

interface Options {
title?: string;
type?: string;
type?: string | null;
}

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

if (path) {
Expand Down Expand Up @@ -47,7 +47,7 @@ function makeFeedTag(this: LocalsType, path: string, options: Options = {}, conf
return '';
}

function feedTagHelper(this: LocalsType, path: string, options: Options = {}) {
function feedTagHelper(this: LocalsType, path?: string, options: Options = {}) {
const { config } = this;
return moize.deep(makeFeedTag.bind(this))(path, options, (config as any).feed, config.title);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/helper/full_url_for.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
import { full_url_for } from 'hexo-util';
import type { LocalsType } from '../../types';

export = function(this: LocalsType, path: string) {
export = function(this: LocalsType, path?: string) {
return full_url_for.call(this, path);
}
1 change: 1 addition & 0 deletions lib/plugins/helper/image_tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { LocalsType } from '../../types';

interface Options {
src?: string;
alt?: string;
class?: string | string[];
}

Expand Down
3 changes: 2 additions & 1 deletion lib/plugins/helper/link_to.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { htmlTag, url_for } from 'hexo-util';
import type { LocalsType } from '../../types';

interface Options {
id?: string;
href?: string;
title?: string;
external?: boolean | null;
Expand All @@ -20,7 +21,7 @@ interface Attrs {
[key: string]: string | boolean | null | undefined;
}

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

if (!text) text = path.replace(/^https?:\/\/|\/$/g, '');
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/helper/list_archives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { url_for } from 'hexo-util';
interface Options {
format?: string;
type?: string;
style?: string;
style?: string | false;
transform?: (name: string) => string;
separator?: string;
show_count?: boolean;
Expand Down
6 changes: 2 additions & 4 deletions lib/plugins/helper/list_categories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { CategorySchema, LocalsType } from '../../types';
import type Query from 'warehouse/dist/query';

interface Options {
style?: string;
style?: string | false;
class?: string;
depth?: number | string;
orderby?: string;
Expand All @@ -13,11 +13,9 @@ interface Options {
transform?: (name: string) => string;
separator?: string;
suffix?: string;
children_indicator?: boolean;
children_indicator?: string | boolean;
}

function listCategoriesHelper(this: LocalsType, options?: Options): string;
function listCategoriesHelper(this: LocalsType, categories: Query<CategorySchema>, options?: Options): string;
function listCategoriesHelper(this: LocalsType, categories?: Query<CategorySchema> | Options, options?: Options) {
if (!options && (!categories || !Object.prototype.hasOwnProperty.call(categories, 'length'))) {
options = categories as Options;
Expand Down
4 changes: 1 addition & 3 deletions lib/plugins/helper/list_posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { LocalsType, PostSchema } from '../../types';
import type Query from 'warehouse/dist/query';

interface Options {
style?: string;
style?: string | false;
class?: string;
amount?: number;
orderby?: string;
Expand All @@ -12,8 +12,6 @@ interface Options {
separator?: string;
}

function listPostsHelper(this: LocalsType, options?: Options): string;
function listPostsHelper(this: LocalsType, posts: Query<PostSchema>, options?: Options): string;
function listPostsHelper(this: LocalsType, posts?: Query<PostSchema> | Options, options?: Options) {
if (!options && (!posts || !Object.prototype.hasOwnProperty.call(posts, 'length'))) {
options = posts as Options;
Expand Down
Loading
Loading