Skip to content

Commit 93d9d35

Browse files
fix(js-sdk): support hex and octal escapes in bracket expressions like Python
Co-Authored-By: [email protected] <[email protected]>
1 parent e223987 commit 93d9d35

3 files changed

Lines changed: 40 additions & 2 deletions

File tree

‎packages/js-sdk/src/template/dockerignore.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function escapeRegex(ch: string): string {
3131
// Escapes that keep their regex meaning in a bracket expression, like in Python
3232
const CLASS_ESCAPES = new Set('dDsSwWfnrtv')
3333
const CLASS_CONTROL_ESCAPES: Record<string, string> = { a: '\\x07', b: '\\x08' }
34+
const CLASS_HEX_ESCAPE_LENGTHS: Record<string, number> = { x: 2, u: 4, U: 8 }
3435
// Characters that must stay escaped in a bracket expression with the `u` flag
3536
const CLASS_SYNTAX_CHARS = new Set('^$\\.*+?()[]{}|/-')
3637

@@ -50,6 +51,30 @@ function translateClass(body: string, backslashIsEscape: boolean): string {
5051
cls += '\\' + next
5152
} else if (next in CLASS_CONTROL_ESCAPES) {
5253
cls += CLASS_CONTROL_ESCAPES[next]
54+
} else if (next in CLASS_HEX_ESCAPE_LENGTHS) {
55+
const length = CLASS_HEX_ESCAPE_LENGTHS[next]
56+
const hex = body.slice(i, i + length)
57+
if (hex.length < length || !/^[0-9a-fA-F]+$/.test(hex)) {
58+
throw new Error(`incomplete escape \\${next}${hex}`)
59+
}
60+
const code = parseInt(hex, 16)
61+
if (code > 0x10ffff) {
62+
throw new Error(`bad escape \\${next}${hex}`)
63+
}
64+
cls += `\\u{${code.toString(16)}}`
65+
i += length
66+
} else if (/[0-7]/.test(next)) {
67+
let octal = next
68+
while (octal.length < 3 && /[0-7]/.test(body[i] ?? '')) {
69+
octal += body[i++]
70+
}
71+
const code = parseInt(octal, 8)
72+
if (code > 0o377) {
73+
throw new Error(
74+
`octal escape value \\${octal} outside of range 0-0o377`
75+
)
76+
}
77+
cls += `\\u{${code.toString(16)}}`
5378
} else if (/[A-Za-z0-9]/.test(next)) {
5479
throw new Error(`bad escape \\${next}`)
5580
} else {

‎packages/js-sdk/tests/template/utils/getAllFilesInPath.test.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,13 +467,19 @@ describe('getAllFilesInPath', () => {
467467
})
468468

469469
test.skipIf(process.platform === 'win32')(
470-
'should reject unknown escapes in a bracket expression',
470+
'should handle escapes in a bracket expression like Python',
471471
async () => {
472472
await expect(
473473
getAllFilesInPath('.', testDir, ['[\\q]'])
474474
).rejects.toThrow("Invalid ignore pattern '[\\q]'")
475475
await writeFile(join(testDir, 'a'), 'x')
476476
expect(await relativePaths('*', ['[\\a]'])).toContain('a')
477+
await writeFile(join(testDir, 'b'), 'x')
478+
await writeFile(join(testDir, 'c'), 'x')
479+
const files = await relativePaths('*', ['[\\x61]', '[\\142]'])
480+
expect(files).not.toContain('a')
481+
expect(files).not.toContain('b')
482+
expect(files).toContain('c')
477483
}
478484
)
479485

‎packages/python-sdk/tests/shared/template/utils/test_get_all_files_in_path.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,14 +462,21 @@ def test_should_raise_on_an_invalid_pattern(self, test_dir):
462462
get_all_files_in_path(".", test_dir, ["[abc"])
463463

464464
@pytest.mark.skipif(sys.platform == "win32", reason="Backslash is a separator")
465-
def test_should_reject_unknown_escapes_in_a_bracket_expression(self, test_dir):
465+
def test_should_handle_escapes_in_a_bracket_expression_like_python(self, test_dir):
466466
with pytest.raises(
467467
TemplateException, match=r"Invalid ignore pattern '\[\\q\]'"
468468
):
469469
get_all_files_in_path(".", test_dir, ["[\\q]"])
470470
with open(os.path.join(test_dir, "a"), "w") as f:
471471
f.write("x")
472472
assert "a" in self.relative_paths("*", test_dir, ["[\\a]"])
473+
for name in ["b", "c"]:
474+
with open(os.path.join(test_dir, name), "w") as f:
475+
f.write("x")
476+
files = self.relative_paths("*", test_dir, ["[\\x61]", "[\\142]"])
477+
assert "a" not in files
478+
assert "b" not in files
479+
assert "c" in files
473480

474481
def test_should_keep_the_files_hash_stable_when_ignored_files_change(
475482
self, test_dir

0 commit comments

Comments
 (0)