Skip to content

Commit de6d59e

Browse files
committed
Clean up
1 parent e63ac6f commit de6d59e

File tree

15 files changed

+222
-206
lines changed

15 files changed

+222
-206
lines changed

.eslintrc.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
################################################################################################
55
---
66
root: true
7-
# extends:
8-
# - "eslint:recommended"
9-
# - "plugin:@typescript-eslint/recommended"
7+
extends:
8+
- "eslint:recommended"
9+
- "plugin:@typescript-eslint/recommended"
1010
parser: "@typescript-eslint/parser"
1111
parserOptions:
1212
# ecmaVersion: 11

.vscode/settings.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* Copyright (c) Rowe Wilson Frederisk Holme. All rights reserved.
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
5-
65
// Place your settings in this file to overwrite default and user settings.
76
{
87
"files.exclude": {

package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -284,27 +284,27 @@
284284
"@types/bluebird": "^3.5.33",
285285
"@types/cheerio": "^0.22.28",
286286
"@types/glob": "^7.2.0",
287-
"@types/luxon": "^2.0.5",
287+
"@types/luxon": "^2.0.7",
288288
"@types/mocha": "^9.0.0",
289-
"@types/node": "^16.11.1",
289+
"@types/node": "^16.11.10",
290290
"@types/node-fetch": "^2.5.12",
291291
"@types/vscode": "^1.43.0",
292-
"@typescript-eslint/eslint-plugin": "^5.1.0",
293-
"@typescript-eslint/parser": "^5.1.0",
294-
"eslint": "^8.0.1",
292+
"@typescript-eslint/eslint-plugin": "^5.4.0",
293+
"@typescript-eslint/parser": "^5.4.0",
294+
"eslint": "^8.3.0",
295295
"glob": "^7.2.0",
296296
"js-yaml": "^4.1.0",
297297
"mocha": "^9.1.3",
298298
"mwbot": "^2.0.0",
299299
"ts-loader": "^9.2.3",
300-
"typescript": "^4.4.4",
300+
"typescript": "^4.5.2",
301301
"vscode-test": "^1.4.1",
302-
"webpack": "^5.59.1",
302+
"webpack": "^5.64.2",
303303
"webpack-cli": "^4.9.1"
304304
},
305305
"dependencies": {
306306
"cheerio": "^1.0.0-rc.9",
307-
"luxon": "^2.0.1",
307+
"luxon": "^2.1.1",
308308
"node-fetch": "^2.6.5"
309309
}
310-
}
310+
}

src/export_command/cite_function/web.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class WebCiteInfo {
112112
}
113113

