dmnd / dedent

⬅️ ES6 string tag that strips indentation from multi-line strings.
MIT License
910 stars 35 forks source link

Does not support escape sequences #24

Open lydell opened 6 years ago

lydell commented 6 years ago
$ node
> dedent = require(".").default
[Function: dedent]
> `a\xa0\tb`
'a \tb'
> dedent`a\xa0b`
'a\\xa0\\tb'
> ⏎       

(For people stumbling upon this issue: https://github.com/MartinKolarik/dedent-js might work better for you.)

theneva commented 3 years ago

Hi! I just stumbled on a variation of this surprising-to-me behaviour.

As a worksaround, it's possible to do what I do in the second ('works'): Replace \${hi} with ${'${hi}'}. That way, dedent only sees a string literal and doesn't have to care about escaping.

I figured I'd share in case anyone else comes here looking for answers :smile:

// test.js
const dedent = require('dedent');

test('fails', () => {
  const text = dedent`\${hi}`;
  expect(text).toBe('${hi}');
});

test('works', () => {
  const text = dedent`${'${hi}'}`;
  expect(text).toBe('${hi}');
});

image

(y is an alias for yarn.)