Skip to content

Commit

Permalink
feat: show info that empty constraint lists can be removed (#572)
Browse files Browse the repository at this point in the history
Closes #570

### Summary of Changes

Show an info if a constraint list is empty.

---------

Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
  • Loading branch information
lars-reimann and megalinter-bot authored Sep 21, 2023
1 parent cba3abf commit af13e28
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 38 deletions.
15 changes: 8 additions & 7 deletions src/language/validation/safe-ds-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import {
annotationParameterListShouldNotBeEmpty,
assignmentShouldHaveMoreThanWildcardsAsAssignees,
classBodyShouldNotBeEmpty,
classTypeParameterListShouldNotBeEmpty,
constraintListShouldNotBeEmpty,
enumBodyShouldNotBeEmpty,
enumVariantParameterListShouldNotBeEmpty,
enumVariantTypeParameterListShouldNotBeEmpty,
functionResultListShouldNotBeEmpty,
functionTypeParameterListShouldNotBeEmpty,
segmentResultListShouldNotBeEmpty,
typeParameterListShouldNotBeEmpty,
unionTypeShouldNotHaveASingularTypeArgument,
} from './style.js';
import { templateStringMustHaveExpressionBetweenTwoStringParts } from './other/expressions/templateStrings.js';
Expand All @@ -31,17 +30,19 @@ export const registerValidationChecks = function (services: SafeDsServices) {
SdsAssignment: [assignmentShouldHaveMoreThanWildcardsAsAssignees],
SdsAnnotation: [annotationParameterListShouldNotBeEmpty],
SdsAttribute: [attributeMustHaveTypeHint],
SdsClass: [classBodyShouldNotBeEmpty, classTypeParameterListShouldNotBeEmpty],
SdsClassBody: [classBodyShouldNotBeEmpty],
SdsConstraintList: [constraintListShouldNotBeEmpty],
SdsDeclaration: [nameMustNotStartWithBlockLambdaPrefix, nameShouldHaveCorrectCasing],
SdsEnum: [enumBodyShouldNotBeEmpty],
SdsEnumVariant: [enumVariantParameterListShouldNotBeEmpty, enumVariantTypeParameterListShouldNotBeEmpty],
SdsFunction: [functionResultListShouldNotBeEmpty, functionTypeParameterListShouldNotBeEmpty],
SdsEnumBody: [enumBodyShouldNotBeEmpty],
SdsEnumVariant: [enumVariantParameterListShouldNotBeEmpty],
SdsFunction: [functionResultListShouldNotBeEmpty],
SdsModule: [moduleDeclarationsMustMatchFileKind, moduleWithDeclarationsMustStatePackage],
SdsParameter: [parameterMustHaveTypeHint],
SdsResult: [resultMustHaveTypeHint],
SdsSegment: [segmentResultListShouldNotBeEmpty],
SdsTemplateString: [templateStringMustHaveExpressionBetweenTwoStringParts],
SdsTypeParameterConstraint: [typeParameterConstraintLeftOperandMustBeOwnTypeParameter],
SdsTypeParameterList: [typeParameterListShouldNotBeEmpty],
SdsUnionType: [unionTypeShouldNotHaveASingularTypeArgument],
SdsYield: [yieldMustNotBeUsedInPipeline],
};
Expand Down
55 changes: 24 additions & 31 deletions src/language/validation/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import {
isSdsWildcard,
SdsAnnotation,
SdsAssignment,
SdsClass,
SdsEnum,
SdsClassBody,
SdsConstraintList,
SdsEnumBody,
SdsEnumVariant,
SdsFunction,
SdsSegment,
SdsTypeParameterList,
SdsUnionType,
} from '../generated/ast.js';
import { ValidationAcceptor } from 'langium';
Expand All @@ -15,6 +17,7 @@ import { isEmpty } from 'radash';
export const CODE_STYLE_UNNECESSARY_ASSIGNMENT = 'style/unnecessary-assignment';
export const CODE_STYLE_UNNECESSARY_ARGUMENT_LIST = 'style/unnecessary-argument-list';
export const CODE_STYLE_UNNECESSARY_BODY = 'style/unnecessary-body';
export const CODE_STYLE_UNNECESSARY_CONSTRAINT_LIST = 'style/unnecessary-constraint-list';
export const CODE_STYLE_UNNECESSARY_ELVIS_OPERATOR = 'style/unnecessary-elvis-operator';
export const CODE_STYLE_UNNECESSARY_SAFE_ACCESS = 'style/unnecessary-safe-access';
export const CODE_STYLE_UNNECESSARY_PARAMETER_LIST = 'style/unnecessary-parameter-list';
Expand Down Expand Up @@ -44,26 +47,37 @@ export const assignmentShouldHaveMoreThanWildcardsAsAssignees = (
// Unnecessary bodies
// -----------------------------------------------------------------------------

export const classBodyShouldNotBeEmpty = (node: SdsClass, accept: ValidationAcceptor) => {
if (node.body && isEmpty(node.body.members)) {
export const classBodyShouldNotBeEmpty = (node: SdsClassBody, accept: ValidationAcceptor) => {
if (isEmpty(node.members)) {
accept('info', 'This body can be removed.', {
node,
property: 'body',
code: CODE_STYLE_UNNECESSARY_BODY,
});
}
};

export const enumBodyShouldNotBeEmpty = (node: SdsEnum, accept: ValidationAcceptor) => {
if (node.body && isEmpty(node.body.variants)) {
export const enumBodyShouldNotBeEmpty = (node: SdsEnumBody, accept: ValidationAcceptor) => {
if (isEmpty(node.variants)) {
accept('info', 'This body can be removed.', {
node,
property: 'body',
code: CODE_STYLE_UNNECESSARY_BODY,
});
}
};

// -----------------------------------------------------------------------------
// Unnecessary constraint list
// -----------------------------------------------------------------------------

export const constraintListShouldNotBeEmpty = (node: SdsConstraintList, accept: ValidationAcceptor) => {
if (isEmpty(node.constraints)) {
accept('info', 'This constraint list can be removed.', {
node,
code: CODE_STYLE_UNNECESSARY_CONSTRAINT_LIST,
});
}
};

// -----------------------------------------------------------------------------
// Unnecessary parameter lists
// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -116,31 +130,10 @@ export const segmentResultListShouldNotBeEmpty = (node: SdsSegment, accept: Vali
// Unnecessary type parameter lists
// -----------------------------------------------------------------------------

export const classTypeParameterListShouldNotBeEmpty = (node: SdsClass, accept: ValidationAcceptor) => {
if (node.typeParameterList && isEmpty(node.typeParameterList.typeParameters)) {
accept('info', 'This type parameter list can be removed.', {
node,
property: 'typeParameterList',
code: CODE_STYLE_UNNECESSARY_TYPE_PARAMETER_LIST,
});
}
};

export const enumVariantTypeParameterListShouldNotBeEmpty = (node: SdsEnumVariant, accept: ValidationAcceptor) => {
if (node.typeParameterList && isEmpty(node.typeParameterList.typeParameters)) {
accept('info', 'This type parameter list can be removed.', {
node,
property: 'typeParameterList',
code: CODE_STYLE_UNNECESSARY_TYPE_PARAMETER_LIST,
});
}
};

export const functionTypeParameterListShouldNotBeEmpty = (node: SdsFunction, accept: ValidationAcceptor) => {
if (node.typeParameterList && isEmpty(node.typeParameterList.typeParameters)) {
export const typeParameterListShouldNotBeEmpty = (node: SdsTypeParameterList, accept: ValidationAcceptor) => {
if (isEmpty(node.typeParameters)) {
accept('info', 'This type parameter list can be removed.', {
node,
property: 'typeParameterList',
code: CODE_STYLE_UNNECESSARY_TYPE_PARAMETER_LIST,
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package tests.validation.style.unnecessaryConstraintListInAnnotation

// $TEST$ info "This constraint list can be removed."
annotation MyAnnotation1 »where {}«

// $TEST$ no info "This constraint list can be removed."
annotation MyAnnotation2 »where {
T sub Int
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package tests.validation.style.unnecessaryConstraintListInClass

// $TEST$ info "This constraint list can be removed."
class MyClass1 »where {}«

// $TEST$ no info "This constraint list can be removed."
class MyClass2<T> »where {
T sub Int
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package tests.validation.style.unnecessaryConstraintInEnumVariant

enum MyEnum {
// $TEST$ info "This constraint list can be removed."
MyVariant1 »where {}«

// $TEST$ no info "This constraint list can be removed."
MyVariant2<T>() »where {
T sub Int
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package tests.validation.style.unnecessaryConstraintListInFunction

// $TEST$ info "This constraint list can be removed."
fun myFunction1() »where {}«

// $TEST$ no info "This constraint list can be removed."
fun myFunction2<T>() »where {
T sub Int

0 comments on commit af13e28

Please sign in to comment.