-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added diagnostics to all errors (#1963)
* fix: errors for sourceless nodes * initial promise support * revert * feat: final implementation * fix: imports * code * lint * code * fix: tests * expose internals * i was wrong * fix: entire check * fix: lint * type * somehow tests were getting this out of order * fix: removed unused method * feat: added unnamed class test * fix: test * documented promise unwrapping * Update src/NodeParser/FunctionNodeParser.ts Co-authored-by: Dominik Moritz <[email protected]> * fix: removed allowUnionTypes * chore: style * feat: code * fixes * non tty errors * remove useless test * fix: keep same error message * fix: remove TSJG prefixes * fix yarn * keep exact error message --------- Co-authored-by: Dominik Moritz <[email protected]>
- Loading branch information
1 parent
4ac21b2
commit 49df2de
Showing
33 changed files
with
424 additions
and
333 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,63 @@ | ||
import ts from "typescript"; | ||
|
||
export type PartialDiagnostic = Omit<ts.Diagnostic, "category" | "file" | "start" | "length"> & { | ||
file?: ts.SourceFile; | ||
start?: number; | ||
length?: number; | ||
|
||
/** If we should populate `file`, `source`, `start` and `length` with this node information */ | ||
node?: ts.Node; | ||
|
||
/** @default Error */ | ||
category?: ts.DiagnosticCategory; | ||
}; | ||
|
||
const isTTY = process.env.TTY || process.stdout.isTTY; | ||
|
||
/** | ||
* Base error for ts-json-schema-generator | ||
*/ | ||
export abstract class BaseError extends Error { | ||
public constructor(message?: string) { | ||
super(message); | ||
readonly diagnostic: ts.Diagnostic; | ||
|
||
constructor(diagnostic: PartialDiagnostic) { | ||
super(ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n")); | ||
this.diagnostic = BaseError.createDiagnostic(diagnostic); | ||
} | ||
|
||
static createDiagnostic(diagnostic: PartialDiagnostic): ts.Diagnostic { | ||
// Swap the node for the file, source, start and length properties | ||
// sourceless nodes cannot be referenced in the diagnostic | ||
if (diagnostic.node && diagnostic.node.pos !== -1) { | ||
diagnostic.file = diagnostic.node.getSourceFile(); | ||
diagnostic.start = diagnostic.node.getStart(); | ||
diagnostic.length = diagnostic.node.getWidth(); | ||
|
||
diagnostic.node = undefined; | ||
} | ||
|
||
// @ts-expect-error - Differentiates from errors from the TypeScript compiler | ||
// error TSJ - 100: message | ||
diagnostic.code = `J - ${diagnostic.code}`; | ||
|
||
return Object.assign( | ||
{ | ||
category: ts.DiagnosticCategory.Error, | ||
file: undefined, | ||
length: 0, | ||
start: 0, | ||
}, | ||
diagnostic, | ||
); | ||
} | ||
|
||
format() { | ||
const formatter = isTTY ? ts.formatDiagnosticsWithColorAndContext : ts.formatDiagnostics; | ||
|
||
return formatter([this.diagnostic], { | ||
getCanonicalFileName: (fileName) => fileName, | ||
getCurrentDirectory: () => "", | ||
getNewLine: () => "\n", | ||
}); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import ts from "typescript"; | ||
import { type PartialDiagnostic, BaseError } from "./BaseError.js"; | ||
import type { BaseType } from "../Type/BaseType.js"; | ||
import type { JSONSchema7 } from "json-schema"; | ||
|
||
export class UnknownNodeError extends BaseError { | ||
constructor(readonly node: ts.Node) { | ||
super({ | ||
code: 100, | ||
node, | ||
messageText: `Unknown node of kind "${ts.SyntaxKind[node.kind]}"`, | ||
}); | ||
} | ||
} | ||
|
||
export class UnknownTypeError extends BaseError { | ||
constructor(readonly type: BaseType) { | ||
super({ | ||
code: 101, | ||
messageText: `Unknown type "${type.getId()}"`, | ||
}); | ||
} | ||
} | ||
|
||
export class RootlessError extends BaseError { | ||
constructor(readonly fullName: string) { | ||
super({ | ||
code: 102, | ||
messageText: `No root type "${fullName}" found`, | ||
}); | ||
} | ||
} | ||
|
||
export class MultipleDefinitionsError extends BaseError { | ||
constructor( | ||
readonly name: string, | ||
readonly defA: BaseType, | ||
readonly defB?: BaseType, | ||
) { | ||
super({ | ||
code: 103, | ||
messageText: `Type "${name}" has multiple definitions.`, | ||
}); | ||
} | ||
} | ||
|
||
export class LogicError extends BaseError { | ||
constructor( | ||
readonly node: ts.Node, | ||
messageText: string, | ||
) { | ||
super({ | ||
code: 104, | ||
messageText, | ||
node, | ||
}); | ||
} | ||
} | ||
|
||
export class ExpectationFailedError extends BaseError { | ||
constructor( | ||
messageText: string, | ||
readonly node?: ts.Node, | ||
) { | ||
super({ | ||
code: 105, | ||
messageText, | ||
node, | ||
}); | ||
} | ||
} | ||
|
||
export class JsonTypeError extends BaseError { | ||
constructor( | ||
messageText: string, | ||
readonly type: BaseType, | ||
) { | ||
super({ | ||
code: 106, | ||
messageText, | ||
}); | ||
} | ||
} | ||
|
||
export class DefinitionError extends BaseError { | ||
constructor( | ||
messageText: string, | ||
readonly definition: JSONSchema7, | ||
) { | ||
super({ | ||
code: 107, | ||
messageText, | ||
}); | ||
} | ||
} | ||
export class BuildError extends BaseError { | ||
constructor(diag: Omit<PartialDiagnostic, "code">) { | ||
super({ | ||
code: 108, | ||
...diag, | ||
}); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.