Skip to content

Commit

Permalink
update ts-morph to 18.0.0 (#2061)
Browse files Browse the repository at this point in the history
Updates dependencies to make spot work with later TS versions and
monorepos (yarn workspaces)
  • Loading branch information
dhmw authored Oct 12, 2023
1 parent 67dba6e commit 6354896
Show file tree
Hide file tree
Showing 15 changed files with 197 additions and 215 deletions.
2 changes: 1 addition & 1 deletion cli/src/commands/checksum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default class Checksum extends Command {
const hash = hashContract(contract);
this.log(hash);
} catch (e) {
this.error(e, { exit: 1 });
this.error(e as Error, { exit: 1 });
}
}
}
2 changes: 1 addition & 1 deletion cli/src/commands/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default class Docs extends Command {
this.log(`Open http://localhost:${port} to view documentation`);
await server.listen(port);
} catch (err) {
this.error(err, { exit: 1 });
this.error(err as Error, { exit: 1 });
}
};
start();
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export default class Mock extends Command {
}).defer();
this.log(`Mock server is running on port ${port}.`);
} catch (e) {
this.error(e, { exit: 1 });
this.error(e as Error, { exit: 1 });
}
}
}
2 changes: 1 addition & 1 deletion cli/src/commands/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default class Validate extends Command {
parse(args[ARG_API]);
this.log("Contract is valid");
} catch (e) {
this.error(e);
this.error(e as Error);
}
}
}
2 changes: 1 addition & 1 deletion cli/src/commands/validation-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default class ValidationServer extends Command {
await runValidationServer(port, contract).defer();
this.log(`Validation server running on port ${port}`);
} catch (e) {
this.error(e, { exit: 1 });
this.error(e as Error, { exit: 1 });
}
}
}
10 changes: 3 additions & 7 deletions lib/src/parsers/endpoint-parser.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
ClassDeclaration,
ObjectLiteralExpression,
TypeGuards
} from "ts-morph";
import { ClassDeclaration, ObjectLiteralExpression, Node } from "ts-morph";
import { Endpoint, Response } from "../definitions";
import { ParserError } from "../errors";
import { LociTable } from "../locations";
Expand Down Expand Up @@ -59,7 +55,7 @@ export function parseEndpoint(
?.getTags()
.find(tag => tag.getTagName() === "summary");

const summary = summaryNode?.getComment();
const summary = summaryNode?.getComment()?.toString();

// Handle draft
const draft = klass.getDecorator("draft") !== undefined;
Expand Down Expand Up @@ -161,7 +157,7 @@ function extractEndpointTags(

for (const elementExpr of tagsLiteral.getElements()) {
// Sanity check, typesafety should prevent any non-string tags
if (!TypeGuards.isStringLiteral(elementExpr)) {
if (!Node.isStringLiteral(elementExpr)) {
return err(
new ParserError("endpoint tag must be a string", {
file: elementExpr.getSourceFile().getFilePath(),
Expand Down
5 changes: 3 additions & 2 deletions lib/src/parsers/example-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ export function extractJSDocExamples(
const examples: Example[] = [];
let exampleError;
rawExamples.every(example => {
const exampleName = example?.split("\n")[0]?.trim();
const exampleValue = example?.split("\n")[1]?.trim();
const exampleStr = example?.toString();
const exampleName = exampleStr?.split("\n")[0]?.trim();
const exampleValue = exampleStr?.split("\n")[1]?.trim();

if (!exampleName || !exampleValue) {
exampleError = err(
Expand Down
2 changes: 1 addition & 1 deletion lib/src/parsers/oa3server-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ function parseOa3Variables(
const defaultTagNode = jsDocNode
?.getTags()
.find(tag => tag.getTagName() === "default");
const defaultTag = defaultTagNode?.getComment();
const defaultTag = defaultTagNode?.getComment()?.toString();
if (!defaultTag) {
return err(
new ParserError("@default tag is mandatory ! ", {
Expand Down
16 changes: 8 additions & 8 deletions lib/src/parsers/parser-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
SourceFile,
StringLiteral,
ts,
TypeGuards,
Node,
TypeNode
} from "ts-morph";
import { HttpMethod, QueryParamArrayStrategy } from "../definitions";
Expand Down Expand Up @@ -163,21 +163,21 @@ export function getParameterPropertySignaturesOrThrow(
export function parseTypeReferencePropertySignaturesOrThrow(
typeNode: TypeNode
): PropertySignature[] {
if (TypeGuards.isTypeReferenceNode(typeNode)) {
if (Node.isTypeReference(typeNode)) {
const typeReferenceNode = getTargetDeclarationFromTypeReference(typeNode);
if (typeReferenceNode.isErr()) throw typeReferenceNode;
const declaration = typeReferenceNode.unwrap();
// return early if the declaration is an interface
if (TypeGuards.isInterfaceDeclaration(declaration)) {
if (Node.isInterfaceDeclaration(declaration)) {
return declaration.getProperties();
}
const declarationAliasTypeNode = declaration.getTypeNodeOrThrow();
return parseTypeReferencePropertySignaturesOrThrow(
declarationAliasTypeNode
);
} else if (TypeGuards.isTypeLiteralNode(typeNode)) {
} else if (Node.isTypeLiteral(typeNode)) {
return typeNode.getProperties();
} else if (TypeGuards.isIntersectionTypeNode(typeNode)) {
} else if (Node.isIntersectionTypeNode(typeNode)) {
return typeNode
.getTypeNodes()
.map(parseTypeReferencePropertySignaturesOrThrow)
Expand Down Expand Up @@ -213,7 +213,7 @@ export function getDecoratorConfigOrThrow(
}
// expect the argument to be an object literal expression
const decoratorArg = decoratorArgs[0];
if (!TypeGuards.isObjectLiteralExpression(decoratorArg)) {
if (!Node.isObjectLiteralExpression(decoratorArg)) {
throw new Error(
`expected decorator factory configuration argument to be an object literal`
);
Expand All @@ -239,7 +239,7 @@ export function getObjLiteralProp<T>(
if (!property) {
return undefined;
}
if (!TypeGuards.isPropertyAssignment(property)) {
if (!Node.isPropertyAssignment(property)) {
throw new Error("expected property assignment");
}
return property;
Expand All @@ -258,7 +258,7 @@ export function getObjLiteralPropOrThrow<T>(
propertyName: Extract<keyof T, string>
): PropertyAssignment {
const property = objectLiteral.getPropertyOrThrow(propertyName);
if (!TypeGuards.isPropertyAssignment(property)) {
if (!Node.isPropertyAssignment(property)) {
throw new Error("expected property assignment");
}
return property;
Expand Down
5 changes: 3 additions & 2 deletions lib/src/parsers/schemaprop-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ export function extractJSDocSchemaProps(
}

rawSchemaProps.every(schemaProp => {
const schemaPropName = schemaProp?.split("\n")[0]?.trim();
const schemaPropValue = schemaProp?.split("\n")[1]?.trim();
const schemaPropStr = schemaProp?.toString();
const schemaPropName = schemaPropStr?.split("\n")[0]?.trim();
const schemaPropValue = schemaPropStr?.split("\n")[1]?.trim();

if (!schemaPropName || !schemaPropValue) {
schemaPropError = err(
Expand Down
59 changes: 32 additions & 27 deletions lib/src/parsers/type-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
InterfaceDeclaration,
IntersectionTypeNode,
LiteralTypeNode,
Node,
TypeAliasDeclaration,
TypeGuards,
TypeLiteralNode,
TypeNode,
TypeReferenceNode,
Expand Down Expand Up @@ -66,7 +66,7 @@ export function parseType(
lociTable: LociTable
): Result<Type, ParserError> {
// Type references must be parsed first to ensure internal type aliases are handled
if (TypeGuards.isTypeReferenceNode(typeNode)) {
if (Node.isTypeReference(typeNode)) {
if (
typeNode.getType().isArray() &&
typeNode.getTypeArguments().length > 0
Expand All @@ -76,25 +76,25 @@ export function parseType(
}
return parseTypeReference(typeNode, typeTable, lociTable);
// TODO: discourage native boolean keyword?
} else if (TypeGuards.isBooleanKeyword(typeNode)) {
} else if (Node.isBooleanKeyword(typeNode)) {
return ok(booleanType());
// TODO: discourage native string keyword?
} else if (TypeGuards.isStringKeyword(typeNode)) {
} else if (Node.isStringKeyword(typeNode)) {
return ok(stringType());
// TODO: discourage native number keyword?
} else if (TypeGuards.isNumberKeyword(typeNode)) {
} else if (Node.isNumberKeyword(typeNode)) {
return ok(floatType());
} else if (TypeGuards.isLiteralTypeNode(typeNode)) {
} else if (Node.isLiteralTypeNode(typeNode)) {
return parseLiteralType(typeNode);
} else if (TypeGuards.isArrayTypeNode(typeNode)) {
} else if (Node.isArrayTypeNode(typeNode)) {
return parseArrayType(typeNode, typeTable, lociTable);
} else if (TypeGuards.isTypeLiteralNode(typeNode)) {
} else if (Node.isTypeLiteral(typeNode)) {
return parseObjectLiteralType(typeNode, typeTable, lociTable);
} else if (TypeGuards.isUnionTypeNode(typeNode)) {
} else if (Node.isUnionTypeNode(typeNode)) {
return parseUnionType(typeNode, typeTable, lociTable);
} else if (TypeGuards.isIndexedAccessTypeNode(typeNode)) {
} else if (Node.isIndexedAccessTypeNode(typeNode)) {
return parseIndexedAccessType(typeNode, typeTable, lociTable);
} else if (TypeGuards.isIntersectionTypeNode(typeNode)) {
} else if (Node.isIntersectionTypeNode(typeNode)) {
return parseIntersectionTypeNode(typeNode, typeTable, lociTable);
} else {
throw new TypeNotAllowedError("unknown type", {
Expand Down Expand Up @@ -147,12 +147,12 @@ function parseTypeReference(
const jsDocNode = getJsDoc(declaration);
const description = jsDocNode?.getDescription().trim();

if (TypeGuards.isTypeAliasDeclaration(declaration)) {
if (Node.isTypeAliasDeclaration(declaration)) {
const decTypeNode = declaration.getTypeNodeOrThrow();
// if the type name is one of of the internal ones ensure they have not been redefined
// TODO: introduce some more type safety
if (SPOT_TYPE_ALIASES.includes(name)) {
if (TypeGuards.isTypeReferenceNode(decTypeNode)) {
if (Node.isTypeReference(decTypeNode)) {
throw new Error(`Internal type ${name} must not be redefined`);
} else if (declaration.getType().isString()) {
switch (name) {
Expand Down Expand Up @@ -251,18 +251,18 @@ function parseLiteralType(
ParserError
> {
const literal = typeNode.getLiteral();
if (TypeGuards.isBooleanLiteral(literal)) {
if (Node.isTrueLiteral(literal) || Node.isFalseLiteral(literal)) {
return ok(booleanLiteralType(literal.getLiteralValue()));
} else if (TypeGuards.isStringLiteral(literal)) {
} else if (Node.isStringLiteral(literal)) {
return ok(stringLiteralType(literal.getLiteralText()));
} else if (TypeGuards.isNumericLiteral(literal)) {
} else if (Node.isNumericLiteral(literal)) {
const numericValue = literal.getLiteralValue();
return ok(
Number.isInteger(numericValue)
? intLiteralType(numericValue)
: floatLiteralType(numericValue)
);
} else if (TypeGuards.isNullLiteral(literal)) {
} else if (Node.isNullLiteral(literal)) {
return ok(nullType());
} else {
return err(
Expand Down Expand Up @@ -420,7 +420,7 @@ function parseInterfaceDeclaration(
.getProperties()
.map(propertySymbol => {
const vd = propertySymbol.getValueDeclarationOrThrow();
if (!TypeGuards.isPropertySignature(vd)) {
if (!Node.isPropertySignature(vd)) {
throw new Error("expected property signature");
}
return vd;
Expand Down Expand Up @@ -629,10 +629,10 @@ function resolveIndexedAccessRootReference(
typeNode: IndexedAccessTypeNode
): Result<TypeReferenceNode, ParserError> {
const objectType = typeNode.getObjectTypeNode();
if (TypeGuards.isIndexedAccessTypeNode(objectType)) {
if (Node.isIndexedAccessTypeNode(objectType)) {
return resolveIndexedAccessRootReference(objectType);
}
if (!TypeGuards.isTypeReferenceNode(objectType)) {
if (!Node.isTypeReference(objectType)) {
return err(
new TypeNotAllowedError("Indexed access type must be reference", {
file: objectType.getSourceFile().getFilePath(),
Expand All @@ -657,7 +657,7 @@ function resolveIndexAccessPropertyAccessChain(
const acc = accResult.unwrap();

const literalTypeNode = typeNode.getIndexTypeNode();
if (!TypeGuards.isLiteralTypeNode(literalTypeNode)) {
if (!Node.isLiteralTypeNode(literalTypeNode)) {
throw new Error("expected type literal");
}
const literalTypeResult = parseLiteralType(literalTypeNode);
Expand All @@ -668,7 +668,7 @@ function resolveIndexAccessPropertyAccessChain(
}

const chainParent = typeNode.getObjectTypeNode();
if (TypeGuards.isIndexedAccessTypeNode(chainParent)) {
if (Node.isIndexedAccessTypeNode(chainParent)) {
return resolveIndexAccessPropertyAccessChain(
chainParent,
ok(acc.concat(literalType.value))
Expand Down Expand Up @@ -723,7 +723,7 @@ export function getTargetDeclarationFromTypeReference(

// Enums are not supported:
// enum SomeEnum { A, B, C }
if (TypeGuards.isEnumDeclaration(targetDeclaration)) {
if (Node.isEnumDeclaration(targetDeclaration)) {
return err(
new TypeNotAllowedError("Enums are not supported", {
file: targetDeclaration.getSourceFile().getFilePath(),
Expand All @@ -733,7 +733,7 @@ export function getTargetDeclarationFromTypeReference(
}

// References to enum constants (e.g SomeEnum.A) are not supported either.
if (TypeGuards.isEnumMember(targetDeclaration)) {
if (Node.isEnumMember(targetDeclaration)) {
return err(
new TypeNotAllowedError("Enums are not supported", {
file: targetDeclaration.getSourceFile().getFilePath(),
Expand All @@ -743,11 +743,16 @@ export function getTargetDeclarationFromTypeReference(
}

if (
TypeGuards.isInterfaceDeclaration(targetDeclaration) ||
TypeGuards.isTypeAliasDeclaration(targetDeclaration)
Node.isInterfaceDeclaration(targetDeclaration) ||
Node.isTypeAliasDeclaration(targetDeclaration)
) {
return ok(targetDeclaration);
}

throw new Error("expected a type alias or interface declaration");
return err(
new TypeNotAllowedError("expected a type alias or interface declaration", {
file: targetDeclaration.getSourceFile().getFilePath(),
position: targetDeclaration.getPos()
})
);
}
2 changes: 2 additions & 0 deletions lib/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export function tryCatch<T, E extends Error>(
try {
return ok(op());
} catch (e) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return err(e);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/validation-server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function runValidationServer(port: number, contract: Contract) {
};
res.json(responseBody);
} catch (error) {
res.status(500).send(makeInternalServerError([error.message]));
res.status(500).send(makeInternalServerError([(error as Error).message]));
}
});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"js-yaml": "^4.1.0",
"qs": "^6.10.1",
"randomstring": "^1.2.1",
"ts-morph": "^8.2.0",
"ts-morph": "18.0.0",
"typescript": "^4.3.4",
"validator": "^13.6.0"
},
Expand Down
Loading

0 comments on commit 6354896

Please sign in to comment.