Skip to content

Commit

Permalink
refactor: typeParametersOrEmpty can now directly handle named type …
Browse files Browse the repository at this point in the history
…declarations
  • Loading branch information
lars-reimann committed Oct 6, 2023
1 parent 74208f1 commit 2d367f3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/language/builtins/safe-ds-core-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { SafeDsServices } from '../safe-ds-module.js';
import { resolveRelativePathToBuiltinFile } from './fileFinder.js';
import { isSdsClass, isSdsModule, SdsClass } from '../generated/ast.js';
import { LangiumDocuments } from 'langium';

import { moduleMembersOrEmpty } from '../helpers/nodeProperties.js';

const CORE_CLASSES_URI = resolveRelativePathToBuiltinFile('safeds/lang/coreClasses.sdsstub');
Expand All @@ -25,6 +24,7 @@ export class SafeDsCoreClasses {
}

private cachedBoolean: SdsClass | undefined;

/* c8 ignore stop */

get Boolean(): SdsClass | undefined {
Expand Down
66 changes: 47 additions & 19 deletions src/language/helpers/nodeProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import {
isSdsClass,
isSdsDeclaration,
isSdsEnum,
isSdsEnumVariant,
isSdsFunction,
isSdsModule,
isSdsModuleMember,
isSdsPlaceholder,
isSdsSegment,
isSdsTypeParameterList,
SdsAbstractCall,
SdsAnnotatedObject,
SdsAnnotationCall,
Expand All @@ -31,6 +33,7 @@ import {
SdsLiteralType,
SdsModule,
SdsModuleMember,
SdsNamedTypeDeclaration,
SdsParameter,
SdsPlaceholder,
SdsQualifiedImport,
Expand Down Expand Up @@ -77,7 +80,7 @@ export const isStatic = (node: SdsClassMember): boolean => {
// Accessors for list elements
// -------------------------------------------------------------------------------------------------

export const annotationCallsOrEmpty = function (node: SdsAnnotatedObject | undefined): SdsAnnotationCall[] {
export const annotationCallsOrEmpty = (node: SdsAnnotatedObject | undefined): SdsAnnotationCall[] => {
if (!node) {
/* c8 ignore next 2 */
return [];
Expand All @@ -90,62 +93,87 @@ export const annotationCallsOrEmpty = function (node: SdsAnnotatedObject | undef
return node?.annotationCalls ?? [];
}
};
export const argumentsOrEmpty = function (node: SdsAbstractCall | undefined): SdsArgument[] {
export const argumentsOrEmpty = (node: SdsAbstractCall | undefined): SdsArgument[] => {
return node?.argumentList?.arguments ?? [];
};
export const assigneesOrEmpty = function (node: SdsAssignment | undefined): SdsAssignee[] {
export const assigneesOrEmpty = (node: SdsAssignment | undefined): SdsAssignee[] => {
return node?.assigneeList?.assignees ?? [];
};
export const blockLambdaResultsOrEmpty = function (node: SdsBlockLambda | undefined): SdsBlockLambdaResult[] {
export const blockLambdaResultsOrEmpty = (node: SdsBlockLambda | undefined): SdsBlockLambdaResult[] => {
return stream(statementsOrEmpty(node?.body))
.filter(isSdsAssignment)
.flatMap(assigneesOrEmpty)
.filter(isSdsBlockLambdaResult)
.toArray();
};
export const importedDeclarationsOrEmpty = function (node: SdsQualifiedImport | undefined): SdsImportedDeclaration[] {
export const importedDeclarationsOrEmpty = (node: SdsQualifiedImport | undefined): SdsImportedDeclaration[] => {
return node?.importedDeclarationList?.importedDeclarations ?? [];
};
export const literalsOrEmpty = function (node: SdsLiteralType | undefined): SdsLiteral[] {

export const literalsOrEmpty = (node: SdsLiteralType | undefined): SdsLiteral[] => {
return node?.literalList?.literals ?? [];
};
export const classMembersOrEmpty = function (
export const classMembersOrEmpty = (
node: SdsClass | undefined,
filterFunction: (member: SdsClassMember) => boolean = () => true,
): SdsClassMember[] {
): SdsClassMember[] => {
return node?.body?.members?.filter(filterFunction) ?? [];
};
export const enumVariantsOrEmpty = function (node: SdsEnum | undefined): SdsEnumVariant[] {

export const enumVariantsOrEmpty = (node: SdsEnum | undefined): SdsEnumVariant[] => {
return node?.body?.variants ?? [];
};
export const importsOrEmpty = function (node: SdsModule | undefined): SdsImport[] {

export const importsOrEmpty = (node: SdsModule | undefined): SdsImport[] => {
return node?.imports ?? [];
};
export const moduleMembersOrEmpty = function (node: SdsModule | undefined): SdsModuleMember[] {

export const moduleMembersOrEmpty = (node: SdsModule | undefined): SdsModuleMember[] => {
return node?.members?.filter(isSdsModuleMember) ?? [];
};
export const packageNameOrUndefined = function (node: AstNode | undefined): string | undefined {

export const packageNameOrUndefined = (node: AstNode | undefined): string | undefined => {
return getContainerOfType(node, isSdsModule)?.name;
};
export const parametersOrEmpty = function (node: SdsCallable | undefined): SdsParameter[] {

export const parametersOrEmpty = (node: SdsCallable | undefined): SdsParameter[] => {
return node?.parameterList?.parameters ?? [];
};
export const placeholdersOrEmpty = function (node: SdsBlock | undefined): SdsPlaceholder[] {

export const placeholdersOrEmpty = (node: SdsBlock | undefined): SdsPlaceholder[] => {
return stream(statementsOrEmpty(node))
.filter(isSdsAssignment)
.flatMap(assigneesOrEmpty)
.filter(isSdsPlaceholder)
.toArray();
};
export const resultsOrEmpty = function (node: SdsResultList | undefined): SdsResult[] {

export const resultsOrEmpty = (node: SdsResultList | undefined): SdsResult[] => {
return node?.results ?? [];
};
export const statementsOrEmpty = function (node: SdsBlock | undefined): SdsStatement[] {

export const statementsOrEmpty = (node: SdsBlock | undefined): SdsStatement[] => {
return node?.statements ?? [];
};
export const typeArgumentsOrEmpty = function (node: SdsTypeArgumentList | undefined): SdsTypeArgument[] {

export const typeArgumentsOrEmpty = (node: SdsTypeArgumentList | undefined): SdsTypeArgument[] => {
return node?.typeArguments ?? [];
};
export const typeParametersOrEmpty = function (node: SdsTypeParameterList | undefined): SdsTypeParameter[] {
return node?.typeParameters ?? [];

export const typeParametersOrEmpty = (
node: SdsTypeParameterList | SdsNamedTypeDeclaration | undefined,
): SdsTypeParameter[] => {
if (!node) {
return [];
}

if (isSdsTypeParameterList(node)) {
return node.typeParameters;
} else if (isSdsClass(node)) {
return typeParametersOrEmpty(node.typeParameterList);
} else if (isSdsEnumVariant(node)) {
return typeParametersOrEmpty(node.typeParameterList);
} /* c8 ignore start */ else {
return [];
} /* c8 ignore stop */
};
15 changes: 1 addition & 14 deletions src/language/helpers/safe-ds-node-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import {
isSdsBlock,
isSdsCall,
isSdsCallable,
isSdsClass,
isSdsEnumVariant,
isSdsNamedType,
isSdsReference,
isSdsSegment,
Expand All @@ -16,7 +14,6 @@ import {
SdsAbstractCall,
SdsArgument,
SdsCallable,
SdsNamedTypeDeclaration,
SdsParameter,
SdsPlaceholder,
SdsReference,
Expand Down Expand Up @@ -202,17 +199,7 @@ export class SafeDsNodeMapper {

// Find type parameter at the same position
const namedTypeDeclaration = containingType.declaration.ref;
const typeParameters = this.typeParametersOfNamedTypeDeclarationOrEmpty(namedTypeDeclaration);
const typeParameters = typeParametersOrEmpty(namedTypeDeclaration);
return typeParameters[typeArgumentPosition];
}

private typeParametersOfNamedTypeDeclarationOrEmpty(node: SdsNamedTypeDeclaration | undefined): SdsTypeParameter[] {
if (isSdsClass(node)) {
return typeParametersOrEmpty(node.typeParameterList);
} else if (isSdsEnumVariant(node)) {
return typeParametersOrEmpty(node.typeParameterList);
} else {
return [];
}
}
}

0 comments on commit 2d367f3

Please sign in to comment.