From bcd04a349cd8fdb6f1a03a725914d54144ee726d Mon Sep 17 00:00:00 2001 From: glacambre Date: Tue, 26 Mar 2019 06:34:40 +0100 Subject: [PATCH] Make compiler tslint/tslint-sonarts ready --- compiler/metadata/ClassMetadata.ts | 8 ++++---- compiler/metadata/FileMetadata.ts | 18 +++++++++--------- compiler/metadata/ProgramMetadata.ts | 6 +++--- compiler/metadata/SymbolMetadata.ts | 2 +- compiler/types/AnyType.ts | 12 +++++------- compiler/types/ArrayType.ts | 8 ++++---- compiler/types/BooleanType.ts | 19 ++++++++++--------- compiler/types/FunctionType.ts | 8 ++++---- compiler/types/LiteralTypeType.ts | 12 +++++++----- compiler/types/NumberType.ts | 20 +++++++++++--------- compiler/types/ObjectType.ts | 12 ++++++------ compiler/types/StringType.ts | 14 ++++++++------ compiler/types/TupleType.ts | 10 +++++----- compiler/types/Type.ts | 2 +- compiler/types/TypeReferenceType.ts | 8 ++++---- compiler/types/UnionType.ts | 10 +++++----- compiler/types/VoidType.ts | 10 +++++----- 17 files changed, 92 insertions(+), 87 deletions(-) diff --git a/compiler/metadata/ClassMetadata.ts b/compiler/metadata/ClassMetadata.ts index 7eb0248d..d91915c3 100644 --- a/compiler/metadata/ClassMetadata.ts +++ b/compiler/metadata/ClassMetadata.ts @@ -9,19 +9,19 @@ export class ClassMetadata { >(), ) {} - setMember(name: string, s: SymbolMetadata) { + public setMember(name: string, s: SymbolMetadata) { this.members.set(name, s) } - getMember(name: string) { + public getMember(name: string) { return this.members.get(name) } - getMembers() { + public getMembers() { return this.members.keys() } - toConstructor() { + public toConstructor() { return ( `new ClassMetadata(new Map([` + Array.from(this.members.entries()) diff --git a/compiler/metadata/FileMetadata.ts b/compiler/metadata/FileMetadata.ts index a28e779d..a1c61185 100644 --- a/compiler/metadata/FileMetadata.ts +++ b/compiler/metadata/FileMetadata.ts @@ -1,5 +1,5 @@ -import { SymbolMetadata } from "./SymbolMetadata" import { ClassMetadata } from "./ClassMetadata" +import { SymbolMetadata } from "./SymbolMetadata" export class FileMetadata { constructor( @@ -13,35 +13,35 @@ export class FileMetadata { >(), ) {} - setClass(name: string, c: ClassMetadata) { + public setClass(name: string, c: ClassMetadata) { this.classes.set(name, c) } - getClass(name: string) { + public getClass(name: string) { return this.classes.get(name) } - getClasses() { + public getClasses() { return Array.from(this.classes.keys()) } - setFunction(name: string, f: SymbolMetadata) { + public setFunction(name: string, f: SymbolMetadata) { this.functions.set(name, f) } - getFunction(name: string) { + public getFunction(name: string) { return this.functions.get(name) } - getFunctions() { + public getFunctions() { return Array.from(this.functions.entries()) } - getFunctionNames() { + public getFunctionNames() { return Array.from(this.functions.keys()) } - toConstructor() { + public toConstructor() { return ( `new FileMetadata(new Map([` + Array.from(this.classes.entries()) diff --git a/compiler/metadata/ProgramMetadata.ts b/compiler/metadata/ProgramMetadata.ts index 8d082616..2b8dfbdc 100644 --- a/compiler/metadata/ProgramMetadata.ts +++ b/compiler/metadata/ProgramMetadata.ts @@ -8,15 +8,15 @@ export class ProgramMetadata { >(), ) {} - setFile(name: string, file: FileMetadata) { + public setFile(name: string, file: FileMetadata) { this.files.set(name, file) } - getFile(name: string) { + public getFile(name: string) { return this.files.get(name) } - toConstructor() { + public toConstructor() { return ( `new ProgramMetadata(new Map([` + Array.from(this.files.entries()) diff --git a/compiler/metadata/SymbolMetadata.ts b/compiler/metadata/SymbolMetadata.ts index 513d0f65..9a93e42d 100644 --- a/compiler/metadata/SymbolMetadata.ts +++ b/compiler/metadata/SymbolMetadata.ts @@ -3,7 +3,7 @@ import { Type } from "../types/AllTypes" export class SymbolMetadata { constructor(public doc: string, public type: Type, public hidden = false) {} - toConstructor() { + public toConstructor() { return `new SymbolMetadata(${JSON.stringify( this.doc, )}, ${this.type.toConstructor()}, ${this.hidden})` diff --git a/compiler/types/AnyType.ts b/compiler/types/AnyType.ts index 521d6d0f..f1ac7581 100644 --- a/compiler/types/AnyType.ts +++ b/compiler/types/AnyType.ts @@ -1,20 +1,18 @@ import { Type } from "./Type" export class AnyType implements Type { - static instance = new AnyType() - kind = "any" + public static instance = new AnyType() + public kind = "any" - constructor() {} - - toConstructor() { + public toConstructor() { return "AnyType.instance" } - toString() { + public toString() { return this.kind } - convert(argument) { + public convert(argument) { return argument } } diff --git a/compiler/types/ArrayType.ts b/compiler/types/ArrayType.ts index a155cb1b..c894538d 100644 --- a/compiler/types/ArrayType.ts +++ b/compiler/types/ArrayType.ts @@ -1,19 +1,19 @@ import { Type } from "./Type" export class ArrayType implements Type { - kind = "array" + public kind = "array" constructor(public elemType: Type) {} - toConstructor() { + public toConstructor() { return `new ArrayType(${this.elemType.toConstructor()})` } - toString() { + public toString() { return `${this.elemType.toString()}[]` } - convert(argument) { + public convert(argument) { if (!Array.isArray(argument)) { try { argument = JSON.parse(argument) diff --git a/compiler/types/BooleanType.ts b/compiler/types/BooleanType.ts index 759a3358..26f126c7 100644 --- a/compiler/types/BooleanType.ts +++ b/compiler/types/BooleanType.ts @@ -1,22 +1,23 @@ import { Type } from "./Type" export class BooleanType implements Type { - static instance = new BooleanType() - kind = "boolean" + public static instance = new BooleanType() + public kind = "boolean" - constructor() {} - - toConstructor() { + public toConstructor() { return "BooleanType.instance" } - toString() { + public toString() { return this.kind } - convert(argument) { - if (argument === "true") return true - else if (argument === "false") return false + public convert(argument) { + if (argument === "true") { + return true + } else if (argument === "false") { + return false + } throw new Error("Can't convert ${argument} to boolean") } } diff --git a/compiler/types/FunctionType.ts b/compiler/types/FunctionType.ts index 12fcb872..4a84e2c9 100644 --- a/compiler/types/FunctionType.ts +++ b/compiler/types/FunctionType.ts @@ -1,11 +1,11 @@ import { Type } from "./Type" export class FunctionType implements Type { - kind = "function" + public kind = "function" constructor(public args: Type[], public ret: Type) {} - toConstructor() { + public toConstructor() { return ( `new FunctionType([` + // Convert every argument type to its string constructor representation @@ -14,11 +14,11 @@ export class FunctionType implements Type { ) } - toString() { + public toString() { return `(${this.args.map(a => a.toString()).join(", ")}) => ${this.ret.toString()}` } - convert(argument) { + public convert(argument) { // Possible strategies: // - eval() // - window[argument] diff --git a/compiler/types/LiteralTypeType.ts b/compiler/types/LiteralTypeType.ts index 6f0e9656..76b13a84 100644 --- a/compiler/types/LiteralTypeType.ts +++ b/compiler/types/LiteralTypeType.ts @@ -1,20 +1,22 @@ import { Type } from "./Type" export class LiteralTypeType implements Type { - kind = "LiteralType" + public kind = "LiteralType" constructor(public value: string) {} - toConstructor() { + public toConstructor() { return `new LiteralTypeType(${JSON.stringify(this.value)})` } - toString() { + public toString() { return JSON.stringify(this.value) } - convert(argument) { - if (argument === this.value) return argument + public convert(argument) { + if (argument === this.value) { + return argument + } throw new Error( `Argument does not match expected value (${ this.value diff --git a/compiler/types/NumberType.ts b/compiler/types/NumberType.ts index db2b5200..61f4eb98 100644 --- a/compiler/types/NumberType.ts +++ b/compiler/types/NumberType.ts @@ -1,24 +1,26 @@ import { Type } from "./Type" export class NumberType implements Type { - static instance = new NumberType() - kind = "number" + public static instance = new NumberType() + public kind = "number" - constructor() {} - - toConstructor() { + public toConstructor() { return "NumberType.instance" } - toString() { + public toString() { return this.kind } - convert(argument) { + public convert(argument) { let n = parseInt(argument) - if (!Number.isNaN(n)) return n + if (!Number.isNaN(n)) { + return n + } n = parseFloat(argument) - if (!Number.isNaN(n)) return n + if (!Number.isNaN(n)) { + return n + } throw new Error(`Can't convert to number: ${argument}`) } } diff --git a/compiler/types/ObjectType.ts b/compiler/types/ObjectType.ts index e32d5430..fb19ede5 100644 --- a/compiler/types/ObjectType.ts +++ b/compiler/types/ObjectType.ts @@ -1,23 +1,23 @@ import { Type } from "./Type" export class ObjectType implements Type { - kind = "object" + public kind = "object" // Note: a map that has an empty key ("") uses the corresponding type as default type constructor(public members: Map = new Map()) {} - toConstructor() { + public toConstructor() { return `new ObjectType(new Map([` + Array.from(this.members.entries()).map(([n, m]) => `[${JSON.stringify(n)}, ${m.toConstructor()}]`) .join(", ") + `]))` } - toString() { + public toString() { return this.kind } - convertMember(memberName: string[], memberValue: string) { + public convertMember(memberName: string[], memberValue: string) { let type = this.members.get(memberName[0]) if (!type) { // No type, try to get the default type @@ -27,13 +27,13 @@ export class ObjectType implements Type { return memberValue } } - if (type.kind == "object") { + if (type.kind === "object") { return (type as ObjectType).convertMember(memberName.slice(1), memberValue) } return type.convert(memberValue) } - convert(argument) { + public convert(argument) { try { return JSON.parse(argument) } catch (e) { diff --git a/compiler/types/StringType.ts b/compiler/types/StringType.ts index 57fe8c83..eb049c89 100644 --- a/compiler/types/StringType.ts +++ b/compiler/types/StringType.ts @@ -1,21 +1,23 @@ import { Type } from "./Type" export class StringType implements Type { - static instance = new StringType() - kind = "string" + public static instance = new StringType() + public kind = "string" constructor() {} - toConstructor() { + public toConstructor() { return "StringType.instance" } - toString() { + public toString() { return this.kind } - convert(argument) { - if (typeof argument === "string") return argument + public convert(argument) { + if (typeof argument === "string") { + return argument + } throw new Error(`Can't convert to string: ${argument}`) } } diff --git a/compiler/types/TupleType.ts b/compiler/types/TupleType.ts index 6fd08e2a..e1389a43 100644 --- a/compiler/types/TupleType.ts +++ b/compiler/types/TupleType.ts @@ -1,11 +1,11 @@ import { Type } from "./Type" export class TupleType implements Type { - kind = "tuple" + public kind = "tuple" constructor(public elemTypes: Type[]) {} - toConstructor() { + public toConstructor() { return ( `new TupleType([` + // Convert every element type to its constructor representation @@ -14,11 +14,11 @@ export class TupleType implements Type { ) } - toString() { + public toString() { return `[${this.elemTypes.map(e => e.toString()).join(", ")}]` } - convert(argument) { + public convert(argument) { if (!Array.isArray(argument)) { try { argument = JSON.parse(argument) @@ -29,7 +29,7 @@ export class TupleType implements Type { throw new Error(`Can't convert to tuple: ${argument}`) } } - if (argument.length != this.elemTypes.length) { + if (argument.length !== this.elemTypes.length) { throw new Error( `Error converting tuple: number of elements and type mismatch ${argument}`, ) diff --git a/compiler/types/Type.ts b/compiler/types/Type.ts index 008b998e..09557f66 100644 --- a/compiler/types/Type.ts +++ b/compiler/types/Type.ts @@ -5,5 +5,5 @@ export interface Type { name?: string toConstructor(): string toString(): string - convert: (argument: string) => any + convert(argument: string): any } diff --git a/compiler/types/TypeReferenceType.ts b/compiler/types/TypeReferenceType.ts index 8f135737..65b99985 100644 --- a/compiler/types/TypeReferenceType.ts +++ b/compiler/types/TypeReferenceType.ts @@ -1,9 +1,9 @@ import { Type } from "./Type" export class TypeReferenceType implements Type { - constructor(public kind: string, public args: Type[]) {} + public constructor(public kind: string, public args: Type[]) {} - toConstructor() { + public toConstructor() { return ( `new TypeReferenceType(${JSON.stringify(this.kind)}, [` + // Turn every type argument into its constructor representation @@ -12,11 +12,11 @@ export class TypeReferenceType implements Type { ) } - toString() { + public toString() { return `${this.kind}<${this.args.map(a => a.toString()).join(", ")}>` } - convert(argument) { + public convert(argument) { throw new Error("Conversion of simple type references not implemented.") } } diff --git a/compiler/types/UnionType.ts b/compiler/types/UnionType.ts index 5caf84bc..e261b259 100644 --- a/compiler/types/UnionType.ts +++ b/compiler/types/UnionType.ts @@ -1,11 +1,11 @@ import { Type } from "./Type" export class UnionType implements Type { - kind = "union" + public kind = "union" constructor(public types: Type[]) {} - toConstructor() { + public toConstructor() { return ( `new UnionType([` + // Convert every type to its string constructor representation @@ -14,12 +14,12 @@ export class UnionType implements Type { ) } - toString() { + public toString() { return this.types.map(t => t.toString()).join(" | ") } - convert(argument) { - for (let t of this.types) { + public convert(argument) { + for (const t of this.types) { try { return t.convert(argument) } catch (e) {} diff --git a/compiler/types/VoidType.ts b/compiler/types/VoidType.ts index 4d110a4f..3fecd1c6 100644 --- a/compiler/types/VoidType.ts +++ b/compiler/types/VoidType.ts @@ -1,20 +1,20 @@ import { Type } from "./Type" export class VoidType implements Type { - static instance = new VoidType() - kind = "void" + public static instance = new VoidType() + public kind = "void" constructor() {} - toConstructor() { + public toConstructor() { return "VoidType.instance" } - toString() { + public toString() { return this.kind } - convert(argument) { + public convert(argument) { return null } }