🌐 AI搜索 & 代理 主页
Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 deletions packages/typescript-estree/src/check-modifiers.ts
Original file line number Diff line number Diff 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 deletions packages/typescript-estree/src/convert.ts
Original file line number Diff line number Diff 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 Down Expand 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 deletions packages/typescript-estree/src/node-utils.ts
Original file line number Diff line number Diff 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