diff --git a/assembly/index.d.ts b/assembly/index.d.ts index f717d62..dff9187 100644 --- a/assembly/index.d.ts +++ b/assembly/index.d.ts @@ -33,4 +33,4 @@ declare function omitnull(): Function; * Property decorator that allows a field to be flattened. * @param fieldName - Points to the field to flatten. Can use dot-notation here like @omit("foo.identifier.text") */ -declare function flatten(fieldName: string = "value"): Function; +declare function flatten(fieldName: string = "value"): Function; \ No newline at end of file diff --git a/assembly/index.ts b/assembly/index.ts index 1ed4d39..73d1281 100644 --- a/assembly/index.ts +++ b/assembly/index.ts @@ -13,21 +13,151 @@ import { deserializeFloat } from "./deserialize/float"; import { deserializeObject } from "./deserialize/object"; import { deserializeMap } from "./deserialize/map"; import { deserializeDate } from "./deserialize/date"; -import { NULL_WORD } from "./custom/chars"; +import { BRACKET_LEFT, NULL_WORD } from "./custom/chars"; import { deserializeInteger } from "./deserialize/integer"; import { deserializeString } from "./deserialize/string"; +import { Sink } from "./custom/sink"; +import { bs } from "./custom/bs"; + +/** + * Offset of the 'storage' property in the JSON.Value class. + */ +// @ts-ignore: Decorator valid here +@inline const STORAGE = offsetof("storage"); /** * JSON Encoder/Decoder for AssemblyScript */ export namespace JSON { + /** + * Enum representing the different types supported by JSON. + */ + export enum Types { + Raw = 0, + U8 = 1, + U16 = 2, + U32 = 3, + U64 = 4, + F32 = 5, + F64 = 6, + Bool = 7, + String = 8, + Obj = 8, + Array = 9 + } + export type Raw = string; + export class Value { + public type: i32; + + // @ts-ignore + private storage: u64; + + private constructor() { unreachable(); } + + /** + * Creates an JSON.Value instance from a given value. + * @param value - The value to be encapsulated. + * @returns An instance of JSON.Value. + */ + @inline static from(value: T): JSON.Value { + if (value instanceof JSON.Value) { + return value; + } + const out = changetype(__new(offsetof(), idof())); + out.set(value); + return out; + } + + /** + * Sets the value of the JSON.Value instance. + * @param value - The value to be set. + */ + @inline set(value: T): void { + if (isBoolean()) { + this.type = JSON.Types.Boolean; + store(changetype(this), value, STORAGE); + } else if (value instanceof u8 || value instanceof i8) { + this.type = JSON.Types.U8; + store(changetype(this), value, STORAGE); + } else if (value instanceof u16 || value instanceof i16) { + this.type = JSON.Types.U16; + store(changetype(this), value, STORAGE); + } else if (value instanceof u32 || value instanceof i32) { + this.type = JSON.Types.U32; + store(changetype(this), value, STORAGE); + } else if (value instanceof u64 || value instanceof i64) { + this.type = JSON.Types.U64; + store(changetype(this), value, STORAGE); + } else if (value instanceof f32) { + this.type = JSON.Types.F64; + store(changetype(this), value, STORAGE); + } else if (value instanceof f64) { + this.type = JSON.Types.F64; + store(changetype(this), value, STORAGE); + } else if (isString()) { + this.type = JSON.Types.String; + store(changetype(this), value, STORAGE); + } else if (value instanceof Map) { + if (idof() !== idof>()) { + abort("Maps must be of type Map!"); + } + this.type = JSON.Types.Obj; + store(changetype(this), value, STORAGE); + } else if (isArray()) { + // @ts-ignore: T satisfies constraints of any[] + this.type = JSON.Types.Array + getArrayDepth(0); + store(changetype(this), value, STORAGE); + } + } + + /** + * Gets the value of the JSON.Value instance. + * @returns The encapsulated value. + */ + @inline get(): T { + return load(changetype(this), STORAGE); + } + + /** + * Converts the JSON.Value to a string representation. + * @param useString - If true, treats Buffer as a string. + * @returns The string representation of the JSON.Value. + */ + toString(): string { + switch (this.type) { + case JSON.Types.U8: return this.get().toString(); + case JSON.Types.U16: return this.get().toString(); + case JSON.Types.U32: return this.get().toString(); + case JSON.Types.U64: return this.get().toString(); + case JSON.Types.String: return "\"" + this.get() + "\""; + case JSON.Types.Bool: return this.get() ? "true" : "false"; + default: { + const arr = this.get(); + if (!arr.length) return "[]"; + const out = Sink.fromStringLiteral("["); + const end = arr.length - 1; + for (let i = 0; i < end; i++) { + const element = unchecked(arr[i]); + out.write(element.toString()); + out.write(","); + } + + const element = unchecked(arr[end]); + out.write(element.toString()); + + out.write("]"); + return out.toString(); + } + } + } + } export class Box { - constructor(public value: T) {} + constructor(public value: T) { } @inline static from(value: T): Box { return new Box(value); } } - + /** * Stringifies valid JSON data. * ```js @@ -52,7 +182,7 @@ export namespace JSON { // @ts-ignore } else if (isString>()) { return serializeString(changetype(data)); - // @ts-ignore: Supplied by trasnform + // @ts-ignore: Supplied by transform } else if (isDefined(data.__SERIALIZE)) { // @ts-ignore return serializeObject(changetype>(data)); diff --git a/assembly/test.ts b/assembly/test.ts index be6c82d..801bb46 100644 --- a/assembly/test.ts +++ b/assembly/test.ts @@ -1,42 +1,36 @@ // import { JSON } from "."; -import { bs } from "./custom/bs"; -// @json -// class Vec3 { -// x: f32 = 0.0; -// y: f32 = 0.0; -// z: f32 = 0.0; -// } +import { JSON } from "."; +@json +class Vec3 { + x: f32 = 0.0; + y: f32 = 0.0; + z: f32 = 0.0; +} -// @json -// class Player { -// @alias("first name") -// firstName!: string; -// lastName!: string; -// lastActive!: i32[]; -// // Drop in a code block, function, or expression that evaluates to a boolean -// @omitif("this.age < 18") -// age!: i32; -// @omitnull() -// pos!: Vec3 | null; -// isVerified!: boolean; -// } +@json +class Player { + @alias("first name") + firstName!: string; + lastName!: string; + lastActive!: i32[]; + // Drop in a code block, function, or expression that evaluates to a boolean + @omitif("this.age < 18") + age!: i32; + pos!: JSON.Raw; + isVerified!: boolean; +} -// const player: Player = { -// firstName: "Emmet", -// lastName: "West", -// lastActive: [8, 27, 2022], -// age: 23, -// pos: { -// x: 3.4, -// y: 1.2, -// z: 8.3 -// }, -// isVerified: true -// }; +const player: Player = { + firstName: "Emmet", + lastName: "West", + lastActive: [8, 27, 2022], + age: 23, + pos: "{\"x\":3.4,\"y\":1.2,\"z\":8.3}", + isVerified: true +}; -// const stringified = JSON.stringify(player); - -// const parsed = JSON.parse(stringified); - -bs.write_32(6422620); -console.log(bs.out()) \ No newline at end of file +const stringified = JSON.stringify(player); +console.log(stringified); +console.log(idof().toString()); +console.log(idof().toString()) +// const parsed = JSON.parse(stringified); \ No newline at end of file diff --git a/transform/lib/index.js b/transform/lib/index.js index e707e74..e1215f0 100644 --- a/transform/lib/index.js +++ b/transform/lib/index.js @@ -117,8 +117,13 @@ class JSONTransform extends BaseVisitor { } if (!mem.flags.length) { mem.flags = [PropertyFlags.None]; - mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${__SERIALIZE<" + type + ">(this." + name.text + ")}"; - mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<" + type + ">(data.substring(value_start, value_end));"; + if (type == "JSON.Raw") { + mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":" + "\"${this." + name.text + "}\""; + } + else { + mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${__SERIALIZE<" + type + ">(this." + name.text + ")}"; + mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<" + type + ">(data.substring(value_start, value_end));"; + } } if (mem.flags.includes(PropertyFlags.OmitNull)) { mem.serialize = "${changetype(this." + mem.name + ") == 0" + " ? \"\" : '" + escapeString(JSON.stringify(mem.alias || mem.name)) + ":' + __SERIALIZE<" + type + ">(this." + name.text + ") + \",\"}"; @@ -136,11 +141,11 @@ class JSONTransform extends BaseVisitor { else if (mem.flags.includes(PropertyFlags.Flatten)) { const nullable = mem.node.type.isNullable; if (nullable) { - mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${this." + name.text + " ? __SERIALIZE(changetype>(this." + name.text + ")" + (mem.args?.length ? '.' + mem.args[0] : '') + ") : \"null\"}"; + mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${this." + name.text + " ? __SERIALIZE(changetype>(this." + name.text + ")" + (mem.args?.length ? '.' + mem.args.join(".") : '') + ") : \"null\"}"; mem.deserialize = "if (value_end - value_start == 4 && load(changetype(data) + (value_start << 1)) == " + charCodeAt64("null", 0) + ") {\n this." + name.text + " = null;\n } else {\n this." + name.text + " = " + "__DESERIALIZE<" + type + ">('{\"" + mem.args[0] + "\":' + data.substring(value_start, value_end) + \"}\");\n }"; } else { - mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${this." + name.text + " ? __SERIALIZE(this." + name.text + (mem.args?.length ? '.' + mem.args[0] : '') + ") : \"null\"}"; + mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${this." + name.text + " ? __SERIALIZE(this." + name.text + (mem.args?.length ? '.' + mem.args.join(".") : '') + ") : \"null\"}"; mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<" + type + ">('{\"" + mem.args[0] + "\":' + data.substring(value_start, value_end) + \"}\");"; } mem.name = name.text; @@ -164,6 +169,9 @@ class JSONTransform extends BaseVisitor { else if (t === "bool" || t === "boolean") { mem.initialize = "this." + name.text + " = false"; } + else if (t === "JSON.Raw") { + mem.initialize = "this." + name.text + " = \"\""; + } else if (t === "u8" || t === "u16" || t === "u32" || @@ -284,6 +292,8 @@ class JSONTransform extends BaseVisitor { let f = true; for (let i = 0; i < memberSet.length; i++) { const member = memberSet[i]; + if (!member.deserialize) + continue; const name = encodeKey(member.alias || member.name); if (name.length === 1) { DESERIALIZE += ` case ${name.charCodeAt(0)}: {\n ${member.deserialize}\n return true;\n }\n`; diff --git a/transform/src/index.ts b/transform/src/index.ts index 2d59360..4cf7734 100644 --- a/transform/src/index.ts +++ b/transform/src/index.ts @@ -135,8 +135,12 @@ class JSONTransform extends BaseVisitor { if (!mem.flags.length) { mem.flags = [PropertyFlags.None]; - mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${__SERIALIZE<" + type + ">(this." + name.text + ")}"; - mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<" + type + ">(data.substring(value_start, value_end));" + if (type == "JSON.Raw") { + mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":" + "\"${this." + name.text + "}\""; + } else { + mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${__SERIALIZE<" + type + ">(this." + name.text + ")}"; + mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<" + type + ">(data.substring(value_start, value_end));" + } } if (mem.flags.includes(PropertyFlags.OmitNull)) { @@ -152,10 +156,10 @@ class JSONTransform extends BaseVisitor { } else if (mem.flags.includes(PropertyFlags.Flatten)) { const nullable = (mem.node.type as NamedTypeNode).isNullable; if (nullable) { - mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${this." + name.text + " ? __SERIALIZE(changetype>(this." + name.text + ")" + (mem.args?.length ? '.' + mem.args[0]! : '') + ") : \"null\"}"; + mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${this." + name.text + " ? __SERIALIZE(changetype>(this." + name.text + ")" + (mem.args?.length ? '.' + mem.args.join(".") : '') + ") : \"null\"}"; mem.deserialize = "if (value_end - value_start == 4 && load(changetype(data) + (value_start << 1)) == " + charCodeAt64("null", 0) + ") {\n this." + name.text + " = null;\n } else {\n this." + name.text + " = " + "__DESERIALIZE<" + type + ">('{\"" + mem.args![0]! + "\":' + data.substring(value_start, value_end) + \"}\");\n }"; } else { - mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${this." + name.text + " ? __SERIALIZE(this." + name.text + (mem.args?.length ? '.' + mem.args[0]! : '') + ") : \"null\"}"; + mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${this." + name.text + " ? __SERIALIZE(this." + name.text + (mem.args?.length ? '.' + mem.args.join(".") : '') + ") : \"null\"}"; mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<" + type + ">('{\"" + mem.args![0]! + "\":' + data.substring(value_start, value_end) + \"}\");"; } mem.name = name.text; @@ -174,6 +178,8 @@ class JSONTransform extends BaseVisitor { mem.initialize = "this." + name.text + " = instantiate<" + mem.type + ">()"; } else if (t === "bool" || t === "boolean") { mem.initialize = "this." + name.text + " = false"; + } else if (t === "JSON.Raw") { + mem.initialize = "this." + name.text + " = \"\""; } else if ( t === "u8" || t === "u16" || @@ -300,6 +306,7 @@ class JSONTransform extends BaseVisitor { let f = true; for (let i = 0; i < memberSet.length; i++) { const member = memberSet[i]!; + if (!member.deserialize) continue; const name = encodeKey(member.alias || member.name); if (name.length === 1) { DESERIALIZE += ` case ${name.charCodeAt(0)}: {\n ${member.deserialize}\n return true;\n }\n`; diff --git a/transform/src/visitor.ts b/transform/src/visitor.ts deleted file mode 100644 index 6d0d2f9..0000000 --- a/transform/src/visitor.ts +++ /dev/null @@ -1,543 +0,0 @@ -import { - ArrayLiteralExpression, - AssertionExpression, - BinaryExpression, - CallExpression, - ElementAccessExpression, - FloatLiteralExpression, - FunctionTypeNode, - IdentifierExpression, - NamedTypeNode, - Node, - ObjectLiteralExpression, - Source, - TypeNode, - TypeParameterNode, - BlockStatement, - BreakStatement, - ClassDeclaration, - ClassExpression, - CommaExpression, - ConstructorExpression, - ContinueStatement, - DecoratorNode, - DoStatement, - EmptyStatement, - EnumDeclaration, - EnumValueDeclaration, - ExportDefaultStatement, - ExportImportStatement, - ExportMember, - ExportStatement, - Expression, - ExpressionStatement, - FalseExpression, - FieldDeclaration, - ForStatement, - FunctionDeclaration, - FunctionExpression, - IfStatement, - ImportDeclaration, - ImportStatement, - IndexSignatureNode, - InstanceOfExpression, - IntegerLiteralExpression, - InterfaceDeclaration, - LiteralExpression, - MethodDeclaration, - NamespaceDeclaration, - NewExpression, - NullExpression, - ParameterNode, - ParenthesizedExpression, - PropertyAccessExpression, - RegexpLiteralExpression, - ReturnStatement, - Statement, - StringLiteralExpression, - SuperExpression, - SwitchCase, - SwitchStatement, - TemplateLiteralExpression, - TernaryExpression, - ThisExpression, - ThrowStatement, - TrueExpression, - TryStatement, - TypeDeclaration, - TypeName, - UnaryExpression, - UnaryPostfixExpression, - UnaryPrefixExpression, - VariableDeclaration, - VariableStatement, - VoidStatement, - WhileStatement, -} from "assemblyscript/dist/assemblyscript.js"; - -export declare type Collection = - | Node - | T[] - | Map> - | Iterable; - -export class Visitor { - public currentSource: Source | null = null; - public depth = 0; - visit(node: T | T[] | Map> | Iterable): void { - if (node == null) { - return; - } else if (node instanceof Array) { - for (const element of node) { - this.visit(element); - } - } else if (node instanceof Map) { - for (const element of node.values()) { - this.visit(element); - } - // @ts-ignore - } else if (typeof node[Symbol.iterator] === "function") { - // @ts-ignore - for (const element of node) { - this.visit(element); - } - } else if (node instanceof Node) { - this._visit(node); - } else { - throw new Error("Could not visit invalid type!"); - } - } - private _visit(node: Node): void { - if (node instanceof Source) { - this.visitSource(node); - } else if (node instanceof NamedTypeNode) { - this.visitNamedTypeNode(node); - } else if (node instanceof FunctionTypeNode) { - this.visitFunctionTypeNode(node); - } else if (node instanceof TypeName) { - this.visitTypeName(node); - } else if (node instanceof TypeParameterNode) { - this.visitTypeParameter(node); - } else if (node instanceof IdentifierExpression) { - this.visitIdentifierExpression(node); - } else if (node instanceof AssertionExpression) { - this.visitAssertionExpression(node); - } else if (node instanceof BinaryExpression) { - this.visitBinaryExpression(node); - } else if (node instanceof CallExpression) { - this.visitCallExpression(node); - } else if (node instanceof ClassExpression) { - this.visitClassExpression(node); - } else if (node instanceof CommaExpression) { - this.visitCommaExpression(node); - } else if (node instanceof ElementAccessExpression) { - this.visitElementAccessExpression(node); - } else if (node instanceof FunctionExpression) { - this.visitFunctionExpression(node); - } else if (node instanceof InstanceOfExpression) { - this.visitInstanceOfExpression(node); - } else if (node instanceof LiteralExpression) { - this.visitLiteralExpression(node); - } else if (node instanceof NewExpression) { - this.visitNewExpression(node); - } else if (node instanceof ParenthesizedExpression) { - this.visitParenthesizedExpression(node); - } else if (node instanceof PropertyAccessExpression) { - this.visitPropertyAccessExpression(node); - } else if (node instanceof TernaryExpression) { - this.visitTernaryExpression(node); - } else if (node instanceof UnaryPostfixExpression) { - this.visitUnaryPostfixExpression(node); - } else if (node instanceof UnaryPrefixExpression) { - this.visitUnaryPrefixExpression(node); - } else if (node instanceof BlockStatement) { - this.visitBlockStatement(node); - } else if (node instanceof BreakStatement) { - this.visitBreakStatement(node); - } else if (node instanceof ContinueStatement) { - this.visitContinueStatement(node); - } else if (node instanceof DoStatement) { - this.visitDoStatement(node); - } else if (node instanceof EmptyStatement) { - this.visitEmptyStatement(node); - } else if (node instanceof ExportStatement) { - this.visitExportStatement(node); - } else if (node instanceof ExportDefaultStatement) { - this.visitExportDefaultStatement(node); - } else if (node instanceof ExportImportStatement) { - this.visitExportImportStatement(node); - } else if (node instanceof ExpressionStatement) { - this.visitExpressionStatement(node); - } else if (node instanceof ForStatement) { - this.visitForStatement(node); - } else if (node instanceof IfStatement) { - this.visitIfStatement(node); - } else if (node instanceof ImportStatement) { - this.visitImportStatement(node); - } else if (node instanceof ReturnStatement) { - this.visitReturnStatement(node); - } else if (node instanceof SwitchStatement) { - this.visitSwitchStatement(node); - } else if (node instanceof ThrowStatement) { - this.visitThrowStatement(node); - } else if (node instanceof TryStatement) { - this.visitTryStatement(node); - } else if (node instanceof VariableStatement) { - this.visitVariableStatement(node); - } else if (node instanceof WhileStatement) { - this.visitWhileStatement(node); - } else if (node instanceof ClassDeclaration) { - this.visitClassDeclaration(node); - } else if (node instanceof EnumDeclaration) { - this.visitEnumDeclaration(node); - } else if (node instanceof EnumValueDeclaration) { - this.visitEnumValueDeclaration(node); - } else if (node instanceof FieldDeclaration) { - this.visitFieldDeclaration(node); - } else if (node instanceof FunctionDeclaration) { - this.visitFunctionDeclaration(node); - } else if (node instanceof ImportDeclaration) { - this.visitImportDeclaration(node); - } else if (node instanceof InterfaceDeclaration) { - this.visitInterfaceDeclaration(node); - } else if (node instanceof MethodDeclaration) { - this.visitMethodDeclaration(node); - } else if (node instanceof NamespaceDeclaration) { - this.visitNamespaceDeclaration(node); - } else if (node instanceof TypeDeclaration) { - this.visitTypeDeclaration(node); - } else if (node instanceof VariableDeclaration) { - this.visitVariableDeclaration(node); - } else if (node instanceof DecoratorNode) { - this.visitDecoratorNode(node); - } else if (node instanceof ExportMember) { - this.visitExportMember(node); - } else if (node instanceof ParameterNode) { - this.visitParameter(node); - } else if (node instanceof SwitchCase) { - this.visitSwitchCase(node); - } else if (node instanceof IndexSignatureNode) { - this.visitIndexSignature(node); - } else { - throw new Error("Could not visit invalid type!"); - } - } - visitSource(node: Source): void { - this.currentSource = node; - for (const stmt of node.statements) { - this.depth++; - this._visit(stmt); - this.depth--; - } - this.currentSource = null; - } - visitTypeNode(node: TypeNode): void { } - visitTypeName(node: TypeName): void { - this.visit(node.identifier); - this.visit(node.next); - } - visitNamedTypeNode(node: NamedTypeNode): void { - this.visit(node.name); - this.visit(node.typeArguments); - } - visitFunctionTypeNode(node: FunctionTypeNode): void { - this.visit(node.parameters); - this.visit(node.returnType); - this.visit(node.explicitThisType); - } - visitTypeParameter(node: TypeParameterNode): void { - this.visit(node.name); - this.visit(node.extendsType); - this.visit(node.defaultType); - } - visitIdentifierExpression(node: IdentifierExpression): void { } - visitArrayLiteralExpression(node: ArrayLiteralExpression) { - this.visit(node.elementExpressions); - } - visitObjectLiteralExpression(node: ObjectLiteralExpression) { - this.visit(node.names); - this.visit(node.values); - } - visitAssertionExpression(node: AssertionExpression) { - this.visit(node.toType); - this.visit(node.expression); - } - visitBinaryExpression(node: BinaryExpression) { - this.visit(node.left); - this.visit(node.right); - } - visitCallExpression(node: CallExpression) { - this.visit(node.expression); - this.visitArguments(node.typeArguments, node.args); - } - visitArguments(typeArguments: TypeNode[] | null, args: Expression[]) { - this.visit(typeArguments); - this.visit(args); - } - visitClassExpression(node: ClassExpression) { - this.visit(node.declaration); - } - visitCommaExpression(node: CommaExpression) { - this.visit(node.expressions); - } - visitElementAccessExpression(node: ElementAccessExpression) { - this.visit(node.elementExpression); - this.visit(node.expression); - } - visitFunctionExpression(node: FunctionExpression) { - this.visit(node.declaration); - } - visitLiteralExpression(node: LiteralExpression) { - if (node instanceof FloatLiteralExpression) { - this.visitFloatLiteralExpression(node); - } else if (node instanceof IntegerLiteralExpression) { - this.visitIntegerLiteralExpression(node); - } else if (node instanceof StringLiteralExpression) { - this.visitStringLiteralExpression(node); - } else if (node instanceof TemplateLiteralExpression) { - this.visitTemplateLiteralExpression(node); - } else if (node instanceof RegexpLiteralExpression) { - this.visitRegexpLiteralExpression(node); - } else if (node instanceof ArrayLiteralExpression) { - this.visitArrayLiteralExpression(node); - } else if (node instanceof ObjectLiteralExpression) { - this.visitObjectLiteralExpression(node); - } else { - throw new Error( - "Invalid LiteralKind at visitLiteralExpression(): " + node.literalKind, - ); - } - } - visitFloatLiteralExpression(node: FloatLiteralExpression) { } - visitInstanceOfExpression(node: InstanceOfExpression) { - this.visit(node.expression); - this.visit(node.isType); - } - visitIntegerLiteralExpression(node: IntegerLiteralExpression) { } - visitStringLiteral(str: string, singleQuoted: boolean = false) { } - visitStringLiteralExpression(node: StringLiteralExpression) { - this.visitStringLiteral(node.value); - } - visitTemplateLiteralExpression(node: TemplateLiteralExpression) { } - visitRegexpLiteralExpression(node: RegexpLiteralExpression) { } - visitNewExpression(node: NewExpression) { - this.visit(node.typeArguments); - this.visitArguments(node.typeArguments, node.args); - this.visit(node.args); - } - visitParenthesizedExpression(node: ParenthesizedExpression) { - this.visit(node.expression); - } - visitPropertyAccessExpression(node: PropertyAccessExpression) { - this.visit(node.property); - this.visit(node.expression); - } - visitTernaryExpression(node: TernaryExpression) { - this.visit(node.condition); - this.visit(node.ifThen); - this.visit(node.ifElse); - } - visitUnaryExpression(node: UnaryExpression) { - this.visit(node.operand); - } - visitUnaryPostfixExpression(node: UnaryPostfixExpression) { - this.visit(node.operand); - } - visitUnaryPrefixExpression(node: UnaryPrefixExpression) { - this.visit(node.operand); - } - visitSuperExpression(node: SuperExpression) { } - visitFalseExpression(node: FalseExpression) { } - visitTrueExpression(node: TrueExpression) { } - visitThisExpression(node: ThisExpression) { } - visitNullExperssion(node: NullExpression) { } - visitConstructorExpression(node: ConstructorExpression) { } - visitNodeAndTerminate(statement: Statement) { } - visitBlockStatement(node: BlockStatement) { - this.depth++; - this.visit(node.statements); - this.depth--; - } - visitBreakStatement(node: BreakStatement) { - this.visit(node.label); - } - visitContinueStatement(node: ContinueStatement) { - this.visit(node.label); - } - visitClassDeclaration(node: ClassDeclaration, isDefault: boolean = false) { - this.visit(node.name); - this.depth++; - this.visit(node.decorators); - if ( - node.isGeneric ? node.typeParameters != null : node.typeParameters == null - ) { - this.visit(node.typeParameters); - this.visit(node.extendsType); - this.visit(node.implementsTypes); - this.visit(node.members); - this.depth--; - } else { - throw new Error( - "Expected to type parameters to match class declaration, but found type mismatch instead!", - ); - } - } - visitDoStatement(node: DoStatement) { - this.visit(node.condition); - this.visit(node.body); - } - visitEmptyStatement(node: EmptyStatement) { } - visitEnumDeclaration(node: EnumDeclaration, isDefault: boolean = false) { - this.visit(node.name); - this.visit(node.decorators); - this.visit(node.values); - } - visitEnumValueDeclaration(node: EnumValueDeclaration) { - this.visit(node.name); - this.visit(node.initializer); - } - visitExportImportStatement(node: ExportImportStatement) { - this.visit(node.name); - this.visit(node.externalName); - } - visitExportMember(node: ExportMember) { - this.visit(node.localName); - this.visit(node.exportedName); - } - visitExportStatement(node: ExportStatement) { - this.visit(node.path); - this.visit(node.members); - } - visitExportDefaultStatement(node: ExportDefaultStatement) { - this.visit(node.declaration); - } - visitExpressionStatement(node: ExpressionStatement) { - this.visit(node.expression); - } - visitFieldDeclaration(node: FieldDeclaration) { - this.visit(node.name); - this.visit(node.type); - this.visit(node.initializer); - this.visit(node.decorators); - } - visitForStatement(node: ForStatement) { - this.visit(node.initializer); - this.visit(node.condition); - this.visit(node.incrementor); - this.visit(node.body); - } - visitFunctionDeclaration( - node: FunctionDeclaration, - isDefault: boolean = false, - ) { - this.visit(node.name); - this.visit(node.decorators); - this.visit(node.typeParameters); - this.visit(node.signature); - this.depth++; - this.visit(node.body); - this.depth--; - } - visitIfStatement(node: IfStatement) { - this.visit(node.condition); - this.visit(node.ifTrue); - this.visit(node.ifFalse); - } - visitImportDeclaration(node: ImportDeclaration) { - this.visit(node.foreignName); - this.visit(node.name); - this.visit(node.decorators); - } - visitImportStatement(node: ImportStatement) { - this.visit(node.namespaceName); - this.visit(node.declarations); - } - visitIndexSignature(node: IndexSignatureNode) { - this.visit(node.keyType); - this.visit(node.valueType); - } - visitInterfaceDeclaration( - node: InterfaceDeclaration, - isDefault: boolean = false, - ) { - this.visit(node.name); - this.visit(node.typeParameters); - this.visit(node.implementsTypes); - this.visit(node.extendsType); - this.depth++; - this.visit(node.members); - this.depth--; - } - visitMethodDeclaration(node: MethodDeclaration) { - this.visit(node.name); - this.visit(node.typeParameters); - this.visit(node.signature); - this.visit(node.decorators); - this.depth++; - this.visit(node.body); - this.depth--; - } - visitNamespaceDeclaration( - node: NamespaceDeclaration, - isDefault: boolean = false, - ) { - this.visit(node.name); - this.visit(node.decorators); - this.visit(node.members); - } - visitReturnStatement(node: ReturnStatement) { - this.visit(node.value); - } - visitSwitchCase(node: SwitchCase) { - this.visit(node.label); - this.visit(node.statements); - } - visitSwitchStatement(node: SwitchStatement) { - this.visit(node.condition); - this.depth++; - this.visit(node.cases); - this.depth--; - } - visitThrowStatement(node: ThrowStatement) { - this.visit(node.value); - } - visitTryStatement(node: TryStatement) { - this.visit(node.bodyStatements); - this.visit(node.catchVariable); - this.visit(node.catchStatements); - this.visit(node.finallyStatements); - } - visitTypeDeclaration(node: TypeDeclaration) { - this.visit(node.name); - this.visit(node.decorators); - this.visit(node.type); - this.visit(node.typeParameters); - } - visitVariableDeclaration(node: VariableDeclaration) { - this.visit(node.name); - this.visit(node.type); - this.visit(node.initializer); - } - visitVariableStatement(node: VariableStatement) { - this.visit(node.decorators); - this.visit(node.declarations); - } - visitWhileStatement(node: WhileStatement) { - this.visit(node.condition); - this.depth++; - this.visit(node.body); - this.depth--; - } - visitVoidStatement(node: VoidStatement) { } - visitComment(node: Comment) { } - visitDecoratorNode(node: DecoratorNode) { - this.visit(node.name); - this.visit(node.args); - } - visitParameter(node: ParameterNode) { - this.visit(node.name); - this.visit(node.implicitFieldDeclaration); - this.visit(node.initializer); - this.visit(node.type); - } -}