escape.js
This file
Path
lib/arguments/escape.js
91 lines · 3.8 KiB · JavaScript
Last changed by
Fix `escapedCommand` and `verbose` quoting arguments when using the `shell` optionSindre Sorhusbefore adoptionb2c3a54
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 1–4why, one step back1import {platform} from 'node:process';2import {stripVTControlCharacters} from 'node:util';34// Compute `result.command` and `result.escapedCommand`Fix `escapedCommand` and `verbose` quoting arguments when using the `shell` optionSindre Sorhusbefore adoptionb2c3a54
line 5why, one step back5export const joinCommand = (filePath, rawArguments, shell) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 6–7why, one step back6 const fileAndArguments = [filePath, ...rawArguments];7 const command = fileAndArguments.join(' ');Fix `escapedCommand` and `verbose` quoting arguments when using the `shell` optionSindre Sorhusbefore adoptionb2c3a54
lines 8–13why, one step back8 // With `shell`, the shell interprets the command, so it must not be quoted9 const escapedCommand = shell10 ? escapeControlCharacters(command)11 : fileAndArguments12 .map(fileAndArgument => quoteString(escapeControlCharacters(fileAndArgument)))13 .join(' ');100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 14–43why, one step back14 return {command, escapedCommand};15};1617// Remove ANSI sequences and escape control characters and newlines18export const escapeLines = lines => stripVTControlCharacters(lines)19 .split('\n')20 .map(line => escapeControlCharacters(line))21 .join('\n');2223const escapeControlCharacters = line => line.replaceAll(SPECIAL_CHAR_REGEXP, character => escapeControlCharacter(character));2425const escapeControlCharacter = character => {26 const commonEscape = COMMON_ESCAPES[character];27 if (commonEscape !== undefined) {28 return commonEscape;29 }3031 const codepoint = character.codePointAt(0);32 const codepointHex = codepoint.toString(16);33 return codepoint <= ASTRAL_START34 ? `\\u${codepointHex.padStart(4, '0')}`35 : `\\U${codepointHex}`;36};3738// Characters that would create issues when printed are escaped using the \u or \U notation.39// Those include control characters and newlines.40// The \u and \U notation is Bash specific, but there is no way to do this in a shell-agnostic way.41// Some shells do not even have a way to print those characters in an escaped fashion.42// Therefore, we prioritize printing those safely, instead of allowing those to be copy-pasted.43// List of Unicode character categories: https://www.fileformat.info/info/unicode/category/index.htmFix Node.js without ICU support (#1144)ehmickybefore adoption074ea4a
lines 44–54why, one step back44const getSpecialCharRegExp = () => {45 try {46 // This throws when using Node.js without ICU support.47 // When using a RegExp literal, this would throw at parsing-time, instead of runtime.48 // eslint-disable-next-line prefer-regex-literals49 return new RegExp('\\p{Separator}|\\p{Other}', 'gu');50 } catch {51 // Similar to the above RegExp, but works even when Node.js has been built without ICU support.52 // Unlike the above RegExp, it only covers whitespaces and C0/C1 control characters.53 // It does not cover some edge cases, such as Unicode reserved characters.54 // See https://github.com/sindresorhus/execa/issues/1143Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454
line 55why, one step back55 // eslint-disable-next-line no-control-regex, regexp/no-control-character, unicorn/prefer-unicode-code-point-escapesFix Node.js without ICU support (#1144)ehmickybefore adoption074ea4a
lines 56–60why, one step back56 return /[\s\u0000-\u001F\u007F-\u009F\u00AD]/g;57 }58};5960const SPECIAL_CHAR_REGEXP = getSpecialCharRegExp();100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 61–72why, one step back6162// Accepted by $'...' in Bash.63// Exclude \a \e \v which are accepted in Bash but not in JavaScript (except \v) and JSON.64const COMMON_ESCAPES = {65 ' ': ' ',66 '\b': '\\b',67 '\f': '\\f',68 '\n': '\\n',69 '\r': '\\r',70 '\t': '\\t',71};72Fix template string tokenization of escape sequencesSindre Sorhusbefore adoptionb67f199
lines 73–74why, one step back73// Up until that code point, a character is a single UTF-16 code unit, so the \u notation can be used instead of \U74export const ASTRAL_START = 0xFF_FF;100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 75–90why, one step back7576// Some characters are shell-specific, i.e. need to be escaped when the command is copy-pasted then run.77// Escaping is shell-specific. We cannot know which shell is used: `process.platform` detection is not enough.78// For example, Windows users could be using `cmd.exe`, Powershell or Bash for Windows which all use different escaping.79// We use '...' on Unix, which is POSIX shell compliant and escape all characters but ' so this is fairly safe.80// On Windows, we assume cmd.exe is used and escape with "...", which also works with Powershell.81const quoteString = escapedArgument => {82 if (NO_ESCAPE_REGEXP.test(escapedArgument)) {83 return escapedArgument;84 }8586 return platform === 'win32'87 ? `"${escapedArgument.replaceAll('"', '""')}"`88 : `'${escapedArgument.replaceAll('\'', '\'\\\'\'')}'`;89};90Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454
line 91why, one step back91const NO_ESCAPE_REGEXP = /^[\w\-./]+$/;