From b1e0db627c22020a6a15520ec2ec3956a693e115 Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Mon, 6 Jan 2025 14:02:07 +0200 Subject: [PATCH] fix typings so they are usable for building the project itself --- .gitignore | 1 + src/lib/libs/utils.ts | 2 +- src/lib/transpiler/map.ts | 6 ----- src/lib/transpiler/transpiler.d.ts | 10 ++++++++ src/lib/transpiler/types.ts | 10 -------- src/lib/transpiler/typescript.ts | 9 ------- src/main/resources/lib/errors.d.ts | 27 ++++++++++++++++++++ src/main/resources/lib/globals/json.d.ts | 4 +++ src/main/resources/lib/globals/map.d.ts | 19 ++++++++++++++ src/main/resources/lib/globals/set.d.ts | 18 +++++++++++++ src/main/resources/lib/globals/weak-map.d.ts | 11 ++++++++ src/main/resources/lib/lib.d.ts | 18 ++++++++++++- src/main/resources/lib/typing.d.ts | 4 +-- src/main/resources/lib/values/array.d.ts | 6 ++--- src/main/resources/lib/values/function.d.ts | 7 ++++- src/main/resources/lib/values/string.d.ts | 4 +-- src/main/tsconfig.json => tsconfig.json | 4 +-- 17 files changed, 122 insertions(+), 38 deletions(-) create mode 100644 src/lib/transpiler/transpiler.d.ts delete mode 100644 src/lib/transpiler/types.ts create mode 100644 src/main/resources/lib/errors.d.ts create mode 100644 src/main/resources/lib/globals/json.d.ts create mode 100644 src/main/resources/lib/globals/map.d.ts create mode 100644 src/main/resources/lib/globals/set.d.ts create mode 100644 src/main/resources/lib/globals/weak-map.d.ts rename src/main/tsconfig.json => tsconfig.json (80%) diff --git a/.gitignore b/.gitignore index 93740a1..fdbebb6 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ !/package.json !/rollup.config.js +!/tsconfig.json diff --git a/src/lib/libs/utils.ts b/src/lib/libs/utils.ts index bdd1964..eb939db 100644 --- a/src/lib/libs/utils.ts +++ b/src/lib/libs/utils.ts @@ -25,7 +25,7 @@ export interface TypeMap { export function unwrapThis(self: any, type: T, constr: Function, name: string, arg = "this", defaultVal?: TypeMap[T]): TypeMap[T] { if (typeof self === type) return self; - if (self instanceof constr && valueKey in self) self = (self as any)[valueKey]; + if (self instanceof constr && valueKey in self) self = self[valueKey]; if (typeof self === type) return self; if (defaultVal !== undefined) return defaultVal; throw new TypeError(name + " requires that '" + arg + "' be a " + constr.name); diff --git a/src/lib/transpiler/map.ts b/src/lib/transpiler/map.ts index 915cf66..33ee5d7 100644 --- a/src/lib/transpiler/map.ts +++ b/src/lib/transpiler/map.ts @@ -6,8 +6,6 @@ for (let i = 97; i <= 122; i++) map[i] = j++; map[43] = j++; map[47] = j++; -export type Location = readonly [file: string, line: number, start: number]; - export function decodeVLQ(val: string): number[][][] { const lines: number[][][] = []; @@ -83,10 +81,6 @@ export namespace Location { } } -export interface SourceMap { - (loc: Location): Location | undefined; -} - export class VLQSourceMap { public constructor( public readonly array: Map, diff --git a/src/lib/transpiler/transpiler.d.ts b/src/lib/transpiler/transpiler.d.ts new file mode 100644 index 0000000..79f990a --- /dev/null +++ b/src/lib/transpiler/transpiler.d.ts @@ -0,0 +1,10 @@ +type Location = readonly [file: string, line: number, start: number]; +type SourceMap = (loc: Location) => Location | undefined; + +type CompilerFactory = (next: Compiler) => Compiler; +type Compiler = (filename: string, src: string, mapper: SourceMap) => (this: any, ...args: any[]) => any; + +declare function getResource(name: string): string | undefined; +declare function print(...args: any[]): void; +declare function register(factory: CompilerFactory): void; +declare function registerSource(filename: string, src: string): void; diff --git a/src/lib/transpiler/types.ts b/src/lib/transpiler/types.ts deleted file mode 100644 index aadb44d..0000000 --- a/src/lib/transpiler/types.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { type SourceMap } from "./map.ts"; - -declare global { - type CompilerFactory = (next: Compiler) => Compiler; - type Compiler = (filename: string, src: string, mapper: SourceMap) => Function; - - function print(...args: any[]): void; - function register(factory: CompilerFactory): void; - function registerSource(filename: string, src: string): void; -} diff --git a/src/lib/transpiler/typescript.ts b/src/lib/transpiler/typescript.ts index d5d6b37..5ed95fd 100644 --- a/src/lib/transpiler/typescript.ts +++ b/src/lib/transpiler/typescript.ts @@ -1,17 +1,8 @@ import { createDocumentRegistry, createLanguageService, ModuleKind, ScriptSnapshot, ScriptTarget, type Diagnostic, type CompilerOptions, type IScriptSnapshot, flattenDiagnosticMessageText, CompilerHost, LanguageService } from "typescript"; import { SourceMap } from "./map.ts"; -declare function getResource(name: string): string | undefined; -declare function print(...args: any[]): void; -declare function register(factory: CompilerFactory): void; -declare function registerSource(filename: string, src: string): void; - -type CompilerFactory = (next: Compiler) => Compiler; -type Compiler = (filename: string, src: string, mapper: SourceMap) => Function; - const resources: Record = {}; - function resource(name: string) { if (name in resources) return resources[name]; else return resources[name] = getResource(name); diff --git a/src/main/resources/lib/errors.d.ts b/src/main/resources/lib/errors.d.ts new file mode 100644 index 0000000..aeb5f7c --- /dev/null +++ b/src/main/resources/lib/errors.d.ts @@ -0,0 +1,27 @@ +interface Error { + message: string; + name: string; +} +interface ErrorConstructor { + (msg?: string): Error; + new (msg?: string): Error; +} + +interface TypeError extends Error { } +interface TypeErrorConstructor { + (msg?: string): TypeError; + new (msg?: string): TypeError; +} + + +interface SyntaxError extends Error { } +interface SyntaxErrorConstructor { + (msg?: string): SyntaxError; + new (msg?: string): SyntaxError; +} + +interface RangeError extends Error { } +interface RangeErrorConstructor { + (msg?: string): RangeError; + new (msg?: string): RangeError; +} \ No newline at end of file diff --git a/src/main/resources/lib/globals/json.d.ts b/src/main/resources/lib/globals/json.d.ts new file mode 100644 index 0000000..1797110 --- /dev/null +++ b/src/main/resources/lib/globals/json.d.ts @@ -0,0 +1,4 @@ +interface JSON { + stringify(val: any): string; + parse(val: string): any; +} diff --git a/src/main/resources/lib/globals/map.d.ts b/src/main/resources/lib/globals/map.d.ts new file mode 100644 index 0000000..aa74c00 --- /dev/null +++ b/src/main/resources/lib/globals/map.d.ts @@ -0,0 +1,19 @@ +interface Map { + readonly size: number; + get(key: K): V; + has(key: K): boolean; + set(key: K, val: V): this; + delete(key: K): boolean; + clear(): void; + + keys(): K[]; + values(): V[]; + entries(): [K, V][]; + + forEach(cb: (val: V, key: K, map: this) => void, self?: any): void; + + [Symbol.iterator](): Iterator<[K, V]>; +} +interface MapConstructor { + new (iterable?: Iterable<[K, V]>): Map; +} diff --git a/src/main/resources/lib/globals/set.d.ts b/src/main/resources/lib/globals/set.d.ts new file mode 100644 index 0000000..d6ad9a0 --- /dev/null +++ b/src/main/resources/lib/globals/set.d.ts @@ -0,0 +1,18 @@ +interface Set { + readonly size: number; + add(val: V): this; + has(key: V): boolean; + delete(key: V): boolean; + clear(): void; + + keys(): V[]; + values(): V[]; + entries(): [V, V][]; + + forEach(cb: (val: V, key: V, map: this) => void, self?: any): void; + + [Symbol.iterator](): Iterator; +} +interface SetConstructor { + new (iterable?: Iterable): Set; +} diff --git a/src/main/resources/lib/globals/weak-map.d.ts b/src/main/resources/lib/globals/weak-map.d.ts new file mode 100644 index 0000000..767f660 --- /dev/null +++ b/src/main/resources/lib/globals/weak-map.d.ts @@ -0,0 +1,11 @@ +interface WeakMap { + readonly size: number; + get(key: K): V; + set(key: K, val: V): this; + has(key: K): boolean; + delete(key: K): boolean; + clear(): void; +} +interface WeakMapConstructor { + new (iterable?: Iterable<[K, V]>): WeakMap; +} diff --git a/src/main/resources/lib/lib.d.ts b/src/main/resources/lib/lib.d.ts index 1cec2d3..2b26246 100644 --- a/src/main/resources/lib/lib.d.ts +++ b/src/main/resources/lib/lib.d.ts @@ -1,6 +1,8 @@ /// /// /// +/// +/// /// /// /// @@ -9,12 +11,20 @@ /// /// /// -/// +/// +/// +/// +/// declare function print(...args: any[]): void; declare type IArguments = Array; +declare var Error: ErrorConstructor; +declare var TypeError: TypeErrorConstructor; +declare var SyntaxError: SyntaxErrorConstructor; +declare var RangeError: RangeErrorConstructor; + declare var Array: ArrayConstructor; declare var Boolean: BooleanConstructor; declare var Function: FunctionConstructor; @@ -24,3 +34,9 @@ declare var Object: ObjectConstructor; declare var RegExp: RegExpConstructor; declare var String: StringConstructor; declare var Promise: PromiseConstructor; + +declare var JSON: JSON; + +declare var Set: SetConstructor; +declare var Map: MapConstructor; +declare var WeakMap: WeakMapConstructor; \ No newline at end of file diff --git a/src/main/resources/lib/typing.d.ts b/src/main/resources/lib/typing.d.ts index 4d6e5cd..d0274d9 100644 --- a/src/main/resources/lib/typing.d.ts +++ b/src/main/resources/lib/typing.d.ts @@ -1,6 +1,4 @@ -declare interface Record { - [key: Key]: Val; -} +declare type Record = { [x in Key]: Val } declare type InstanceType = T extends new (...args: any[]) => infer T ? T : never; declare type ReturnType = T extends (...args: any[]) => infer T ? T : never; declare type Arguments = diff --git a/src/main/resources/lib/values/array.d.ts b/src/main/resources/lib/values/array.d.ts index 61179bd..1ce1b8b 100644 --- a/src/main/resources/lib/values/array.d.ts +++ b/src/main/resources/lib/values/array.d.ts @@ -3,7 +3,7 @@ declare interface Array { [i: number]: T; forEach(this: T[], cb: (val: T, i: number, self: this) => void, self?: any): void; - join(this: T[], delim?: string): void; + join(this: T[], delim?: string): string; push(this: T[], ...elements: T[]): number; pop(this: T[]): T | undefined; @@ -11,8 +11,8 @@ declare interface Array { unshift(this: T[], ...elements: T[]): number; shift(this: T[]): T | undefined; - concat(this: T[], ...elements: (T | T[])[]): T | undefined; - slice(this: T[], start?: number, end?: number): T | undefined; + concat(this: T[], ...elements: (T | T[])[]): T[]; + slice(this: T[], start?: number, end?: number): T[]; splice(this: T[], start?: number, count?: number): T[]; splice(this: T[], start: number | undefined, count: number | undefined, ...elements: T[]): T[]; diff --git a/src/main/resources/lib/values/function.d.ts b/src/main/resources/lib/values/function.d.ts index e9c69cd..ea61b82 100644 --- a/src/main/resources/lib/values/function.d.ts +++ b/src/main/resources/lib/values/function.d.ts @@ -1,11 +1,16 @@ declare interface Function { - prototype: unknown; + prototype: object; + + name: string; + length: number; valueOf(): this; toString(): string; apply(this: (this: Self, ...args: Args) => Ret, self: Self, args: Args): Ret; + apply(this: Function, self: any, args: any[]): any; call(this: (this: Self, ...args: Args) => Ret, self: Self, ...args: Args): Ret; + call(this: Function, self: any, ...args: any): any; bind any>(this: T): T; bind< Bound extends readonly any[], diff --git a/src/main/resources/lib/values/string.d.ts b/src/main/resources/lib/values/string.d.ts index 93a087d..e4fa52d 100644 --- a/src/main/resources/lib/values/string.d.ts +++ b/src/main/resources/lib/values/string.d.ts @@ -7,8 +7,8 @@ declare interface String { at(index: number): string | undefined; charAt(i: number): string | undefined; - charCodeAt(i: number): string; - codePointAt(i: number): string; + charCodeAt(i: number): number; + codePointAt(i: number): number; includes(search: string, offset?: number): number; indexOf(search: string, offset?: number): number; diff --git a/src/main/tsconfig.json b/tsconfig.json similarity index 80% rename from src/main/tsconfig.json rename to tsconfig.json index d7d4524..c24d63a 100644 --- a/src/main/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,5 @@ { - "include": ["resources/lib/**/*.ts", "resources/lib/values/.number.d.ts"], + "include": ["src/lib/**/*.ts", "src/main/resources/lib/lib.d.ts"], "compilerOptions": { "strict": true, "esModuleInterop": true, @@ -7,7 +7,7 @@ "moduleResolution": "Bundler", "module": "ESNext", "target": "ESNext", - "lib": [], + "noLib": true, "forceConsistentCasingInFileNames": true, "emitDecoratorMetadata": true, "experimentalDecorators": true,