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: extract AST check from convert.ts to ast-checks.ts#11748

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

Open
Lonercode wants to merge5 commits intotypescript-eslint:main
base:main
Choose a base branch
Loading
fromLonercode:extract-ast-checks-from-converter
Open
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
54 changes: 54 additions & 0 deletionspackages/typescript-estree/src/ast-checks.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
import * as ts from 'typescript';

import type { TSESTree } from './ts-estree';

import { isValidAssignmentTarget } from './node-utils';

export function checkForStatementDeclaration(
initializer: ts.ForInitializer,
kind: ts.SyntaxKind.ForInStatement | ts.SyntaxKind.ForOfStatement,
throwError: (
node: number | ts.Node | TSESTree.Range,
message: string,
) => never,
): void {
const loop = kind === ts.SyntaxKind.ForInStatement ? 'for...in' : 'for...of';
if (ts.isVariableDeclarationList(initializer)) {
if (initializer.declarations.length !== 1) {
throwError(
initializer,
`Only a single variable declaration is allowed in a '${loop}' statement.`,
);
}
const declaration = initializer.declarations[0];
if (declaration.initializer) {
throwError(
declaration,
`The variable declaration of a '${loop}' statement cannot have an initializer.`,
);
} else if (declaration.type) {
throwError(
declaration,
`The variable declaration of a '${loop}' statement cannot have a type annotation.`,
);
}
if (
kind === ts.SyntaxKind.ForInStatement &&
initializer.flags & ts.NodeFlags.Using
) {
throwError(
initializer,
"The left-hand side of a 'for...in' statement cannot be a 'using' declaration.",
);
}
} else if (
!isValidAssignmentTarget(initializer) &&
initializer.kind !== ts.SyntaxKind.ObjectLiteralExpression &&
initializer.kind !== ts.SyntaxKind.ArrayLiteralExpression
) {
throwError(
initializer,
`The left-hand side of a '${loop}' statement must be a variable or a property access.`,
);
}
}
42 changes: 2 additions & 40 deletionspackages/typescript-estree/src/convert.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,6 +10,7 @@ import type {
import type { SemanticOrSyntacticError } from './semantic-or-syntactic-errors';
import type { TSESTree, TSESTreeToTSNode, TSNode } from './ts-estree';

import { checkForStatementDeclaration } from './ast-checks';
import { checkModifiers } from './check-modifiers';
import { getDecorators, getModifiers } from './getModifiers';
import {
Expand DownExpand Up@@ -109,46 +110,7 @@ export class Converter {
initializer: ts.ForInitializer,
kind: ts.SyntaxKind.ForInStatement | ts.SyntaxKind.ForOfStatement,
): void {
const loop =
kind === ts.SyntaxKind.ForInStatement ? 'for...in' : 'for...of';
if (ts.isVariableDeclarationList(initializer)) {
if (initializer.declarations.length !== 1) {
this.#throwError(
initializer,
`Only a single variable declaration is allowed in a '${loop}' statement.`,
);
}
const declaration = initializer.declarations[0];
if (declaration.initializer) {
this.#throwError(
declaration,
`The variable declaration of a '${loop}' statement cannot have an initializer.`,
);
} else if (declaration.type) {
this.#throwError(
declaration,
`The variable declaration of a '${loop}' statement cannot have a type annotation.`,
);
}
if (
kind === ts.SyntaxKind.ForInStatement &&
initializer.flags & ts.NodeFlags.Using
) {
this.#throwError(
initializer,
"The left-hand side of a 'for...in' statement cannot be a 'using' declaration.",
);
}
} else if (
!isValidAssignmentTarget(initializer) &&
initializer.kind !== ts.SyntaxKind.ObjectLiteralExpression &&
initializer.kind !== ts.SyntaxKind.ArrayLiteralExpression
) {
this.#throwError(
initializer,
`The left-hand side of a '${loop}' statement must be a variable or a property access.`,
);
}
checkForStatementDeclaration(initializer, kind, this.#throwError);
}

#checkModifiers(node: ts.Node): void {
Expand Down
146 changes: 146 additions & 0 deletionspackages/typescript-estree/tests/lib/ast-checks.test.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
import * as ts from 'typescript';

import type { TSESTree } from '../../src/ts-estree';

import { checkForStatementDeclaration } from '../../src/ast-checks';

describe(checkForStatementDeclaration, () => {
let throwError: (
node: number | ts.Node | TSESTree.Range,
message: string,
) => never;

beforeEach(() => {
throwError = vi.fn((node, message: string) => {
throw new Error(message);
}) as unknown as typeof throwError;
});

it('allows single variable declaration with no initializer or type', () => {
const node = ts.factory.createVariableDeclarationList([
ts.factory.createVariableDeclaration('x'),
]);

expect(() =>
checkForStatementDeclaration(
node,
ts.SyntaxKind.ForOfStatement,
throwError,
),
).not.toThrow();
});

it('throws when multiple declarations', () => {
const node = ts.factory.createVariableDeclarationList([
ts.factory.createVariableDeclaration('x'),
ts.factory.createVariableDeclaration('y'),
]);

expect(() =>
checkForStatementDeclaration(
node,
ts.SyntaxKind.ForOfStatement,
throwError,
),
).toThrow(
"Only a single variable declaration is allowed in a 'for...of' statement.",
);
});

it('throws when declaration has an initializer', () => {
const node = ts.factory.createVariableDeclarationList([
ts.factory.createVariableDeclaration(
'x',
undefined,
undefined,
ts.factory.createNumericLiteral(5),
),
]);

expect(() =>
checkForStatementDeclaration(
node,
ts.SyntaxKind.ForOfStatement,
throwError,
),
).toThrow(
"The variable declaration of a 'for...of' statement cannot have an initializer.",
);
});

it('throws when declaration has a type annotation', () => {
const node = ts.factory.createVariableDeclarationList([
ts.factory.createVariableDeclaration(
'x',
undefined,
ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
),
]);

expect(() =>
checkForStatementDeclaration(
node,
ts.SyntaxKind.ForOfStatement,
throwError,
),
).toThrow(
"The variable declaration of a 'for...of' statement cannot have a type annotation.",
);
});

it('throws when "using" declaration in for...in', () => {
const node = ts.factory.createVariableDeclarationList(
[ts.factory.createVariableDeclaration('x')],
ts.NodeFlags.Using,
);

expect(() =>
checkForStatementDeclaration(
node,
ts.SyntaxKind.ForInStatement,
throwError,
),
).toThrow(
"The left-hand side of a 'for...in' statement cannot be a 'using' declaration.",
);
});

it('throws when initializer is not a valid assignment target', () => {
const node = ts.factory.createNumericLiteral(42);

expect(() =>
checkForStatementDeclaration(
node,
ts.SyntaxKind.ForOfStatement,
throwError,
),
).toThrow(
"The left-hand side of a 'for...of' statement must be a variable or a property access.",
);
});

it('handles both for...in and for...of paths', () => {
const forInNode = ts.factory.createVariableDeclarationList([
ts.factory.createVariableDeclaration('x'),
]);
const forOfNode = ts.factory.createVariableDeclarationList([
ts.factory.createVariableDeclaration('x'),
]);

expect(() =>
checkForStatementDeclaration(
forInNode,
ts.SyntaxKind.ForInStatement,
throwError,
),
).not.toThrow();

expect(() =>
checkForStatementDeclaration(
forOfNode,
ts.SyntaxKind.ForOfStatement,
throwError,
),
).not.toThrow();
});
});

[8]ページ先頭

©2009-2025 Movatter.jp