microsoft / microsoft/TypeScript
Support JSX transformations with custom jsxFactory
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.4k
- Merge moyen
- 1 j 19 h
- PR mergées (30 j)
- 117
Description
Search Terms
jsx, jsxFactory, transformer
Suggestion
Allow tapping into the JSX generation process to control React.createElement output format when using --jsxFactory.
Specific cases requested (e.g. #15217, #32619) would also benefit from a generalized solution.
Use Cases
Emit component children as thunks
Useful to reactive frameworks which attach dependency tracking via the JSX component hierarchy.
The internal createJsxFactoryExpression() call LHS is controlled using the --jsxFactory compiler option:
// jsx
<div>
<div>a</div>
<div>b</div>
</div>
// current emit (jsxFactory: "h")
h('div', null,
h('div', null, 'a'),
h('div', null, 'b')
)
This produces a nested React.createElement()/h() chain which evaluates eagerly.
With (still synchronous) thunks, frameworks can track each component dependency as they traverse the tree, for fine-grained change detection:
// thunks (needs transformer)
h('div', null, () => [
h('div', null, () => ['a']),
h('div', null, () => ['b'])
])
This requires recursively transforming the JSX emits to a signature of h() which accepts some args as a thunk.
I've written a hacky (#16607) transformer for this but had to extract most of the JSX-related internals from the TypeScript compiler, whereas making the change to createExpressionForJsxElement() is trivial:
const cs = argumentsList.slice(2);
const argThunk = ts.createArrowFunction(
[],
[],
[],
undefined,
undefined,
ts.createArrayLiteral(cs)
);
const thunkified = [...argumentsList.slice(0, 2), argThunk];
According to #34547, plans are already in motion to pass nested component args as arrays instead of varargs. Specifying the jsxFactory implies we are assuming control of the composition behavior; we should also control the function signature.
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par lire createJsxFactoryExpression() et createExpressionForJsxElement() dans src/compiler/factory.ts, puis examinez les exigences associées dans les issues #15217, #32619 et #34547. Le travail est considéré comme terminé lorsqu’une manière généralisée de contrôler la sortie de la factory JSX, y compris les arguments de composants imbriqués, est définie et implémentée sans dépendre d’un transformer privé.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- javascript, typescript
- Domaine
- compilers
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 25/100