-
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: error if single use annotations are used multiple times (#631)
Closes partially #543 ### Summary of Changes Show an error if annotation that is not repeatable (default, unless it has the `@Repeatable` annotation) is repeated. --------- Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
- Loading branch information
1 parent
e8e2bf6
commit 17a5b7a
Showing
5 changed files
with
57 additions
and
0 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,22 @@ | ||
import { ValidationAcceptor } from 'langium'; | ||
import { SdsAnnotatedObject } from '../../generated/ast.js'; | ||
import { SafeDsServices } from '../../safe-ds-module.js'; | ||
import { annotationCallsOrEmpty } from '../../helpers/nodeProperties.js'; | ||
import { duplicatesBy } from '../../helpers/collectionUtils.js'; | ||
|
||
export const CODE_ANNOTATION_NOT_REPEATABLE = 'annotation/not-repeatable'; | ||
|
||
export const singleUseAnnotationsMustNotBeRepeated = | ||
(services: SafeDsServices) => (node: SdsAnnotatedObject, accept: ValidationAcceptor) => { | ||
const callsOfSingleUseAnnotations = annotationCallsOrEmpty(node).filter((it) => { | ||
const annotation = it.annotation?.ref; | ||
return annotation && !services.builtins.Annotations.isRepeatable(annotation); | ||
}); | ||
|
||
for (const duplicate of duplicatesBy(callsOfSingleUseAnnotations, (it) => it.annotation?.ref)) { | ||
accept('error', `The annotation '${duplicate.annotation.$refText}' is not repeatable.`, { | ||
node: duplicate, | ||
code: CODE_ANNOTATION_NOT_REPEATABLE, | ||
}); | ||
} | ||
}; |
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
24 changes: 24 additions & 0 deletions
24
tests/resources/validation/builtins/annotations/repeatable/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,24 @@ | ||
package tests.validation.builtins.annotations.repeatable | ||
|
||
annotation SingleUse | ||
|
||
@Repeatable | ||
annotation MultiUse | ||
|
||
// $TEST$ no error r"The annotation '\w*' is not repeatable\." | ||
»@SingleUse« | ||
// $TEST$ no error r"The annotation '\w*' is not repeatable\." | ||
»@MultiUse« | ||
// $TEST$ no error r"The annotation '\w*' is not repeatable\." | ||
»@MultiUse« | ||
// $TEST$ no error r"The annotation '\w*' is not repeatable\." | ||
»@UnresolvedAnnotation« | ||
// $TEST$ no error r"The annotation '\w*' is not repeatable\." | ||
»@UnresolvedAnnotation« | ||
class CorrectUse | ||
|
||
// $TEST$ no error r"The annotation '\w*' is not repeatable\." | ||
»@SingleUse« | ||
// $TEST$ error "The annotation 'SingleUse' is not repeatable." | ||
»@SingleUse« | ||
class IncorrectUse |
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