-
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 annotation target (#670)
Closes #543 ### Summary of Changes * Show an error if an annotation is called on an incorrect target * Show a warning if the target list of an annotation has duplicate entries
- Loading branch information
1 parent
ba1e9a8
commit fa7631d
Showing
30 changed files
with
1,739 additions
and
65 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,16 @@ | ||
import { isSdsEnum, SdsEnum } from '../generated/ast.js'; | ||
import { SafeDsModuleMembers } from './safe-ds-module-members.js'; | ||
import { resourceNameToUri } from '../../helpers/resources.js'; | ||
import { URI } from 'langium'; | ||
|
||
const ANNOTATION_USAGE_URI = resourceNameToUri('builtins/safeds/lang/annotationUsage.sdsstub'); | ||
|
||
export class SafeDsEnums extends SafeDsModuleMembers<SdsEnum> { | ||
get AnnotationTarget(): SdsEnum | undefined { | ||
return this.getEnum(ANNOTATION_USAGE_URI, 'AnnotationTarget'); | ||
} | ||
|
||
private getEnum(uri: URI, name: string): SdsEnum | undefined { | ||
return this.getModuleMember(uri, name, isSdsEnum); | ||
} | ||
} |
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
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,156 @@ | ||
import { ValidationAcceptor } from 'langium'; | ||
import { | ||
isSdsAnnotation, | ||
isSdsAttribute, | ||
isSdsClass, | ||
isSdsEnum, | ||
isSdsEnumVariant, | ||
isSdsFunction, | ||
isSdsModule, | ||
isSdsParameter, | ||
isSdsPipeline, | ||
isSdsResult, | ||
isSdsSegment, | ||
isSdsTypeParameter, | ||
SdsAnnotation, | ||
SdsAnnotationCall, | ||
} from '../../generated/ast.js'; | ||
import { SafeDsServices } from '../../safe-ds-module.js'; | ||
import { duplicatesBy, isEmpty } from '../../../helpers/collectionUtils.js'; | ||
import { pluralize } from '../../../helpers/stringUtils.js'; | ||
import { findFirstAnnotationCallOf, getAnnotationCallTarget } from '../../helpers/nodeProperties.js'; | ||
|
||
export const CODE_TARGET_DUPLICATE_TARGET = 'target/duplicate-target'; | ||
export const CODE_TARGET_WRONG_TARGET = 'target/wrong-target'; | ||
|
||
export const targetShouldNotHaveDuplicateEntries = (services: SafeDsServices) => { | ||
const builtinAnnotations = services.builtins.Annotations; | ||
|
||
return (node: SdsAnnotation, accept: ValidationAcceptor) => { | ||
const annotationCall = findFirstAnnotationCallOf(node, builtinAnnotations.Target); | ||
if (!annotationCall) { | ||
return; | ||
} | ||
|
||
const validTargets = builtinAnnotations.streamValidTargets(node).map((it) => `'${it.name}'`); | ||
const duplicateTargets = duplicatesBy(validTargets, (it) => it) | ||
.distinct() | ||
.toArray(); | ||
|
||
if (isEmpty(duplicateTargets)) { | ||
return; | ||
} | ||
|
||
const noun = pluralize(duplicateTargets.length, 'target'); | ||
const duplicateTargetString = duplicateTargets.join(', '); | ||
const verb = pluralize(duplicateTargets.length, 'occurs', 'occur'); | ||
|
||
accept('warning', `The ${noun} ${duplicateTargetString} ${verb} multiple times.`, { | ||
node: annotationCall, | ||
property: 'annotation', | ||
code: CODE_TARGET_DUPLICATE_TARGET, | ||
}); | ||
}; | ||
}; | ||
|
||
export const annotationCallMustHaveCorrectTarget = (services: SafeDsServices) => { | ||
const builtinAnnotations = services.builtins.Annotations; | ||
|
||
return (node: SdsAnnotationCall, accept: ValidationAcceptor) => { | ||
const annotation = node.annotation?.ref; | ||
if (!annotation) { | ||
return; | ||
} | ||
|
||
const actualTarget = getActualTarget(node); | ||
/* c8 ignore start */ | ||
if (!actualTarget) { | ||
return; | ||
} | ||
/* c8 ignore stop */ | ||
|
||
const validTargets = builtinAnnotations | ||
.streamValidTargets(annotation) | ||
.map((it) => it.name) | ||
.toSet(); | ||
|
||
if (!validTargets.has(actualTarget.enumVariantName)) { | ||
accept('error', `The annotation '${annotation.name}' cannot be applied to ${actualTarget.prettyName}.`, { | ||
node, | ||
property: 'annotation', | ||
code: CODE_TARGET_WRONG_TARGET, | ||
}); | ||
} | ||
}; | ||
}; | ||
|
||
const getActualTarget = (node: SdsAnnotationCall): GetActualTargetResult | void => { | ||
const annotatedObject = getAnnotationCallTarget(node); | ||
|
||
if (isSdsAnnotation(annotatedObject)) { | ||
return { | ||
enumVariantName: 'Annotation', | ||
prettyName: 'an annotation', | ||
}; | ||
} else if (isSdsAttribute(annotatedObject)) { | ||
return { | ||
enumVariantName: 'Attribute', | ||
prettyName: 'an attribute', | ||
}; | ||
} else if (isSdsClass(annotatedObject)) { | ||
return { | ||
enumVariantName: 'Class', | ||
prettyName: 'a class', | ||
}; | ||
} else if (isSdsEnum(annotatedObject)) { | ||
return { | ||
enumVariantName: 'Enum', | ||
prettyName: 'an enum', | ||
}; | ||
} else if (isSdsEnumVariant(annotatedObject)) { | ||
return { | ||
enumVariantName: 'EnumVariant', | ||
prettyName: 'an enum variant', | ||
}; | ||
} else if (isSdsFunction(annotatedObject)) { | ||
return { | ||
enumVariantName: 'Function', | ||
prettyName: 'a function', | ||
}; | ||
} else if (isSdsModule(annotatedObject)) { | ||
return { | ||
enumVariantName: 'Module', | ||
prettyName: 'a module', | ||
}; | ||
} else if (isSdsParameter(annotatedObject)) { | ||
return { | ||
enumVariantName: 'Parameter', | ||
prettyName: 'a parameter', | ||
}; | ||
} else if (isSdsPipeline(annotatedObject)) { | ||
return { | ||
enumVariantName: 'Pipeline', | ||
prettyName: 'a pipeline', | ||
}; | ||
} else if (isSdsResult(annotatedObject)) { | ||
return { | ||
enumVariantName: 'Result', | ||
prettyName: 'a result', | ||
}; | ||
} else if (isSdsSegment(annotatedObject)) { | ||
return { | ||
enumVariantName: 'Segment', | ||
prettyName: 'a segment', | ||
}; | ||
} else if (isSdsTypeParameter(annotatedObject)) { | ||
return { | ||
enumVariantName: 'TypeParameter', | ||
prettyName: 'a type parameter', | ||
}; | ||
} | ||
}; | ||
|
||
interface GetActualTargetResult { | ||
enumVariantName: string; | ||
prettyName: string; | ||
} |
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
Oops, something went wrong.