Skip to content

Commit 9fef701

Browse files
committed
feat: show info that empty constraint lists can be removed
1 parent 1bfcc32 commit 9fef701

File tree

6 files changed

+68
-11
lines changed

6 files changed

+68
-11
lines changed

src/language/validation/safe-ds-validator.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1-
import { ValidationChecks } from 'langium';
2-
import { SafeDsAstType } from '../generated/ast.js';
3-
import type { SafeDsServices } from '../safe-ds-module.js';
4-
import { nameMustNotStartWithBlockLambdaPrefix, nameShouldHaveCorrectCasing } from './names.js';
1+
import {ValidationChecks} from 'langium';
2+
import {SafeDsAstType} from '../generated/ast.js';
3+
import type {SafeDsServices} from '../safe-ds-module.js';
4+
import {nameMustNotStartWithBlockLambdaPrefix, nameShouldHaveCorrectCasing} from './names.js';
55
import {
66
annotationParameterListShouldNotBeEmpty,
77
assignmentShouldHaveMoreThanWildcardsAsAssignees,
8-
classBodyShouldNotBeEmpty,
8+
classBodyShouldNotBeEmpty, constraintListShouldNotBeEmpty,
99
enumBodyShouldNotBeEmpty,
1010
enumVariantParameterListShouldNotBeEmpty,
1111
functionResultListShouldNotBeEmpty,
1212
segmentResultListShouldNotBeEmpty,
1313
typeParameterListShouldNotBeEmpty,
1414
unionTypeShouldNotHaveASingularTypeArgument,
1515
} from './style.js';
16-
import { templateStringMustHaveExpressionBetweenTwoStringParts } from './other/expressions/templateStrings.js';
17-
import { yieldMustNotBeUsedInPipeline } from './other/statements/assignments.js';
18-
import { attributeMustHaveTypeHint, parameterMustHaveTypeHint, resultMustHaveTypeHint } from './types.js';
19-
import { moduleDeclarationsMustMatchFileKind, moduleWithDeclarationsMustStatePackage } from './other/modules.js';
20-
import { typeParameterConstraintLeftOperandMustBeOwnTypeParameter } from './other/declarations/typeParameterConstraints.js';
16+
import {templateStringMustHaveExpressionBetweenTwoStringParts} from './other/expressions/templateStrings.js';
17+
import {yieldMustNotBeUsedInPipeline} from './other/statements/assignments.js';
18+
import {attributeMustHaveTypeHint, parameterMustHaveTypeHint, resultMustHaveTypeHint} from './types.js';
19+
import {moduleDeclarationsMustMatchFileKind, moduleWithDeclarationsMustStatePackage} from './other/modules.js';
20+
import {
21+
typeParameterConstraintLeftOperandMustBeOwnTypeParameter
22+
} from './other/declarations/typeParameterConstraints.js';
2123

2224
/**
2325
* Register custom validation checks.
@@ -30,6 +32,7 @@ export const registerValidationChecks = function (services: SafeDsServices) {
3032
SdsAnnotation: [annotationParameterListShouldNotBeEmpty],
3133
SdsAttribute: [attributeMustHaveTypeHint],
3234
SdsClassBody: [classBodyShouldNotBeEmpty],
35+
SdsConstraintList: [constraintListShouldNotBeEmpty],
3336
SdsDeclaration: [nameMustNotStartWithBlockLambdaPrefix, nameShouldHaveCorrectCasing],
3437
SdsEnumBody: [enumBodyShouldNotBeEmpty],
3538
SdsEnumVariant: [enumVariantParameterListShouldNotBeEmpty],
@@ -50,4 +53,5 @@ export const registerValidationChecks = function (services: SafeDsServices) {
5053
/**
5154
* Implementation of custom validations.
5255
*/
53-
export class SafeDsValidator {}
56+
export class SafeDsValidator {
57+
}

src/language/validation/style.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
SdsAnnotation,
44
SdsAssignment,
55
SdsClassBody,
6+
SdsConstraintList,
67
SdsEnumBody,
78
SdsEnumVariant,
89
SdsFunction,
@@ -16,6 +17,7 @@ import { isEmpty } from 'radash';
1617
export const CODE_STYLE_UNNECESSARY_ASSIGNMENT = 'style/unnecessary-assignment';
1718
export const CODE_STYLE_UNNECESSARY_ARGUMENT_LIST = 'style/unnecessary-argument-list';
1819
export const CODE_STYLE_UNNECESSARY_BODY = 'style/unnecessary-body';
20+
export const CODE_STYLE_UNNECESSARY_CONSTRAINT_LIST = 'style/unnecessary-constraint-list';
1921
export const CODE_STYLE_UNNECESSARY_ELVIS_OPERATOR = 'style/unnecessary-elvis-operator';
2022
export const CODE_STYLE_UNNECESSARY_SAFE_ACCESS = 'style/unnecessary-safe-access';
2123
export const CODE_STYLE_UNNECESSARY_PARAMETER_LIST = 'style/unnecessary-parameter-list';
@@ -63,6 +65,19 @@ export const enumBodyShouldNotBeEmpty = (node: SdsEnumBody, accept: ValidationAc
6365
}
6466
};
6567

68+
// -----------------------------------------------------------------------------
69+
// Unnecessary constraint list
70+
// -----------------------------------------------------------------------------
71+
72+
export const constraintListShouldNotBeEmpty = (node: SdsConstraintList, accept: ValidationAcceptor) => {
73+
if (isEmpty(node.constraints)) {
74+
accept('info', 'This constraint list can be removed.', {
75+
node,
76+
code: CODE_STYLE_UNNECESSARY_CONSTRAINT_LIST,
77+
});
78+
}
79+
};
80+
6681
// -----------------------------------------------------------------------------
6782
// Unnecessary parameter lists
6883
// -----------------------------------------------------------------------------
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package tests.validation.style.unnecessaryConstraintListInAnnotation
2+
3+
// $TEST$ info "This constraint list can be removed."
4+
annotation MyAnnotation1 »where {}«
5+
6+
// $TEST$ no info "This constraint list can be removed."
7+
annotation MyAnnotation2 »where {
8+
T sub Int
9+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package tests.validation.style.unnecessaryConstraintListInClass
2+
3+
// $TEST$ info "This constraint list can be removed."
4+
class MyClass1 »where {}«
5+
6+
// $TEST$ no info "This constraint list can be removed."
7+
class MyClass2<T> »where {
8+
T sub Int
9+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package tests.validation.style.unnecessaryConstraintInEnumVariant
2+
3+
enum MyEnum {
4+
// $TEST$ info "This constraint list can be removed."
5+
MyVariant1 »where {}«
6+
7+
// $TEST$ no info "This constraint list can be removed."
8+
MyVariant2<T>() »where {
9+
T sub Int
10+
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package tests.validation.style.unnecessaryConstraintListInFunction
2+
3+
// $TEST$ info "This constraint list can be removed."
4+
fun myFunction1() »where {}«
5+
6+
// $TEST$ no info "This constraint list can be removed."
7+
fun myFunction2<T>() »where {
8+
T sub Int
9+

0 commit comments

Comments
 (0)