Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: error if wildcard import has alias #574

Merged
merged 2 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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«