diff --git a/src/main/resources/org/eolang/lints/design/broad-scope.xsl b/src/main/resources/org/eolang/lints/design/broad-scope.xsl new file mode 100644 index 000000000..6ea9c02e3 --- /dev/null +++ b/src/main/resources/org/eolang/lints/design/broad-scope.xsl @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + warning + + The private attribute + + is used only inside the object + + , its scope is too broad + + + + + + diff --git a/src/main/resources/org/eolang/motives/design/broad-scope.md b/src/main/resources/org/eolang/motives/design/broad-scope.md new file mode 100644 index 000000000..b78caafe4 --- /dev/null +++ b/src/main/resources/org/eolang/motives/design/broad-scope.md @@ -0,0 +1,25 @@ +# Broad scope + +A private attribute should be declared as close as possible to its only +usage. If a private attribute is used only inside a single nested object, +declaring it on the level of the parent object makes its scope unnecessarily +broad. + +Incorrect: + +```eo +[] > foo + 42 >> a + [] >> b + a.plus 1 > c +``` + +Here, the scope of `a` is too broad: it is used only inside `b`. It should be +moved closer, into `b`: + +```eo +[] > foo + [] >> b + 42 >> a + a.plus 1 > c +``` diff --git a/src/test/resources/org/eolang/lints/packs/single/broad-scope/allows-used-in-multiple-objects.yaml b/src/test/resources/org/eolang/lints/packs/single/broad-scope/allows-used-in-multiple-objects.yaml new file mode 100644 index 000000000..82d00cc8d --- /dev/null +++ b/src/test/resources/org/eolang/lints/packs/single/broad-scope/allows-used-in-multiple-objects.yaml @@ -0,0 +1,14 @@ +# SPDX-FileCopyrightText: Copyright (c) 2016-2026 Objectionary.com +# SPDX-License-Identifier: MIT +--- +sheets: + - /org/eolang/lints/design/broad-scope.xsl +asserts: + - /defects[count(defect[@severity='warning'])=0] +input: | + [] > foo + 42 >> a + [] >> b + a.plus 1 > c + [] > z + a.plus 2 > d diff --git a/src/test/resources/org/eolang/lints/packs/single/broad-scope/allows-used-on-top-level.yaml b/src/test/resources/org/eolang/lints/packs/single/broad-scope/allows-used-on-top-level.yaml new file mode 100644 index 000000000..64883cf2b --- /dev/null +++ b/src/test/resources/org/eolang/lints/packs/single/broad-scope/allows-used-on-top-level.yaml @@ -0,0 +1,13 @@ +# SPDX-FileCopyrightText: Copyright (c) 2016-2026 Objectionary.com +# SPDX-License-Identifier: MIT +--- +sheets: + - /org/eolang/lints/design/broad-scope.xsl +asserts: + - /defects[count(defect[@severity='warning'])=0] +input: | + [] > foo + 42 >> a + a.plus 1 > c + [] >> b + a.plus 2 > d diff --git a/src/test/resources/org/eolang/lints/packs/single/broad-scope/catches-broad-scope.yaml b/src/test/resources/org/eolang/lints/packs/single/broad-scope/catches-broad-scope.yaml new file mode 100644 index 000000000..a04646c28 --- /dev/null +++ b/src/test/resources/org/eolang/lints/packs/single/broad-scope/catches-broad-scope.yaml @@ -0,0 +1,13 @@ +# SPDX-FileCopyrightText: Copyright (c) 2016-2026 Objectionary.com +# SPDX-License-Identifier: MIT +--- +sheets: + - /org/eolang/lints/design/broad-scope.xsl +asserts: + - /defects[count(defect[@severity='warning'])=1] + - /defects/defect[@line='2'] +input: | + [] > foo + 42 >> a + [] >> b + a.plus 1 > c