Skip to content

Commit f652882

Browse files
authored
Fix location reporting for lint rules on a AstNode with a name (#26435)
Fixes the location reporting for AstNode's that have name locations. A previous [PR](#26191) had accidentally broken this functionality,m this PR restores it. Testing - [x] ran `start_test test/chplcheck` [Reviewed by @DanilaFe]
2 parents a44cebd + 1b6f41f commit f652882

File tree

4 files changed

+31
-10
lines changed

4 files changed

+31
-10
lines changed

test/chplcheck/UseNameLocations.chpl

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module UseNameLocations {
2+
/*
3+
This test is to make sure name locations are used instead of normal
4+
locations when reporting
5+
*/
6+
7+
proc
8+
my_func
9+
() {}
10+
11+
record
12+
my_rec
13+
{}
14+
15+
class
16+
my_class
17+
{}
18+
}

test/chplcheck/UseNameLocations.good

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
UseNameLocations.chpl:8: node violates rule CamelCaseFunctions
2+
UseNameLocations.chpl:12: node violates rule CamelCaseRecords
3+
UseNameLocations.chpl:16: node violates rule PascalCaseClasses

tools/chplcheck/src/lsp.py

-8
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,6 @@
3636
from driver import LintDriver
3737

3838

39-
def _get_location(node: chapel.AstNode):
40-
"""Helper to get the location of a node"""
41-
if isinstance(node, chapel.NamedDecl):
42-
return chapel.lsp.location_to_range(node.name_location())
43-
else:
44-
return chapel.lsp.location_to_range(node.location())
45-
46-
4739
def get_lint_diagnostics(
4840
context: chapel.Context, driver: LintDriver, asts: List[chapel.AstNode]
4941
) -> List[Diagnostic]:

tools/chplcheck/src/rule_types.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ def _build_ignore_fixit(
4040
return ignore
4141

4242

43+
def _get_location(node: chapel.AstNode):
44+
"""Helper to get the location of a node"""
45+
if isinstance(node, chapel.NamedDecl):
46+
return node.name_location()
47+
else:
48+
return node.location()
49+
50+
4351
@dataclass
4452
class RuleLocation:
4553
path_: str
@@ -332,7 +340,7 @@ def _check_single(
332340
# addition to the fixits in the result itself)
333341
# add the fixits from the hooks to the fixits from the rule
334342
fixits = self.run_fixit_hooks(context, result) + fixits
335-
loc = RuleLocation.from_chapel(result.node.location())
343+
loc = RuleLocation.from_chapel(_get_location(result.node))
336344
return (loc, result.node, self.name, fixits)
337345

338346
def check(
@@ -392,7 +400,7 @@ def check(
392400
# addition to the fixits in the result itself)
393401
# add the fixits from the hooks to the fixits from the rule
394402
fixits = self.run_fixit_hooks(context, result) + fixits
395-
loc = RuleLocation.from_chapel(node.location())
403+
loc = RuleLocation.from_chapel(_get_location(node))
396404
yield (loc, node, self.name, fixits)
397405

398406

0 commit comments

Comments
 (0)