Skip to content

Commit 4ba7873

Browse files
feat: error if wildcard import has alias (#574)
Closes partially #543 ### Summary of Changes Show an error if a wildcard import has an alias. --------- Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
1 parent bd73bc5 commit 4ba7873

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

src/language/helpers/astShortcuts.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ import {
33
isSdsDeclaration,
44
SdsAnnotatedObject,
55
SdsAnnotationCall,
6+
SdsImport,
67
SdsLiteral,
7-
SdsLiteralType, SdsResult, SdsResultList,
8+
SdsLiteralType,
9+
SdsResult,
10+
SdsResultList,
811
SdsTypeArgument,
912
SdsTypeArgumentList,
1013
} from '../generated/ast.js';
@@ -23,8 +26,13 @@ export const literalsOrEmpty = function (node: SdsLiteralType | undefined): SdsL
2326

2427
export const resultsOrEmpty = function (node: SdsResultList | undefined): SdsResult[] {
2528
return node?.results ?? [];
26-
}
29+
};
2730

2831
export const typeArgumentsOrEmpty = function (node: SdsTypeArgumentList | undefined): SdsTypeArgument[] {
2932
return node?.typeArguments ?? [];
3033
};
34+
35+
export const isWildcardImport = function (node: SdsImport): boolean {
36+
const importedNamespace = node.importedNamespace ?? '';
37+
return importedNamespace.endsWith('*');
38+
};

src/language/validation/imports.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { ValidationAcceptor } from 'langium';
2+
import { SdsImportAlias } from '../generated/ast.js';
3+
import { isWildcardImport } from '../helpers/astShortcuts.js';
4+
5+
export const CODE_IMPORT_WILDCARD_IMPORT_WITH_ALIAS = 'import/wildcard-import-with-alias';
6+
7+
export const importAliasMustNotBeUsedForWildcardImports = (node: SdsImportAlias, accept: ValidationAcceptor): void => {
8+
const importNode = node.$container;
9+
10+
if (importNode && isWildcardImport(importNode)) {
11+
accept('error', 'A wildcard import must not have an alias.', {
12+
node,
13+
code: CODE_IMPORT_WILDCARD_IMPORT_WITH_ALIAS,
14+
});
15+
}
16+
};

src/language/validation/safe-ds-validator.ts

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
parameterListMustNotHaveRequiredParametersAfterOptionalParameters,
2525
parameterListVariadicParameterMustBeLast,
2626
} from './other/declarations/parameterLists.js';
27+
import { importAliasMustNotBeUsedForWildcardImports } from './imports.js';
2728

2829
/**
2930
* Register custom validation checks.
@@ -41,6 +42,7 @@ export const registerValidationChecks = function (services: SafeDsServices) {
4142
SdsEnumBody: [enumBodyShouldNotBeEmpty],
4243
SdsEnumVariant: [enumVariantParameterListShouldNotBeEmpty],
4344
SdsFunction: [functionResultListShouldNotBeEmpty],
45+
SdsImportAlias: [importAliasMustNotBeUsedForWildcardImports],
4446
SdsModule: [moduleDeclarationsMustMatchFileKind, moduleWithDeclarationsMustStatePackage],
4547
SdsParameter: [parameterMustHaveTypeHint],
4648
SdsParameterList: [
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package tests.validation.other.modules.wildcardImportMustNotHaveAlias
2+
3+
// $TEST$ error "A wildcard import must not have an alias."
4+
import stuff.* »as S«
5+
// $TEST$ no error "A wildcard import must not have an alias."
6+
import stuff.Stuff »as S«

0 commit comments

Comments
 (0)