114114
private setPublishedDate(): void {
115-
const date =
115+
const date: string | undefined =
116116
this.getAttr("meta[property='article:published_time']") ||
117117
this.getAttr("time", "datetime");
118118
if (date) {
@@ -126,7 +126,7 @@ class WebCiteInfo {
126126
this.getAttr("meta[property='twitter:site']");
127127
}
128128

129-
private getAttr(ioName: string, attrName = "content"): string | undefined {
129+
private getAttr(ioName: string, attrName = 'content'): string | undefined {
130130
const io: cheerio.Cheerio = this.metaData(ioName);
131131
if (io.length) {
132132
return io.attr(attrName) || undefined;

src/export_command/host_function/host.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export async function getHost(): Promise<string | undefined> {
1111
// if host is existed, return it.
1212
if (host) { return host; }
1313
// else ask to edit
14-
const selection = await vscode.window.showWarningMessage(
14+
const selection: string | undefined = await vscode.window.showWarningMessage(
1515
`No Host Be Defined!
1616
You haven't defined the host of previewer yet, please input host value in the dialog box (or in settings) and try again.`
1717
, "Edit", "Cancel");

src/export_command/uri_function/viewPage.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import * as vscode from 'vscode';
22
import MWBot from 'mwbot';
33
import { getDefaultBot } from '../wikimedia_function/bot';
44
import { Action, alterNativeValues, Prop } from '../wikimedia_function/args';
5-
import { getView } from '../wikimedia_function/view';
5+
import { showViewer } from '../wikimedia_function/view';
66
import { isRemoteBot, parseArgs } from './uri';
77
import { getHost } from '../host_function/host';
88

99
export async function viewPage(query: string): Promise<void> {
10-
function setArgs(par: string, defaultValue?: string) {
10+
function setArgs(par: string, defaultValue?: string): void {
1111
args[par.toLowerCase()] = pars[par] ?? defaultValue;
1212
}
1313

@@ -24,7 +24,9 @@ export async function viewPage(query: string): Promise<void> {
2424
}
2525

2626
// TODO: getHost()
27-
const baseHref: string = isRemoteBot(pars) ? pars["TransferProtocol"] + pars["SiteHost"] + pars["APIPath"] : config.get("transferProtocol") + (await getHost() || '') + config.get("articlePath");
27+
const baseHref: string = isRemoteBot(pars)
28+
? pars["TransferProtocol"] + pars["SiteHost"] + pars["APIPath"]
29+
: config.get("transferProtocol") + (await getHost() || '') + config.get("articlePath");
2830

2931
// args value
3032
const args: Record<string, string> = { 'action': Action.parse };
@@ -34,11 +36,11 @@ export async function viewPage(query: string): Promise<void> {
3436
Prop.categoriesHTML,
3537
(config.get("getCss") ? Prop.headHTML : undefined)
3638
));
37-
const undefParNames = ['Text', 'Title', 'Summary', 'RevID', 'Page',
39+
const undefParNames: string[] = ['Text', 'Title', 'Summary', 'RevID', 'Page',
3840
'PageID', 'OldID', 'Redirects', 'OnlyPST', 'Section',
3941
'SectionTitle', 'UseSkin', 'ContentFormat', 'ContentModel'];
4042
undefParNames.forEach((value: string): void => setArgs(value));
4143
setArgs("PST", "true");
4244

43-
getView("pageViewer", "WikiViewer", args, tBot, baseHref);
45+
showViewer("pageViewer", "WikiViewer", args, tBot, baseHref);
4446
}

src/export_command/wikimedia_function/args.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
export function alterNativeValues(...values: (string | undefined)[]): string {
7-
values = values.filter(item => { return item !== undefined; });
7+
values = values.filter(
8+
(item: string | undefined): boolean => item !== undefined
9+
);
810
return values.join("|");
911
}
1012

src/export_command/wikimedia_function/bot.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ export async function login(): Promise<boolean> {
3232
});
3333
const barMessage: vscode.Disposable = vscode.window.setStatusBarMessage("Wikitext: Login...");
3434
try {
35-
const response = await bot.login(userInfo);
35+
const response: any = await bot.login(userInfo);
36+
// TODO:
3637
vscode.window.showInformationMessage(`User "${response.lgusername}"(UserID:"${response.lguserid}") Login Result is "${response.result}". Login Token is "${response.token}".`
3738
);
3839
return true;
@@ -58,7 +59,7 @@ export async function logout(): Promise<void> {
5859
});
5960
// clear bot
6061
bot = undefined;
61-
vscode.window.showInformationMessage('result: "Success"');
62+
vscode.window.showInformationMessage('result: Success');
6263
}
6364
catch (error) {
6465
showMWErrorMessage('logout', error);
@@ -89,34 +90,26 @@ export async function getLoggedInBot(): Promise<MWBot | undefined> {
8990
if (bot === undefined) {
9091
switch (config.get('autoLogin')) {
9192
case 'Always':
92-
if (!await login()) {
93-
// login failed
94-
return undefined;
95-
}
96-
break;
93+
return await login() ? bot : undefined;
9794
case 'Never':
9895
vscode.window.showWarningMessage('You are not logged in. Please log in and try again.');
9996
return undefined;
10097
case 'Ask me':
10198
default:
102-
const result: string | undefined = await vscode.window.showWarningMessage("You are not logged in. Do you want to login now?", 'Yes', 'No', 'Always', 'Never');
103-
switch (result) {
99+
switch (await vscode.window.showWarningMessage("You are not logged in. Do you want to login now?", 'Yes', 'No', 'Always', 'Never')) {
104100
case 'Always':
105101
config.update('autoLogin', 'Always', true);
102+
return await login() ? bot : undefined;
106103
case 'Yes':
107-
if (!await login()) {
108-
// login failed
109-
return undefined;
110-
}
111-
break;
104+
return await login() ? bot : undefined;
112105
case 'Never':
113106
config.update('autoLogin', 'Never', true);
107+
return undefined;
114108
case 'No':
115109
case undefined:
116110
default:
117111
return undefined;
118112
}
119-
break;
120113
}
121114
}
122115
return bot;

src/export_command/wikimedia_function/err_msg.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as vscode from 'vscode';
22
import { instanceOfMWError } from '../../interface_definition/commonInterface';
33

4-
export function showMWErrorMessage(name: string, error: unknown, moreInfo: string = ''): void {
4+
export function showMWErrorMessage(name: string, error: unknown, moreInfo = ''): void {
55
if (instanceOfMWError(error)) {
66
vscode.window.showErrorMessage(`ErrorCode: ${error.code}; ErrorInfo: ${error.info}; ${moreInfo}`.trim());
77
} else if (error instanceof Error) {

src/export_command/wikimedia_function/page.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import * as vscode from 'vscode';
7-
import type Bluebird from 'bluebird';
87
import type MWBot from 'mwbot';
98
import { Action, Prop, RvProp, alterNativeValues, List } from './args';
109
import { ReadPageConvert, ReadPageResult, Main, Revision, Jump, Page } from '../../interface_definition/readPageInterface';
@@ -30,7 +29,7 @@ export async function postPage(): Promise<void> {
3029
meta: 'tokens',
3130
type: 'csrf'
3231
};
33-
const result: Bluebird<unknown> = await bot.request(args);
32+
const result: unknown = await bot.request(args);
3433
const reNew: TokensResult = TokensConvert.toTokensResult(result);
3534
const token: string | undefined = reNew.query?.tokens?.csrftoken;
3635
if (token) {
@@ -46,7 +45,7 @@ export async function postPage(): Promise<void> {
4645
action: "tokens",
4746
type: "edit"
4847
};
49-
const result = await bot.request(args);
48+
const result: unknown = await bot.request(args);
5049
const reOld: OldTokensResult = OldTokensConvert.toOldTokensResult(result);
5150
const token: string | undefined = reOld.tokens?.edittoken;
5251
if (token) {
@@ -58,7 +57,7 @@ export async function postPage(): Promise<void> {
5857
}
5958
}
6059

61-
const error = Error('Could not get edit token:' +
60+
const error: Error = new Error('Could not get edit token:' +
6261
' NEW: ' + ((errors[0] instanceof Error) ? errors[0].message : '') +
6362
' OLD: ' + ((errors[1] instanceof Error) ? errors[1].message : ''));
6463
throw error;
@@ -111,7 +110,7 @@ export async function postPage(): Promise<void> {
111110
// tags: 'WikitextExtensionForVSCode',
112111
token: await getEditToken(tBot)
113112
};
114-
const wikitextTag: string = 'AWB';
113+
const wikitextTag = 'WikitextExtensionForVSCode';
115114
const tagList: string[] = await getValidTagList(tBot);
116115
if (tagList.includes(wikitextTag)) {
117116
args['tags'] = wikitextTag;
@@ -175,19 +174,19 @@ export async function pullPage(): Promise<void> {
175174
vscode.window.showTextDocument(document);
176175
}
177176

178-
export function closeEditor() {
177+
export function closeEditor(): Thenable<void | undefined> | undefined {
179178
const editor: vscode.TextEditor | undefined = vscode.window.activeTextEditor;
180179

181-
// Delete all text
182-
editor?.edit((editBuilder: vscode.TextEditorEdit) =>
180+
return editor?.edit((editBuilder: vscode.TextEditorEdit): void =>
181+
// delete all text
183182
editBuilder.delete(
184-
new vscode.Range( // All
185-
new vscode.Position(0, 0), // Start
186-
editor.document.lineAt(editor.document.lineCount - 1).rangeIncludingLineBreak.end // End
183+
new vscode.Range( // the range of all document: from the beginning to the end
184+
new vscode.Position(0, 0), // beginning
185+
editor.document.lineAt(editor.document.lineCount - 1).rangeIncludingLineBreak.end // end
187186
)
188187
)
189-
).then(() =>
190-
// Close editor
188+
).then((): Thenable<void | undefined> =>
189+
// close the activate editor
191190
vscode.commands.executeCommand('workbench.action.closeActiveEditor')
192191
);
193192
}
@@ -220,7 +219,7 @@ ${infoLine}
220219
const barMessage: vscode.Disposable = vscode.window.setStatusBarMessage("Wikitext: Getting code...");
221220
try {
222221
// get request result
223-
const result = await tBot.request(args);
222+
const result: unknown = await tBot.request(args);
224223
// console.log(result);
225224
// Convert result as class
226225
const re: ReadPageResult = ReadPageConvert.toReadPageResult(result);
@@ -328,8 +327,8 @@ async function getValidTagList(tBot: MWBot): Promise<string[]> {
328327

329328
const tagList: string[] = [];
330329
// TODO: interface
331-
do {
332-
const result = await tBot.request(args);
330+
for (; ;) {
331+
const result: any = await tBot.request(args);
333332
const tags: any[] = result.query.tags;
334333
tagList.push(
335334
...tags.filter(tag =>
@@ -339,7 +338,7 @@ async function getValidTagList(tBot: MWBot): Promise<string[]> {
339338
Object.keys(result.continue)
340339
.forEach(key => args[key] = result.continue[key]);
341340
} else { break; }
342-
} while (true);
341+
}
343342

344343
return tagList;
345344
}

src/export_command/wikimedia_function/view.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export async function getPreview(): Promise<void> {
6666

6767
const baseHref: string = config.get("transferProtocol") + host + config.get("articlePath");
6868

69-
getView(previewCurrentPanel, viewerTitle, args, tBot, baseHref);
69+
showViewer(previewCurrentPanel, viewerTitle, args, tBot, baseHref);
7070
}
7171

7272
export async function getPageView(): Promise<void> {
@@ -82,9 +82,9 @@ export async function getPageView(): Promise<void> {
8282
if (!pageTitle) { return undefined; }
8383

8484
const args: Record<string, string> = {
85-
'action': Action.parse,
86-
'page': pageTitle,
87-
'prop': alterNativeValues(
85+
action: Action.parse,
86+
page: pageTitle,
87+
prop: alterNativeValues(
8888
Prop.text,
8989
Prop.displayTitle,
9090
Prop.categoriesHTML,
@@ -102,7 +102,7 @@ export async function getPageView(): Promise<void> {
102102

103103
const baseHref: string = config.get("transferProtocol") + host + config.get("articlePath");
104104

105-
getView("pageViewer", "WikiViewer", args, tBot, baseHref);
105+
showViewer("pageViewer", "WikiViewer", args, tBot, baseHref);
106106
}
107107

108108
/**
@@ -114,21 +114,21 @@ export async function getPageView(): Promise<void> {
114114
* @param baseURI url base
115115
* @returns task
116116
*/
117-
export async function getView(currentPanel: vscode.WebviewPanel | string, viewerTitle: string, args: Record<string, string>, tBot: MWBot, baseURI: string): Promise<void> {
117+
export async function showViewer(currentPanel: vscode.WebviewPanel | string, viewerTitle: string, args: Record<string, string>, tBot: MWBot, baseURI: string): Promise<void> {
118118
const config: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("wikitext");
119119

120120
const barMessage: vscode.Disposable = vscode.window.setStatusBarMessage("Wikitext: Getting view...");
121121
try {
122-
const result = await tBot.request(args);
123-
const re: GetViewResult = ViewConverter.getViewResultToJson(result);
122+
const result: unknown = await tBot.request(args);
123+
const re: GetViewResult = ViewConverter.toGetViewResult(result);
124124
if (!re.parse) { return undefined; }
125125

126126
const baseElem = `<base href="${baseURI}" />"`;
127127

128128
const style = `<style>${config.get("previewCssStyle")}</style>`;
129129

130130
const htmlHead: string = re.parse.headhtml?.["*"]?.replace("<head>", "<head>" + baseElem + style) ?? `<!DOCTYPE html><html><head>${baseElem + style}</head><body>`;
131-
const htmlText: string = re.parse.text?.["*"] || "";
131+
const htmlText: string = re.parse.text?.["*"] ?? '';
132132
const htmlCategories: string = re.parse.categorieshtml?.["*"] ? "<hr />" + re.parse.categorieshtml?.["*"] : "";
133133
const htmlEnd = "</body></html>";
134134

src/interface_definition/commonInterface.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { u, o, r } from "./convertFunction";
1+
import { u, o, r, TypeMap } from "./convertFunction";
22
//#region Error
33
export interface MWError {
44
code: string;
@@ -11,7 +11,7 @@ export const mWErrorTypeMapInline: { json: string; js: string; typ: any; } = {
1111
};
1212

1313
/* eslint-disable @typescript-eslint/naming-convention */
14-
export const mWErrorTypeMapOutline: any = {
14+
export const mWErrorTypeMapOutline: TypeMap = {
1515
"MWError": o([
1616
{ json: "code", js: "code", typ: "" },
1717
{ json: "info", js: "info", typ: "" },
@@ -20,7 +20,7 @@ export const mWErrorTypeMapOutline: any = {
2020
};
2121

2222
export function instanceOfMWError(o: any): o is MWError {
23-
return ('code ' in o) && ('info ' in o) && ('*' in o);
23+
return ('code' in o) && ('info' in o) && ('*' in o);
2424
}
2525

2626
//#endregion Error
@@ -51,4 +51,4 @@ export const mWWarningsTypeMapOutline: any = {
5151
export function instanceOfMWWarnings(o: any): o is MWWarnings {
5252
return 'main' in o;
5353
}
54-
//#endregion Warnings
54+
//#endregion Warnings

0 commit comments

Comments
 (0)