Content-Length: 426857 | pFad | https://redirect.github.com/babel/babel/pull/15286

05 fix: Props are lost when the template replaces the node by liuxingbaoyu · Pull Request #15286 · babel/babel · GitHub
Skip to content
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: Props are lost when the template replaces the node #15286

Merged
merged 3 commits into from
Jul 25, 2024

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








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://redirect.github.com/babel/babel/pull/15286

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy