brsh 3.1.0 → 3.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -1286,6 +1286,26 @@ class Shell extends EventEmitter {
1286
1286
  continue;
1287
1287
  }
1288
1288
 
1289
+ // Escape handling inside double quotes
1290
+ if (doubleQuoteOpen && ch === '\\' && i + 1 < string.length) {
1291
+ const next = string[i + 1];
1292
+ if (next === '"' || next === '\\' || next === '$') {
1293
+ tokenBuffer.push(next);
1294
+ i++;
1295
+ continue;
1296
+ }
1297
+ }
1298
+
1299
+ // Escape handling outside quotes
1300
+ if (!singleQuoteOpen && !doubleQuoteOpen && ch === '\\' && i + 1 < string.length) {
1301
+ const next = string[++i];
1302
+ tokenBuffer.push(next);
1303
+ if (next === '"' || next === "'") {
1304
+ tokenHasQuote = true;
1305
+ }
1306
+ continue;
1307
+ }
1308
+
1289
1309
  // Quote handling
1290
1310
  if (ch === "'" && !doubleQuoteOpen) {
1291
1311
  singleQuoteOpen = !singleQuoteOpen;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brsh",
3
- "version": "3.1.0",
3
+ "version": "3.1.1",
4
4
  "description": "A unix like shell written in javascript",
5
5
  "main": "./dist/shell.min.js",
6
6
  "module": "./dist/shell.es.js",
@@ -215,6 +215,55 @@ describe('unclosed quote error', () => {
215
215
  });
216
216
  });
217
217
 
218
+ describe('backslash quote escaping', () => {
219
+ let shell;
220
+ beforeEach(async () => { shell = await createShell(); });
221
+
222
+ it('parses echo "hello \\"world\\"" to [\'echo\', \'hello "world"\']', async () => {
223
+ const cmd = 'echo "hello \\"world\\""';
224
+ const parts = Shell._splitCommand(cmd).map(p => p.value);
225
+ expect(parts).toEqual(['echo', 'hello "world"']);
226
+
227
+ const { output, errors, exitCode } = await run(shell, cmd);
228
+ expect(errors).toHaveLength(0);
229
+ expect(exitCode).toBe(0);
230
+ expect(output).toEqual(['hello "world"']);
231
+ });
232
+
233
+ it('parses echo "Eliot\'s CV with \\"nested quotes\\"" cleanly without throwing quote expression not closed', async () => {
234
+ const cmd = 'echo "Eliot\'s CV with \\"nested quotes\\""';
235
+ const parts = Shell._splitCommand(cmd).map(p => p.value);
236
+ expect(parts).toEqual(['echo', 'Eliot\'s CV with "nested quotes"']);
237
+
238
+ const { output, errors, exitCode } = await run(shell, cmd);
239
+ expect(errors).toHaveLength(0);
240
+ expect(exitCode).toBe(0);
241
+ expect(output).toEqual(['Eliot\'s CV with "nested quotes"']);
242
+ });
243
+
244
+ it('parses echo \'Eliot\'\\\'\'s experience\' correctly into [\'echo\', "Eliot\'s experience"]', async () => {
245
+ const cmd = "echo 'Eliot'\\''s experience'";
246
+ const parts = Shell._splitCommand(cmd).map(p => p.value);
247
+ expect(parts).toEqual(['echo', "Eliot's experience"]);
248
+
249
+ const { output, errors, exitCode } = await run(shell, cmd);
250
+ expect(errors).toHaveLength(0);
251
+ expect(exitCode).toBe(0);
252
+ expect(output).toEqual(["Eliot's experience"]);
253
+ });
254
+
255
+ it('parses echo \\"unquoted\\" to [\'echo\', \'"unquoted"\']', async () => {
256
+ const cmd = 'echo \\"unquoted\\"';
257
+ const parts = Shell._splitCommand(cmd).map(p => p.value);
258
+ expect(parts).toEqual(['echo', '"unquoted"']);
259
+
260
+ const { output, errors, exitCode } = await run(shell, cmd);
261
+ expect(errors).toHaveLength(0);
262
+ expect(exitCode).toBe(0);
263
+ expect(output).toEqual(['"unquoted"']);
264
+ });
265
+ });
266
+
218
267
  describe('output redirection', () => {
219
268
  it('writes command stdout to a file with >', async () => {
220
269
  const shell = await createShell({ filesystem: { home: {} } });