test262/tools/lint/lib/frontmatter.py
Mike Pennisi c4e54648c0 tools: enforce restriction on YAML
Some consumers have reported difficulty parsing an uncommon YAML
construction [1] [2] [3]. Extend the linter to help ensure that
construction is not used in future contributions.

[1] https://github.com/tc39/test262/issues/1997
[2] https://github.com/tc39/test262/pull/2505
[3] https://github.com/tc39/test262/issues/3171
2021-09-07 17:41:36 -04:00

34 lines
955 B
Python

import re
import yaml
class Result(dict):
def __init__(self, meta, events):
self.parsing_events = events
super(Result, self).__init__(**meta)
class MyLoader(yaml.SafeLoader):
events = None
def __init__(self, *args, **kwargs):
MyLoader.events = []
super(MyLoader, self).__init__(*args, **kwargs)
def get_event(self):
event = super(MyLoader, self).get_event()
MyLoader.events.append(event)
return event
def parse(src):
'''Parse the YAML-formatted metadata found in a given string of source
code. Tolerate missing or invalid metadata; those conditions are handled by
a dedicated "Check" instance.'''
match = re.search(r'/\*---(.*)---\*/', src, re.DOTALL)
if not match:
return None
try:
return Result(yaml.load(match.group(1), MyLoader), MyLoader.events)
except (yaml.scanner.ScannerError, yaml.parser.ParserError):
return None