-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: validation for results of segments (#613)
Closes partially #543 ### Summary of Changes Ensure that a segment yields exactly one value for each result. --------- Co-authored-by: megalinter-bot <[email protected]>
- Loading branch information
1 parent
3a2e9cc
commit bf20c7c
Showing
9 changed files
with
97 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { SdsSegment } from '../../../generated/ast.js'; | ||
import { ValidationAcceptor } from 'langium'; | ||
import { parametersOrEmpty, resultsOrEmpty } from '../../../helpers/nodeProperties.js'; | ||
import { SafeDsServices } from '../../../safe-ds-module.js'; | ||
|
||
export const CODE_SEGMENT_DUPLICATE_YIELD = 'segment/duplicate-yield'; | ||
export const CODE_SEGMENT_UNASSIGNED_RESULT = 'segment/unassigned-result'; | ||
export const CODE_SEGMENT_UNUSED_PARAMETER = 'segment/unused-parameter'; | ||
|
||
export const segmentResultMustBeAssignedExactlyOnce = | ||
(services: SafeDsServices) => (node: SdsSegment, accept: ValidationAcceptor) => { | ||
const results = resultsOrEmpty(node.resultList); | ||
for (const result of results) { | ||
const yields = services.helpers.NodeMapper.resultToYields(result); | ||
if (yields.isEmpty()) { | ||
accept('error', 'Nothing is assigned to this result.', { | ||
node: result, | ||
property: 'name', | ||
code: CODE_SEGMENT_UNASSIGNED_RESULT, | ||
}); | ||
continue; | ||
} | ||
|
||
const duplicateYields = yields.tail(1); | ||
for (const duplicate of duplicateYields) { | ||
accept('error', `The result '${result.name}' has been assigned already.`, { | ||
node: duplicate, | ||
property: 'result', | ||
code: CODE_SEGMENT_DUPLICATE_YIELD, | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
export const segmentParameterShouldBeUsed = | ||
(services: SafeDsServices) => (node: SdsSegment, accept: ValidationAcceptor) => { | ||
for (const parameter of parametersOrEmpty(node)) { | ||
const usages = services.helpers.NodeMapper.parameterToReferences(parameter); | ||
|
||
if (usages.isEmpty()) { | ||
accept('warning', 'This parameter is unused and can be removed.', { | ||
node: parameter, | ||
property: 'name', | ||
code: CODE_SEGMENT_UNUSED_PARAMETER, | ||
}); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
tests/resources/scoping/references/in same file/to modules/main.sdstest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package tests | ||
|
||
pipeline myPipeline { | ||
// $TEST$ unresolved | ||
»tests«; | ||
} |
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...larations/placeholder/unused/main.sdstest → ...arations/placeholders/unused/main.sdstest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
tests/resources/validation/other/declarations/segments/duplicate yield/main.sdstest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package tests.validation.other.declarations.segments.duplicateYield | ||
|
||
segment mySegment() -> (a: Int, b: Int, c: Int) { | ||
// $TEST$ no error r"The result '\w*' has been assigned already\." | ||
yield »a« = 1; | ||
|
||
// $TEST$ no error r"The result '\w*' has been assigned already\." | ||
yield »b« = 1; | ||
// $TEST$ error "The result 'b' has been assigned already." | ||
yield »b« = 1; | ||
|
||
// $TEST$ no error r"The result '\w*' has been assigned already\." | ||
// $TEST$ error "The result 'c' has been assigned already." | ||
yield »c«, yield »c« = 1; | ||
|
||
// $TEST$ no error r"The result '\w*' has been assigned already\." | ||
yield »unresolved« = 1; | ||
// $TEST$ no error r"The result '\w*' has been assigned already\." | ||
yield »unresolved« = 1; | ||
} |
17 changes: 17 additions & 0 deletions
17
tests/resources/validation/other/declarations/segments/unassigned result/main.sdstest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package tests.validation.other.declarations.segments.unassignedResult | ||
|
||
// $TEST$ no error "Nothing is assigned to this result." | ||
// $TEST$ no error "Nothing is assigned to this result." | ||
// $TEST$ no error "Nothing is assigned to this result." | ||
// $TEST$ no error "Nothing is assigned to this result." | ||
// $TEST$ error "Nothing is assigned to this result." | ||
segment mySegment() -> (»a«: Int, »b«: Int, »c«: Int, »d«: Int, »e«: Int) { | ||
yield b = 1; | ||
yield a = 1; | ||
|
||
yield c = 1; | ||
yield c = 1; | ||
|
||
// While nothing is assigned to d, the programmer still states their intention to do so. We already show another error for this. | ||
_, yield d = 1; | ||
} |
2 changes: 1 addition & 1 deletion
2
...eclarations/parameter/unused/main.sdstest → ...ns/segments/unused parameter/main.sdstest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters