-
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: port additional validation checks to `Langium (#576)
Closes partially #543 ### Summary of Changes Show an error if: * A union type has no type arguments * A callable type has optional parameters * Positional type arguments occur after named type arguments * Positional arguments occur after named arguments * A parameter is variadic and optional --------- Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
- Loading branch information
1 parent
47ce782
commit 8f5d57a
Showing
22 changed files
with
297 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { SdsArgumentList } from '../../generated/ast.js'; | ||
import { ValidationAcceptor } from 'langium'; | ||
|
||
export const CODE_ARGUMENT_LIST_POSITIONAL_AFTER_NAMED = 'argument-list/positional-after-named'; | ||
|
||
export const argumentListMustNotHavePositionalArgumentsAfterNamedArguments = ( | ||
node: SdsArgumentList, | ||
accept: ValidationAcceptor, | ||
): void => { | ||
let foundNamed = false; | ||
for (const argument of node.arguments) { | ||
if (argument.parameter) { | ||
foundNamed = true; | ||
} else if (foundNamed) { | ||
accept('error', 'After the first named argument all arguments must be named.', { | ||
node: argument, | ||
code: CODE_ARGUMENT_LIST_POSITIONAL_AFTER_NAMED, | ||
}); | ||
} | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { SdsParameter } from '../../../generated/ast.js'; | ||
import { ValidationAcceptor } from 'langium'; | ||
|
||
export const CODE_PARAMETER_VARIADIC_AND_OPTIONAL = 'parameter/variadic-and-optional'; | ||
|
||
export const parameterMustNotBeVariadicAndOptional = (node: SdsParameter, accept: ValidationAcceptor) => { | ||
if (node.variadic && node.defaultValue) { | ||
accept('error', 'Variadic parameters must not be optional.', { | ||
node, | ||
property: 'name', | ||
code: CODE_PARAMETER_VARIADIC_AND_OPTIONAL, | ||
}); | ||
} | ||
}; |
4 changes: 2 additions & 2 deletions
4
src/language/validation/imports.ts → src/language/validation/other/imports.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
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,17 @@ | ||
import { SdsCallableType } from '../../../generated/ast.js'; | ||
import { ValidationAcceptor } from 'langium'; | ||
import { parametersOrEmpty } from '../../../ast/shortcuts.js'; | ||
|
||
export const CODE_CALLABLE_TYPE_NO_OPTIONAL_PARAMETERS = 'callable-type/no-optional-parameters'; | ||
|
||
export const callableTypeMustNotHaveOptionalParameters = (node: SdsCallableType, accept: ValidationAcceptor): void => { | ||
for (const parameter of parametersOrEmpty(node.parameterList)) { | ||
if (parameter.defaultValue && !parameter.variadic) { | ||
accept('error', 'A callable type must not have optional parameters.', { | ||
node: parameter, | ||
property: 'defaultValue', | ||
code: CODE_CALLABLE_TYPE_NO_OPTIONAL_PARAMETERS, | ||
}); | ||
} | ||
} | ||
}; |
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 @@ | ||
import { SdsTypeArgumentList } from '../../../generated/ast.js'; | ||
import { ValidationAcceptor } from 'langium'; | ||
|
||
export const CODE_TYPE_ARGUMENT_LIST_POSITIONAL_AFTER_NAMED = 'type-argument-list/positional-after-named'; | ||
|
||
export const typeArgumentListMustNotHavePositionalArgumentsAfterNamedArguments = ( | ||
node: SdsTypeArgumentList, | ||
accept: ValidationAcceptor, | ||
): void => { | ||
let foundNamed = false; | ||
for (const typeArgument of node.typeArguments) { | ||
if (typeArgument.typeParameter) { | ||
foundNamed = true; | ||
} else if (foundNamed) { | ||
accept('error', 'After the first named type argument all type arguments must be named.', { | ||
node: typeArgument, | ||
code: CODE_TYPE_ARGUMENT_LIST_POSITIONAL_AFTER_NAMED, | ||
}); | ||
} | ||
} | ||
}; |
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,16 @@ | ||
import { SdsUnionType } from '../../../generated/ast.js'; | ||
import { ValidationAcceptor } from 'langium'; | ||
import { typeArgumentsOrEmpty } from '../../../ast/shortcuts.js'; | ||
import { isEmpty } from 'radash'; | ||
|
||
export const CODE_UNION_TYPE_MISSING_TYPE_ARGUMENTS = 'union-type/missing-type-arguments'; | ||
|
||
export const unionTypeMustHaveTypeArguments = (node: SdsUnionType, accept: ValidationAcceptor): void => { | ||
if (isEmpty(typeArgumentsOrEmpty(node.typeArgumentList))) { | ||
accept('error', 'A union type must have least one type argument.', { | ||
node, | ||
property: 'typeArgumentList', | ||
code: CODE_UNION_TYPE_MISSING_TYPE_ARGUMENTS, | ||
}); | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// TODO: implement value-related checks here, including: | ||
// - division by zero | ||
// - must be constant |
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
5 changes: 5 additions & 0 deletions
5
tests/resources/grammar/expressions/calls/good-named type argument with test markers.sdstest
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,5 @@ | ||
// $TEST$ no_syntax_error | ||
|
||
pipeline myPipeline { | ||
f<»T = Int«>(); | ||
} |
5 changes: 5 additions & 0 deletions
5
...ources/grammar/expressions/calls/good-positional type argument with tests markers.sdstest
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,5 @@ | ||
// $TEST$ no_syntax_error | ||
|
||
pipeline myPipeline { | ||
f<»Int«>(); | ||
} |
63 changes: 63 additions & 0 deletions
63
.../other/argument lists/must not have positional argument after named argument/main.sdstest
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,63 @@ | ||
package tests.validation.other.argumentLists.mustNotHavePositionalArgumentAfterNamedArgument | ||
|
||
annotation MyAnnotation(a: Int, b: Int = 0, c: Int = 0, d: Int = 0, vararg e: Int) | ||
|
||
// $TEST$ no error "After the first named argument all arguments must be named." | ||
// $TEST$ no error "After the first named argument all arguments must be named." | ||
// $TEST$ error "After the first named argument all arguments must be named." | ||
// $TEST$ no error "After the first named argument all arguments must be named." | ||
// $TEST$ error "After the first named argument all arguments must be named." | ||
@MyAnnotation(»0«, »a = 1«, »2«, »b = 3«, »4«) | ||
class MyClass1 | ||
|
||
// $TEST$ no error "After the first named argument all arguments must be named." | ||
@MyAnnotation(»0«) | ||
class MyClass2 | ||
|
||
// $TEST$ no error "After the first named argument all arguments must be named." | ||
@MyAnnotation(»a = 0«) | ||
class MyClass3 | ||
|
||
// $TEST$ no error "After the first named argument all arguments must be named." | ||
// $TEST$ no error "After the first named argument all arguments must be named." | ||
// $TEST$ error "After the first named argument all arguments must be named." | ||
// $TEST$ no error "After the first named argument all arguments must be named." | ||
@UnresolvedAnnotation(»0«, »a = 1«, »2«, »b = 3«) | ||
class MyClass4 | ||
|
||
// $TEST$ no error "After the first named argument all arguments must be named." | ||
@UnresolvedAnnotation(»0«) | ||
class MyClass5 | ||
|
||
// $TEST$ no error "After the first named argument all arguments must be named." | ||
@UnresolvedAnnotation(»a = 0«) | ||
class MyClass3 | ||
|
||
fun f(a: Int, b: Int = 0, c: Int = 0, d: Int = 0, vararg e: Int) | ||
|
||
pipeline myPipeline { | ||
// $TEST$ no error "After the first named argument all arguments must be named." | ||
// $TEST$ no error "After the first named argument all arguments must be named." | ||
// $TEST$ error "After the first named argument all arguments must be named." | ||
// $TEST$ no error "After the first named argument all arguments must be named." | ||
// $TEST$ error "After the first named argument all arguments must be named." | ||
f(»0«, »a = 1«, »2«, »b = 3«, »4«); | ||
|
||
// $TEST$ no error "After the first named argument all arguments must be named." | ||
f(»0«); | ||
|
||
// $TEST$ no error "After the first named argument all arguments must be named." | ||
f(»a = 0«); | ||
|
||
// $TEST$ no error "After the first named argument all arguments must be named." | ||
// $TEST$ no error "After the first named argument all arguments must be named." | ||
// $TEST$ error "After the first named argument all arguments must be named." | ||
// $TEST$ no error "After the first named argument all arguments must be named." | ||
unresolvedCallable(»0«, »a = 1«, »2«, »b = 3«); | ||
|
||
// $TEST$ no error "After the first named argument all arguments must be named." | ||
unresolvedCallable(»0«); | ||
|
||
// $TEST$ no error "After the first named argument all arguments must be named." | ||
unresolvedCallable(»a = 0«); | ||
} |
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
12 changes: 12 additions & 0 deletions
12
...ources/validation/other/declarations/parameter/variadic must not be optional/main.sdstest
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,12 @@ | ||
package tests.validation.declarations.parameters.variadicMustNotBeOptional | ||
|
||
fun f( | ||
// $TEST$ no error "Variadic parameters must not be optional." | ||
»a«: Int, | ||
// $TEST$ no error "Variadic parameters must not be optional." | ||
»b«: Int = 1, | ||
// $TEST$ no error "Variadic parameters must not be optional." | ||
vararg »c«: Int, | ||
// $TEST$ error "Variadic parameters must not be optional." | ||
vararg »d«: Int = 2 | ||
) |
8 changes: 8 additions & 0 deletions
8
.../validation/other/types/callable types/must not have optional parameters/optional.sdstest
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,8 @@ | ||
package tests.validation.types.callableTypes.mustNotHaveOptionalParameters | ||
|
||
fun f( | ||
g: ( | ||
// $TEST$ error "A callable type must not have optional parameters." | ||
p: Int = »1«, | ||
) -> () | ||
) |
8 changes: 8 additions & 0 deletions
8
.../validation/other/types/callable types/must not have optional parameters/required.sdstest
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,8 @@ | ||
package tests.validation.types.callableTypes.mustNotHaveOptionalParameters | ||
|
||
fun f( | ||
g: ( | ||
// $TEST$ no error "A callable type must not have optional parameters." | ||
p: Int, | ||
) -> () | ||
) |
8 changes: 8 additions & 0 deletions
8
...on/other/types/callable types/must not have optional parameters/variadic optional.sdstest
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,8 @@ | ||
package tests.validation.types.callableTypes.mustNotHaveOptionalParameters | ||
|
||
fun f( | ||
g: ( | ||
// $TEST$ no error "A callable type must not have optional parameters." | ||
vararg p: Int = 1, | ||
) -> () | ||
) |
Oops, something went wrong.