-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve error messages from linker (#1272)
Closes #1268 ### Summary of Changes * Improve default error message of the linker. * Add a default resolution to the message that might solve the issue (check spelling and imports). * Add special resolutions, if users likely used Python keywords instead of Safe-DS keywords by accident (e.g. `True` instead of `true`).
- Loading branch information
1 parent
9b5e488
commit eddd868
Showing
5 changed files
with
65 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
packages/safe-ds-lang/src/language/scoping/safe-ds-linker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { AstNodeDescription, DefaultLinker, isLinkingError, LinkingError, ReferenceInfo } from 'langium'; | ||
import { isSdsMemberAccess, isSdsReference } from '../generated/ast.js'; | ||
|
||
export class SafeDsLinker extends DefaultLinker { | ||
override getCandidate(refInfo: ReferenceInfo): AstNodeDescription | LinkingError { | ||
const superResult = super.getCandidate(refInfo); | ||
|
||
if (!isLinkingError(superResult)) { | ||
return superResult; | ||
} | ||
|
||
const node = refInfo.container; | ||
|
||
// Create a default error message | ||
const message = `Could not find a declaration named '${refInfo.reference.$refText}' in this context.`; | ||
let resolution = 'Did you spell the name correctly and add all needed imports?'; | ||
|
||
// Improve the error message if Python keywords False, True, or None are used as references | ||
if (isSdsReference(node) && refInfo.property === 'target' && !isSdsMemberAccess(node.$container)) { | ||
if (refInfo.reference.$refText === 'False') { | ||
resolution = "Did you mean to write 'false'?"; | ||
} else if (refInfo.reference.$refText === 'True') { | ||
resolution = "Did you mean to write 'true'?"; | ||
} else if (refInfo.reference.$refText === 'None') { | ||
resolution = "Did you mean to write 'null'?"; | ||
} | ||
} | ||
|
||
return { | ||
...superResult, | ||
message: `${message}\n${resolution}`, | ||
}; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
packages/safe-ds-lang/tests/resources/validation/linking/default error/main.sds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package tests.validation.linking.defaultError | ||
|
||
pipeline test { | ||
// $TEST$ error r"Could not find a declaration named 'Unknown' in this context\.\nDid you spell the name correctly and add all needed imports\?" | ||
»Unknown«; | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/safe-ds-lang/tests/resources/validation/linking/usage of python keywords/main.sds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package tests.validation.linking.usageOfPythonKeywords | ||
|
||
pipeline test { | ||
// $TEST$ error r"Did you mean to write 'false'\?" | ||
out »False«; | ||
|
||
// $TEST$ error r"Did you mean to write 'true'\?" | ||
out »True«; | ||
|
||
// $TEST$ error r"Did you mean to write 'null'\?" | ||
out »None«; | ||
|
||
// $TEST$ no error r"Did you mean to write 'false'\?" | ||
out a.»False«; | ||
|
||
// $TEST$ no error r"Did you mean to write 'true'\?" | ||
out a.»True«; | ||
|
||
// $TEST$ no error r"Did you mean to write 'null'\?" | ||
out a».None«; | ||
} |