Convert remaining `<T>` casts to `as T` (#466)

The regex I used in the last round was flawed and missed these.

Also remove some casts where possible, and change TypeInstantiationMap
set function's return type from string to void.
This commit is contained in:
Nick Guerrera 2021-04-21 09:48:52 -07:00 коммит произвёл GitHub
Родитель 55e7d5e88a
Коммит ea281c601c
3 изменённых файлов: 16 добавлений и 18 удалений

Просмотреть файл

@ -55,10 +55,9 @@ class MultiKeyMap<K extends object[], V> {
return this.#items.get(this.compositeKeyFor(items));
}
set(items: K, value: V): string {
set(items: K, value: V): void {
const key = this.compositeKeyFor(items);
this.#items.set(key, value);
return key;
}
private compositeKeyFor(items: K) {
@ -190,9 +189,9 @@ export function createChecker(program: Program) {
// template instantiation
const args = model.templateArguments.map(getTypeName);
return `${modelName}<${args.join(", ")}>`;
} else if ((<ModelStatementNode>model.node).templateParameters?.length > 0) {
} else if ((model.node as ModelStatementNode).templateParameters?.length > 0) {
// template
const params = (<ModelStatementNode>model.node).templateParameters.map((t) => t.id.sv);
const params = (model.node as ModelStatementNode).templateParameters.map((t) => t.id.sv);
return `${model.name}<${params.join(", ")}>`;
} else {
// regular old model.
@ -381,13 +380,13 @@ export function createChecker(program: Program) {
for (const statement of node.statements.map(getTypeForNode)) {
switch (statement.kind) {
case "Model":
type.models.set(statement.name, statement as ModelType);
type.models.set(statement.name, statement);
break;
case "Operation":
type.operations.set(statement.name, statement as OperationType);
type.operations.set(statement.name, statement);
break;
case "Namespace":
type.namespaces.set(statement.name, statement as NamespaceType);
type.namespaces.set(statement.name, statement);
break;
}
}
@ -681,13 +680,13 @@ export function createChecker(program: Program) {
// model =
// this will likely have to change, as right now `model =` is really just
// alias and so disappears. That means you can't easily rename symbols.
const assignmentType = getTypeForNode((<ModelStatementNode>node).assignment!);
const assignmentType = getTypeForNode(node.assignment!);
if (assignmentType.kind === "Model") {
const type: ModelType = createType({
...(<ModelType>assignmentType),
...assignmentType,
node: node,
name: (<ModelStatementNode>node).id.sv,
name: node.id.sv,
assignmentType,
namespace: getParentNamespaceType(node),
});
@ -768,7 +767,7 @@ export function createChecker(program: Program) {
// the types here aren't ideal and could probably be refactored.
function createType<T extends Type>(typeDef: T): T {
(<any>typeDef).templateArguments = templateInstantiation;
(typeDef as any).templateArguments = templateInstantiation;
program.executeDecorators(typeDef);
return typeDef;

Просмотреть файл

@ -115,8 +115,8 @@ export async function createProgram(
}
function executeDecorators(type: Type) {
if ((<any>type.node).decorators) {
for (const dec of (<any>type.node).decorators) {
if (type.node && "decorators" in type.node) {
for (const dec of type.node.decorators) {
executeDecorator(dec, program, type);
}
}
@ -139,12 +139,11 @@ export async function createProgram(
/**
* returns the JSON representation of a type. This is generally
* just the raw type objects, but string and number literals are
* treated specially.
* just the raw type objects, but literals are treated specially.
*/
function toJSON(type: Type): Type | string | number {
function toJSON(type: Type): Type | string | number | boolean {
if ("value" in type) {
return (<any>type).value;
return type.value;
}
return type;

Просмотреть файл

@ -146,7 +146,7 @@ export interface SymbolTable extends Map<string, Sym> {
*/
export interface TypeInstantiationMap {
get(args: Type[]): Type | undefined;
set(args: Type[], type: Type): string;
set(args: Type[], type: Type): void;
}
/**