Skip to content

[Bug]: templateElement({ raw: 'string' }) leaves cooked unset / @babel/plugin-transform-template-literals requires cooked to be set #14682

@ehoogeveen-medweb

Description

@ehoogeveen-medweb

💻

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

import { transformFromAstSync } from '@babel/core';
import * as t from '@babel/types';

const { code } = transformFromAstSync(
  t.program([
    t.expressionStatement(
      t.templateLiteral(
        [t.templateElement({ raw: 'leading' }), t.templateElement({ raw: 'trailing' }, true)],
        [t.identifier('identifier')]
      )
    ),
  ]),
  '',
  { plugins: ['@babel/plugin-transform-template-literals'] }
);

console.log(code);

Configuration file name

No response

Configuration

No response

Current and expected behavior

Current output:

"".concat(identifier);

Expected output:

"leading".concat(identifier, "trailing");

Environment

  • Babel 7.18.5
  • Node 16.15.1

Possible solution

No response

Additional context

  • The cooked property is marked as optional, so I would expect it not to matter (e.g. match raw) if omitted.
  • With @babel/plugin-transform-template-literals disabled, I get the expected result `leading${identifier}trailing`.

Metadata

Metadata

Assignees

No one assigned

    Labels

    i: discussionoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issuepkg: types

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions