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

[Bug]: TSLiteralType should accept UnaryExpression #13327

Closed
1 task
alexandre-bbx opened this issue May 17, 2021 · 10 comments · Fixed by #13525
Closed
1 task

[Bug]: TSLiteralType should accept UnaryExpression #13327

alexandre-bbx opened this issue May 17, 2021 · 10 comments · Fixed by #13525
Labels
area: typescript good first issue i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: types

Comments

@alexandre-bbx
Copy link

alexandre-bbx commented May 17, 2021

💻

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

See REPL: https://runkit.com/alexandre-cai/60a2ae9feaab4400130133c8

transform(`
  type Test = 1 | -1;
`, {
  parserOpts: {
    plugins: ["typescript"],
  },
  plugins: [
    ({ types: t }) => ({
      visitor: {
        TSLiteralType(path) {
          path.node = t.tsLiteralType(path.node.literal)
        }
      }
    }),
  ],
})

Configuration file name

No response

Configuration

No response

Current and expected behavior

Babel currently accepts UnaryExpression while parsing TSLiteralType, but can’t generate one. Output:

TypeError: unknown: Property literal of TSLiteralType expected node to be of a type 
["NumericLiteral","StringLiteral","BooleanLiteral","BigIntLiteral"] but instead got "UnaryExpression"

Environment

  • @babel/core: 7.14.2

Possible solution

https://github.com/babel/babel/blob/main/packages/babel-types/src/definitions/typescript.ts#L342
Add UnaryExpression.

Additional context

No response

@babel-bot
Copy link
Collaborator

Hey @alexandre-cai! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite."

@nicolo-ribaudo
Copy link
Member

Thanks for the bug report!
The accepted type should be:

  • one of "NumericLiteral","StringLiteral","BooleanLiteral","BigIntLiteral"
  • or a UnaryExpression whose argument is one of "NumericLiteral","StringLiteral","BooleanLiteral","BigIntLiteral".

It can be implemented by replacing https://github.com/babel/babel/blob/main/packages/babel-types/src/definitions/typescript.ts#L342 with a custom validator, similarly to

validate: (function () {
.

@alexandre-bbx
Copy link
Author

I think UnaryExpression should only accept NumericLiteral and BigIntLiteral with an operand of -.

@Patil2099
Copy link

@nicolo-ribaudo I would love to help in fixing this issue. I am new to this project. Can you guide me a little more on closing this issue? Thanks!

@nicolo-ribaudo
Copy link
Member

nicolo-ribaudo commented May 19, 2021

@Patil2099 You follow these steps: (you need to have make and yarn available on your machine)

  1. Fork the repo
  2. Run git clone https://github.com/<YOUR_USERNAME>/babel.git && cd babel
  3. Run yarn && make bootstrap
  4. Wait ⏳
  5. Run make watch (or make build whenever you change a file)
  6. Add one or more tests in https://github.com/babel/babel/tree/main/packages/babel-types/test/builders/typescript
  7. Update the code!
  8. yarn jest babel-types to run the tests
  9. If it is working, run make test to run all the tests
  10. Run git push and open a PR!

I'm assigning this issue to you; if you need more help feel free to ask!

@tony-go
Copy link
Contributor

tony-go commented May 30, 2021

Hi 👋

Is this issue always available ?

Cc @nicolo-ribaudo @Patil2099

@nicolo-ribaudo
Copy link
Member

@tony-go Let's give @Patil2099 a few more days to work on this. If @Patil2099 doesn't reply within Wednesday, you can work on this.

As an alternative, if you prefer I opened #13399.

@tony-go
Copy link
Contributor

tony-go commented May 31, 2021

Hey @nicolo-ribaudo

Yeah for sure ! ✌🏼

@nicolo-ribaudo
Copy link
Member

@Patil2099 Hi! Are you still working on this?

@lala7573
Copy link
Contributor

lala7573 commented Jun 6, 2021

Is this issue available? I'd like to try this one.

colinaaa added a commit to colinaaa/babel that referenced this issue Jul 2, 2021
…ession

seems that the generated files is missing typings

fix babel#13327
nicolo-ribaudo pushed a commit that referenced this issue Jul 2, 2021
* fix(babel-types): add validator for TSLiteralType to accept UnaryExpression

seems that the generated files is missing typings

fix #13327

* fix(babel-types): add allowlist for TSLiteralType

now generated types seems correct

* fix(babel-types): use oneOfNodeTypes instead of oneOf
nicolo-ribaudo pushed a commit to nicolo-ribaudo/babel that referenced this issue Jul 30, 2021
…13525)

* fix(babel-types): add validator for TSLiteralType to accept UnaryExpression

seems that the generated files is missing typings

fix babel#13327

* fix(babel-types): add allowlist for TSLiteralType

now generated types seems correct

* fix(babel-types): use oneOfNodeTypes instead of oneOf
@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 2, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 2, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: typescript good first issue i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: types
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants