Skip to content

Commit

Permalink
fix: remove TSJG prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed May 23, 2024
1 parent 964546a commit 7ec60d4
Show file tree
Hide file tree
Showing 23 changed files with 79 additions and 87 deletions.
12 changes: 6 additions & 6 deletions factory/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ import * as path from "node:path";
import normalize from "normalize-path";
import ts from "typescript";
import type { CompletedConfig, Config } from "../src/Config.js";
import { BuildTJSGError } from "../src/Error/Errors.js";
import { BuildError } from "../src/Error/Errors.js";

function loadTsConfigFile(configFile: string) {
const raw = ts.sys.readFile(configFile);

if (!raw) {
throw new BuildTJSGError({
throw new BuildError({
messageText: `Cannot read config file "${configFile}"`,
});
}

const config = ts.parseConfigFileTextToJson(configFile, raw);

if (config.error) {
throw new BuildTJSGError(config.error);
throw new BuildError(config.error);
}

if (!config.config) {
throw new BuildTJSGError({
throw new BuildError({
messageText: `Invalid parsed config file "${configFile}"`,
});
}
Expand Down Expand Up @@ -70,7 +70,7 @@ export function createProgram(config: CompletedConfig): ts.Program {
const rootNames = rootNamesFromPath.length ? rootNamesFromPath : tsconfig.fileNames;

if (!rootNames.length) {
throw new BuildTJSGError({
throw new BuildError({
messageText: "No input files",
});
}
Expand All @@ -81,7 +81,7 @@ export function createProgram(config: CompletedConfig): ts.Program {
const diagnostics = ts.getPreEmitDiagnostics(program);

if (diagnostics.length) {
throw new BuildTJSGError({
throw new BuildError({
messageText: "Type check error",
relatedInformation: [...diagnostics],
});
Expand Down
4 changes: 2 additions & 2 deletions src/ChainNodeParser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type ts from "typescript";
import { UnknownNodeTJSGError } from "./Error/Errors.js";
import { UnknownNodeError } from "./Error/Errors.js";
import type { MutableParser } from "./MutableParser.js";
import type { Context } from "./NodeParser.js";
import type { SubNodeParser } from "./SubNodeParser.js";
Expand Down Expand Up @@ -47,6 +47,6 @@ export class ChainNodeParser implements SubNodeParser, MutableParser {
}
}

throw new UnknownNodeTJSGError(node);
throw new UnknownNodeError(node);
}
}
4 changes: 2 additions & 2 deletions src/ChainTypeFormatter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UnknownTypeTJSGError } from "./Error/Errors.js";
import { UnknownTypeError } from "./Error/Errors.js";
import type { MutableTypeFormatter } from "./MutableTypeFormatter.js";
import type { Definition } from "./Schema/Definition.js";
import type { SubTypeFormatter } from "./SubTypeFormatter.js";
Expand Down Expand Up @@ -29,6 +29,6 @@ export class ChainTypeFormatter implements SubTypeFormatter, MutableTypeFormatte
}
}

throw new UnknownTypeTJSGError(type);
throw new UnknownTypeError(type);
}
}
7 changes: 4 additions & 3 deletions src/Error/BaseError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ const isTTY = process.env.TTY || process.stdout.isTTY;
/**
* Base error for ts-json-schema-generator
*/
export abstract class TJSGError extends Error {
export abstract class BaseError extends Error {
readonly diagnostic: ts.Diagnostic;

constructor(diagnostic: PartialDiagnostic) {
super(ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"));
this.diagnostic = TJSGError.createDiagnostic(diagnostic);
this.diagnostic = BaseError.createDiagnostic(diagnostic);
}

static createDiagnostic(diagnostic: PartialDiagnostic): ts.Diagnostic {
Expand All @@ -37,7 +37,8 @@ export abstract class TJSGError extends Error {
}

// @ts-expect-error - Differentiates from errors from the TypeScript compiler
diagnostic.code = `tjsg - ${diagnostic.code}`;
// error TSJ - 100: message
diagnostic.code = `J - ${diagnostic.code}`;

return Object.assign(
{
Expand Down
22 changes: 11 additions & 11 deletions src/Error/Errors.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import ts from "typescript";
import { PartialDiagnostic, TJSGError } from "./BaseError.js";
import { type PartialDiagnostic, BaseError } from "./BaseError.js";
import type { BaseType } from "../Type/BaseType.js";
import { JSONSchema7 } from "json-schema";
import type { JSONSchema7 } from "json-schema";

export class UnknownNodeTJSGError extends TJSGError {
export class UnknownNodeError extends BaseError {
constructor(readonly node: ts.Node) {
super({
code: 100,
Expand All @@ -13,7 +13,7 @@ export class UnknownNodeTJSGError extends TJSGError {
}
}

export class UnknownTypeTJSGError extends TJSGError {
export class UnknownTypeError extends BaseError {
constructor(readonly type: BaseType) {
super({
code: 101,
Expand All @@ -22,7 +22,7 @@ export class UnknownTypeTJSGError extends TJSGError {
}
}

export class RootlessTJSGError extends TJSGError {
export class RootlessError extends BaseError {
constructor(readonly fullName: string) {
super({
code: 102,
Expand All @@ -31,7 +31,7 @@ export class RootlessTJSGError extends TJSGError {
}
}

export class MultipleDefinitionsTJSGError extends TJSGError {
export class MultipleDefinitionsError extends BaseError {
constructor(
readonly name: string,
readonly defA: BaseType,
Expand All @@ -44,7 +44,7 @@ export class MultipleDefinitionsTJSGError extends TJSGError {
}
}

export class LogicTJSGError extends TJSGError {
export class LogicError extends BaseError {
constructor(
readonly node: ts.Node,
messageText: string,
Expand All @@ -57,7 +57,7 @@ export class LogicTJSGError extends TJSGError {
}
}

export class ExpectationFailedTJSGError extends TJSGError {
export class ExpectationFailedError extends BaseError {
constructor(
messageText: string,
readonly node?: ts.Node,
Expand All @@ -70,7 +70,7 @@ export class ExpectationFailedTJSGError extends TJSGError {
}
}

export class TypeTJSGError extends TJSGError {
export class JsonTypeError extends BaseError {
constructor(
messageText: string,
readonly type: BaseType,
Expand All @@ -82,7 +82,7 @@ export class TypeTJSGError extends TJSGError {
}
}

export class DefinitionTJSGError extends TJSGError {
export class DefinitionError extends BaseError {
constructor(
messageText: string,
readonly definition: JSONSchema7,
Expand All @@ -93,7 +93,7 @@ export class DefinitionTJSGError extends TJSGError {
});
}
}
export class BuildTJSGError extends TJSGError {
export class BuildError extends BaseError {
constructor(diag: Omit<PartialDiagnostic, "code">) {
super({
code: 108,
Expand Down
11 changes: 4 additions & 7 deletions src/NodeParser/IndexedAccessTypeNodeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { TupleType } from "../Type/TupleType.js";
import { UnionType } from "../Type/UnionType.js";
import { derefType } from "../Utils/derefType.js";
import { getTypeByKey } from "../Utils/typeKeys.js";
import { LogicTJSGError } from "../Error/Errors.js";
import { LogicError } from "../Error/Errors.js";

export class IndexedAccessTypeNodeParser implements SubNodeParser {
public constructor(
Expand Down Expand Up @@ -61,7 +61,7 @@ export class IndexedAccessTypeNodeParser implements SubNodeParser {
const indexTypes = indexType instanceof UnionType ? indexType.getTypes() : [indexType];
const propertyTypes = indexTypes.map((type) => {
if (!(type instanceof LiteralType || type instanceof StringType || type instanceof NumberType)) {
throw new LogicTJSGError(
throw new LogicError(
node,
`Unexpected type "${type.getId()}" (expected "LiteralType.js" or "StringType.js" or "NumberType.js")`,
);
Expand All @@ -78,13 +78,10 @@ export class IndexedAccessTypeNodeParser implements SubNodeParser {
return objectType;
}

throw new LogicTJSGError(
node,
`Invalid index "${type.getValue()}" in type "${objectType.getId()}"`,
);
throw new LogicError(node, `Invalid index "${type.getValue()}" in type "${objectType.getId()}"`);
}

throw new LogicTJSGError(node, `No additional properties in type "${objectType.getId()}"`);
throw new LogicError(node, `No additional properties in type "${objectType.getId()}"`);
}

return propertyType;
Expand Down
4 changes: 2 additions & 2 deletions src/NodeParser/IntersectionNodeParser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ts from "typescript";
import { ExpectationFailedTJSGError } from "../Error/Errors.js";
import { ExpectationFailedError } from "../Error/Errors.js";
import type { Context, NodeParser } from "../NodeParser.js";
import type { SubNodeParser } from "../SubNodeParser.js";
import type { BaseType } from "../Type/BaseType.js";
Expand Down Expand Up @@ -114,5 +114,5 @@ export function translate(types: BaseType[]): BaseType {
return new UnionType(result);
}

throw new ExpectationFailedTJSGError("Could not translate intersection to union.");
throw new ExpectationFailedError("Could not translate intersection to union.");
}
6 changes: 3 additions & 3 deletions src/NodeParser/IntrinsicNodeParser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ts from "typescript";
import { LogicTJSGError } from "../Error/Errors.js";
import { LogicError } from "../Error/Errors.js";
import type { Context } from "../NodeParser.js";
import type { SubNodeParser } from "../SubNodeParser.js";
import type { BaseType } from "../Type/BaseType.js";
Expand All @@ -23,7 +23,7 @@ export class IntrinsicNodeParser implements SubNodeParser {
const method = intrinsicMethods[methodName];

if (!method) {
throw new LogicTJSGError(node, `Unknown intrinsic method: ${methodName}`);
throw new LogicError(node, `Unknown intrinsic method: ${methodName}`);
}

const literals = extractLiterals(context.getArguments()[0])
Expand All @@ -40,7 +40,7 @@ function getParentName(node: ts.KeywordTypeNode): string {
const parent = node.parent;

if (!ts.isTypeAliasDeclaration(parent)) {
throw new LogicTJSGError(node, "Only intrinsics part of a TypeAliasDeclaration are supported.");
throw new LogicError(node, "Only intrinsics part of a TypeAliasDeclaration are supported.");
}

return parent.name.text;
Expand Down
4 changes: 2 additions & 2 deletions src/NodeParser/MappedTypeNodeParser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ts from "typescript";
import { ExpectationFailedTJSGError } from "../Error/Errors.js";
import { ExpectationFailedError } from "../Error/Errors.js";
import { Context, NodeParser } from "../NodeParser.js";
import { SubNodeParser } from "../SubNodeParser.js";
import { AnnotatedType } from "../Type/AnnotatedType.js";
Expand Down Expand Up @@ -92,7 +92,7 @@ export class MappedTypeNodeParser implements SubNodeParser {
return new ObjectType(id, [], [], false);
}

throw new ExpectationFailedTJSGError(
throw new ExpectationFailedError(
`Unexpected key type "${
constraintType ? constraintType.getId() : constraintType
}" for this node. (expected "UnionType" or "StringType")`,
Expand Down
6 changes: 3 additions & 3 deletions src/NodeParser/PrefixUnaryExpressionNodeParser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ts from "typescript";
import { ExpectationFailedTJSGError } from "../Error/Errors.js";
import { ExpectationFailedError } from "../Error/Errors.js";
import type { Context, NodeParser } from "../NodeParser.js";
import type { SubNodeParser } from "../SubNodeParser.js";
import type { BaseType } from "../Type/BaseType.js";
Expand Down Expand Up @@ -27,10 +27,10 @@ export class PrefixUnaryExpressionNodeParser implements SubNodeParser {
return new LiteralType(!operand.getValue());
}

throw new ExpectationFailedTJSGError("Unsupported prefix unary operator", node);
throw new ExpectationFailedError("Unsupported prefix unary operator", node);
}

throw new ExpectationFailedTJSGError(
throw new ExpectationFailedError(
`Expected operand to be "LiteralType" but is "${operand ? operand.constructor.name : operand}"`,
node,
);
Expand Down
6 changes: 3 additions & 3 deletions src/NodeParser/PromiseNodeParser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ts from "typescript";
import { ExpectationFailedTJSGError } from "../Error/Errors.js";
import { ExpectationFailedError } from "../Error/Errors.js";
import { Context, type NodeParser } from "../NodeParser.js";
import type { SubNodeParser } from "../SubNodeParser.js";
import { AliasType } from "../Type/AliasType.js";
Expand Down Expand Up @@ -64,7 +64,7 @@ export class PromiseNodeParser implements SubNodeParser {
const awaitedNode = this.typeChecker.typeToTypeNode(awaitedType, undefined, ts.NodeBuilderFlags.IgnoreErrors);

if (!awaitedNode) {
throw new ExpectationFailedTJSGError("Could not find awaited node", node);
throw new ExpectationFailedError("Could not find awaited node", node);
}

const baseNode = this.childNodeParser.createType(awaitedNode, new Context(node));
Expand All @@ -86,7 +86,7 @@ export class PromiseNodeParser implements SubNodeParser {
) {
if (ts.isExpressionWithTypeArguments(node)) {
if (!ts.isHeritageClause(node.parent)) {
throw new ExpectationFailedTJSGError(
throw new ExpectationFailedError(
"Expected ExpressionWithTypeArguments to have a HeritageClause parent",
node.parent,
);
Expand Down
4 changes: 2 additions & 2 deletions src/NodeParser/StringTemplateLiteralNodeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { LiteralType } from "../Type/LiteralType.js";
import { StringType } from "../Type/StringType.js";
import { UnionType } from "../Type/UnionType.js";
import { extractLiterals } from "../Utils/extractLiterals.js";
import { UnknownTypeTJSGError } from "../Error/Errors.js";
import { UnknownTypeError } from "../Error/Errors.js";

export class StringTemplateLiteralNodeParser implements SubNodeParser {
public constructor(protected childNodeParser: NodeParser) {}
Expand Down Expand Up @@ -41,7 +41,7 @@ export class StringTemplateLiteralNodeParser implements SubNodeParser {

return new UnionType(expandedTypes);
} catch (error) {
if (error instanceof UnknownTypeTJSGError) {
if (error instanceof UnknownTypeError) {
return new StringType();
}

Expand Down
11 changes: 4 additions & 7 deletions src/NodeParser/TypeofNodeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { getKey } from "../Utils/nodeKey.js";
import { LiteralType } from "../Type/LiteralType.js";
import { NeverType } from "../Type/NeverType.js";
import { FunctionType } from "../Type/FunctionType.js";
import { LogicTJSGError } from "../Error/Errors.js";
import { LogicError } from "../Error/Errors.js";

export class TypeofNodeParser implements SubNodeParser {
public constructor(
Expand All @@ -33,7 +33,7 @@ export class TypeofNodeParser implements SubNodeParser {
return new NeverType();
}

throw new LogicTJSGError(node, `No value declaration found for symbol "${symbol.name}"`);
throw new LogicError(node, `No value declaration found for symbol "${symbol.name}"`);
}

if (ts.isEnumDeclaration(valueDec)) {
Expand Down Expand Up @@ -67,10 +67,7 @@ export class TypeofNodeParser implements SubNodeParser {
return new FunctionType(<ts.FunctionDeclaration>valueDec);
}

throw new LogicTJSGError(
valueDec,
`Invalid type query for this declaration. (ts.SyntaxKind = ${valueDec.kind})`,
);
throw new LogicError(valueDec, `Invalid type query for this declaration. (ts.SyntaxKind = ${valueDec.kind})`);
}

protected createObjectFromEnum(node: ts.EnumDeclaration, context: Context, reference?: ReferenceType): ObjectType {
Expand All @@ -91,7 +88,7 @@ export class TypeofNodeParser implements SubNodeParser {
} else if (type instanceof LiteralType && typeof type.getValue() === "number") {
type = new LiteralType(+type.getValue() + 1);
} else {
throw new LogicTJSGError(member.name, `Enum initializer missing for "${name}"`);
throw new LogicError(member.name, `Enum initializer missing for "${name}"`);
}

return new ObjectProperty(name, type, true);
Expand Down
Loading

0 comments on commit 7ec60d4

Please sign in to comment.