Skip to content

Commit

Permalink
feat(typescript-estree): throw custom error instead of plain object (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
armano2 committed Feb 28, 2021
1 parent c65a139 commit ae14bf5
Show file tree
Hide file tree
Showing 79 changed files with 263 additions and 234 deletions.
29 changes: 17 additions & 12 deletions packages/typescript-estree/src/node-utils.ts
Expand Up @@ -649,16 +649,26 @@ export function convertTokens(ast: ts.SourceFile): TSESTree.Token[] {
return result;
}

export interface TSError {
index: number;
lineNumber: number;
column: number;
message: string;
export class TSError extends Error {
constructor(
message: string,
public readonly fileName: string,
public readonly index: number,
public readonly lineNumber: number,
public readonly column: number,
) {
super(message);
Object.defineProperty(this, 'name', {
value: new.target.name,
enumerable: false,
configurable: true,
});
}
}

/**
* @param ast the AST object
* @param start the index at which the error starts
* @param start the index at which the error starts
* @param message the error message
* @returns converted error object
*/
Expand All @@ -668,12 +678,7 @@ export function createError(
message: string,
): TSError {
const loc = ast.getLineAndCharacterOfPosition(start);
return {
index: start,
lineNumber: loc.line + 1,
column: loc.character,
message,
};
return new TSError(message, ast.fileName, start, loc.line + 1, loc.character);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion packages/typescript-estree/tests/ast-fixtures.test.ts
@@ -1,10 +1,13 @@
import fs from 'fs';
import glob from 'glob';
import 'jest-specific-snapshot';
import { addSerializer } from 'jest-specific-snapshot';
import makeDir from 'make-dir';
import path from 'path';
import { parseAndGenerateServices } from '../src/parser';
import { isJSXFileType } from '../tools/test-utils';
import { serializer } from '../tools/tserror-serializer';

addSerializer(serializer);

// Assign a segment set to this variable to limit the test to only this segment
// This is super helpful if you need to debug why a specific fixture isn't producing the correct output
Expand Down

0 comments on commit ae14bf5

Please sign in to comment.