mirror of
https://github.com/vale981/tridactyl
synced 2025-03-06 10:01:39 -05:00

This commit makes the compiler pass use different classes in order to represent the metadata. This enables adding per-class toString/convert functions. This enables easy type checking and conversion in the `:set` excmd.
29 lines
747 B
TypeScript
29 lines
747 B
TypeScript
import { Type } from "./Type"
|
|
|
|
export class ArrayType implements Type {
|
|
kind = "array"
|
|
|
|
constructor(public elemType: Type) {}
|
|
|
|
toConstructor() {
|
|
return `new ArrayType(${this.elemType.toConstructor()})`
|
|
}
|
|
|
|
toString() {
|
|
return `${this.elemType.toString()}[]`
|
|
}
|
|
|
|
convert(argument) {
|
|
if (!Array.isArray(argument)) {
|
|
try {
|
|
argument = JSON.parse(argument)
|
|
} catch (e) {
|
|
throw new Error(`Can't convert ${argument} to array:`)
|
|
}
|
|
if (!Array.isArray(argument)) {
|
|
throw new Error(`Can't convert ${argument} to array:`)
|
|
}
|
|
}
|
|
return argument.map(v => this.elemType.convert(v))
|
|
}
|
|
}
|