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

stricter code checks (TypeScript and Lint) #404

Merged
merged 33 commits into from
Aug 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
fa995ce
stricter code checks (TypeScript and Lint)
danielweck Jul 10, 2019
8e78c19
Merge branch 'develop' into feature/stricterCode
danielweck Jul 11, 2019
d5b4b99
lint fixes: prefer-for-of and forin rules
danielweck Jul 24, 2019
289fcb2
disable lint code ordering
danielweck Jul 24, 2019
c692d57
added tslint language service (TS compiler hook)
danielweck Jul 24, 2019
9c593ed
noImplicitThis TypeScript rule
danielweck Jul 24, 2019
fb0046e
Merge branch 'develop' into feature/stricterCode
danielweck Jul 24, 2019
1638657
strictFunctionTypes fixes
danielweck Jul 24, 2019
e242a3f
noImplicitReturns fixes, as well as floating promises, missing async,…
danielweck Jul 24, 2019
6b7d152
noUnusedLocals fixes (many!)
danielweck Jul 24, 2019
cd3968d
noUnusedParameters fixes
danielweck Jul 24, 2019
17c4f14
TSLint and TypeScript strict compiler passes, except for
danielweck Jul 24, 2019
a23a31c
strictNullChecks fixes in unit tests, and various other linting (code…
danielweck Jul 24, 2019
6eb74a2
further strictNullChecks fixes in unit tests
danielweck Jul 24, 2019
4f50b20
last safe / consensual commit before strictNullChecks (which requires…
danielweck Jul 24, 2019
1e8a5ea
replaced any with proper OPDS type in API code
danielweck Jul 24, 2019
d617773
Merge branch 'develop' into feature/stricterCode
danielweck Jul 29, 2019
d13f15b
compile error fixed
danielweck Jul 29, 2019
bc572e4
Merge branch 'develop' into feature/stricterCode
danielweck Jul 29, 2019
d7ba053
fixed TS lint errors
danielweck Jul 29, 2019
b1206f3
strongly-typed IPC messages (win and sync)
danielweck Jul 30, 2019
88cae1f
strongly-typed i18n / l10n / translator action (locale payload)
danielweck Jul 30, 2019
afdf5d8
Merge branch 'develop' into feature/stricterCode
danielweck Jul 31, 2019
6130771
Merge branch 'develop' into feature/stricterCode
danielweck Aug 1, 2019
4a06ef0
merge conflict solved
danielweck Aug 1, 2019
2ee24c6
removed unused import
danielweck Aug 1, 2019
8d2797f
Merge branch 'develop' into feature/stricterCode
danielweck Aug 1, 2019
45d0760
Merge branch 'develop' into feature/stricterCode
danielweck Aug 1, 2019
305addf
Merge branch 'develop' into feature/stricterCode
danielweck Aug 2, 2019
3f5a263
Merge branch 'develop' into feature/stricterCode
danielweck Aug 2, 2019
79dbc9e
fixed the TypeScript types for httpGet() (promisify)
danielweck Aug 2, 2019
b454ee6
Merge branch 'develop' into feature/stricterCode
danielweck Aug 6, 2019
51a6da5
Merge branch 'develop' into feature/stricterCode
danielweck Aug 6, 2019
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
51 changes: 51 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"version": "2.0.0",
"isBackground": false,
"presentation": {
"reveal": "always",
"panel": "new"
},
"env": {
"DEBUG": "*",
"NODE_DEBUG": "1",
"NODE_ENV": "development"
},
"tasks": [
{
"label": "build",
"group": "build",
"type": "shell",
"command": "npm",
"args": [
"run",
"build"
],
"problemMatcher": [
"$tsc",
"$eslint-stylish"
],
"presentation": {
"reveal": "always",
"panel": "shared"
}
},
{
"label": "lint",
"group": "build",
"type": "shell",
"command": "npm",
"args": [
"run",
"lint"
],
"problemMatcher": [
"$tsc",
"$eslint-stylish"
],
"presentation": {
"reveal": "always",
"panel": "shared"
}
}
]
}
33 changes: 33 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"build": "cross-env NODE_ENV=production webpack --config webpack.config.js && ncp src/package.json dist/package.json",
"build:dev:main": "webpack --config webpack.config.main.js",
"start": "npm run build && cross-env DEBUG=r2:*,readium-desktop:* electron .",
"lint:ts": "tslint './src/**/*.ts' './src/**/*.tsx'",
"lint:ts": "tslint --project tsconfig.json -c tslint.json './src/**/*.ts' './src/**/*.tsx' './test/**/*.ts' './test/**/*.tsx'",
"lint:editorconfig": "eclint check '**/*' '!.vscode/**/*' '!.git/**/*' '!node_modules/**/*' '!resources/**/*' '!src/renderer/assets/**/*' '!dist/**/*' '!**/.DS_Store' '!src/typings/en.translation.d.ts'",
"lint": "npm run lint:editorconfig && npm run lint:ts",
"start:dev:renderer-reader": "concurrently --kill-others \"npm run start:dev:renderer\" \"npm run start:dev:reader\"",
Expand Down Expand Up @@ -270,6 +270,7 @@
"svg-sprite-loader": "^3.9.2",
"ts-jest": "^23.10.5",
"tslint": "^5.16.0",
"tslint-language-service": "^0.9.9",
"typescript": "^3.4.3",
"webpack": "^4.30.0",
"webpack-cli": "^3.1.2",
Expand Down
15 changes: 12 additions & 3 deletions src/common/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@
// that can be found in the LICENSE file exposed on Github (readium) in the project repository.
// ==LICENSE-END==

export class CodeError extends Error {
public static fromJson(json: any): CodeError {
export interface CodeMessage {
code: number | string;
message: string;
}

export interface CodeMessageWithClass extends CodeMessage {
class: string;
}

export class CodeError extends Error implements CodeMessage {
public static fromJson(json: CodeMessage): CodeError {
return new CodeError(
json.code,
json.message,
Expand All @@ -22,7 +31,7 @@ export class CodeError extends Error {
this.message = message;
}

public toJson() {
public toJson(): CodeMessageWithClass {
return {
class: "CodeError",
code: this.code,
Expand Down
12 changes: 12 additions & 0 deletions src/common/ipc/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,21 @@
* Synchronization of redux actions
*/

import { Action } from "../models/redux";

import { WindowSender } from "../models/sync";

export enum EventType {
RendererAction = "RENDERER_ACTION",
MainAction = "MAIN_ACTION",
}

export const CHANNEL = "SYNC";

export interface EventPayload {
type: EventType;
payload: {
action: Action;
};
sender: WindowSender;
}
7 changes: 7 additions & 0 deletions src/common/ipc/win.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ export enum EventType {
}

export const CHANNEL = "WIN";

export interface EventPayload {
type: EventType;
payload: {
winId: string;
};
}
8 changes: 0 additions & 8 deletions src/common/models/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@ import { Catalog } from "./catalog";
import { Download } from "./download";
import { Publication } from "./publication";

interface LibraryImportMessage {
paths: string[]; // List of files to import
}

interface UIMessage {
message: string;
}

export interface UrlMessage {
url: string;
}
Expand Down
1 change: 0 additions & 1 deletion src/common/models/publication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import { Contributor } from "./contributor";
import { CustomCover } from "./custom-cover";
import { Downloadable } from "./downloadable";
import { File } from "./file";
import { Identifiable } from "./identifiable";
import { Language } from "./language";
Expand Down
11 changes: 11 additions & 0 deletions src/common/models/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,18 @@
// that can be found in the LICENSE file exposed on Github (readium) in the project repository.
// ==LICENSE-END==

import { Action } from "redux";

export enum SenderType {
Main, // Main process
Renderer, // Renderer process
}

export interface WindowSender {
type: SenderType;
winId: string;
}

export interface ActionWithSender extends Action {
sender: WindowSender;
}
10 changes: 9 additions & 1 deletion src/common/redux/actions/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ export enum ActionType {
Set = "LOCALE_SET",
}

export function setLocale(locale: string): Action {
export interface PayloadLocale {
locale: string;
}

export interface ActionLocale extends Action {
payload: PayloadLocale;
}

export function setLocale(locale: string): ActionLocale {
return {
type: ActionType.Set,
payload: {
Expand Down
6 changes: 2 additions & 4 deletions src/common/redux/reducers/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
// that can be found in the LICENSE file exposed on Github (readium) in the project repository.
// ==LICENSE-END==

import { Action } from "readium-desktop/common/models/redux";

import { i18nActions } from "readium-desktop/common/redux/actions";

import { I18NState } from "readium-desktop/common/redux/states/i18n";
Expand All @@ -17,13 +15,13 @@ const initialState: I18NState = {

export function i18nReducer(
state: I18NState = initialState,
action: Action,
action: i18nActions.ActionLocale,
): I18NState {
switch (action.type) {
case i18nActions.ActionType.Set:
return Object.assign({}, state, {
locale: action.payload.locale,
});
} as I18NState);
default:
return state;
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/services/opds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class OPDSParser {
/**
* Parse OPDS feed and returns a catalog
*/
public parse(opdsFeed: string): Promise<Catalog> {
public async parse(opdsFeed: string): Promise<Catalog> {
return this.parser
.parse(opdsFeed)
.then((feed: AcquisitionFeed) => {
Expand Down
20 changes: 11 additions & 9 deletions src/common/services/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,25 @@ import { injectable} from "inversify";

import { CodeError } from "readium-desktop/common/errors";

import { Action } from "../models/redux";

@injectable()
export class ActionSerializer {
public serialize(action: any) {
public serialize(action: Action): Action {
if (action.error && action.payload instanceof CodeError) {
return Object.assign(
{},
action,
{
payload: action.payload.toJson(),
},
payload: (action.payload as CodeError).toJson(),
} as Action,
);
} else {
return action;
}
}

public deserialize(json: any): any {
public deserialize(json: Action): Action {
if (json.error &&
json.payload &&
json.payload.class &&
Expand All @@ -35,11 +37,11 @@ export class ActionSerializer {
{},
json,
{
payload: new CodeError(
json.payload.code,
json.payload.message,
),
},
payload: CodeError.fromJson({
code: json.payload.code,
message: json.payload.message,
}),
} as Action,
);
} else {
return json;
Expand Down
14 changes: 10 additions & 4 deletions src/common/utils/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ import { Store } from "redux";
import * as request from "request";
import { promisify } from "util";

type TRequestCoreOptionsRequiredUriUrl = request.CoreOptions & request.RequiredUriUrl;
type TRequestCoreOptionsOptionalUriUrl = request.CoreOptions & request.OptionalUriUrl;

/**
* @param url url of your GET request
* @param options request options
* @returns body of url response. 'String' type returned in many cases except for options.json = true
*/
// tslint:disable-next-line: max-line-length
export async function httpGet<T extends JsonMap | string = string>(url: string, options?: request.CoreOptions): Promise<T> {
options = options || {};
export async function httpGet<T extends JsonMap | string = string>(url: string, options?: TRequestCoreOptionsOptionalUriUrl): Promise<T> {
options = options || {} as TRequestCoreOptionsOptionalUriUrl;
options.headers = options.headers || {};

const headerFromOptions = {};
Expand All @@ -44,8 +47,11 @@ export async function httpGet<T extends JsonMap | string = string>(url: string,
encoding: undefined,
headers,
},
);
const response = await promisify<request.CoreOptions, request.Response>(request)(requestOptions);
) as TRequestCoreOptionsRequiredUriUrl;

const promisifiedRequest = promisify<TRequestCoreOptionsRequiredUriUrl, request.Response>(request);
const response = await promisifiedRequest(requestOptions);

if (!response) {
throw new Error(`No HTTP response?! ${url}`);
}
Expand Down
Loading