8000 feat(typescript-estree): throw custom error instead of plain object (… · SanderHeling/typescript-eslint@ae14bf5 · GitHub
[go: up one dir, main page]

Skip to content

Commit ae14bf5

Browse files
authored
feat(typescript-estree): throw custom error instead of plain object (typescript-eslint#3011)
1 parent c65a139 commit ae14bf5

File tree

79 files changed

+263
-234
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+263
-234
lines changed

packages/typescript-estree/src/node-utils.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -649,16 +649,26 @@ export function convertTokens(ast: ts.SourceFile): TSESTree.Token[] {
649649
return result;
650650
}
651651

652-
export interface TSError {
653-
index: number;
654-
lineNumber: number;
655-
column: number;
656-
message: string;
652+
export class TSError extends Error {
653+
constructor(
654+
message: string,
655+
public readonly fileName: string,
656+
public readonly index: number,
657+
public readonly lineNumber: number,
658+
public readonly column: number,
659+
) {
660+
super(message);
661+
Object.defineProperty(this, 'name', {
662+
value: new.target.name,
663+
enumerable: false,
664+
configurable: true,
665+
});
666+
}
657667
}
658668

659669
/**
660670
* @param ast the AST object
661-
* @param start the index at which the error starts
671+
* @param start the index at which the error starts
662672
* @param message the error message
663673
* @returns converted error object
664674
*/
@@ -668,12 +678,7 @@ export function createError(
668678
message: string,
669679
): TSError {
670680
const loc = ast.getLineAndCharacterOfPosition(start);
671-
return {
672-
index: start,
673-
lineNumber: loc.line + 1,
674-
column: loc.character,
675-
message,
676-
};
681+
return new TSError(message, ast.fileName, start, loc.line + 1, loc.character);
677682
}
678683

679684
/**

packages/typescript-estree/tests/ast-fixtures.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import fs from 'fs';
22
import glob from 'glob';
3-
import 'jest-specific-snapshot';
3+
import { addSerializer } from 'jest-specific-snapshot';
44
import makeDir from 'make-dir';
55
import path from 'path';
66
import { parseAndGenerateServices } from '../src/parser';
77
import { isJSXFileType } from '../tools/test-utils';
8+
import { serializer } from '../tools/tserror-serializer';
9+
10+
addSerializer(serializer);
811

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

0 commit comments

Comments
 (0)
0