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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Invalid "Missing initializer in const declaration" error reported in .d.ts files #14871

Closed
1 task
michaldudak opened this issue Aug 23, 2022 · 8 comments 路 Fixed by #14923
Closed
1 task
Labels
area: typescript good first issue i: enhancement outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@michaldudak
Copy link

michaldudak commented Aug 23, 2022

馃捇

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

import * as babel from "@babel/core";

const source = `
export const foo: string;
`;

babel.parse(source, {
  configFile: false,
  filename: "source.d.ts",
  plugins: ["@babel/plugin-syntax-typescript"],
});

Configuration file name

No response

Configuration

No response

Current and expected behavior

After updating @babel/parser from 7.17.3 to 7.17.7, parsing .d.ts files, as shown above, fails with the following error:

SyntaxError: C:\Users\Michal\Desktop\babel-repro\source.d.ts: Missing initializer in const declaration. (2:24)

  1 |
> 2 | export const foo: string;
    |                         ^
  3 |
    at instantiate (C:\Users\Michal\Desktop\babel-repro\node_modules\@babel\parser\lib\index.js:72:32)
    at constructor (C:\Users\Michal\Desktop\babel-repro\node_modules\@babel\parser\lib\index.js:358:12)
    at Object.raise (C:\Users\Michal\Desktop\babel-repro\node_modules\@babel\parser\lib\index.js:3334:19)
    at Object.parseVar (C:\Users\Michal\Desktop\babel-repro\node_modules\@babel\parser\lib\index.js:15292:16)
    at Object.parseVarStatement (C:\Users\Michal\Desktop\babel-repro\node_modules\@babel\parser\lib\index.js:15085:10)
    at Object.parseVarStatement (C:\Users\Michal\Desktop\babel-repro\node_modules\@babel\parser\lib\index.js:10243:31)
    at Object.parseStatementContent (C:\Users\Michal\Desktop\babel-repro\node_modules\@babel\parser\lib\index.js:14631:21)
    at Object.parseStatementContent (C:\Users\Michal\Desktop\babel-repro\node_modules\@babel\parser\lib\index.js:10284:18)
    at Object.parseStatement (C:\Users\Michal\Desktop\babel-repro\node_modules\@babel\parser\lib\index.js:14556:17)
    at Object.parseExportDeclaration (C:\Users\Michal\Desktop\babel-repro\node_modules\@babel\parser\lib\index.js:15932:17) {
  code: 'BABEL_PARSE_ERROR',
  reasonCode: 'DeclarationMissingInitializer',
  loc: Position { line: 2, column: 24, index: 25 },
  pos: [Getter/Setter]
}

The official TypeScript compiler does not complain about this sample code.

Environment

System:
OS: Windows 10 10.0.19044
Binaries:
Node: 16.17.0 - C:\Program Files\nodejs\node.EXE
Yarn: 1.22.19 - ~\AppData\Roaming\npm\yarn.CMD
npm: 8.15.0 - ~\node_modules.bin\npm.CMD
npmPackages:
@babel/core: ^7.18.13 => 7.18.13
@babel/plugin-syntax-typescript: 7.16.7 => 7.16.7

Possible solution

No response

Additional context

No response

@babel-bot
Copy link
Collaborator

Hey @michaldudak! 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

nicolo-ribaudo commented Aug 23, 2022

We should expose the dts option of the typescript plugin of @babel/parser to @babel/plugin-syntax-typescript. As a workaround, try this:

babel.parseSync(source, {
  configFile: false,
  filename: "source.d.ts",
  parserOpts: {
    plugins: [
      ["typescript", { dts: true }]
    ]
  }
});

@nicolo-ribaudo
Copy link
Member

@AnmolBansalDEV I saw in other issues that you are looking for a good first issue: this could be it!

@babel/plugin-syntax-typescript currently accepts a disallowAmbiguousJSXLike option, and "forwards" it to the parser:

["typescript", { disallowAmbiguousJSXLike }],

We should do something similar for the dts option. You can add a test in https://github.com/babel/babel/tree/main/packages/babel-plugin-syntax-typescript/test/fixtures, adding a folder dts/enabled with the input code as in the above issue description and this config:

{
  "plugins": [["syntax-typescript", { "dts": true }]]
}

@AnmolBansalDEV
Copy link

Thank you @nicolo-ribaudo for suggesting! Right now, I'm traveling, I'll start working on this issue next week. Thank you for your cooperation!

@nicolo-ribaudo
Copy link
Member

nicolo-ribaudo commented Aug 25, 2022

Awesome! I'm assigning this to you :)
Let us know if you need any help!

@oceandrama
Copy link
Contributor

@nicolo-ribaudo I could help with this issue, if @AnmolBansalDEV doesn't mind

@sanyamgandhak
Copy link

Hi there ,
Please assign to me

@liuxingbaoyu
Copy link
Member

#14923
Hi, this issue has been fixed, just waiting for the minor release,

@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 Jan 26, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: typescript good first issue i: enhancement outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants