|
| 1 | +import { findLocalReferences, getContainerOfType, hasContainerOfType, ValidationAcceptor } from 'langium'; |
1 | 2 | import {
|
2 | 3 | isSdsCallable,
|
3 | 4 | isSdsClass,
|
| 5 | + isSdsDeclaration, |
| 6 | + isSdsNamedTypeDeclaration, |
4 | 7 | isSdsParameterList,
|
5 | 8 | isSdsUnionType,
|
6 | 9 | SdsTypeParameter,
|
7 | 10 | } from '../../../generated/ast.js';
|
8 |
| -import { findLocalReferences, getContainerOfType, hasContainerOfType, ValidationAcceptor } from 'langium'; |
9 | 11 |
|
10 | 12 | export const CODE_TYPE_PARAMETER_INSUFFICIENT_CONTEXT = 'type-parameter/insufficient-context';
|
| 13 | +export const CODE_TYPE_PARAMETER_USAGE = 'type-parameter/usage'; |
11 | 14 |
|
12 | 15 | export const typeParameterMustHaveSufficientContext = (node: SdsTypeParameter, accept: ValidationAcceptor) => {
|
13 | 16 | const containingCallable = getContainerOfType(node, isSdsCallable);
|
@@ -45,3 +48,29 @@ export const typeParameterMustHaveSufficientContext = (node: SdsTypeParameter, a
|
45 | 48 | });
|
46 | 49 | }
|
47 | 50 | };
|
| 51 | + |
| 52 | +export const typeParameterMustNotBeUsedInNestedNamedTypeDeclarations = ( |
| 53 | + node: SdsTypeParameter, |
| 54 | + accept: ValidationAcceptor, |
| 55 | +) => { |
| 56 | + // Only classes can have nested named type declarations |
| 57 | + const declarationWithTypeParameter = getContainerOfType(node.$container, isSdsDeclaration); |
| 58 | + if (!isSdsClass(declarationWithTypeParameter)) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + findLocalReferences(node).forEach((it) => { |
| 63 | + const reference = it.$refNode?.astNode; |
| 64 | + const containingNamedTypeDeclaration = getContainerOfType(reference, isSdsNamedTypeDeclaration); |
| 65 | + if ( |
| 66 | + reference && |
| 67 | + containingNamedTypeDeclaration && |
| 68 | + containingNamedTypeDeclaration !== declarationWithTypeParameter |
| 69 | + ) { |
| 70 | + accept('error', 'Type parameters cannot be used in nested named type declarations.', { |
| 71 | + node: reference, |
| 72 | + code: CODE_TYPE_PARAMETER_USAGE, |
| 73 | + }); |
| 74 | + } |
| 75 | + }); |
| 76 | +}; |
0 commit comments