[Bug]: Template parser erases function argument type annotation when using syntactic placeholderย #16904
Closed
Description
๐ป
- Would you like to work on a fix?
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(`function foo(%%arg%%: string) : void { }`, {
plugins: ['typescript'],
syntacticPlaceholders: true,
});
const stmt = template({
arg: b.types.identifier('bar'),
});
console.log(stmt.params[0]);
Configuration file name
No response
Configuration
No response
Current and expected behavior
The string
type annotation on the function parameter should still exist in the returned AST, but it does not. The output parameter AST from the code above is just this:
{ type: 'Identifier', name: 'bar' }
Doing the same thing using a non-syntactic placeholder returns this more complete parameter instead, which is expected:
{
type: 'Identifier',
name: 'bar',
typeAnnotation: {
type: 'TSTypeAnnotation',
typeAnnotation: { type: 'TSStringKeyword', loc: undefined },
loc: undefined
}
}
Environment
System:
- OS: Linux 5.15 Ubuntu 22.04.4 LTS 22.04.4 LTS (Jammy Jellyfish) [Windows 11 + WSL2]
Binaries:
- Node: 20.13.0 - ~/.nvm/versions/node/v20.13.0/bin/node
- npm: 10.8.1 - ~/.nvm/versions/node/v20.13.0/bin/npm
npmPackages:
- @babel/core: ^7.25.2 => 7.25.2
- @babel/generator: ^7.25.6 => 7.25.6
- @babel/parser: ^7.25.6 => 7.25.6
- @babel/plugin-syntax-import-attributes: ^7.25.6 => 7.25.6
- @babel/plugin-syntax-typescript: ^7.25.4 => 7.25.4
- @babel/template: ^7.25.0 => 7.25.0
- @babel/traverse: ^7.25.6 => 7.25.6
- @babel/types: ^7.25.6 => 7.25.6
- babel-plugin-istanbul: ^7.0.0 => 7.0.0
Possible solution
No response
Additional context
I have patched my local copy of babel with the fix in #16903 to get past another issue (which worked). This behavior is consistent both with and without that patch.