zspecza / common-tags

🔖 Useful template literal tags for dealing with strings in ES2015+
Other
1.99k stars 60 forks source link

Don't assume array expression has preceding whitespace #21

Closed cnordhougen closed 8 years ago

cnordhougen commented 8 years ago

When includeArrays is used & an array expression has a non-whitespace character preceding it, an error is thrown. Example:

import { commaLists } from 'common-tags';

let templ, myArray = [ 'a', 'b', 'c' ];

// Does not work
templ = commaLists`
    (${myArray})
`;

// Add a space before the expression & suddenly it works.
templ = commaLists`
    ( ${myArray})
`;

I believe it's reasonable to expect to be able to produce output like (a, b, c).

The error is Cannot read property '1' of null, stemming from this line in tags.js:

expression = expression.join(sep + accumulator.match(/(\s+)$/)[1])

If there's no whitespace at the end of accumulator to copy, perhaps it could default to using a single space.

zspecza commented 8 years ago

@cnordhougen good point! Sounds like we need a new test case at the least. Otherwise, I'll look into this as soon as I get a chance!