fix: Props are lost when the template replaces the node#15286
Merged
nicolo-ribaudo merged 3 commits intobabel:mainfrom Jul 25, 2024
Merged
fix: Props are lost when the template replaces the node#15286nicolo-ribaudo merged 3 commits intobabel:mainfrom
nicolo-ribaudo merged 3 commits intobabel:mainfrom
Conversation
liuxingbaoyu
commented
Dec 16, 2022
Comment on lines
+139
to
+155
| items[index] = replacement; | ||
| assignOrSet(items, index, replacement); | ||
| } | ||
| } else { | ||
| items[index] = replacement; | ||
| assignOrSet(items, index, replacement); |
Member
Author
There was a problem hiding this comment.
I didn't find a test example for these two cases.😞
Collaborator
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/57432 |
Member
nicolo-ribaudo
left a comment
There was a problem hiding this comment.
I would prefer to explicitly special-case identifiers: if parent[key].type === "Identifier" && parent[key].typeAnnotation && !replacement.typeAnnotation, we clone replacement and set its .typeAnnotation to parent[key].typeAnnotation.
Member
Author
|
I'm a little concerned that there will be cases other than the example. |
Member
|
The replaced node is always an identifier, because it's what we use as placeholder in templates. However you are right, there are multiple possible properties:
Some tests cases: template.statement.ast`
var ${t.objectPattern([])}: string = x;
`
template.statement.ast`
class X {
f(@dec ${t.identifier("x")}) {}
}
`;
template.statement.ast`
function f(${t.identifier("x")}?) {}
`
template.statement.ast`
function f(${ Object.assign(t.identifier("x"), { optional: true }) }: string) {}
`
template.statement.ast`
class X {
f(@dec ${ Object.assign(t.identifier("x"), { optional: true }) }) {}
}
`
// This should probably throw?
const typeAnotation = t.tsTypeAnnotation(t.tsStringKeyword());
template.statement.ast`
function f(${ Object.assign(t.identifier("x"), { typeAnnotation }) }: number) {}
` |
245a51e to
f53b8e7
Compare
Member
Author
|
Sorry forgot about this! Updated. |
JLHwung
approved these changes
Jul 24, 2024
nicolo-ribaudo
approved these changes
Jul 25, 2024
This was referenced Aug 16, 2024
This was referenced Oct 5, 2024
This was referenced Oct 5, 2024
This was referenced Oct 8, 2024
This was referenced Oct 24, 2024
This was referenced Oct 24, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.