From a796cab879c05a054a8a33c6572549a3c499c8ca Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 17 Nov 2026 11:23:16 +0800 Subject: [PATCH] fix(project-detect): retain multiline continuations and array boundaries --- scripts/lib/project-detect.js | 20 +++++++++++++++++-- .../lib/project-detect-python-arrays.test.js | 9 +++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/scripts/lib/project-detect.js b/scripts/lib/project-detect.js index e458e2608..7be49caa8 100644 --- a/scripts/lib/project-detect.js +++ b/scripts/lib/project-detect.js @@ -198,9 +198,21 @@ function readTomlQuotedValue(content, start) { // TOML trims a newline immediately after a multiline opening delimiter. if (multiline && content.startsWith('\r\n', index)) index += 2; else if (multiline && content[index] === '\n') index++; - const valueStart = index; + let valueStart = index; + const parts = []; + const continuation = /\\[ \t]*\r?\n[ \t\r\n]*/y; while (index < content.length) { if (quote === '"' && content[index] === '\\') { + if (multiline) { + continuation.lastIndex = index; + const match = continuation.exec(content); + if (match) { + parts.push(content.slice(valueStart, index)); + index = continuation.lastIndex; + valueStart = index; + continue; + } + } index += 2; continue; } @@ -210,7 +222,8 @@ function readTomlQuotedValue(content, start) { if (multiline) { while (end < index + 5 && content[end] === quote) end++; } - return { value: content.slice(valueStart, end - delimiter.length), end }; + parts.push(content.slice(valueStart, end - delimiter.length)); + return { value: parts.join(''), end }; } if (!multiline && (content[index] === '\n' || content[index] === '\r')) return null; index++; @@ -253,6 +266,9 @@ function readTomlDependencyArray(content) { continue; } } + // Dependency values are strings; an unquoted opening bracket here can be + // a later table header while the dependency array is still unfinished. + if (values !== null && character === '[') return []; if (values !== null && character === ']') return values; index++; } diff --git a/tests/lib/project-detect-python-arrays.test.js b/tests/lib/project-detect-python-arrays.test.js index d4464a456..5d49061a9 100644 --- a/tests/lib/project-detect-python-arrays.test.js +++ b/tests/lib/project-detect-python-arrays.test.js @@ -47,6 +47,9 @@ test('keeps escaped quotes in environment markers inside one requirement', test('does not parse an unterminated dependency array', '[project]\ndependencies = ["fastapi[standard]"\n', [], []); +test('does not use a later table header to close an unfinished dependency array', + '[project]\ndependencies = ["fastapi",\n[tool.demo]\n', + [], []); test('retains dependencies in a project inline table', 'project = { name = "demo", version = "1.0", dependencies = ["fastapi[standard]", "flask"] }\n', ['fastapi', 'flask'], ['fastapi', 'flask']); @@ -65,6 +68,12 @@ test('keeps single quotes inside multiline literal descriptions', test('trims the opening newline of multiline quoted requirements', "[project]\ndependencies = ['''\nfastapi''', \"\"\"\r\nflask\"\"\"]\n", ['fastapi', 'flask'], ['fastapi', 'flask']); +test('joins escaped newlines inside multiline basic requirements', + '[project]\ndependencies = ["""fast\\\n api""", """fl\\ \t\r\n\n ask"""]\n', + ['fastapi', 'flask'], ['fastapi', 'flask']); +test('leaves multiline literal backslashes unchanged', + "[project]\ndependencies = ['''fast\\\napi''']\n", + ['fast\\'], []); test('allows one or two quote characters before a multiline closing delimiter', '[project]\ndescription = """quoted"""""\ndependencies = ["flask"]\n', ['flask'], ['flask']);