Skip to content

Commit

Permalink
feat: error if wildcard import has alias (#574)
Browse files Browse the repository at this point in the history
Closes partially #543

### Summary of Changes

Show an error if a wildcard import has an alias.

---------

Co-authored-by: megalinter-bot <[email protected]>
  • Loading branch information
lars-reimann and megalinter-bot authored Sep 21, 2023
1 parent bd73bc5 commit 4ba7873
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/language/helpers/astShortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import {
isSdsDeclaration,
SdsAnnotatedObject,
SdsAnnotationCall,
SdsImport,
SdsLiteral,
SdsLiteralType, SdsResult, SdsResultList,
SdsLiteralType,
SdsResult,
SdsResultList,
SdsTypeArgument,
SdsTypeArgumentList,
} from '../generated/ast.js';
Expand All @@ -23,8 +26,13 @@ export const literalsOrEmpty = function (node: SdsLiteralType | undefined): SdsL

export const resultsOrEmpty = function (node: SdsResultList | undefined): SdsResult[] {
return node?.results ?? [];
}
};

export const typeArgumentsOrEmpty = function (node: SdsTypeArgumentList | undefined): SdsTypeArgument[] {
return node?.typeArguments ?? [];
};

export const isWildcardImport = function (node: SdsImport): boolean {
const importedNamespace = node.importedNamespace ?? '';
return importedNamespace.endsWith('*');
};
16 changes: 16 additions & 0 deletions src/language/validation/imports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ValidationAcceptor } from 'langium';
import { SdsImportAlias } from '../generated/ast.js';
import { isWildcardImport } from '../helpers/astShortcuts.js';

export const CODE_IMPORT_WILDCARD_IMPORT_WITH_ALIAS = 'import/wildcard-import-with-alias';

export const importAliasMustNotBeUsedForWildcardImports = (node: SdsImportAlias, accept: ValidationAcceptor): void => {
const importNode = node.$container;

if (importNode && isWildcardImport(importNode)) {
accept('error', 'A wildcard import must not have an alias.', {
node,
code: CODE_IMPORT_WILDCARD_IMPORT_WITH_ALIAS,
});
}
};
2 changes: 2 additions & 0 deletions src/language/validation/safe-ds-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
parameterListMustNotHaveRequiredParametersAfterOptionalParameters,
parameterListVariadicParameterMustBeLast,
} from './other/declarations/parameterLists.js';
import { importAliasMustNotBeUsedForWildcardImports } from './imports.js';

/**
* Register custom validation checks.
Expand All @@ -41,6 +42,7 @@ export const registerValidationChecks = function (services: SafeDsServices) {
SdsEnumBody: [enumBodyShouldNotBeEmpty],
SdsEnumVariant: [enumVariantParameterListShouldNotBeEmpty],
SdsFunction: [functionResultListShouldNotBeEmpty],
SdsImportAlias: [importAliasMustNotBeUsedForWildcardImports],
SdsModule: [moduleDeclarationsMustMatchFileKind, moduleWithDeclarationsMustStatePackage],
SdsParameter: [parameterMustHaveTypeHint],
SdsParameterList: [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package tests.validation.other.modules.wildcardImportMustNotHaveAlias

// $TEST$ error "A wildcard import must not have an alias."
import stuff.* »as S«
// $TEST$ no error "A wildcard import must not have an alias."
import stuff.Stuff »as S«

0 comments on commit 4ba7873

Please sign in to comment.