benjamn / benjamn/ast-types

[Feature Request] API to ask ast-types if a field is optional, and if so what the default value is

Open
#504 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
1.2k
Forks
194
Avg merge
22h 43m
Merged PRs (30d)
10

Description

`ast-types` has `getFieldValue` to apply defaults, but no API to determine if a field is optional AFAICT. With `@babel/types` this is easy:

```
> require('@babel/types').NODE_FIELDS.Identifier
{
typeAnnotation: {
validate: [Function: validate] { oneOfNodeTypes: [Array] },
optional: true,
default: null
},
...
```
Would you be open to a PR to expose something like this as public API?

In [astx](https://github.com/codemodsquad/astx), a structural search-and-replace library I'm working on, I need to have a loose mode and a strict mode. For example in strict mode, if the pattern is an identifier without a type annotation, it would only match identifiers without a type annotation in the source code. Whereas in loose mode, it would also match identifiers that have a type annotation. Supporting both modes is only possible if I can ask `ast-types` if a field is optional.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reading ast-types' existing getFieldValue API and compare the metadata exposed by @babel/types NODE_FIELDS. Determine how a public API could report optional fields and their defaults, including the strict and loose matching needs described for astx. Done means the API shape and expected behavior are agreed and covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.