Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/main/resources/org/eolang/lints/aliases/same-package-alias.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
* SPDX-FileCopyrightText: Copyright (c) 2016-2026 Objectionary.com
* SPDX-License-Identifier: MIT
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:eo="https://www.eolang.org" id="same-package-alias" version="2.0">
<xsl:import href="/org/eolang/funcs/lineno.xsl"/>
<xsl:import href="/org/eolang/funcs/escape.xsl"/>
<xsl:import href="/org/eolang/funcs/defect-context.xsl"/>
<xsl:output encoding="UTF-8" method="xml"/>
<xsl:template match="/">
<defects>
<xsl:variable name="pkg" select="/object/metas/meta[head='package'][1]/tail"/>
<xsl:for-each select="/object/metas/meta[head='alias' and count(part)=2]">
<xsl:variable name="local" select="part[1]"/>
<xsl:variable name="fqn" select="replace(part[2], '^Φ\.', '')"/>
<xsl:if test="$pkg != '' and $fqn = concat($pkg, '.', $local)">
<xsl:element name="defect">
<xsl:variable name="line" select="eo:lineno(@line)"/>
<xsl:attribute name="line">
<xsl:value-of select="$line"/>
</xsl:attribute>
<xsl:if test="$line = '0'">
<xsl:attribute name="context">
<xsl:value-of select="eo:defect-context(.)"/>
</xsl:attribute>
</xsl:if>
<xsl:attribute name="severity">
<xsl:text>error</xsl:text>
</xsl:attribute>
<xsl:text>The alias </xsl:text>
<xsl:value-of select="eo:escape($fqn)"/>
<xsl:text> is redundant, because it refers to an object in the same package (</xsl:text>
<xsl:value-of select="eo:escape($pkg)"/>
<xsl:text>) as the current file</xsl:text>
</xsl:element>
</xsl:if>
</xsl:for-each>
</defects>
</xsl:template>
</xsl:stylesheet>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Same-package alias

An `+alias` meta that points to an object in the same package as the
current file's `+package` meta is redundant. The compiler resolves a
bare reference to an object of the same package on its own, so such
an alias only adds noise.

Incorrect:

```eo
+package org.eolang.txt
+alias org.eolang.txt.sprintf

# Foo.
[x] > foo
sprintf x > @
```

Correct:

```eo
+package org.eolang.txt

# Foo.
[x] > foo
sprintf x > @
```

An alias that points to a different package, or one that renames the
object to a new local name, is not redundant and is left alone:

```eo
+package org.eolang.txt
+alias sp org.eolang.txt.sprintf

# Foo.
[x] > foo
sp x > @
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# SPDX-FileCopyrightText: Copyright (c) 2016-2026 Objectionary.com
# SPDX-License-Identifier: MIT
---
sheets:
- /org/eolang/lints/aliases/same-package-alias.xsl
asserts:
- /defects[count(defect[@severity='error'])=0]
input: |
+alias org.eolang.txt.sprintf

[x] > foo
sprintf x > @
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# SPDX-FileCopyrightText: Copyright (c) 2016-2026 Objectionary.com
# SPDX-License-Identifier: MIT
---
sheets:
- /org/eolang/lints/aliases/same-package-alias.xsl
asserts:
- /defects[count(defect[@severity='error'])=0]
input: |
+package org.eolang.txt
+alias org.eolang.io.stdout

[x] > foo
stdout x > @
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# SPDX-FileCopyrightText: Copyright (c) 2016-2026 Objectionary.com
# SPDX-License-Identifier: MIT
---
sheets:
- /org/eolang/lints/aliases/same-package-alias.xsl
asserts:
- /defects[count(defect[@severity='error'])=0]
input: |
+package org.eolang.txt
+alias sp org.eolang.txt.sprintf

[x] > foo
sp x > @
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# SPDX-FileCopyrightText: Copyright (c) 2016-2026 Objectionary.com
# SPDX-License-Identifier: MIT
---
sheets:
- /org/eolang/lints/aliases/same-package-alias.xsl
asserts:
- /defects[count(defect[@severity='error'])=1]
- /defects/defect[@line='2']
input: |
+package org.eolang.txt
+alias org.eolang.txt.sprintf

[x] > foo
sprintf x > @
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# SPDX-FileCopyrightText: Copyright (c) 2016-2026 Objectionary.com
# SPDX-License-Identifier: MIT
---
sheets:
- /org/eolang/lints/aliases/same-package-alias.xsl
asserts:
- /defects[count(defect[@context and @severity='error'])=1]
document: |
<object author="tests">
<metas>
<meta>
<head>package</head>
<tail>org.eolang.txt</tail>
<part>org.eolang.txt</part>
</meta>
<meta>
<head>alias</head>
<tail>sprintf Φ.org.eolang.txt.sprintf</tail>
<part>sprintf</part>
<part>Φ.org.eolang.txt.sprintf</part>
</meta>
</metas>
<o name="foo"/>
</object>
Loading