Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(typescript-estree): throw custom error instead of plain object #3011

Merged
merged 7 commits into from Feb 28, 2021
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