Skip to content

Commit

Permalink
feat: improve error message
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-reimann committed Nov 11, 2023
1 parent 310a7fb commit 869dfb0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 38 deletions.
20 changes: 14 additions & 6 deletions packages/safe-ds-lang/src/language/validation/inheritance.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ValidationAcceptor } from 'langium';
import { expandToStringWithNL, ValidationAcceptor } from 'langium';
import { isEmpty } from '../../helpers/collectionUtils.js';
import { SdsClass, type SdsClassMember } from '../generated/ast.js';
import { getParentTypes } from '../helpers/nodeProperties.js';
Expand All @@ -25,11 +25,19 @@ export const classMemberMustMatchOverriddenMember = (services: SafeDsServices) =
const overriddenMemberType = typeComputer.computeType(overriddenMember);

if (!typeChecker.isAssignableTo(ownMemberType, overriddenMemberType)) {
accept('error', 'Overriding member does not match the overridden member.', {
node,
property: 'name',
code: CODE_INHERITANCE_MUST_MATCH_OVERRIDDEN_MEMBER,
});
accept(
'error',
expandToStringWithNL`
Overriding member does not match the overridden member:
- Expected type: ${overriddenMemberType}
- Actual type: ${ownMemberType}
`,
{
node,
property: 'name',
code: CODE_INHERITANCE_MUST_MATCH_OVERRIDDEN_MEMBER,
},
);
}
};
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { afterEach, beforeEach, describe, it } from 'vitest';
import { createSafeDsServices } from '../../../src/language/index.js';
import { NodeFileSystem } from 'langium/node';
import { createValidationTests, ExpectedIssue } from './creator.js';
import { Diagnostic, DiagnosticSeverity } from 'vscode-languageserver';
import { AssertionError } from 'assert';
import { NodeFileSystem } from 'langium/node';
import { clearDocuments, isRangeEqual } from 'langium/test';
import { afterEach, beforeEach, describe, it } from 'vitest';
import { Diagnostic, DiagnosticSeverity } from 'vscode-languageserver';
import { createSafeDsServices } from '../../../src/language/index.js';
import { locationToString } from '../../helpers/location.js';
import { loadDocuments } from '../../helpers/testResources.js';
import { createValidationTests, ExpectedIssue } from './creator.js';

const services = createSafeDsServices(NodeFileSystem).SafeDs;

Expand Down Expand Up @@ -94,7 +94,7 @@ const getMatchingActualIssues = (expectedIssue: ExpectedIssue): Diagnostic[] =>
// Filter by message
if (expectedIssue.message) {
if (expectedIssue.messageIsRegex) {
const regex = new RegExp(expectedIssue.message, 'gu');
const regex = new RegExp(expectedIssue.message, 'u');
result = result.filter((d) => regex.test(d.message));
} else {
result = result.filter((d) => d.message === expectedIssue.message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,67 +9,67 @@ class MySuperClass {
}

class MyClass1 sub MySuperClass {
// $TEST$ no error "Overriding member does not match the overridden member."
// $TEST$ no error r"Overriding member does not match the overridden member:[\s\S]*"
attr »myInstanceAttribute«: Int
// $TEST$ no error "Overriding member does not match the overridden member."
// $TEST$ no error r"Overriding member does not match the overridden member:[\s\S]*"
static attr »myStaticAttribute«: Int

// $TEST$ no error "Overriding member does not match the overridden member."
// $TEST$ no error r"Overriding member does not match the overridden member:[\s\S]*"
attr »myInstanceAttribute«: String
// $TEST$ no error "Overriding member does not match the overridden member."
// $TEST$ no error r"Overriding member does not match the overridden member:[\s\S]*"
static attr »myStaticAttribute«: String

// $TEST$ no error "Overriding member does not match the overridden member."
// $TEST$ no error r"Overriding member does not match the overridden member:[\s\S]*"
attr »myOwnInstanceAttribute«: Int
// $TEST$ no error "Overriding member does not match the overridden member."
// $TEST$ no error r"Overriding member does not match the overridden member:[\s\S]*"
static attr »myOwnStaticAttribute«: Int


// $TEST$ no error "Overriding member does not match the overridden member."
// $TEST$ no error r"Overriding member does not match the overridden member:[\s\S]*"
fun »myInstanceMethod«(a: Int = 0) -> r: Int
// $TEST$ no error "Overriding member does not match the overridden member."
// $TEST$ no error r"Overriding member does not match the overridden member:[\s\S]*"
static fun »myStaticMethod«(a: Int = 0) -> r: Int

// $TEST$ no error "Overriding member does not match the overridden member."
// $TEST$ no error r"Overriding member does not match the overridden member:[\s\S]*"
fun »myInstanceMethod«() -> r: Int
// $TEST$ no error "Overriding member does not match the overridden member."
// $TEST$ no error r"Overriding member does not match the overridden member:[\s\S]*"
static fun »myStaticMethod«() -> r: Int

// $TEST$ no error "Overriding member does not match the overridden member."
// $TEST$ no error r"Overriding member does not match the overridden member:[\s\S]*"
fun »myOwnInstanceMethod«(a: Int = 0) -> r: Int
// $TEST$ no error "Overriding member does not match the overridden member."
// $TEST$ no error r"Overriding member does not match the overridden member:[\s\S]*"
static fun »myOwnStaticMethod«(a: Int = 0) -> r: Int
}

class MyClass2 sub MySuperClass {
// $TEST$ error "Overriding member does not match the overridden member."
// $TEST$ error r"Overriding member does not match the overridden member:[\s\S]*"
attr »myInstanceAttribute«: String
// $TEST$ no error "Overriding member does not match the overridden member."
// $TEST$ no error r"Overriding member does not match the overridden member:[\s\S]*"
static attr »myStaticAttribute«: String

// $TEST$ no error "Overriding member does not match the overridden member."
// $TEST$ no error r"Overriding member does not match the overridden member:[\s\S]*"
attr »myInstanceAttribute«: Int
// $TEST$ no error "Overriding member does not match the overridden member."
// $TEST$ no error r"Overriding member does not match the overridden member:[\s\S]*"
static attr »myStaticAttribute«: Int

// $TEST$ no error "Overriding member does not match the overridden member."
// $TEST$ no error r"Overriding member does not match the overridden member:[\s\S]*"
attr »myOwnInstanceAttribute«: Int
// $TEST$ no error "Overriding member does not match the overridden member."
// $TEST$ no error r"Overriding member does not match the overridden member:[\s\S]*"
static attr »myOwnStaticAttribute«: Int


// $TEST$ error "Overriding member does not match the overridden member."
// $TEST$ error r"Overriding member does not match the overridden member:[\s\S]*"
fun »myInstanceMethod«() -> r: Int
// $TEST$ no error "Overriding member does not match the overridden member."
// $TEST$ no error r"Overriding member does not match the overridden member:[\s\S]*"
static fun »myStaticMethod«() -> r: Int

// $TEST$ no error "Overriding member does not match the overridden member."
// $TEST$ no error r"Overriding member does not match the overridden member:[\s\S]*"
fun »myInstanceMethod«(a: Int = 0) -> r: Int
// $TEST$ no error "Overriding member does not match the overridden member."
// $TEST$ no error r"Overriding member does not match the overridden member:[\s\S]*"
static fun »myStaticMethod«(a: Int = 0) -> r: Int

// $TEST$ no error "Overriding member does not match the overridden member."
// $TEST$ no error r"Overriding member does not match the overridden member:[\s\S]*"
fun »myOwnInstanceMethod«(a: Int = 0) -> r: Int
// $TEST$ no error "Overriding member does not match the overridden member."
// $TEST$ no error r"Overriding member does not match the overridden member:[\s\S]*"
static fun »myOwnStaticMethod«(a: Int = 0) -> r: Int
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fun myFunction(
p7: MyClassWithTwoTypeParameters»<>«,
// $TEST$ error "The type parameter 'V' must be set here."
p8: MyClassWithTwoTypeParameters»<Int>«,
// $TEST$ no error r"The type parameters? .* must be set here\."
// $TEST$ error "The type parameters 'K', 'V' must be set here."
p9: MyClassWithTwoTypeParameters»<T = Int>«,


Expand All @@ -52,7 +52,7 @@ fun myFunction(
q7: MyEnum.MyEnumVariantWithTwoTypeParameters»<>«,
// $TEST$ error "The type parameter 'V' must be set here."
q8: MyEnum.MyEnumVariantWithTwoTypeParameters»<Int>«,
// $TEST$ no error r"The type parameters? .* must be set here\."
// $TEST$ error "The type parameters 'K', 'V' must be set here."
q9: MyEnum.MyEnumVariantWithTwoTypeParameters»<T = Int>«,


Expand Down

0 comments on commit 869dfb0

Please sign in to comment.