Skip to content

Better documentation of escape characters to avoid: throw new Error('Found extraneous } in Chalk template literal'); #4

Description

@zmorris

Hi I hit this error while using template literals for a long string. I'm coming at this from a backend PHP and shell programming perspective where heredoc syntax (EOF, 'EOF') is often used when working with multiline text verbatim.

Anyway, the closest thing to that in Javascript is template literals using backticks. But since they're interpolated before Chalk receives them, it can be difficult to find the right escape sequence that provides proper formatting without triggering parsing errors. Also, some packages apply multiple levels of template parsing and character escaping, which makes it difficult to derive the exact incantation.

The project's readme.md barely mentions curly braces or escape characters. And so far even project issues don't reveal much:

chalk/chalk#498
chalk/chalk#349

I usually try every permutation of {{, \{, \\{, \{{, \\{{, etc. But sometimes things refuse to cooperate, and once programming becomes a random walk like that, it's better to reference a pre-built table of working examples. Which is why I'd like to see more open source projects adopt a third person view of themselves and understand that the smallest breadcrumbs can save their users many hours or sometimes even days of work. So I'm going to list some working examples here, and hopefully other people can add more permutations as they discover what works for other packages that use Chalk:

const chalk = require('chalk');

console.log(chalk`Hello, ${'world'}!`);
const chalk = require('chalk');

console.log(chalk`puppeteer.launch(\{ headless: false \}`);
const commandLineUsage = require('command-line-usage'); // uses chalk internally

const sections = [{
  content: `puppeteer.launch(\\{ headless: false \\}`
}];

console.log(commandLineUsage(sections));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    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