Skip to content

Conversation

@JLHwung
Copy link
Contributor

@JLHwung JLHwung commented Aug 28, 2025

Q                       A
License MIT

In this PR we enabled the @ts-check on all babel-types scripts.
In this PR we converted babel-types scripts to TS.

The field Validator type is also revised such that it can be distributed to a series of sub types given specific validator kind such as oneOf or oneOfNodeTypes. Thanks to the improved types, we can get rid of a few type castings or suppressed errors in our AST definitions.

The toFunctionName helper is replaced by the formatBuilderName since they serve for the same builder name calculation purpose.

@JLHwung JLHwung added the PR: Internal 🏠 A type of pull request used for our changelog categories label Aug 28, 2025
@babel-bot
Copy link
Collaborator

babel-bot commented Aug 28, 2025

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/59898

return indexA - indexB;
});
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These 3 methods are extracted to fieldHelpers, shared by the ast-types builders.

return Array.isArray(ret) && ret.length === 1 && ret[0] === "any"
? stringifyValidator(validator.chainOf[0], nodePrefix)
: ret;
if ("chainOf" in validator) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Previously the chainOf depends on the special return value ["any"] for general validator ValidatorImpl, here we always return any for such validators and then extract the first meaningful type when iterating from the end to start.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Aug 28, 2025

Open in StackBlitz

commit: 4550ff7

shapeKey +
(isOptional ? "?: " : ": ") +
stringifyValidator(propertyDefinition.validate)
stringifyValidator(propertyDefinition.validate, nodePrefix)
Copy link
Contributor Author

@JLHwung JLHwung Aug 28, 2025

Choose a reason for hiding this comment

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

This is a fix. nodePrefix should be passed through all stringifyValidator calls, although we don't have shapeOf({ a: assertOfNodeType("...") }) usage yet.

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

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

Eventually we'll be able to just rewrite these scripts to TS, since if I remember correctly we don't run them on old Node.js versions.

@JLHwung
Copy link
Contributor Author

JLHwung commented Aug 29, 2025

Eventually we'll be able to just rewrite these scripts to TS, since if I remember correctly we don't run them on old Node.js versions.

Yes, they are build scripts so probably safe to do so. Attached the converted TS.

@JLHwung JLHwung force-pushed the type-checking-babel-types-scripts branch from 057efe7 to 6f737f9 Compare August 29, 2025 15:21
@JLHwung JLHwung requested a review from nicolo-ribaudo August 29, 2025 15:22
Comment on lines 33 to 34
* @param {Validator | undefined} validator
* @returns {string[]}
Copy link
Member

Choose a reason for hiding this comment

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

Since we are already using ts, can we remove these JSDOC type definitions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The JSdoc provides more information than type definitions. It is a convenient place for examples, brief summary of the method or the parameter. So I think it'd better to keep it as-is.

Choose a reason for hiding this comment

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

Only removing the types should work, since they're redundant.

If you're using eslint-plugin-jsdoc, I have the following near the top of the typescript-specific eslint config for my project:

{
		'jsdoc/no-types': 'error', // Parameters should get their type info from TypeScript.
		'jsdoc/no-undefined-types': 'off', // TypeScript already checks whether types are known.
		'jsdoc/require-param-type': 'off', // Param types should be set using TypeScript.
		'jsdoc/require-property-type': 'off', // Property types should be set using TypeScript.
		'jsdoc/require-returns-type': 'off', // Return type should be set using TypeScript.
		'jsdoc/require-template': 'off', // Templates are set using TypeScript.
}

Probably not needed if you're using the flat/recommended-typescript config or similar though.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I meant to also remove type definitions that are identical to TS types, while keeping the comments. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I will add eslint-plugin-jsdoc in a new PR so that the changeset here will not be bloated.

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

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

(pending fixing/ignoring the lint error)

@JLHwung JLHwung merged commit 092d2e2 into babel:main Sep 5, 2025
76 checks passed
@JLHwung JLHwung deleted the type-checking-babel-types-scripts branch September 5, 2025 16:12
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Dec 6, 2025
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 6, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: types PR: Internal 🏠 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants