From 999feaa8352a0cae147b924c0371cb84031ddb51 Mon Sep 17 00:00:00 2001 From: Lars Reimann Date: Thu, 21 Sep 2023 14:06:42 +0200 Subject: [PATCH 1/2] feat: error if wildcard import has alias --- src/language/helpers/astShortcuts.ts | 7 ++++++- src/language/validation/imports.ts | 16 ++++++++++++++++ src/language/validation/safe-ds-validator.ts | 2 ++ .../main.sdstest | 6 ++++++ 4 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 src/language/validation/imports.ts create mode 100644 tests/resources/validation/other/imports/wildcard import must not have alias/main.sdstest diff --git a/src/language/helpers/astShortcuts.ts b/src/language/helpers/astShortcuts.ts index 3fbbe3bba..08f608dfc 100644 --- a/src/language/helpers/astShortcuts.ts +++ b/src/language/helpers/astShortcuts.ts @@ -2,7 +2,7 @@ import { isSdsDeclaration, SdsAnnotatedObject, - SdsAnnotationCall, + SdsAnnotationCall, SdsImport, SdsLiteral, SdsLiteralType, SdsResult, SdsResultList, SdsTypeArgument, @@ -28,3 +28,8 @@ export const resultsOrEmpty = function (node: SdsResultList | undefined): SdsRes 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('*') +} diff --git a/src/language/validation/imports.ts b/src/language/validation/imports.ts new file mode 100644 index 000000000..f96ab2792 --- /dev/null +++ b/src/language/validation/imports.ts @@ -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, + }); + } +}; diff --git a/src/language/validation/safe-ds-validator.ts b/src/language/validation/safe-ds-validator.ts index 9241c8656..ce7d9016c 100644 --- a/src/language/validation/safe-ds-validator.ts +++ b/src/language/validation/safe-ds-validator.ts @@ -24,6 +24,7 @@ import { parameterListMustNotHaveRequiredParametersAfterOptionalParameters, parameterListVariadicParameterMustBeLast, } from './other/declarations/parameterLists.js'; +import { importAliasMustNotBeUsedForWildcardImports } from './imports.js'; /** * Register custom validation checks. @@ -41,6 +42,7 @@ export const registerValidationChecks = function (services: SafeDsServices) { SdsEnumBody: [enumBodyShouldNotBeEmpty], SdsEnumVariant: [enumVariantParameterListShouldNotBeEmpty], SdsFunction: [functionResultListShouldNotBeEmpty], + SdsImportAlias: [importAliasMustNotBeUsedForWildcardImports], SdsModule: [moduleDeclarationsMustMatchFileKind, moduleWithDeclarationsMustStatePackage], SdsParameter: [parameterMustHaveTypeHint], SdsParameterList: [ diff --git a/tests/resources/validation/other/imports/wildcard import must not have alias/main.sdstest b/tests/resources/validation/other/imports/wildcard import must not have alias/main.sdstest new file mode 100644 index 000000000..98ffbd81e --- /dev/null +++ b/tests/resources/validation/other/imports/wildcard import must not have alias/main.sdstest @@ -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« From 5c7643692f806f069ae79760108c2412cc3d2b75 Mon Sep 17 00:00:00 2001 From: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com> Date: Thu, 21 Sep 2023 12:09:26 +0000 Subject: [PATCH 2/2] style: apply automated linter fixes --- src/language/helpers/astShortcuts.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/language/helpers/astShortcuts.ts b/src/language/helpers/astShortcuts.ts index 08f608dfc..fe119d3c4 100644 --- a/src/language/helpers/astShortcuts.ts +++ b/src/language/helpers/astShortcuts.ts @@ -2,9 +2,12 @@ import { isSdsDeclaration, SdsAnnotatedObject, - SdsAnnotationCall, SdsImport, + SdsAnnotationCall, + SdsImport, SdsLiteral, - SdsLiteralType, SdsResult, SdsResultList, + SdsLiteralType, + SdsResult, + SdsResultList, SdsTypeArgument, SdsTypeArgumentList, } from '../generated/ast.js'; @@ -23,7 +26,7 @@ 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 ?? []; @@ -31,5 +34,5 @@ export const typeArgumentsOrEmpty = function (node: SdsTypeArgumentList | undefi export const isWildcardImport = function (node: SdsImport): boolean { const importedNamespace = node.importedNamespace ?? ''; - return importedNamespace.endsWith('*') -} + return importedNamespace.endsWith('*'); +};