mirror of
https://github.com/yt-dlp/yt-dlp.git
synced 2026-12-01 21:36:42 +02:00
[utils] js_to_json: Fix line comment removal (#17845)
Authored by: doe1080
This commit is contained in:
@@ -1273,6 +1273,11 @@ class TestUtil(unittest.TestCase):
|
||||
on = js_to_json('[//comment\n"abc" //comment\n,//comment\n"def",//comment\n]')
|
||||
self.assertEqual(json.loads(on), ['abc', 'def'])
|
||||
|
||||
self.assertEqual(json.loads(js_to_json('[1]//comment')), [1])
|
||||
for newline in ('\n', '\r\n', '\r', '\u2028', '\u2029'):
|
||||
with self.subTest(newline=newline):
|
||||
self.assertEqual(json.loads(js_to_json(f'[1,//comment{newline}2,\n3]')), [1, 2, 3])
|
||||
|
||||
on = js_to_json('{"abc": "def",}')
|
||||
self.assertEqual(json.loads(on), {'abc': 'def'})
|
||||
|
||||
|
||||
@@ -2769,7 +2769,7 @@ def js_to_json(code, vars={}, *, strict=False):
|
||||
# vars is a dict of var, val pairs to substitute
|
||||
STRING_QUOTES = '\'"`'
|
||||
STRING_RE = '|'.join(rf'{q}(?:\\.|[^\\{q}])*{q}' for q in STRING_QUOTES)
|
||||
COMMENT_RE = r'/\*(?:(?!\*/).)*?\*/|//[^\n]*\n'
|
||||
COMMENT_RE = r'/\*(?:(?!\*/).)*?\*/|//[^\r\n\u2028\u2029]*(?:\r\n|[\r\n\u2028\u2029]|$)'
|
||||
SKIP_RE = fr'\s*(?:{COMMENT_RE})?\s*'
|
||||
INTEGER_TABLE = (
|
||||
(fr'(?s)^(0[xX][0-9a-fA-F]+){SKIP_RE}:?$', 16),
|
||||
|
||||
Reference in New Issue
Block a user