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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix t.arrowFunctionExpression and t.functionExpression builders by adding all fields #12334
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 56aa9b9:
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/32020/ |
53895b3
to
56aa9b9
Compare
I force pushed as I was branching from master, rebased from main and now I've broken a bunch of tests. So adding these properties to |
To answer my side note I guess t.identifier could be used, as that has a const g = require("@babel/generator").default;
console.log(
g({
name: "foo",
optional: true,
type: "Identifier",
typeAnnotation: {
type: "TSTypeAnnotation",
typeAnnotation: {
type: "TSStringKeyword",
},
},
}).code
); // foo I would expect something like |
@deificx As you may have noticed, we don't provide builder parameters for types in JS nodes. We only support parameters for JavaScript syntax and for JavaScript proposal. The recommended way of achieving what you need is this: Object.assign(t.arrowFunctionExpression(params, body, async), {
returnType: yourReturnTypeNode,
});
Object.assign(t.identifier("foo"), {
typeAnnotation: t.tsTypeAnnotation(t.tsStringKeyword())
}); |
It looks like you used to, ie. identifier:
Or is Adding a more complete example here, mostly for my understanding, identifier with type annotation must be used in parameters for it to print the type annotation, because by itself it would only print the identifier: const g = require("@babel/generator").default;
const t = require("@babel/types");
console.log(
g({
...t.arrowFunctionExpression(
[
{
...t.identifier("foo"),
typeAnnotation: t.tsTypeAnnotation(t.tsStringKeyword()),
},
{
...t.identifier("bar"),
typeAnnotation: t.tsTypeAnnotation(t.tsBooleanKeyword()),
},
],
t.stringLiteral("")
),
returnType: t.tsTypeAnnotation(t.tsStringKeyword()),
}).code
); // (foo: string, bar: boolean): string => "" |
The recommended way is what is shown in #12334 (comment). Thanks for opening the PR anyway, it's always good to check if old team decisions are still valid 🙏 Btw, you might also be interested in #9545. |
This fix adds the additional fields to the builder arrays for the definitions.
However, this does feel like a workaround, I also found this bug to apply to
assignmentPattern
, and there are probably more.It looks like defineType in utils.js makes sure to add both builder and visitor fields to a node while the builder function in builder.js only checks the builder values.
Somewhat unrelated, but I can't figure out how to add types to function parameters.
t.tsParameterProperty looks like the wrong type to me, it looks like something you'd add to a class (ie:
readonly property = "value"
).The closest thing I can find for this is t.tsPropertySignature, but that looks like some thing for an interface (ie:
key: type;
note the trailing;
).