-
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: optional parameters must be passed by name
- Loading branch information
1 parent
790fc17
commit 297a0b9
Showing
6 changed files
with
76 additions
and
11 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
25 changes: 25 additions & 0 deletions
25
packages/safe-ds-lang/src/language/validation/other/expressions/arguments.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,25 @@ | ||
import { SafeDsServices } from '../../../safe-ds-module.js'; | ||
import type { SdsArgument } from '../../../generated/ast.js'; | ||
import { ValidationAcceptor } from 'langium'; | ||
import { Argument, Parameter } from '../../../helpers/nodeProperties.js'; | ||
|
||
export const CODE_ARGUMENT_POSITIONAL = 'argument/positional'; | ||
|
||
export const argumentMustBeNamedIfParameterIsOptional = (services: SafeDsServices) => { | ||
const nodeMapper = services.helpers.NodeMapper; | ||
|
||
return (node: SdsArgument, accept: ValidationAcceptor) => { | ||
const parameter = nodeMapper.argumentToParameter(node); | ||
if (!Parameter.isOptional(parameter)) { | ||
return; | ||
} | ||
|
||
if (!Argument.isNamed(node)) { | ||
accept('error', 'Argument must be named if the parameter is optional.', { | ||
node, | ||
property: 'value', | ||
code: CODE_ARGUMENT_POSITIONAL, | ||
}); | ||
} | ||
}; | ||
}; |
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
19 changes: 19 additions & 0 deletions
19
...ther/expressions/arguments/must be named if parameter is optional/annotation calls.sdsdev
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,19 @@ | ||
package tests.validation.other.expressions.arguments.mustBeNamedIfParameterIsOptional | ||
|
||
@Repeatable | ||
annotation MyAnnotation(required: Int, optional: Int = 0) | ||
|
||
// $TEST$ no error "Argument must be named if the parameter is optional." | ||
// $TEST$ error "Argument must be named if the parameter is optional." | ||
@MyAnnotation(»1«, »2«) | ||
// $TEST$ no error "Argument must be named if the parameter is optional." | ||
// $TEST$ no error "Argument must be named if the parameter is optional." | ||
@MyAnnotation(»required = 1«, »optional = 2«) | ||
|
||
// $TEST$ no error "Argument must be named if the parameter is optional." | ||
@Unresolved(»1«) | ||
// $TEST$ no error "Argument must be named if the parameter is optional." | ||
@MyAnnotation(1, 2, »3«) | ||
// $TEST$ no error "Argument must be named if the parameter is optional." | ||
@MyAnnotation(unresolved = »1«) | ||
pipeline testPipeline {} |
19 changes: 19 additions & 0 deletions
19
...alidation/other/expressions/arguments/must be named if parameter is optional/calls.sdsdev
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,19 @@ | ||
package tests.validation.other.expressions.arguments.mustBeNamedIfParameterIsOptional | ||
|
||
@Pure fun myFunction(required: Int, optional: Int = 0) | ||
|
||
pipeline testPipeline { | ||
// $TEST$ no error "Argument must be named if the parameter is optional." | ||
// $TEST$ error "Argument must be named if the parameter is optional." | ||
myFunction(»1«, »2«); | ||
// $TEST$ no error "Argument must be named if the parameter is optional." | ||
// $TEST$ no error "Argument must be named if the parameter is optional." | ||
myFunction(»required = 1«, »optional = 2«); | ||
|
||
// $TEST$ no error "Argument must be named if the parameter is optional." | ||
unresolved(»1«); | ||
// $TEST$ no error "Argument must be named if the parameter is optional." | ||
myFunction(1, 2, »3«); | ||
// $TEST$ no error "Argument must be named if the parameter is optional." | ||
myFunction(unresolved = »1«); | ||
} |