Skip to content

@babel/template crash when trying to parse template literal with an upper case variables #8723

@AlexMost

Description

@AlexMost

Bug Report

Current Behavior
Execution of template with a template literal with uppercase variable DDD - crashes. If I am changing variable name to ddd - it works.

Input Code
Thist code crashes in the node repl:

const tpl = require('@babel/template').default;
tpl('`test test ${DDD}`')()

this one works:

const tpl = require('@babel/template').default;
tpl('`test test ${ddd}`')()

Expected behavior/code
Should create an object with ast property.

Babel Configuration (.babelrc, package.json, cli command)
Was running this code in a repl without any additional babel configurations.

Environment

  • Babel version(s): 7.1.0
  • @babel/template 7.1.0
  • Node/npm version: Node 8.9.1/npm 5.5.1
  • OS: Ubuntu 14.04
  • Monorepo [no]

Additional context/Screenshots
That actually worked in version 6.x and started failing in 7.x.

In my case, I was able to fix that with calling the ast func:

const tpl = require('@babel/template').default;
tpl.ast('`test test ${DDD}`')

Stack trace:

TypeError: @babel/template placeholder "DDD": Property expressions[0] of TemplateLiteral expected node to be of a type ["Expression"] but instead got null
    at validate (/home/a.mostovenko/git/babel-plugin-ttag/node_modules/@babel/types/lib/definitions/utils.js:128:13)
    at validator (/home/a.mostovenko/git/babel-plugin-ttag/node_modules/@babel/types/lib/definitions/utils.js:97:7)
    at Object.validate (/home/a.mostovenko/git/babel-plugin-ttag/node_modules/@babel/types/lib/definitions/utils.js:172:7)
    at Object.validate (/home/a.mostovenko/git/babel-plugin-ttag/node_modules/@babel/types/lib/validators/validate.js:17:9)
    at applyReplacement (/home/a.mostovenko/git/babel-plugin-ttag/node_modules/@babel/template/lib/populate.js:130:9)
    at metadata.placeholders.slice.reverse.forEach.placeholder (/home/a.mostovenko/git/babel-plugin-ttag/node_modules/@babel/template/lib/populate.js:42:7)
    at Array.forEach (<anonymous>)
    at populatePlaceholders (/home/a.mostovenko/git/babel-plugin-ttag/node_modules/@babel/template/lib/populate.js:40:43)
    at arg (/home/a.mostovenko/git/babel-plugin-ttag/node_modules/@babel/template/lib/string.js:22:51)
    at arg (/home/a.mostovenko/git/babel-plugin-ttag/node_modules/@babel/template/lib/builder.js:77:14)
    =============
    at repl:1:1
    at ContextifyScript.Script.runInThisContext (vm.js:50:33)
    at REPLServer.defaultEval (repl.js:240:29)
    at bound (domain.js:301:14)
    at REPLServer.runBound [as eval] (domain.js:314:12)
    at REPLServer.onLine (repl.js:441:10)
    at emitOne (events.js:121:20)
    at REPLServer.emit (events.js:211:7)

Metadata

Metadata

Assignees

No one assigned

    Labels

    outdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions