Skip to content

Commit

Permalink
feat: show info that empty constraint lists can be removed
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-reimann committed Sep 21, 2023
1 parent 1bfcc32 commit 9fef701
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/language/validation/safe-ds-validator.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import { ValidationChecks } from 'langium';
import { SafeDsAstType } from '../generated/ast.js';
import type { SafeDsServices } from '../safe-ds-module.js';
import { nameMustNotStartWithBlockLambdaPrefix, nameShouldHaveCorrectCasing } from './names.js';
import {ValidationChecks} from 'langium';
import {SafeDsAstType} from '../generated/ast.js';
import type {SafeDsServices} from '../safe-ds-module.js';
import {nameMustNotStartWithBlockLambdaPrefix, nameShouldHaveCorrectCasing} from './names.js';
import {
annotationParameterListShouldNotBeEmpty,
assignmentShouldHaveMoreThanWildcardsAsAssignees,
classBodyShouldNotBeEmpty,
classBodyShouldNotBeEmpty, constraintListShouldNotBeEmpty,
enumBodyShouldNotBeEmpty,
enumVariantParameterListShouldNotBeEmpty,
functionResultListShouldNotBeEmpty,
segmentResultListShouldNotBeEmpty,
typeParameterListShouldNotBeEmpty,
unionTypeShouldNotHaveASingularTypeArgument,
} from './style.js';
import { templateStringMustHaveExpressionBetweenTwoStringParts } from './other/expressions/templateStrings.js';
import { yieldMustNotBeUsedInPipeline } from './other/statements/assignments.js';
import { attributeMustHaveTypeHint, parameterMustHaveTypeHint, resultMustHaveTypeHint } from './types.js';
import { moduleDeclarationsMustMatchFileKind, moduleWithDeclarationsMustStatePackage } from './other/modules.js';
import { typeParameterConstraintLeftOperandMustBeOwnTypeParameter } from './other/declarations/typeParameterConstraints.js';
import {templateStringMustHaveExpressionBetweenTwoStringParts} from './other/expressions/templateStrings.js';
import {yieldMustNotBeUsedInPipeline} from './other/statements/assignments.js';
import {attributeMustHaveTypeHint, parameterMustHaveTypeHint, resultMustHaveTypeHint} from './types.js';
import {moduleDeclarationsMustMatchFileKind, moduleWithDeclarationsMustStatePackage} from './other/modules.js';
import {
typeParameterConstraintLeftOperandMustBeOwnTypeParameter
} from './other/declarations/typeParameterConstraints.js';

/**
* Register custom validation checks.
Expand All @@ -30,6 +32,7 @@ export const registerValidationChecks = function (services: SafeDsServices) {
SdsAnnotation: [annotationParameterListShouldNotBeEmpty],
SdsAttribute: [attributeMustHaveTypeHint],
SdsClassBody: [classBodyShouldNotBeEmpty],
SdsConstraintList: [constraintListShouldNotBeEmpty],
SdsDeclaration: [nameMustNotStartWithBlockLambdaPrefix, nameShouldHaveCorrectCasing],
SdsEnumBody: [enumBodyShouldNotBeEmpty],
SdsEnumVariant: [enumVariantParameterListShouldNotBeEmpty],
Expand All @@ -50,4 +53,5 @@ export const registerValidationChecks = function (services: SafeDsServices) {
/**
* Implementation of custom validations.
*/
export class SafeDsValidator {}
export class SafeDsValidator {
}
15 changes: 15 additions & 0 deletions src/language/validation/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
SdsAnnotation,
SdsAssignment,
SdsClassBody,
SdsConstraintList,
SdsEnumBody,
SdsEnumVariant,
SdsFunction,
Expand All @@ -16,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 @@ -63,6 +65,19 @@ export const enumBodyShouldNotBeEmpty = (node: SdsEnumBody, accept: ValidationAc
}
};

// -----------------------------------------------------------------------------
// 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
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 9fef701

Please sign in to comment.