Skip to content

Commit

Permalink
feat: error if pipeline file is in a safeds.xy package (#673)
Browse files Browse the repository at this point in the history
Closes #671

### Summary of Changes

Show an error if a pipeline file is in a `safeds.xy` package.
  • Loading branch information
lars-reimann authored Oct 22, 2023
1 parent fa7631d commit 867bae3
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 15 deletions.
40 changes: 26 additions & 14 deletions src/language/validation/other/modules.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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,
});
}
};
7 changes: 6 additions & 1 deletion src/language/validation/safe-ds-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -228,6 +232,7 @@ export const registerValidationChecks = function (services: SafeDsServices) {
moduleMemberMustHaveNameThatIsUniqueInPackage(services),
moduleMustContainUniqueNames,
moduleWithDeclarationsMustStatePackage,
pipelineFileMustNotBeInSafedsPackage,
pythonModuleShouldDifferFromSafeDsPackage(services),
],
SdsNamedType: [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// $TEST$ no error "A pipeline file must not be in a 'safeds' package."

package »tests.validation.other.modules.pipelineFilesMustNotBeInSafedsPackage«
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// $TEST$ error "A pipeline file must not be in a 'safeds' package."

package »safeds«
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// $TEST$ error "A pipeline file must not be in a 'safeds' package."

package »safeds.validation.other.modules.pipelineFilesMustNotBeInSafedsPackage«
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// $TEST$ no error "A pipeline file must not be in a 'safeds' package."

package »tests.validation.other.modules.pipelineFilesMustNotBeInSafedsPackage«
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// $TEST$ no error "A pipeline file must not be in a 'safeds' package."

package »safeds«
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// $TEST$ no error "A pipeline file must not be in a 'safeds' package."

package »safeds.validation.other.modules.pipelineFilesMustNotBeInSafedsPackage«
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// $TEST$ no error "A pipeline file must not be in a 'safeds' package."

package »tests.validation.other.modules.pipelineFilesMustNotBeInSafedsPackage«
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// $TEST$ no error "A pipeline file must not be in a 'safeds' package."

package »safeds«
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// $TEST$ no error "A pipeline file must not be in a 'safeds' package."

package »safeds.validation.other.modules.pipelineFilesMustNotBeInSafedsPackage«

0 comments on commit 867bae3

Please sign in to comment.