babel / babel

🐠 Babel is a compiler for writing next generation JavaScript.
https://babel.dev
MIT License
43.22k stars 5.64k forks source link

[Bug]: Template parser fails on syntactic placeholder for TS namespace name #16902

Open kbjr opened 2 weeks ago

kbjr commented 2 weeks ago

💻

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

const b = require('@babel/core');
const t = require('@babel/template');

const template = t.smart('export namespace %%foo%% { }', {
  plugins: ['typescript'],
  syntacticPlaceholders: true,
});

template({
  foo: b.types.identifier('bar'),
});

Configuration file name

No response

Configuration

No response

Current and expected behavior

The above code throws an error:

Uncaught [SyntaxError: Missing semicolon. (2:24)
  1 | /* @babel/template */;
> 2 | export namespace %%foo%% { }
    |                        ^
    =============
] {
  code: 'BABEL_TEMPLATE_PARSE_ERROR',
  reasonCode: 'MissingSemicolon',
  loc: Position { line: 2, column: 24, index: 47 },
  pos: 47,
  syntaxPlugin: undefined
}

It should not throw an error, it should successfully replace the placeholder with the given identifier and return an AST.

Environment

System:

Binaries:

npmPackages:

Possible solution

No response

Additional context

No response

babel-bot commented 2 weeks ago

Hey @kbjr! 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.