Skip to content

Commit

Permalink
refactor: more TypeScript ESLint checks (#530)
Browse files Browse the repository at this point in the history
- adjacent-overload-signatures
- array-type
- ban-ts-comment
- ban-types
- consistent-generic-constructors
- consistent-indexed-object-style
- consistent-type-assertions
- no-array-delete
- no-confusing-non-null-assertion
- no-confusing-void-expression
- no-duplicate-enum-values
- no-duplicate-type-constituents
- no-dynamic-delete
- no-explicit-any
- no-extra-non-null-assertion
- no-extraneous-class
- no-floating-promises
- no-for-in-array
- no-implied-eval
- no-import-type-side-effects
- no-invalid-void-type
- no-loss-of-precision
- no-meaningless-void-operator
- no-misused-new
- no-misused-promises
- no-mixed-enums
- no-namespace
- no-non-null-asserted-nullish-coalescing
- no-non-null-asserted-optional-chain
- no-redundant-type-constituents
- no-this-alias
- only-throw-error
- no-unnecessary-qualifier
- only-throw-error
- no-unnecessary-qualifier
- no-unsafe-unary-minus
- no-unused-expressions
- prefer-for-of
- prefer-function-type
- prefer-includes
- prefer-literal-enum-member
- prefer-nullish-coalescing
- prefer-optional-chain
  • Loading branch information
anton-trunov committed Jul 4, 2024
1 parent 5ee2716 commit b119b7b
Show file tree
Hide file tree
Showing 43 changed files with 242 additions and 226 deletions.
52 changes: 52 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,57 @@ module.exports = {
"@typescript-eslint/no-unnecessary-boolean-literal-compare": "error",
"@typescript-eslint/no-unnecessary-condition": "error",
"@typescript-eslint/switch-exhaustiveness-check": "error",
"@typescript-eslint/adjacent-overload-signatures": "error",
"@typescript-eslint/array-type": "error",
"@typescript-eslint/ban-ts-comment": "error",
"@typescript-eslint/ban-types": "error",
"@typescript-eslint/consistent-generic-constructors": [
"error",
"type-annotation",
],
"@typescript-eslint/consistent-indexed-object-style": "error",
"@typescript-eslint/consistent-type-assertions": "error",
"@typescript-eslint/no-array-delete": "error",
"@typescript-eslint/no-confusing-non-null-assertion": "error",
"@typescript-eslint/no-confusing-void-expression": "error",
"@typescript-eslint/no-duplicate-enum-values": "error",
"@typescript-eslint/no-duplicate-type-constituents": "error",
"@typescript-eslint/no-dynamic-delete": "error",
"@typescript-eslint/no-explicit-any": "error",
"@typescript-eslint/no-extra-non-null-assertion": "error",
"@typescript-eslint/no-extraneous-class": "error",
"@typescript-eslint/no-floating-promises": "error",
"@typescript-eslint/no-for-in-array": "error",
"no-implied-eval": "off",
"@typescript-eslint/no-implied-eval": "error",
"@typescript-eslint/no-import-type-side-effects": "error",
"@typescript-eslint/no-invalid-void-type": "error",
"no-loss-of-precision": "off",
"@typescript-eslint/no-loss-of-precision": "error",
"@typescript-eslint/no-meaningless-void-operator": "error",
"@typescript-eslint/no-misused-new": "error",
"@typescript-eslint/no-misused-promises": "error",
"@typescript-eslint/no-mixed-enums": "error",
"@typescript-eslint/no-namespace": "error",
"@typescript-eslint/no-non-null-asserted-nullish-coalescing": "error",
"@typescript-eslint/no-non-null-asserted-optional-chain": "error",
"@typescript-eslint/no-redundant-type-constituents": "error",
"@typescript-eslint/no-this-alias": "error",
"no-throw-literal": "off",
"@typescript-eslint/only-throw-error": "error",
"@typescript-eslint/no-unnecessary-qualifier": "error",
"@typescript-eslint/no-unsafe-unary-minus": "error",
"no-unused-expressions": "off",
"@typescript-eslint/no-unused-expressions": "error",
"@typescript-eslint/prefer-for-of": "error",
"@typescript-eslint/prefer-function-type": "error",
"@typescript-eslint/prefer-includes": "error",
"@typescript-eslint/prefer-literal-enum-member": "error",
"@typescript-eslint/prefer-nullish-coalescing": "error",
"@typescript-eslint/prefer-optional-chain": "error",
"@typescript-eslint/prefer-string-starts-ends-with": "error",
"require-await": "off",
"@typescript-eslint/require-await": "error",
"@typescript-eslint/unified-signatures": "error",
},
};
2 changes: 1 addition & 1 deletion scripts/pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const stdlibFiles = glob.sync(
const dirPrefixToRemove =
posixNormalize(path.resolve(__dirname, "..", "stdlib")) + "/"; // Remove also the leading slash
let output: string = "";
output = "const files: { [key: string]: string } = {};\n";
output = "const files: Record<string, string> = {};\n";
for (const f of stdlibFiles) {
let code = fs.readFileSync(f).toString("base64");
const name = f.replace(dirPrefixToRemove, "");
Expand Down
2 changes: 1 addition & 1 deletion scripts/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { consoleLogger } from "../src/logger";
import { __DANGER__disableVersionNumber } from "../src/pipeline/version";

// Read cases
(async () => {
void (async () => {
// Disable version number in packages
__DANGER__disableVersionNumber();

Expand Down
40 changes: 14 additions & 26 deletions src/abi/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,18 @@ export const MapFunctions: Map<string, AbiFunction> = new Map([
if (self.key === "Int") {
let bits = 257;
let kind = "int";
if (self.keyAs && self.keyAs.startsWith("int")) {
if (self.keyAs?.startsWith("int")) {
bits = parseInt(self.keyAs.slice(3), 10);
} else if (self.keyAs && self.keyAs.startsWith("uint")) {
} else if (self.keyAs?.startsWith("uint")) {
bits = parseInt(self.keyAs.slice(4), 10);
kind = "uint";
}
if (self.value === "Int") {
let vBits = 257;
let vKind = "int";
if (self.valueAs && self.valueAs.startsWith("int")) {
if (self.valueAs?.startsWith("int")) {
vBits = parseInt(self.valueAs.slice(3), 10);
} else if (
self.valueAs &&
self.valueAs.startsWith("uint")
) {
} else if (self.valueAs?.startsWith("uint")) {
vBits = parseInt(self.valueAs.slice(4), 10);
vKind = "uint";
}
Expand Down Expand Up @@ -148,12 +145,9 @@ export const MapFunctions: Map<string, AbiFunction> = new Map([
if (self.value === "Int") {
let vBits = 257;
let vKind = "int";
if (self.valueAs && self.valueAs.startsWith("int")) {
if (self.valueAs?.startsWith("int")) {
vBits = parseInt(self.valueAs.slice(3), 10);
} else if (
self.valueAs &&
self.valueAs.startsWith("uint")
) {
} else if (self.valueAs?.startsWith("uint")) {
vBits = parseInt(self.valueAs.slice(4), 10);
vKind = "uint";
}
Expand Down Expand Up @@ -258,21 +252,18 @@ export const MapFunctions: Map<string, AbiFunction> = new Map([
if (self.key === "Int") {
let bits = 257;
let kind = "int";
if (self.keyAs && self.keyAs.startsWith("int")) {
if (self.keyAs?.startsWith("int")) {
bits = parseInt(self.keyAs.slice(3), 10);
} else if (self.keyAs && self.keyAs.startsWith("uint")) {
} else if (self.keyAs?.startsWith("uint")) {
bits = parseInt(self.keyAs.slice(4), 10);
kind = "uint";
}
if (self.value === "Int") {
let vBits = 257;
let vKind = "int";
if (self.valueAs && self.valueAs.startsWith("int")) {
if (self.valueAs?.startsWith("int")) {
vBits = parseInt(self.valueAs.slice(3), 10);
} else if (
self.valueAs &&
self.valueAs.startsWith("uint")
) {
} else if (self.valueAs?.startsWith("uint")) {
vBits = parseInt(self.valueAs.slice(4), 10);
vKind = "uint";
}
Expand Down Expand Up @@ -318,12 +309,9 @@ export const MapFunctions: Map<string, AbiFunction> = new Map([
if (self.value === "Int") {
let vBits = 257;
let vKind = "int";
if (self.valueAs && self.valueAs.startsWith("int")) {
if (self.valueAs?.startsWith("int")) {
vBits = parseInt(self.valueAs.slice(3), 10);
} else if (
self.valueAs &&
self.valueAs.startsWith("uint")
) {
} else if (self.valueAs?.startsWith("uint")) {
vBits = parseInt(self.valueAs.slice(4), 10);
vKind = "uint";
}
Expand Down Expand Up @@ -422,9 +410,9 @@ export const MapFunctions: Map<string, AbiFunction> = new Map([
if (self.key === "Int") {
let bits = 257;
let kind = "int";
if (self.keyAs && self.keyAs.startsWith("int")) {
if (self.keyAs?.startsWith("int")) {
bits = parseInt(self.keyAs.slice(3), 10);
} else if (self.keyAs && self.keyAs.startsWith("uint")) {
} else if (self.keyAs?.startsWith("uint")) {
bits = parseInt(self.keyAs.slice(4), 10);
kind = "uint";
}
Expand Down
2 changes: 1 addition & 1 deletion src/bindings/typescript/serializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ const map: Serializer<MapSerializerDescr> = {
value = { kind: "cell" };
}
}
if (primitiveTypes.indexOf(src.value) === -1) {
if (!primitiveTypes.includes(src.value)) {
if (
src.valueFormat === null ||
src.valueFormat === undefined ||
Expand Down
19 changes: 10 additions & 9 deletions src/bindings/writeTypescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,20 @@ export function writeTypescript(
`);
w.append();

const allocations: {
[key: string]: {
const allocations: Record<
string,
{
size: { bits: number; refs: number };
root: AllocationCell;
};
} = {};
}
> = {};

// Structs
if (abi.types) {
// Allocations
const refs = (src: ABIType) => {
const res: ABIType[] = [];
const t = new Set<string>();
const t: Set<string> = new Set();
for (const f of src.fields) {
const r = f.type;
if (r.kind === "simple") {
Expand Down Expand Up @@ -142,7 +143,7 @@ export function writeTypescript(
// Init
if (init) {
// Write serializer
const argTypeName = (abi.name || "Contract") + "_init_args";
const argTypeName = (abi.name ?? "Contract") + "_init_args";
const ops = init.args.map((v) => ({
name: v.name,
type: v.type,
Expand Down Expand Up @@ -191,11 +192,11 @@ export function writeTypescript(
);
w.inIndent(() => {
if (abi.errors) {
Object.entries(abi.errors).forEach(([k, abiError]) =>
Object.entries(abi.errors).forEach(([k, abiError]) => {
w.append(
`${k}: { message: \`${abiError.message.replaceAll("`", "\\`")}\` },`,
),
);
);
});
}
});
w.append(`}`);
Expand Down
2 changes: 1 addition & 1 deletion src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { createVirtualFileSystem } from "./vfs/createVirtualFileSystem";

export async function run(args: {
config: Config;
files: { [key: string]: string };
files: Record<string, string>;
logger?: TactLogger | null | undefined;
}) {
// Verify config
Expand Down
14 changes: 7 additions & 7 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export class CompilerContext {
readonly shared: { [key: symbol]: object | undefined } = {};
readonly shared: Record<symbol, object | undefined> = {};

constructor(
args: { shared: { [key: symbol]: object | undefined } } = {
args: { shared: Record<symbol, object | undefined> } = {
shared: {},
},
) {
Expand All @@ -12,7 +12,7 @@ export class CompilerContext {
}

addShared = <T>(store: symbol, key: string | number, value: T) => {
let sh: { [key: string]: T } = {};
let sh: Record<string, T> = {};
if (this.shared[store]) {
sh = { ...this.shared[store] };
}
Expand All @@ -28,18 +28,18 @@ export function createContextStore<T>() {
if (!ctx.shared[symbol]) {
return null;
}
const m = ctx.shared[symbol] as { [key: string | number]: T };
const m = ctx.shared[symbol] as Record<string | number, T>;
if (m[key]) {
return m[key];
} else {
return null;
}
},
all(ctx: CompilerContext): { [key: string | number]: T } {
all(ctx: CompilerContext): Record<string | number, T> {
if (!ctx.shared[symbol]) {
return {} as { [key: string | number]: T };
return {} as Record<string | number, T>;
}
const m = ctx.shared[symbol] as { [key: string | number]: T };
const m = ctx.shared[symbol] as Record<string | number, T>;
return m;
},
set(ctx: CompilerContext, key: string | number, v: T) {
Expand Down
4 changes: 0 additions & 4 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,6 @@ export function throwConstEvalError(
);
}

export function idTextErr(ident: string): string;
export function idTextErr(ident: AstId): string;
export function idTextErr(ident: AstFuncId): string;
export function idTextErr(ident: AstTypeId): string;
export function idTextErr(
ident: AstId | AstFuncId | AstTypeId | string,
): string {
Expand Down
8 changes: 4 additions & 4 deletions src/generator/Writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class WriterContext {
readonly ctx: CompilerContext;
#name: string;
#functions: Map<string, WrittenFunction> = new Map();
#functionsRendering = new Set<string>();
#functionsRendering: Set<string> = new Set();
#pendingWriter: Writer | null = null;
#pendingCode: Body | null = null;
#pendingDepends: Set<string> | null = null;
Expand All @@ -43,7 +43,7 @@ export class WriterContext {
#pendingContext: string | null = null;
#nextId = 0;
// #headers: string[] = [];
#rendered = new Set<string>();
#rendered: Set<string> = new Set();

constructor(ctx: CompilerContext, name: string) {
this.ctx = ctx;
Expand All @@ -69,7 +69,7 @@ export class WriterContext {

extract(debug: boolean = false) {
// Check dependencies
const missing = new Map<string, string[]>();
const missing: Map<string, string[]> = new Map();
for (const f of this.#functions.values()) {
for (const d of f.depends) {
if (!this.#functions.has(d)) {
Expand All @@ -94,7 +94,7 @@ export class WriterContext {

// Remove unused
if (!debug) {
const used = new Set<string>();
const used: Set<string> = new Set();
const visit = (name: string) => {
used.add(name);
const f = this.#functions.get(name)!;
Expand Down
2 changes: 1 addition & 1 deletion src/generator/createABI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export function createABI(ctx: CompilerContext, name: string): ContractABI {
}

// Errors
const errors: { [key: string]: { message: string } } = {};
const errors: Record<string, { message: string }> = {};
errors["2"] = { message: "Stack underflow" };
errors["3"] = { message: "Stack overflow" };
errors["4"] = { message: "Integer overflow" };
Expand Down
6 changes: 4 additions & 2 deletions src/generator/writeProgram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ function tryExtractModule(
imported: string[],
): WrittenFunction[] | null {
// Put to map
const maps = new Map<string, WrittenFunction>();
const maps: Map<string, WrittenFunction> = new Map();
for (const f of functions) {
maps.set(f.name, f);
}
Expand Down Expand Up @@ -378,7 +378,9 @@ function writeAll(

// Static functions
const sf = getAllStaticFunctions(ctx);
Object.values(sf).forEach((f) => writeFunction(f, wCtx));
Object.values(sf).forEach((f) => {
writeFunction(f, wCtx);
});

// Extensions
for (const c of allTypes) {
Expand Down
6 changes: 3 additions & 3 deletions src/generator/writeReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ export function writeReport(ctx: CompilerContext, pkg: PackageFileFormat) {

// Error Codes
w.write(`# Error Codes`);
Object.entries(abi.errors!).forEach(([t, abiError]) =>
w.write(`${t}: ${abiError.message}`),
);
Object.entries(abi.errors!).forEach(([t, abiError]) => {
w.write(`${t}: ${abiError.message}`);
});

return w.end();
}
4 changes: 0 additions & 4 deletions src/generator/writers/id.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { AstId, idText } from "../../grammar/ast";

export function funcIdOf(ident: string): string;
export function funcIdOf(ident: AstId): string;
export function funcIdOf(ident: AstId | string): string {
if (typeof ident === "string") {
return "$" + ident;
}
return "$" + idText(ident);
}

export function funcInitIdOf(ident: string): string;
export function funcInitIdOf(ident: AstId): string;
export function funcInitIdOf(ident: AstId | string): string {
if (typeof ident === "string") {
return ident + "$init";
Expand Down
2 changes: 1 addition & 1 deletion src/generator/writers/resolveFuncTypeFromAbi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function resolveFuncTypeFromAbi(
if (t.kind !== "struct") {
throw Error("Unsupported type: " + t.kind);
}
if (f.optional || t.fields.length === 0) {
if (f.optional ?? t.fields.length === 0) {
res.push("tuple");
} else {
const loaded = t.fields.map((v) => v.abi.type);
Expand Down
Loading

0 comments on commit b119b7b

Please sign in to comment.