Skip to content
This repository has been archived by the owner on Jun 22, 2020. It is now read-only.

Commit

Permalink
fix(declaration): improve TypeError message
Browse files Browse the repository at this point in the history
Includes the file path that the error was raised for
  • Loading branch information
mattyclarkson committed Feb 7, 2018
1 parent 9ed4db6 commit a7f96fc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
8 changes: 4 additions & 4 deletions lib/es/Declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ export type Interface = (ExportAllDeclaration | ExportNamedDeclaration) | Import
export type IOptions<T extends Interface> = IBaseOptions<T>;

export default class Declaration<T extends Interface> extends Base<T> {
constructor({ declaration, ...rest}: IOptions<T>) {
constructor({ declaration, file, ...rest}: IOptions<T>) {
// RAII checks
const { type, value } = (declaration.source as SimpleLiteral);
if (type !== 'Literal') {
throw new TypeError(`Invalid ES declaration source type: ${type}`);
throw new TypeError(`Invalid ES declaration source type '${type}' in '${file.source}'`);
}
if (typeof value !== 'string') {
throw new TypeError(`The type of the ES source value was not a 'string': ${typeof value}`);
throw new TypeError(`The type '${typeof value}' of the ES source value was not a 'string' for '${file.source}'`);
}

super({declaration, ...rest, path: value});
super({declaration, file, ...rest, path: value});
}

private get literal(): SimpleLiteral {
Expand Down
10 changes: 6 additions & 4 deletions lib/ts/Declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ export type Interface = ExportDeclaration | ImportDeclaration;
export type IOptions<T extends Interface> = IBaseOptions<T>;

export default class Declaration<T extends Interface> extends Base<T> {
constructor({ declaration, ...rest}: IOptions<T>) {

constructor({ declaration, file, ...rest}: IOptions<T>) {
// RAII checks
if (!declaration.moduleSpecifier) {
throw new TypeError(`No TS module specifier in '${file.source}'`);
}
const { kind, text } = (declaration.moduleSpecifier as StringLiteral);
if (kind !== SyntaxKind.StringLiteral) {
throw new TypeError(`Invalid TS declaration source type: ${kind}`);
throw new TypeError(`Invalid TS declaration literal kind '${kind}' in '${file.source}'`);
}

super({declaration, ...rest, path: text});
super({declaration, file, ...rest, path: text});
}

private get literal(): StringLiteral {
Expand Down

0 comments on commit a7f96fc

Please sign in to comment.