Skip to content

Commit 3afc6fc

Browse files
authored
bug(#743): optimized duplicated-names-in-diff-context with for-each-group (#778)
* bug(#743): optimize duplicate names detection using xsl:for-each-group and update test cases * bug(#743): add yamllint disable rule for line-length in test YAML files * bug(#743): add test case for duplicate names detection in deep hierarchy * bug(#743): update duplicate names test case to reflect new line numbers and object names
1 parent 3c98434 commit 3afc6fc

File tree

7 files changed

+75
-12
lines changed

7 files changed

+75
-12
lines changed

src/main/resources/org/eolang/lints/misc/duplicate-names-in-diff-context.xsl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
<xsl:output encoding="UTF-8" method="xml"/>
1111
<xsl:template match="/">
1212
<defects>
13-
<xsl:for-each select="//o[not(ancestor::o[eo:test-attr(.)]) and not(eo:special(@name)) and not(@base='∅')]">
14-
<xsl:variable name="namesakes" select="//o[not(ancestor::o[eo:test-attr(.)]) and @name=current()/@name and not(@base='∅')]"/>
15-
<xsl:if test="count($namesakes)&gt;1">
13+
<xsl:for-each-group select="//o[not(ancestor::o[eo:test-attr(.)]) and not(eo:special(@name)) and not(@base='∅')]" group-by="@name">
14+
<xsl:if test="count(current-group())&gt;1">
1615
<defect>
1716
<xsl:variable name="line" select="eo:lineno(@line)"/>
1817
<xsl:attribute name="line">
@@ -27,8 +26,8 @@
2726
<xsl:text>Object "</xsl:text>
2827
<xsl:value-of select="@name"/>
2928
<xsl:text>" has the same name as </xsl:text>
30-
<xsl:variable name="lines" select="$namesakes[@line and (not(current()/@line) or @line != current()/@line)]/@line"/>
31-
<xsl:variable name="empty" select="count($lines) != count($namesakes) - 1"/>
29+
<xsl:variable name="lines" select="current-group()/@line[. and . != current()/@line]"/>
30+
<xsl:variable name="empty" select="count($lines) != count(current-group()) - 1"/>
3231
<xsl:if test="count($lines) &gt; 0">
3332
<xsl:text>the objects on the lines </xsl:text>
3433
<xsl:value-of select="string-join($lines, ', ')"/>
@@ -41,7 +40,7 @@
4140
</xsl:if>
4241
</defect>
4342
</xsl:if>
44-
</xsl:for-each>
43+
</xsl:for-each-group>
4544
</defects>
4645
</xsl:template>
4746
</xsl:stylesheet>

src/test/resources/org/eolang/lints/packs/single/duplicate-names-in-diff-context/catches-duplicate-names-in-diff-context.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
sheets:
55
- /org/eolang/lints/misc/duplicate-names-in-diff-context.xsl
66
asserts:
7-
- /defects/defect[@line='4' or @line='6']
8-
- /defects[count(defect[@severity='warning'])=2]
7+
- /defects[count(defect[@severity='warning'])=1]
8+
- /defects/defect[@line='4']
99
input: |
1010
# No comments.
1111
[] > foo
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2016-2025 Objectionary.com
2+
# SPDX-License-Identifier: MIT
3+
---
4+
# yamllint disable rule:line-length
5+
sheets:
6+
- /org/eolang/lints/misc/duplicate-names-in-diff-context.xsl
7+
asserts:
8+
- /defects[count(defect[@severity='warning'])=1]
9+
- /defects/defect[@line='5']
10+
- /defects/defect[normalize-space()='Object "s" has the same name as the objects on the lines 11']
11+
input: |
12+
# No comments.
13+
[] > head
14+
[] > first
15+
[] > second
16+
"plus" > s
17+
[] > third
18+
[] > fourth
19+
[] > nested
20+
[] > more-nested
21+
[] > tail
22+
"boom" > s
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2016-2025 Objectionary.com
2+
# SPDX-License-Identifier: MIT
3+
---
4+
# yamllint disable rule:line-length
5+
sheets:
6+
- /org/eolang/lints/misc/duplicate-names-in-diff-context.xsl
7+
asserts:
8+
- /defects[count(defect[@severity='warning'])=1]
9+
- /defects/defect[@line='4']
10+
- /defects/defect[normalize-space()='Object "hello" has the same name as the objects on the lines 6, 8, 10']
11+
input: |
12+
# No comments.
13+
[] > main
14+
[] > f1
15+
"Hello world" > hello
16+
[] > f2
17+
"Привет мир" > hello
18+
[] > f3
19+
"你好,世界" > hello
20+
[] > f4
21+
"こんにちは、世界" > hello

src/test/resources/org/eolang/lints/packs/single/duplicate-names-in-diff-context/catches-duplicates-except-in-tests.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
sheets:
66
- /org/eolang/lints/misc/duplicate-names-in-diff-context.xsl
77
asserts:
8-
- /defects[count(defect[@severity='warning'])=2]
8+
- /defects[count(defect[@severity='warning'])=1]
99
- /defects/defect[@line='4']
10-
- /defects/defect[@line='6']
11-
- /defects/defect[contains(normalize-space(), 'has the same name as the objects on the lines 4')]
1210
- /defects/defect[contains(normalize-space(), 'has the same name as the objects on the lines 6')]
1311
input: |
1412
# Foo.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2016-2025 Objectionary.com
2+
# SPDX-License-Identifier: MIT
3+
---
4+
# yamllint disable rule:line-length
5+
sheets:
6+
- /org/eolang/lints/misc/duplicate-names-in-diff-context.xsl
7+
asserts:
8+
- /defects[count(defect[@severity='warning'])=2]
9+
- /defects/defect[@line='4']
10+
- /defects/defect[@line='8']
11+
- /defects/defect[normalize-space()='Object "foo" has the same name as the objects on the lines 6']
12+
- /defects/defect[normalize-space()='Object "arc" has the same name as the objects on the lines 10']
13+
input: |
14+
# No comments.
15+
[] > app
16+
[] > f1
17+
42 > foo
18+
[] > f2
19+
33 > foo
20+
[] > f3
21+
-8819 > arc
22+
[] > f4
23+
-1 > arc

src/test/resources/org/eolang/lints/packs/single/duplicate-names-in-diff-context/prints-context-when-lines-empty.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
sheets:
55
- /org/eolang/lints/misc/duplicate-names-in-diff-context.xsl
66
asserts:
7-
- /defects[count(defect[@context and @severity='warning'])=2]
7+
- /defects[count(defect[@context and @severity='warning'])=1]
88
document: |
99
<object author="tests">
1010
<o>

0 commit comments

Comments
 (0)