diff --git a/src/language/validation/other/modules.ts b/src/language/validation/other/modules.ts index 2dcc94590..6de62f1e3 100644 --- a/src/language/validation/other/modules.ts +++ b/src/language/validation/other/modules.ts @@ -1,23 +1,12 @@ import { ValidationAcceptor } from 'langium'; import { isSdsDeclaration, isSdsPipeline, isSdsSegment, SdsDeclaration, SdsModule } from '../../generated/ast.js'; import { isInPipelineFile, isInStubFile } from '../../helpers/fileExtensions.js'; +import { getModuleMembers } from '../../helpers/nodeProperties.js'; -export const CODE_MODULE_MISSING_PACKAGE = 'module/missing-package'; export const CODE_MODULE_FORBIDDEN_IN_PIPELINE_FILE = 'module/forbidden-in-pipeline-file'; export const CODE_MODULE_FORBIDDEN_IN_STUB_FILE = 'module/forbidden-in-stub-file'; - -export const moduleWithDeclarationsMustStatePackage = (node: SdsModule, accept: ValidationAcceptor): void => { - if (!node.name) { - const declarations = node.members.filter(isSdsDeclaration); - if (declarations.length > 0) { - accept('error', 'A module with declarations must state its package.', { - node: declarations[0], - property: 'name', - code: CODE_MODULE_MISSING_PACKAGE, - }); - } - } -}; +export const CODE_MODULE_MISSING_PACKAGE = 'module/missing-package'; +export const CODE_MODULE_PIPELINE_FILE_IN_SAFEDS_PACKAGE = 'module/pipeline-file-in-safeds-package'; export const moduleDeclarationsMustMatchFileKind = (node: SdsModule, accept: ValidationAcceptor): void => { const declarations = node.members.filter(isSdsDeclaration); @@ -52,3 +41,26 @@ export const declarationIsAllowedInPipelineFile = (declaration: SdsDeclaration): export const declarationIsAllowedInStubFile = (declaration: SdsDeclaration): boolean => { return !isSdsPipeline(declaration) && !isSdsSegment(declaration); }; + +export const moduleWithDeclarationsMustStatePackage = (node: SdsModule, accept: ValidationAcceptor): void => { + if (!node.name) { + const members = getModuleMembers(node); + if (members.length > 0) { + accept('error', 'A module with declarations must state its package.', { + node: members[0], + property: 'name', + code: CODE_MODULE_MISSING_PACKAGE, + }); + } + } +}; + +export const pipelineFileMustNotBeInSafedsPackage = (node: SdsModule, accept: ValidationAcceptor): void => { + if (isInPipelineFile(node) && node.name?.startsWith('safeds')) { + accept('error', "A pipeline file must not be in a 'safeds' package.", { + node, + property: 'name', + code: CODE_MODULE_PIPELINE_FILE_IN_SAFEDS_PACKAGE, + }); + } +}; diff --git a/src/language/validation/safe-ds-validator.ts b/src/language/validation/safe-ds-validator.ts index a9ef8c3cc..bf67e2ee4 100644 --- a/src/language/validation/safe-ds-validator.ts +++ b/src/language/validation/safe-ds-validator.ts @@ -50,7 +50,11 @@ import { parameterMustHaveTypeHint, resultMustHaveTypeHint, } from './types.js'; -import { moduleDeclarationsMustMatchFileKind, moduleWithDeclarationsMustStatePackage } from './other/modules.js'; +import { + moduleDeclarationsMustMatchFileKind, + moduleWithDeclarationsMustStatePackage, + pipelineFileMustNotBeInSafedsPackage, +} from './other/modules.js'; import { typeParameterConstraintLeftOperandMustBeOwnTypeParameter } from './other/declarations/typeParameterConstraints.js'; import { parameterListMustNotHaveRequiredParametersAfterOptionalParameters } from './other/declarations/parameterLists.js'; import { unionTypeMustHaveTypes, unionTypeShouldNotHaveDuplicateTypes } from './other/types/unionTypes.js'; @@ -228,6 +232,7 @@ export const registerValidationChecks = function (services: SafeDsServices) { moduleMemberMustHaveNameThatIsUniqueInPackage(services), moduleMustContainUniqueNames, moduleWithDeclarationsMustStatePackage, + pipelineFileMustNotBeInSafedsPackage, pythonModuleShouldDifferFromSafeDsPackage(services), ], SdsNamedType: [ diff --git a/tests/resources/validation/other/modules/pipeline files must not be in safeds package/pipe elsewhere.sdspipe b/tests/resources/validation/other/modules/pipeline files must not be in safeds package/pipe elsewhere.sdspipe new file mode 100644 index 000000000..f193b218b --- /dev/null +++ b/tests/resources/validation/other/modules/pipeline files must not be in safeds package/pipe elsewhere.sdspipe @@ -0,0 +1,3 @@ +// $TEST$ no error "A pipeline file must not be in a 'safeds' package." + +package »tests.validation.other.modules.pipelineFilesMustNotBeInSafedsPackage« diff --git a/tests/resources/validation/other/modules/pipeline files must not be in safeds package/pipe in safeds.sdspipe b/tests/resources/validation/other/modules/pipeline files must not be in safeds package/pipe in safeds.sdspipe new file mode 100644 index 000000000..01bbbb021 --- /dev/null +++ b/tests/resources/validation/other/modules/pipeline files must not be in safeds package/pipe in safeds.sdspipe @@ -0,0 +1,3 @@ +// $TEST$ error "A pipeline file must not be in a 'safeds' package." + +package »safeds« diff --git a/tests/resources/validation/other/modules/pipeline files must not be in safeds package/pipe in subpackage of safeds.sdspipe b/tests/resources/validation/other/modules/pipeline files must not be in safeds package/pipe in subpackage of safeds.sdspipe new file mode 100644 index 000000000..84d64908d --- /dev/null +++ b/tests/resources/validation/other/modules/pipeline files must not be in safeds package/pipe in subpackage of safeds.sdspipe @@ -0,0 +1,3 @@ +// $TEST$ error "A pipeline file must not be in a 'safeds' package." + +package »safeds.validation.other.modules.pipelineFilesMustNotBeInSafedsPackage« diff --git a/tests/resources/validation/other/modules/pipeline files must not be in safeds package/stub elsewhere.sdsstub b/tests/resources/validation/other/modules/pipeline files must not be in safeds package/stub elsewhere.sdsstub new file mode 100644 index 000000000..f193b218b --- /dev/null +++ b/tests/resources/validation/other/modules/pipeline files must not be in safeds package/stub elsewhere.sdsstub @@ -0,0 +1,3 @@ +// $TEST$ no error "A pipeline file must not be in a 'safeds' package." + +package »tests.validation.other.modules.pipelineFilesMustNotBeInSafedsPackage« diff --git a/tests/resources/validation/other/modules/pipeline files must not be in safeds package/stub in safeds.sdsstub b/tests/resources/validation/other/modules/pipeline files must not be in safeds package/stub in safeds.sdsstub new file mode 100644 index 000000000..07eecbb35 --- /dev/null +++ b/tests/resources/validation/other/modules/pipeline files must not be in safeds package/stub in safeds.sdsstub @@ -0,0 +1,3 @@ +// $TEST$ no error "A pipeline file must not be in a 'safeds' package." + +package »safeds« diff --git a/tests/resources/validation/other/modules/pipeline files must not be in safeds package/stub in subpackage of safeds.sdsstub b/tests/resources/validation/other/modules/pipeline files must not be in safeds package/stub in subpackage of safeds.sdsstub new file mode 100644 index 000000000..0724b7d84 --- /dev/null +++ b/tests/resources/validation/other/modules/pipeline files must not be in safeds package/stub in subpackage of safeds.sdsstub @@ -0,0 +1,3 @@ +// $TEST$ no error "A pipeline file must not be in a 'safeds' package." + +package »safeds.validation.other.modules.pipelineFilesMustNotBeInSafedsPackage« diff --git a/tests/resources/validation/other/modules/pipeline files must not be in safeds package/test elsewhere.sdstest b/tests/resources/validation/other/modules/pipeline files must not be in safeds package/test elsewhere.sdstest new file mode 100644 index 000000000..f193b218b --- /dev/null +++ b/tests/resources/validation/other/modules/pipeline files must not be in safeds package/test elsewhere.sdstest @@ -0,0 +1,3 @@ +// $TEST$ no error "A pipeline file must not be in a 'safeds' package." + +package »tests.validation.other.modules.pipelineFilesMustNotBeInSafedsPackage« diff --git a/tests/resources/validation/other/modules/pipeline files must not be in safeds package/test in safeds.sdstest b/tests/resources/validation/other/modules/pipeline files must not be in safeds package/test in safeds.sdstest new file mode 100644 index 000000000..07eecbb35 --- /dev/null +++ b/tests/resources/validation/other/modules/pipeline files must not be in safeds package/test in safeds.sdstest @@ -0,0 +1,3 @@ +// $TEST$ no error "A pipeline file must not be in a 'safeds' package." + +package »safeds« diff --git a/tests/resources/validation/other/modules/pipeline files must not be in safeds package/test in subpackage of safeds.sdstest b/tests/resources/validation/other/modules/pipeline files must not be in safeds package/test in subpackage of safeds.sdstest new file mode 100644 index 000000000..0724b7d84 --- /dev/null +++ b/tests/resources/validation/other/modules/pipeline files must not be in safeds package/test in subpackage of safeds.sdstest @@ -0,0 +1,3 @@ +// $TEST$ no error "A pipeline file must not be in a 'safeds' package." + +package »safeds.validation.other.modules.pipelineFilesMustNotBeInSafedsPackage«