Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

chore: refactorcreateError for easier use#11775

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
JoshuaKGoldberg merged 3 commits intotypescript-eslint:mainfromfisker:createError
Nov 26, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 24 additions & 26 deletionspackages/typescript-estree/src/check-modifiers.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -128,18 +128,13 @@ function nodeHasIllegalDecorators(
);
}

function throwError(node: ts.Node, message: string): never {
const ast = node.getSourceFile();
const start = node.getStart(ast);
const end = node.getEnd();

throw createError(message, ast, start, end);
}

export function checkModifiers(node: ts.Node): void {
// typescript<5.0.0
if (nodeHasIllegalDecorators(node)) {
throwError(node.illegalDecorators[0], 'Decorators are not valid here.');
throw createError(
node.illegalDecorators[0],
'Decorators are not valid here.',
);
}

for (const decorator of getDecorators(
Expand All@@ -149,12 +144,12 @@ export function checkModifiers(node: ts.Node): void {
// `checkGrammarModifiers` function in typescript
if (!nodeCanBeDecorated(node as TSNode)) {
if (ts.isMethodDeclaration(node) && !nodeIsPresent(node.body)) {
throwError(
throw createError(
decorator,
'A decorator can only decorate a method implementation, not an overload.',
);
} else {
throwError(decorator, 'Decorators are not valid here.');
throw createError(decorator, 'Decorators are not valid here.');
}
}
}
Expand All@@ -168,7 +163,7 @@ export function checkModifiers(node: ts.Node): void {
node.kind === SyntaxKind.PropertySignature ||
node.kind === SyntaxKind.MethodSignature
) {
throwError(
throw createError(
modifier,
`'${ts.tokenToString(
modifier.kind,
Expand All@@ -181,7 +176,7 @@ export function checkModifiers(node: ts.Node): void {
(modifier.kind !== SyntaxKind.StaticKeyword ||
!ts.isClassLike(node.parent))
) {
throwError(
throw createError(
modifier,
`'${ts.tokenToString(
modifier.kind,
Expand All@@ -196,7 +191,7 @@ export function checkModifiers(node: ts.Node): void {
modifier.kind !== SyntaxKind.ConstKeyword &&
node.kind === SyntaxKind.TypeParameter
) {
throwError(
throw createError(
modifier,
`'${ts.tokenToString(
modifier.kind,
Expand All@@ -214,7 +209,7 @@ export function checkModifiers(node: ts.Node): void {
ts.isTypeAliasDeclaration(node.parent)
))
) {
throwError(
throw createError(
modifier,
`'${ts.tokenToString(
modifier.kind,
Expand All@@ -229,7 +224,7 @@ export function checkModifiers(node: ts.Node): void {
node.kind !== SyntaxKind.IndexSignature &&
node.kind !== SyntaxKind.Parameter
) {
throwError(
throw createError(
modifier,
"'readonly' modifier can only appear on a property declaration or index signature.",
);
Expand All@@ -240,7 +235,7 @@ export function checkModifiers(node: ts.Node): void {
ts.isClassLike(node.parent) &&
!ts.isPropertyDeclaration(node)
) {
throwError(
throw createError(
modifier,
`'${ts.tokenToString(
modifier.kind,
Expand All@@ -254,7 +249,7 @@ export function checkModifiers(node: ts.Node): void {
) {
const declarationKind = getDeclarationKind(node.declarationList);
if (declarationKind === 'using' || declarationKind === 'await using') {
throwError(
throw createError(
modifier,
`'declare' modifier cannot appear on a '${declarationKind}' declaration.`,
);
Expand All@@ -270,7 +265,7 @@ export function checkModifiers(node: ts.Node): void {
node.kind !== SyntaxKind.GetAccessor &&
node.kind !== SyntaxKind.SetAccessor
) {
throwError(
throw createError(
modifier,
`'${ts.tokenToString(
modifier.kind,
Expand All@@ -286,7 +281,7 @@ export function checkModifiers(node: ts.Node): void {
(node.parent.kind === SyntaxKind.ModuleBlock ||
node.parent.kind === SyntaxKind.SourceFile)
) {
throwError(
throw createError(
modifier,
`'${ts.tokenToString(
modifier.kind,
Expand All@@ -298,7 +293,7 @@ export function checkModifiers(node: ts.Node): void {
modifier.kind === SyntaxKind.AccessorKeyword &&
node.kind !== SyntaxKind.PropertyDeclaration
) {
throwError(
throw createError(
modifier,
"'accessor' modifier can only appear on a property declaration.",
);
Expand All@@ -312,7 +307,7 @@ export function checkModifiers(node: ts.Node): void {
node.kind !== SyntaxKind.FunctionExpression &&
node.kind !== SyntaxKind.ArrowFunction
) {
throwError(modifier, "'async' modifier cannot be used here.");
throw createError(modifier, "'async' modifier cannot be used here.");
}

// `checkGrammarModifiers` function in `typescript`
Expand All@@ -323,7 +318,7 @@ export function checkModifiers(node: ts.Node): void {
modifier.kind === SyntaxKind.DeclareKeyword ||
modifier.kind === SyntaxKind.AsyncKeyword)
) {
throwError(
throw createError(
modifier,
`'${ts.tokenToString(
modifier.kind,
Expand All@@ -344,7 +339,10 @@ export function checkModifiers(node: ts.Node): void {
anotherModifier.kind === SyntaxKind.ProtectedKeyword ||
anotherModifier.kind === SyntaxKind.PrivateKeyword)
) {
throwError(anotherModifier, `Accessibility modifier already seen.`);
throw createError(
anotherModifier,
`Accessibility modifier already seen.`,
);
}
}
}
Expand All@@ -365,7 +363,7 @@ export function checkModifiers(node: ts.Node): void {
if (
!(func?.kind === SyntaxKind.Constructor && nodeIsPresent(func.body))
) {
throwError(
throw createError(
modifier,
'A parameter property is only allowed in a constructor implementation.',
);
Expand All@@ -379,7 +377,7 @@ export function checkModifiers(node: ts.Node): void {
node.kind === SyntaxKind.MethodDeclaration &&
node.parent.kind === SyntaxKind.ObjectLiteralExpression
) {
throwError(
throw createError(
modifier,
`'${ts.tokenToString(modifier.kind)}' modifier cannot be used here.`,
);
Expand Down
14 changes: 2 additions & 12 deletionspackages/typescript-estree/src/convert.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -57,9 +57,9 @@ export function convertError(
error: SemanticOrSyntacticError | ts.DiagnosticWithLocation,
): TSError {
return createError(
error.start!,
('message' in error && error.message) || (error.messageText as string),
error.file!,
error.start!,
);
}

Expand DownExpand Up@@ -166,18 +166,8 @@ export class Converter {
if (this.options.allowInvalidAST) {
return;
}
let start;
let end;
if (Array.isArray(node)) {
[start, end] = node;
} else if (typeof node === 'number') {
start = end = node;
} else {
start = node.getStart(this.ast);
end = node.getEnd();
}

throw createError(message, this.ast, start, end);
throw createError(node,message, this.ast);
}

/**
Expand Down
32 changes: 27 additions & 5 deletionspackages/typescript-estree/src/node-utils.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -678,18 +678,40 @@ export class TSError extends Error {
}
}

export function createError(node: ts.Node, message: string): TSError;
export function createError(
node: number | ts.Node | TSESTree.Range,
message: string,
ast: ts.SourceFile,
startIndex: number,
endIndex: number = startIndex,
sourceFile: ts.SourceFile,
): TSError;
export function createError(
node: number | ts.Node | TSESTree.Range,
message: string,
sourceFile?: ts.SourceFile,
): TSError {
let startIndex;
let endIndex;
if (Array.isArray(node)) {
[startIndex, endIndex] = node;
} else if (typeof node === 'number') {
startIndex = endIndex = node;
} else {
sourceFile ??= node.getSourceFile();
startIndex = node.getStart(sourceFile);
endIndex = node.getEnd();
}

if (!sourceFile) {
throw new Error('`sourceFile` is required.');
}

const [start, end] = [startIndex, endIndex].map(offset => {
const { character: column, line } =
ast.getLineAndCharacterOfPosition(offset);
sourceFile.getLineAndCharacterOfPosition(offset);
return { column, line: line + 1, offset };
});
return new TSError(message, ast.fileName, { end, start });

return new TSError(message, sourceFile.fileName, { end, start });
}

export function nodeHasTokens(n: ts.Node, ast: ts.SourceFile): boolean {
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp