removes comments from lambda (#998)

* removes comments from lambda

* include comments in lambda test

* pylint no else return
This commit is contained in:
Guillermo Ruffino 2020-04-05 22:14:49 -03:00 committed by GitHub
parent 3b7a47fb90
commit 43cf3063e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 152 additions and 135 deletions

View File

@ -230,10 +230,23 @@ class Lambda:
self._parts = None
self._requires_ids = None
# https://stackoverflow.com/a/241506/229052
def comment_remover(self, text):
def replacer(match):
s = match.group(0)
if s.startswith('/'):
return " " # note: a space and not an empty string
return s
pattern = re.compile(
r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"',
re.DOTALL | re.MULTILINE
)
return re.sub(pattern, replacer, text)
@property
def parts(self):
if self._parts is None:
self._parts = re.split(LAMBDA_PROG, self._value)
self._parts = re.split(LAMBDA_PROG, self.comment_remover(self._value))
return self._parts
@property

View File

@ -211,6 +211,10 @@ class TestTimePeriod:
SAMPLE_LAMBDA = """
it.strftime(64, 0, id(my_font), TextAlign::TOP_CENTER, "%H:%M:%S", id(esptime).now());
it.printf(64, 16, id(my_font2), TextAlign::TOP_CENTER, "%.1f°C (%.1f%%)", id( office_tmp ).state, id(office_hmd).state);
//id(my_commented_id)
int x = 4;/* id(my_commented_id2)
id(my_commented_id3)
*/
"""
@ -246,7 +250,7 @@ class TestLambda:
"state, ",
"office_hmd",
".",
"state);"
"state);\n \nint x = 4; "
]
def test_requires_ids(self):