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 merge17 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
Show all changes
17 commits
Select commitHold shift + click to select a range
744c678
Chore: Extract AST check from convert.ts to ast-checks.ts
LonercodeNov 9, 2025
b52f216
chore: extract AST check from convert.ts to ast-checks.ts
LonercodeNov 9, 2025
9270fc4
chore: add test for ast-checks.ts
LonercodeNov 9, 2025
6ced6b3
chore: add test for ast-checks.ts
LonercodeNov 9, 2025
65803e4
chore: add test for ast-checks.ts
LonercodeNov 9, 2025
b628bfa
chore: add function in ast-checks to run ts node checks
LonercodeNov 15, 2025
78f1686
chore: fix lint errors
LonercodeNov 15, 2025
001ea12
chore: update test
LonercodeNov 15, 2025
f696ce1
chore: address review
LonercodeNov 19, 2025
5eb48ef
chore: review update
LonercodeNov 19, 2025
37c500f
chore: update based on reviews
LonercodeNov 27, 2025
dc4dcc0
Merge branch 'main' into extract-ast-checks-from-converter
LonercodeNov 27, 2025
4e5902b
chore: minor fix
LonercodeNov 27, 2025
91d2b00
Merge branch 'extract-ast-checks-from-converter' of https://github.co…
LonercodeNov 27, 2025
336c584
chore: merge main and update code
LonercodeNov 27, 2025
ee1ada9
chore: minor update
LonercodeNov 27, 2025
29a9c73
chore: update based on reviews
LonercodeNov 27, 2025
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
70 changes: 70 additions & 0 deletionspackages/typescript-estree/src/check-syntax-errors.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
import * as ts from 'typescript';

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

import { checkModifiers } from './check-modifiers';
import { isValidAssignmentTarget, createError } from './node-utils';

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

We should also define a

const SyntaxKind = ts.SyntaxKind;

here,https://github.com/Lonercode/typescript-eslint/blob/ee1ada9db5229902f70faa19dc27950dc457cc7d/packages/typescript-estree/src/convert.ts#L42

so other checks can be copied easier to here.

Lonercode reacted with thumbs up emoji

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Ehh should we? We don't generally do namespace property shorthands like that in the typescript-eslint monorepo. IMO it just adds a bit of clutter.

Copy link
Contributor

@fiskerfiskerNov 27, 2025
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

We'll add lots ofcases in switch, better align with convert.ts.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

At least keep it before it's done?

const SyntaxKind = ts.SyntaxKind;

export function checkSyntaxError(tsNode: ts.Node): void {
checkModifiers(tsNode);

const node = tsNode as TSNode;

switch (node.kind) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

[Refactor] I don't think this is a positive change for the logic? Converter functions generally directly call thecheckFor* logic they need. For example, for-in and for-of statements directly callcheckForStatementDeclaration. Now this newcheckSyntaxError function has to again switch onnode.kind. That's extra code structure and work.

@fisker please correct me if I'm wrong here, but I think it'd still be in the spirit of the issue to havecheckForStatementDeclaration be a standalone function separated out fromconvert.ts, and still called directly?

As in, the diffs toconvert.ts would look more like:

+ import { checkForStatementDeclaration } from "./checks/...";...      case SyntaxKind.ForInStatement:-       this.#checkForStatementDeclaration(node.initializer, node.kind);+       checkForStatementDeclaration(node.initializer, node.kind);        return this.createNode<TSESTree.ForInStatement>(node, {...-  #checkForStatementDeclaration(...) { ... }

Copy link
Contributor

@fiskerfiskerNov 28, 2025
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Take this

if(
node.caseBlock.clauses.filter(
switchCase=>switchCase.kind===SyntaxKind.DefaultClause,
).length>1
){
this.#throwError(
node,
"A 'default' clause cannot appear more than once in a 'switch' statement.",
);
}
as example, normally the check just few lines (there are even simpler checks, eg:1). Not every node need such long code likeSyntaxKind.ForInStatement.

If we call from covert.ts directly after thecase ..., we'll have lot of lines to add. and forcing many functions to import/export.

I think a single call likecheckModifiers() used did and duplicate the bigswitch(){} in check-syntax-error.ts is much easier to maintain.

Most kind of node check will inline, instead of separate function.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

@JoshuaKGoldberg@fisker Would it prove more efficient to separate outcheckForStatmentDeclaration? I thought the point of the issue was to extract those checks?

case SyntaxKind.ForInStatement:
case SyntaxKind.ForOfStatement: {
checkForStatementDeclaration(node);
break;
}
default: {
break;
}
Comment on lines +21 to +23
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Add this because ESLint complaint? Will this work?

Suggested change
default:{
break;
}
// No default

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I think it's valid, exiting with the default, for now at least.

}
}

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

import {checkModifiers } from './check-modifiers';
import {checkSyntaxError } from './check-syntax-errors';
import { getDecorators, getModifiers } from './getModifiers';
import {
canContainDirective,
Expand DownExpand Up@@ -105,58 +105,12 @@ export class Converter {
this.options = { ...options };
}

#checkForStatementDeclaration(
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.`,
);
}
}

#checkModifiers(node: ts.Node): void {
#checkSyntaxError(node: ts.Node): void {
if (this.options.allowInvalidAST) {
return;
}

checkModifiers(node);
checkSyntaxError(node);
}

#throwError(
Expand DownExpand Up@@ -528,7 +482,7 @@ export class Converter {
return null;
}

this.#checkModifiers(node);
this.#checkSyntaxError(node);

const pattern = this.allowPattern;
if (allowPattern != null) {
Expand DownExpand Up@@ -896,7 +850,6 @@ export class Converter {
});

case SyntaxKind.ForInStatement:
this.#checkForStatementDeclaration(node.initializer, node.kind);
return this.createNode<TSESTree.ForInStatement>(node, {
type: AST_NODE_TYPES.ForInStatement,
body: this.convertChild(node.statement),
Expand All@@ -905,7 +858,6 @@ export class Converter {
});

case SyntaxKind.ForOfStatement: {
this.#checkForStatementDeclaration(node.initializer, node.kind);
return this.createNode<TSESTree.ForOfStatement>(node, {
type: AST_NODE_TYPES.ForOfStatement,
await: Boolean(
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp