Skip to content

Commit

Permalink
Merge pull request #744 from uzmoi/strictify-types
Browse files Browse the repository at this point in the history
型付けを改善
  • Loading branch information
uzmoi authored Aug 18, 2024
2 parents b4b12df + aeb3b5d commit 18ccf14
Show file tree
Hide file tree
Showing 15 changed files with 140 additions and 133 deletions.
53 changes: 23 additions & 30 deletions etc/aiscript.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ type Add = NodeBase & {
type: 'add';
left: Expression;
right: Expression;
operatorLoc: Loc;
};

// @public (undocumented)
Expand All @@ -26,9 +25,9 @@ export const AISCRIPT_VERSION: "1.0.0";

// @public (undocumented)
abstract class AiScriptError extends Error {
constructor(message: string, info?: any);
constructor(message: string, info?: unknown);
// (undocumented)
info?: any;
info: unknown;
// (undocumented)
name: string;
// (undocumented)
Expand All @@ -37,12 +36,12 @@ abstract class AiScriptError extends Error {

// @public
class AiScriptIndexOutOfRangeError extends AiScriptRuntimeError {
constructor(message: string, info?: any);
constructor(message: string, info?: unknown);
}

// @public
class AiScriptNamespaceError extends AiScriptError {
constructor(message: string, pos: Pos, info?: any);
constructor(message: string, pos: Pos, info?: unknown);
// (undocumented)
name: string;
// (undocumented)
Expand All @@ -51,14 +50,14 @@ class AiScriptNamespaceError extends AiScriptError {

// @public
class AiScriptRuntimeError extends AiScriptError {
constructor(message: string, info?: any);
constructor(message: string, info?: unknown);
// (undocumented)
name: string;
}

// @public
class AiScriptSyntaxError extends AiScriptError {
constructor(message: string, pos: Pos, info?: any);
constructor(message: string, pos: Pos, info?: unknown);
// (undocumented)
name: string;
// (undocumented)
Expand All @@ -67,7 +66,7 @@ class AiScriptSyntaxError extends AiScriptError {

// @public
class AiScriptTypeError extends AiScriptError {
constructor(message: string, pos: Pos, info?: any);
constructor(message: string, pos: Pos, info?: unknown);
// (undocumented)
name: string;
// (undocumented)
Expand All @@ -76,7 +75,7 @@ class AiScriptTypeError extends AiScriptError {

// @public
class AiScriptUserError extends AiScriptRuntimeError {
constructor(message: string, info?: any);
constructor(message: string, info?: unknown);
// (undocumented)
name: string;
}
Expand All @@ -86,7 +85,6 @@ type And = NodeBase & {
type: 'and';
left: Expression;
right: Expression;
operatorLoc: Loc;
};

// @public (undocumented)
Expand Down Expand Up @@ -251,7 +249,6 @@ type Div = NodeBase & {
type: 'div';
left: Expression;
right: Expression;
operatorLoc: Loc;
};

// @public (undocumented)
Expand All @@ -267,7 +264,6 @@ type Eq = NodeBase & {
type: 'eq';
left: Expression;
right: Expression;
operatorLoc: Loc;
};

// @public (undocumented)
Expand Down Expand Up @@ -352,15 +348,13 @@ type Gt = NodeBase & {
type: 'gt';
left: Expression;
right: Expression;
operatorLoc: Loc;
};

// @public (undocumented)
type Gteq = NodeBase & {
type: 'gteq';
left: Expression;
right: Expression;
operatorLoc: Loc;
};

// @public (undocumented)
Expand Down Expand Up @@ -394,14 +388,14 @@ export class Interpreter {
in?(q: string): Promise<string>;
out?(value: Value): void;
err?(e: AiScriptError): void;
log?(type: string, params: Record<string, any>): void;
log?(type: string, params: LogObject): void;
maxStep?: number;
abortOnError?: boolean;
});
// (undocumented)
abort(): void;
// (undocumented)
static collectMetadata(script?: Ast.Node[]): Map<any, any> | undefined;
static collectMetadata(script?: Ast.Node[]): Map<string, JsValue> | undefined;
// (undocumented)
exec(script?: Ast.Node[]): Promise<void>;
execFn(fn: VFn, args: Value[]): Promise<Value>;
Expand Down Expand Up @@ -441,7 +435,12 @@ function isStatement(x: Node_2): x is Statement;
function isString(val: Value): val is VStr;

// @public (undocumented)
function jsToVal(val: any): Value;
function jsToVal(val: unknown): Value;

// @public (undocumented)
type JsValue = {
[key: string]: JsValue;
} | JsValue[] | string | number | boolean | null | undefined;

// @public (undocumented)
type Loc = {
Expand All @@ -460,15 +459,13 @@ type Lt = NodeBase & {
type: 'lt';
left: Expression;
right: Expression;
operatorLoc: Loc;
};

// @public (undocumented)
type Lteq = NodeBase & {
type: 'lteq';
left: Expression;
right: Expression;
operatorLoc: Loc;
};

// @public (undocumented)
Expand All @@ -494,7 +491,6 @@ type Mul = NodeBase & {
type: 'mul';
left: Expression;
right: Expression;
operatorLoc: Loc;
};

// @public (undocumented)
Expand All @@ -516,15 +512,14 @@ type Neq = NodeBase & {
type: 'neq';
left: Expression;
right: Expression;
operatorLoc: Loc;
};

// @public (undocumented)
type Node_2 = Namespace | Meta | Statement | Expression | TypeSource | Attribute;

// @public
class NonAiScriptError extends AiScriptError {
constructor(error: any);
constructor(error: unknown);
// (undocumented)
name: string;
}
Expand Down Expand Up @@ -568,7 +563,6 @@ type Or = NodeBase & {
type: 'or';
left: Expression;
right: Expression;
operatorLoc: Loc;
};

// @public (undocumented)
Expand Down Expand Up @@ -599,7 +593,6 @@ type Pow = NodeBase & {
type: 'pow';
left: Expression;
right: Expression;
operatorLoc: Loc;
};

// @public (undocumented)
Expand All @@ -614,7 +607,6 @@ type Rem = NodeBase & {
type: 'rem';
left: Expression;
right: Expression;
operatorLoc: Loc;
};

// @public (undocumented)
Expand Down Expand Up @@ -649,7 +641,7 @@ export class Scope {
nsName?: string;
// (undocumented)
opts: {
log?(type: string, params: Record<string, any>): void;
log?(type: string, params: LogObject): void;
onUpdated?(name: string, value: Value): void;
};
}
Expand All @@ -671,7 +663,6 @@ type Sub = NodeBase & {
type: 'sub';
left: Expression;
right: Expression;
operatorLoc: Loc;
};

// @public (undocumented)
Expand All @@ -684,7 +675,7 @@ type SubAssign = NodeBase & {
// @public (undocumented)
type Tmpl = NodeBase & {
type: 'tmpl';
tmpl: (string | Expression)[];
tmpl: Expression[];
};

// @public (undocumented)
Expand Down Expand Up @@ -719,13 +710,14 @@ declare namespace utils {
valToJs,
jsToVal,
getLangVersion,
reprValue
reprValue,
JsValue
}
}
export { utils }

// @public (undocumented)
function valToJs(val: Value): any;
function valToJs(val: Value): JsValue;

// @public (undocumented)
function valToString(val: Value, simple?: boolean): string;
Expand Down Expand Up @@ -862,6 +854,7 @@ type VUserFn = VFnBase & {

// Warnings were encountered during analysis:
//
// src/interpreter/index.ts:39:4 - (ae-forgotten-export) The symbol "LogObject" needs to be exported by the entry point index.d.ts
// src/interpreter/value.ts:46:2 - (ae-forgotten-export) The symbol "Type" needs to be exported by the entry point index.d.ts

// (No @packageDocumentation comment for this package)
Expand Down
26 changes: 14 additions & 12 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { Pos } from './node.js';

export abstract class AiScriptError extends Error {
// name is read by Error.prototype.toString
public name = 'AiScript';
public info?: any;
public info: unknown;
public pos?: Pos;

constructor(message: string, info?: any) {
constructor(message: string, info?: unknown) {
super(message);

this.info = info;
Expand All @@ -24,8 +23,11 @@ export abstract class AiScriptError extends Error {
*/
export class NonAiScriptError extends AiScriptError {
public name = 'Internal';
constructor(error: any) {
super(error.message ?? `${error}`, error);
constructor(error: unknown) {
const message = String(
(error as { message?: unknown } | null | undefined)?.message ?? error,
);
super(message, error);
}
}

Expand All @@ -34,16 +36,16 @@ export class NonAiScriptError extends AiScriptError {
*/
export class AiScriptSyntaxError extends AiScriptError {
public name = 'Syntax';
constructor(message: string, public pos: Pos, info?: any) {
constructor(message: string, public pos: Pos, info?: unknown) {
super(`${message} (Line ${pos.line}, Column ${pos.column})`, info);
}
}
/**
* Type validation(parser/plugins/validate-type) errors.
*/
*/
export class AiScriptTypeError extends AiScriptError {
public name = 'Type';
constructor(message: string, public pos: Pos, info?: any) {
constructor(message: string, public pos: Pos, info?: unknown) {
super(`${message} (Line ${pos.line}, Column ${pos.column})`, info);
}
}
Expand All @@ -53,7 +55,7 @@ export class AiScriptTypeError extends AiScriptError {
*/
export class AiScriptNamespaceError extends AiScriptError {
public name = 'Namespace';
constructor(message: string, public pos: Pos, info?: any) {
constructor(message: string, public pos: Pos, info?: unknown) {
super(`${message} (Line ${pos.line}, Column ${pos.column})`, info);
}
}
Expand All @@ -63,15 +65,15 @@ export class AiScriptNamespaceError extends AiScriptError {
*/
export class AiScriptRuntimeError extends AiScriptError {
public name = 'Runtime';
constructor(message: string, info?: any) {
constructor(message: string, info?: unknown) {
super(message, info);
}
}
/**
* RuntimeError for illegal access to arrays.
*/
export class AiScriptIndexOutOfRangeError extends AiScriptRuntimeError {
constructor(message: string, info?: any) {
constructor(message: string, info?: unknown) {
super(message, info);
}
}
Expand All @@ -80,7 +82,7 @@ export class AiScriptIndexOutOfRangeError extends AiScriptRuntimeError {
*/
export class AiScriptUserError extends AiScriptRuntimeError {
public name = '';
constructor(message: string, info?: any) {
constructor(message: string, info?: unknown) {
super(message, info);
}
}
Loading

0 comments on commit 18ccf14

Please sign in to comment.