refactor: enable strict TypeScript options

This commit is contained in:
Simon Chan 2022-02-02 19:14:51 +08:00
parent 594648d033
commit 55cd69d47b
15 changed files with 82 additions and 104 deletions

View file

@ -207,7 +207,7 @@ function _Connect(): JSX.Element | null {
return {
items,
};
}, [addWsBackend, addTcpBackend]);
}, [addUsbBackend, addWsBackend, addTcpBackend]);
return (
<Stack

View file

@ -32,6 +32,7 @@
},
"dependencies": {
"@yume-chan/async": "^2.1.4",
"@yume-chan/dataview-bigint-polyfill": "0.0.10",
"@yume-chan/event": "^0.0.10",
"@yume-chan/struct": "^0.0.10",
"tslib": "^2.3.1"

View file

@ -165,7 +165,7 @@ export class Adb {
const pieces = banner.split('::');
if (pieces.length > 1) {
const props = pieces[1];
const props = pieces[1]!;
for (const prop of props.split(';')) {
if (!prop) {
continue;
@ -188,7 +188,7 @@ export class Adb {
this._device = value;
break;
case AdbPropKey.Features:
this._features = value.split(',') as AdbFeatures[];
this._features = value!.split(',') as AdbFeatures[];
break;
}
}

View file

@ -78,7 +78,7 @@ export class AdbReverseCommand extends AutoDisposable {
const response = await AdbReverseStringResponse.deserialize(stream);
return response.content!.split('\n').map(line => {
const [deviceSerial, localName, remoteName] = line.split(' ');
const [deviceSerial, localName, remoteName] = line.split(' ') as [string, string, string];
return { deviceSerial, localName, remoteName };
});

View file

@ -72,7 +72,7 @@ export class AdbShellProtocol implements AdbShell {
this.stderrEvent.fire(packet.data);
break;
case AdbShellProtocolId.Exit:
this.exitEvent.fire(new Uint8Array(packet.data)[0]);
this.exitEvent.fire(new Uint8Array(packet.data)[0]!);
break;
case AdbShellProtocolId.CloseStdin:
case AdbShellProtocolId.Stdin:

View file

@ -51,7 +51,7 @@ export async function adbSyncReadResponse<T extends Record<string, StructLike<an
}
if (types[id]) {
return types[id].deserialize(stream);
return types[id]!.deserialize(stream);
}
throw new Error('Unexpected response id');

View file

@ -1,8 +1,9 @@
import { getBigUint64, setBigUint64 } from '@yume-chan/dataview-bigint-polyfill/esm/fallback';
const BigInt0 = BigInt(0);
const BigInt1 = BigInt(1);
const BigInt2 = BigInt(2);
const BigInt2To64 = BigInt2 ** BigInt(64);
const BigInt64 = BigInt(64);
export function getBig(
buffer: ArrayBuffer,
@ -17,8 +18,8 @@ export function getBig(
// Support for arbitrary length can be easily added
for (let i = offset; i < offset + length; i += 8) {
result *= BigInt2To64;
const value = view.getBigUint64(i, false);
result <<= BigInt64;
const value = getBigUint64(view, i, false);
result += value;
}
@ -29,12 +30,12 @@ export function setBig(buffer: ArrayBuffer, value: bigint, offset: number = 0) {
const uint64Array: bigint[] = [];
while (value > BigInt0) {
uint64Array.push(BigInt.asUintN(64, value));
value /= BigInt2To64;
value >>= BigInt64;
}
const view = new DataView(buffer);
for (let i = uint64Array.length - 1; i >= 0; i -= 1) {
view.setBigUint64(offset, uint64Array[i], false);
setBigUint64(view, offset, uint64Array[i]!, false);
offset += 8;
}
}
@ -42,9 +43,9 @@ export function setBig(buffer: ArrayBuffer, value: bigint, offset: number = 0) {
export function setBigLE(buffer: ArrayBuffer, value: bigint, offset = 0) {
const view = new DataView(buffer);
while (value > BigInt0) {
view.setBigUint64(offset, value, true);
setBigUint64(view, offset, value, true);
offset += 8;
value /= BigInt2To64;
value >>= BigInt64;
}
}
@ -108,7 +109,7 @@ export function modInverse(a: number, m: number) {
let x = 1;
let y = 0;
for (let i = s.length - 2; i >= 0; --i) {
[x, y] = [y, x - y * Math.floor(s[i].a / s[i].b)];
[x, y] = [y, x - y * Math.floor(s[i]!.a / s[i]!.b)];
}
return (y % m + m) % m;
}

View file

@ -6,7 +6,6 @@ const paddingChar = '='.charCodeAt(0);
function addRange(start: string, end: string) {
const charCodeStart = start.charCodeAt(0);
const charCodeEnd = end.charCodeAt(0);
const length = charCodeEnd - charCodeStart + 1;
for (let charCode = charCodeStart; charCode <= charCodeEnd; charCode += 1) {
charToIndex[String.fromCharCode(charCode)] = indexToChar.length;
@ -142,7 +141,7 @@ export function encodeBase64(
if (paddingLength === 2) {
// aaaaaabb
const x = input[inputIndex];
const x = input[inputIndex]!;
inputIndex -= 1;
output[outputIndex] = paddingChar;
@ -151,56 +150,56 @@ export function encodeBase64(
output[outputIndex] = paddingChar;
outputIndex -= 1;
output[outputIndex] = indexToChar[((x & 0b11) << 4)];
output[outputIndex] = indexToChar[((x & 0b11) << 4)]!;
outputIndex -= 1;
output[outputIndex] = indexToChar[x >> 2];
output[outputIndex] = indexToChar[x >> 2]!;
outputIndex -= 1;
} else if (paddingLength === 1) {
// bbbbcccc
const y = input[inputIndex];
const y = input[inputIndex]!;
inputIndex -= 1;
// aaaaaabb
const x = input[inputIndex];
const x = input[inputIndex]!;
inputIndex -= 1;
output[outputIndex] = paddingChar;
outputIndex -= 1;
output[outputIndex] = indexToChar[((y & 0b1111) << 2)];
output[outputIndex] = indexToChar[((y & 0b1111) << 2)]!;
outputIndex -= 1;
output[outputIndex] = indexToChar[((x & 0b11) << 4) | (y >> 4)];
output[outputIndex] = indexToChar[((x & 0b11) << 4) | (y >> 4)]!;
outputIndex -= 1;
output[outputIndex] = indexToChar[x >> 2];
output[outputIndex] = indexToChar[x >> 2]!;
outputIndex -= 1;
}
while (inputIndex >= inputOffset) {
// ccdddddd
const z = input[inputIndex];
const z = input[inputIndex]!;
inputIndex -= 1;
// bbbbcccc
const y = input[inputIndex];
const y = input[inputIndex]!;
inputIndex -= 1;
// aaaaaabb
const x = input[inputIndex];
const x = input[inputIndex]!;
inputIndex -= 1;
output[outputIndex] = indexToChar[z & 0b111111];
output[outputIndex] = indexToChar[z & 0b111111]!;
outputIndex -= 1;
output[outputIndex] = indexToChar[((y & 0b1111) << 2) | (z >> 6)];
output[outputIndex] = indexToChar[((y & 0b1111) << 2) | (z >> 6)]!;
outputIndex -= 1;
output[outputIndex] = indexToChar[((x & 0b11) << 4) | (y >> 4)];
output[outputIndex] = indexToChar[((x & 0b11) << 4) | (y >> 4)]!;
outputIndex -= 1;
output[outputIndex] = indexToChar[x >> 2];
output[outputIndex] = indexToChar[x >> 2]!;
outputIndex -= 1;
}
@ -226,16 +225,16 @@ export function decodeBase64(input: string): ArrayBuffer {
let dIndex = 0;
while (sIndex < input.length - (padding !== 0 ? 4 : 0)) {
const a = charToIndex[input[sIndex]];
const a = charToIndex[input[sIndex]!]!;
sIndex += 1;
const b = charToIndex[input[sIndex]];
const b = charToIndex[input[sIndex]!]!;
sIndex += 1;
const c = charToIndex[input[sIndex]];
const c = charToIndex[input[sIndex]!]!;
sIndex += 1;
const d = charToIndex[input[sIndex]];
const d = charToIndex[input[sIndex]!]!;
sIndex += 1;
result[dIndex] = (a << 2) | ((b & 0b11_0000) >> 4);
@ -249,23 +248,23 @@ export function decodeBase64(input: string): ArrayBuffer {
}
if (padding === 1) {
const a = charToIndex[input[sIndex]];
const a = charToIndex[input[sIndex]!]!;
sIndex += 1;
const b = charToIndex[input[sIndex]];
const b = charToIndex[input[sIndex]!]!;
sIndex += 1;
const c = charToIndex[input[sIndex]];
const c = charToIndex[input[sIndex]!]!;
result[dIndex] = (a << 2) | ((b & 0b11_0000) >> 4);
dIndex += 1;
result[dIndex] = ((b & 0b1111) << 4) | ((c & 0b11_1100) >> 2);
} else if (padding === 2) {
const a = charToIndex[input[sIndex]];
const a = charToIndex[input[sIndex]!]!;
sIndex += 1;
const b = charToIndex[input[sIndex]];
const b = charToIndex[input[sIndex]!]!;
result[dIndex] = (a << 2) | ((b & 0b11_0000) >> 4);
}

View file

@ -22,9 +22,9 @@ interface LinkedListItem<T> {
}
class LinkedList<T>{
private head?: LinkedListItem<T>;
private head?: LinkedListItem<T> | undefined;
private tail?: LinkedListItem<T>;
private tail?: LinkedListItem<T> | undefined;
private _length = 0;
public get length() { return this._length; }

View file

@ -4,27 +4,11 @@ import { EventEmitter } from '@yume-chan/event';
import Struct from '@yume-chan/struct';
import { ScrcpyClientConnection } from "./connection";
import { AndroidKeyEventAction, AndroidMotionEventAction, ScrcpyControlMessageType, ScrcpyInjectKeyCodeControlMessage, ScrcpyInjectScrollControlMessage, ScrcpyInjectTextControlMessage, ScrcpyInjectTouchControlMessage } from './message';
import { ScrcpyLogLevel, ScrcpyOptions } from "./options";
import { ScrcpyOptions } from "./options";
import { pushServer, PushServerOptions } from "./push-server";
import { parse_sequence_parameter_set, SequenceParameterSet } from './sps';
import { decodeUtf8 } from "./utils";
interface ScrcpyError {
type: string;
message: string;
stackTrace: string[];
}
interface ScrcpyOutput {
level: ScrcpyLogLevel;
message: string;
error?: ScrcpyError;
}
function* splitLines(text: string): Generator<string, void, void> {
let start = 0;
@ -100,7 +84,7 @@ export class ScrcpyClient {
client.onOutput((line) => {
const match = line.match(encoderNameRegex);
if (match) {
encoders.push(match[1]);
encoders.push(match[1]!);
}
});

View file

@ -18,7 +18,7 @@ class BitReader {
}
public next(): number {
const value = (this.buffer[this.bytePosition] >> (7 - this.bitPosition)) & 1;
const value = (this.buffer[this.bytePosition]! >> (7 - this.bitPosition)) & 1;
this.bitPosition += 1;
if (this.bitPosition === 8) {
this.bytePosition += 1;
@ -179,13 +179,17 @@ export function parse_sequence_parameter_set(buffer: ArrayBuffer) {
profile_idc === 134) {
const chroma_format_idc = reader.decodeExponentialGolombNumber();
if (chroma_format_idc === 3) {
const separate_colour_plane_flag = !!reader.next();
// separate_colour_plane_flag
reader.next();
}
const bit_depth_luma_minus8 = reader.decodeExponentialGolombNumber();
const bit_depth_chroma_minus8 = reader.decodeExponentialGolombNumber();
// bit_depth_luma_minus8
reader.decodeExponentialGolombNumber();
// bit_depth_chroma_minus8
reader.decodeExponentialGolombNumber();
const qpprime_y_zero_transform_bypass_flag = !!reader.next();
// qpprime_y_zero_transform_bypass_flag
reader.next();
const seq_scaling_matrix_present_flag = !!reader.next();
if (seq_scaling_matrix_present_flag) {
@ -206,14 +210,19 @@ export function parse_sequence_parameter_set(buffer: ArrayBuffer) {
}
}
const log2_max_frame_num_minus4 = reader.decodeExponentialGolombNumber();
// log2_max_frame_num_minus4
reader.decodeExponentialGolombNumber();
const pic_order_cnt_type = reader.decodeExponentialGolombNumber();
if (pic_order_cnt_type === 0) {
const log2_max_pic_order_cnt_lsb_minus4 = reader.decodeExponentialGolombNumber();
// log2_max_pic_order_cnt_lsb_minus4
reader.decodeExponentialGolombNumber();
} else if (pic_order_cnt_type === 1) {
const delta_pic_order_always_zero_flag = reader.next();
const offset_for_non_ref_pic = reader.decodeExponentialGolombNumber();
const offset_for_top_to_bottom_field = reader.decodeExponentialGolombNumber();
// delta_pic_order_always_zero_flag
reader.next();
// offset_for_non_ref_pic
reader.decodeExponentialGolombNumber();
// offset_for_top_to_bottom_field
reader.decodeExponentialGolombNumber();
const num_ref_frames_in_pic_order_cnt_cycle = reader.decodeExponentialGolombNumber();
const offset_for_ref_frame: number[] = [];
for (let i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++) {
@ -221,17 +230,21 @@ export function parse_sequence_parameter_set(buffer: ArrayBuffer) {
}
}
const max_num_ref_frames = reader.decodeExponentialGolombNumber();
const gaps_in_frame_num_value_allowed_flag = reader.next();
// max_num_ref_frames
reader.decodeExponentialGolombNumber();
// gaps_in_frame_num_value_allowed_flag
reader.next();
const pic_width_in_mbs_minus1 = reader.decodeExponentialGolombNumber();
const pic_height_in_map_units_minus1 = reader.decodeExponentialGolombNumber();
const frame_mbs_only_flag = reader.next();
if (!frame_mbs_only_flag) {
const mb_adaptive_frame_field_flag = !!reader.next();
// mb_adaptive_frame_field_flag
reader.next();
}
const direct_8x8_inference_flag = reader.next();
// direct_8x8_inference_flag
reader.next();
const frame_cropping_flag = !!reader.next();
let frame_crop_left_offset: number;

View file

@ -33,9 +33,9 @@
"prepublishOnly": "npm run build"
},
"dependencies": {
"@yume-chan/dataview-bigint-polyfill": "0.0.10",
"bluebird": "^3.7.2",
"tslib": "^2.3.1",
"@yume-chan/dataview-bigint-polyfill": "0.0.10"
"tslib": "^2.3.1"
},
"devDependencies": {
"jest": "^26.6.3",

View file

@ -1,4 +1,3 @@
import { getBigInt64, getBigUint64, setBigInt64, setBigUint64 } from '@yume-chan/dataview-bigint-polyfill/esm/fallback';
import { StructAsyncDeserializeStream, StructDeserializeStream, StructFieldDefinition, StructFieldValue, StructOptions, StructValue } from "../basic";
import { Syncbird } from "../syncbird";

View file

@ -8,35 +8,12 @@ const temp = path.resolve(__dirname, '..', 'temp', process.pid.toString());
fs.mkdirSync(temp, { recursive: true });
if (process.argv[2] !== '--incremental') {
fs.rmSync(path.resolve(cwd, 'cjs'), { force: true, recursive: true });
fs.rmSync(path.resolve(cwd, 'esm'), { force: true, recursive: true });
fs.rmSync(path.resolve(cwd, 'dts'), { force: true, recursive: true });
}
const tsconfigPath = path.resolve(cwd, 'tsconfig.json');
const tsconfigValue = JSON5.parse(fs.readFileSync(tsconfigPath), 'utf8');
// const cjsTsconfigPath = path.resolve(temp, 'tsconfig.cjs.json');
// const cjsTsconfigValue = {
// ...tsconfigValue,
// extends: tsconfigPath,
// compilerOptions: {
// composite: false,
// outDir: path.resolve(cwd, 'cjs'),
// module: 'CommonJS',
// declaration: false,
// declarationDir: null,
// declarationMap: false,
// typeRoots: [
// ...(tsconfigValue.compilerOptions?.typeRoots || []),
// path.resolve(cwd, 'node_modules', '@types'),
// path.resolve(__dirname, '..', 'node_modules', '@types'),
// ],
// },
// references: [],
// };
// fs.writeFileSync(cjsTsconfigPath, JSON.stringify(cjsTsconfigValue, undefined, 4));
const esmTsconfigPath = path.resolve(temp, 'tsconfig.esm.json');
const testTypes = tsconfigValue.testTypes || ['jest'];
const esmTsconfigValue = {
@ -64,7 +41,6 @@ fs.writeFileSync(esmTsconfigPath, JSON.stringify(esmTsconfigValue, undefined, 4)
const tsc = path.resolve(cwd, 'node_modules', '.bin', 'tsc');
const tscResult = childProcess.spawnSync(tsc, [
'--build',
// cjsTsconfigPath,
esmTsconfigPath,
], {
cwd,

View file

@ -8,10 +8,10 @@
"ESNext"
],
"rootDir": "../../../src",
"outDir": "../../../cjs",
"outDir": "../../../esm",
"sourceMap": true,
"declaration": true,
"declarationDir": "../../../dts",
"declarationDir": "../../../esm",
"declarationMap": true,
"stripInternal": true,
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
@ -23,12 +23,17 @@
/* Strict Type-Checking Options */
"strict": true, // /* Enable all strict type-checking options. */
/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
"noUnusedLocals": true, // /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
"noImplicitOverride": true,
"noImplicitReturns": true, // /* Report error when not all code paths in function return a value. */
"noFallthroughCasesInSwitch": true, // /* Report errors for fallthrough cases in switch statement. */
"forceConsistentCasingInFileNames": true, /* Disallow inconsistently-cased references to the same file. */
"exactOptionalPropertyTypes": true,
"noImplicitAny": true,
"noImplicitThis": true,
"noUncheckedIndexedAccess": true,
"resolveJsonModule": true,
/* Module Resolution Options */
"moduleResolution": "node", // /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */