Skip to content

Commit 2cb44ea

Browse files
committed
Pass regexp match into the lint description
As a start, lightly change lint.rules.Regexp, making it a subclass of lint.rules.Rule, thus allowing it to use the generic lint.rules.Rule.error. With that done, we now pass the subpattern matches as the context to the error factor, allowing them to be used in the description, thus letting us give users better error descriptions.
1 parent b31a712 commit 2cb44ea

File tree

3 files changed

+38
-37
lines changed

3 files changed

+38
-37
lines changed

tools/lint/lint.py

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -339,25 +339,29 @@ def filter_ignorelist_errors(data: Ignorelist, errors: Sequence[rules.Error]) ->
339339
return [item for i, item in enumerate(errors) if not skipped[i]]
340340

341341

342-
regexps = [item() for item in # type: ignore
343-
[rules.TrailingWhitespaceRegexp,
344-
rules.TabsRegexp,
345-
rules.CRRegexp,
346-
rules.SetTimeoutRegexp,
347-
rules.W3CTestOrgRegexp,
348-
rules.WebPlatformTestRegexp,
349-
rules.Webidl2Regexp,
350-
rules.ConsoleRegexp,
351-
rules.GenerateTestsRegexp,
352-
rules.PrintRegexp,
353-
rules.LayoutTestsRegexp,
354-
rules.MissingDepsRegexp,
355-
rules.SpecialPowersRegexp,
356-
rules.AssertThrowsRegexp,
357-
rules.PromiseRejectsRegexp,
358-
rules.AssertPreconditionRegexp,
359-
rules.HTMLInvalidSyntaxRegexp,
360-
rules.TestDriverInternalRegexp]]
342+
regexps: Sequence[rules.Regexp] = [
343+
item()
344+
for item in (
345+
rules.TrailingWhitespaceRegexp,
346+
rules.TabsRegexp,
347+
rules.CRRegexp,
348+
rules.SetTimeoutRegexp,
349+
rules.W3CTestOrgRegexp,
350+
rules.WebPlatformTestRegexp,
351+
rules.Webidl2Regexp,
352+
rules.ConsoleRegexp,
353+
rules.GenerateTestsRegexp,
354+
rules.PrintRegexp,
355+
rules.LayoutTestsRegexp,
356+
rules.MissingDepsRegexp,
357+
rules.SpecialPowersRegexp,
358+
rules.AssertThrowsRegexp,
359+
rules.PromiseRejectsRegexp,
360+
rules.AssertPreconditionRegexp,
361+
rules.HTMLInvalidSyntaxRegexp,
362+
rules.TestDriverInternalRegexp,
363+
)
364+
]
361365

362366

363367
def check_regexp_line(repo_root: Text, path: Text, f: IO[bytes]) -> List[rules.Error]:
@@ -367,8 +371,13 @@ def check_regexp_line(repo_root: Text, path: Text, f: IO[bytes]) -> List[rules.E
367371

368372
for i, line in enumerate(f):
369373
for regexp in applicable_regexps:
370-
if regexp.search(line):
371-
errors.append((regexp.name, regexp.description, path, i+1))
374+
m = regexp.search(line)
375+
if m:
376+
groups = tuple(
377+
g.decode("ascii", "backslashreplace") if g is not None else None
378+
for g in m.groups()
379+
)
380+
errors.append(regexp.error(path, context=groups, line_no=i+1))
372381

373382
return errors
374383

tools/lint/rules.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -390,19 +390,11 @@ class MissingTestInWebFeaturesFile(Rule):
390390
EXTENSIONS["js_all"] = EXTENSIONS["markup"] + EXTENSIONS["js"]
391391

392392

393-
class Regexp(metaclass=abc.ABCMeta):
393+
class Regexp(Rule, metaclass=abc.ABCMeta):
394394
@abc.abstractproperty
395395
def pattern(self) -> bytes:
396396
pass
397397

398-
@abc.abstractproperty
399-
def name(self) -> Text:
400-
pass
401-
402-
@abc.abstractproperty
403-
def description(self) -> Text:
404-
pass
405-
406398
file_extensions: Optional[List[Text]] = None
407399

408400
def __init__(self) -> None:
@@ -506,7 +498,7 @@ class LayoutTestsRegexp(Regexp):
506498
pattern = br"(eventSender|testRunner|internals)\."
507499
name = "LAYOUTTESTS APIS"
508500
file_extensions = EXTENSIONS["js_all"]
509-
description = "eventSender/testRunner/internals used; these are LayoutTests-specific APIs (WebKit/Blink)"
501+
description = "%s used; this is a LayoutTests-specific API (WebKit/Blink)"
510502

511503

512504
class MissingDepsRegexp(Regexp):
@@ -560,10 +552,10 @@ class HTMLInvalidSyntaxRegexp(Regexp):
560552
br"dfn|dialog|div|dl|dt|em|fieldset|figcaption|figure|footer|form|h[1-6]|head|header|html|i|iframe|ins|kbd|label|legend|li|"
561553
br"main|map|mark|menu|meter|nav|noscript|object|ol|optgroup|option|output|p|picture|pre|progress|q|rp|rt|ruby|s|samp|script|"
562554
br"search|section|select|slot|small|span|strong|style|sub|summary|sup|table|tbody|td|template|textarea|tfoot|th|thead|time|"
563-
br"title|tr|u|ul|var|video)(\s+[^>]+)?\s*/>")
555+
br"title|tr|u|ul|var|video)(?:\s+[^>]+)?\s*/>")
564556
name = "HTML INVALID SYNTAX"
565557
file_extensions = EXTENSIONS["html"]
566-
description = "Test-file line has a non-void HTML tag with /> syntax"
558+
description = "Test-file line has a non-void HTML %s tag with /> syntax"
567559
to_fix = """Replace with start tag and end tag"""
568560

569561

tools/lint/tests/test_file_lints.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def test_eventSender():
168168
assert errors == [("PARSE-FAILED", "Unable to parse file", filename, 1)]
169169
else:
170170
assert errors == [('LAYOUTTESTS APIS',
171-
'eventSender/testRunner/internals used; these are LayoutTests-specific APIs (WebKit/Blink)',
171+
'eventSender used; this is a LayoutTests-specific API (WebKit/Blink)',
172172
filename,
173173
1)]
174174

@@ -183,7 +183,7 @@ def test_testRunner():
183183
assert errors == [("PARSE-FAILED", "Unable to parse file", filename, 1)]
184184
else:
185185
assert errors == [('LAYOUTTESTS APIS',
186-
'eventSender/testRunner/internals used; these are LayoutTests-specific APIs (WebKit/Blink)',
186+
'testRunner used; this is a LayoutTests-specific API (WebKit/Blink)',
187187
filename,
188188
1)]
189189

@@ -198,7 +198,7 @@ def test_internals():
198198
assert errors == [("PARSE-FAILED", "Unable to parse file", filename, 1)]
199199
else:
200200
assert errors == [('LAYOUTTESTS APIS',
201-
'eventSender/testRunner/internals used; these are LayoutTests-specific APIs (WebKit/Blink)',
201+
'internals used; this is a LayoutTests-specific API (WebKit/Blink)',
202202
filename,
203203
1)]
204204

@@ -240,7 +240,7 @@ def test_html_invalid_syntax():
240240
check_errors(errors)
241241

242242
if kind == "web-lax":
243-
assert errors == [("HTML INVALID SYNTAX", "Test-file line has a non-void HTML tag with /> syntax", filename, 1)]
243+
assert errors == [("HTML INVALID SYNTAX", "Test-file line has a non-void HTML div tag with /> syntax", filename, 1)]
244244

245245

246246
def test_testdriver_internal():

0 commit comments

Comments
 (0)