Fix argument concatenation with `$` (#553)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 6fe7e51 · Browse files · 3 files·+97 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+97 added−8 removed
- tests:+45 added−0 removed
- other:+52 added−8 removed
Provenance
- Commit
07585d0- Tree
4525935- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
6fe7e51
3 files changed+97 added−8 removed
modified:docs/scripts.md+20 added
138 unmodified lines139 const example = await $`echo example`;140 await $`echo ${example}`;141 ```142 143+### Concatenation144+145+```sh146+# Bash147+tmpDir="/tmp"148+mkdir "$tmpDir/filename"149+```150+151+```js152+// zx153+const tmpDir = '/tmp'154+await $`mkdir ${tmpDir}/filename`;155+```156+157+```js158+// Execa159+const tmpDir = '/tmp'160+await $`mkdir ${tmpDir}/filename`;161+```162+163 ### Parallel commands164 165 ```sh166 # Bash510 unmodified linesmodified:lib/command.js+32 added-8 removed
74 unmodified lines75767778797980818283848586878889909192939495968497989910010110210310410510610710810911089909111111211311411511611711811912094959674 unmodified lines
throw new TypeError(`Unexpected "${typeOfExpression}" in template expression`);};
const parseTemplate = (template, index, templates, expressions) => {const concatTokens = (tokens, nextTokens, isNew) => isNew || tokens.length === 0 || nextTokens.length === 0 ? [...tokens, ...nextTokens] : [ ...tokens.slice(0, -1), `${tokens[tokens.length - 1]}${nextTokens[0]}`, ...nextTokens.slice(1), ];
const parseTemplate = ({templates, expressions, tokens, index, template}) => { const templateString = template ?? templates.raw[index]; const templateTokens = templateString.split(SPACES_REGEXP).filter(Boolean); const newTokens = concatTokens( tokens, templateTokens, templateString.startsWith(' '), );
if (index === expressions.length) { return templateTokens; return newTokens; }
const expression = expressions[index]; const expressionTokens = Array.isArray(expression) ? expression.map(expression => parseExpression(expression)) : [parseExpression(expression)]; return concatTokens( newTokens, expressionTokens, templateString.endsWith(' '), );};
return Array.isArray(expression) ? [...templateTokens, ...expression.map(expression => parseExpression(expression))] : [...templateTokens, parseExpression(expression)];export const parseTemplates = (templates, expressions) => { let tokens = [];
for (const [index, template] of templates.entries()) { tokens = parseTemplate({templates, expressions, tokens, index, template}); }
return tokens;};
export const parseTemplates = (templates, expressions) => templates.flatMap( (template, index) => parseTemplate(template, index, templates, expressions),);modified:test/command.js+45 added
111 unmodified lines112 const {stdout} = await $`echo.js ${['foo', 'bar']}`;113 t.is(stdout, 'foo\nbar');114 });115 116+test('$ allows empty array interpolation', async t => {117+ const {stdout} = await $`echo.js foo ${[]} bar`;118+ t.is(stdout, 'foo\nbar');119+});120+121 test('$ allows execa return value interpolation', async t => {122 const foo = await $`echo.js foo`;123 const {stdout} = await $`echo.js ${foo} bar`;124 t.is(stdout, 'foo\nbar');51 unmodified lines176 const {stdout} = await $`echo.js \u`;177 t.is(stdout, '\\u');178 });179 180+test('$ can concatenate at the end of tokens', async t => {181+ const {stdout} = await $`echo.js foo${'bar'}`;182+ t.is(stdout, 'foobar');183+});184+185+test('$ does not concatenate at the end of tokens with a space', async t => {186+ const {stdout} = await $`echo.js foo ${'bar'}`;187+ t.is(stdout, 'foo\nbar');188+});189+190+test('$ can concatenate at the end of tokens followed by an array', async t => {191+ const {stdout} = await $`echo.js foo${['bar', 'foo']}`;192+ t.is(stdout, 'foobar\nfoo');193+});194+195+test('$ can concatenate at the start of tokens', async t => {196+ const {stdout} = await $`echo.js ${'foo'}bar`;197+ t.is(stdout, 'foobar');198+});199+200+test('$ does not concatenate at the start of tokens with a space', async t => {201+ const {stdout} = await $`echo.js ${'foo'} bar`;202+ t.is(stdout, 'foo\nbar');203+});204+205+test('$ can concatenate at the start of tokens followed by an array', async t => {206+ const {stdout} = await $`echo.js ${['foo', 'bar']}foo`;207+ t.is(stdout, 'foo\nbarfoo');208+});209+210+test('$ can concatenate at the start and end of tokens followed by an array', async t => {211+ const {stdout} = await $`echo.js foo${['bar', 'foo']}bar`;212+ t.is(stdout, 'foobar\nfoobar');213+});214+215+test('$ can concatenate multiple tokens', async t => {216+ const {stdout} = await $`echo.js ${'foo'}bar${'foo'}`;217+ t.is(stdout, 'foobarfoo');218+});219+220 test('$ allows escaping spaces in commands with interpolation', async t => {221 const {stdout} = await $`${'command with space.js'} foo bar`;222 t.is(stdout, 'foo\nbar');223 });112 unmodified lines