Skip to content

fix: Props are lost when the template replaces the node#15286

Merged
nicolo-ribaudo merged 3 commits intobabel:mainfrom
liuxingbaoyu:fix-template-node-props
Jul 25, 2024
Merged

fix: Props are lost when the template replaces the node#15286
nicolo-ribaudo merged 3 commits intobabel:mainfrom
liuxingbaoyu:fix-template-node-props

Conversation

@liuxingbaoyu
Copy link
Member

@liuxingbaoyu liuxingbaoyu commented Dec 16, 2022

Q                       A
Fixed Issues? Fixes #10636
Patch: Bug Fix?
Major: Breaking Change? ?
Minor: New Feature?
Tests Added + Pass?
Documentation PR Link
Any Dependency Changes?
License MIT

@liuxingbaoyu liuxingbaoyu added PR: Bug Fix 🐛 A type of pull request used for our changelog categories pkg: template labels Dec 16, 2022
Comment on lines +139 to +155
items[index] = replacement;
assignOrSet(items, index, replacement);
}
} else {
items[index] = replacement;
assignOrSet(items, index, replacement);
Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't find a test example for these two cases.😞

@babel-bot
Copy link
Collaborator

babel-bot commented Dec 16, 2022

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

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.

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.

@liuxingbaoyu
Copy link
Member Author

I'm a little concerned that there will be cases other than the example.

@nicolo-ribaudo
Copy link
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:

  • typeAnnotation
  • decorators
  • optional

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) {}
`

@liuxingbaoyu liuxingbaoyu force-pushed the fix-template-node-props branch from 245a51e to f53b8e7 Compare July 24, 2024 10:19
@liuxingbaoyu
Copy link
Member Author

Sorry forgot about this! Updated.

@liuxingbaoyu liuxingbaoyu marked this pull request as draft July 24, 2024 12:09
@liuxingbaoyu liuxingbaoyu marked this pull request as ready for review July 24, 2024 12:17
@liuxingbaoyu liuxingbaoyu changed the title fix: Props are lost when the template replaces the node. fix: Props are lost when the template replaces the node Jul 24, 2024
@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 Oct 25, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 25, 2024
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: template pkg: types PR: Bug Fix 🐛 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Type annotation is stripped from Typescript template

4 participants