Skip to content

Commit

Permalink
feat: error if placeholder is alias for parameter or placeholder (#628)
Browse files Browse the repository at this point in the history
Closes #564
Closes partially #543

### Summary of Changes

To provide a cleaner graphical view, this PR makes it an error if a
placeholder just acts as an alias for another placeholder or a
parameter.

---------

Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
  • Loading branch information
lars-reimann and megalinter-bot authored Oct 10, 2023
1 parent 18641de commit b99ab25
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 18 deletions.
Binary file added img/safe-ds_logo_rounded.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 9 additions & 7 deletions language-configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,28 @@
"blockComment": ["/*", "*/"]
},
"brackets": [
["(", ")"],
["{", "}"],
["[", "]"],
["(", ")"],
["<", ">"],
["»", "«"]
],
"autoClosingPairs": [
["(", ")"],
["{", "}"],
["[", "]"],
["(", ")"],
["»", "«"],
["<", ">"],
["\"", "\""],
["`", "`"]
["`", "`"],
["»", "«"]
],
"surroundingPairs": [
["(", ")"],
["{", "}"],
["[", "]"],
["(", ")"],
["<", ">"],
["»", "«"],
["\"", "\""],
["`", "`"]
["`", "`"],
["»", "«"]
]
}
54 changes: 46 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,59 @@
{
"name": "safe-ds",
"displayName": "safe-ds",
"description": "Statically checked Data Science programs.",
"version": "0.1.0",
"preview": true,
"publisher": "Safe-DS",
"displayName": "Safe-DS",
"description": "Statically checked Data Science programs.",
"license": "MIT",
"categories": [
"Programming Languages",
"Machine Learning",
"Data Science"
],
"keywords": [
"dsl",
"static checking"
],
"galleryBanner": {
"color": "#e3e9e9"
},
"icon": "img/safe-ds_logo_rounded.png",
"homepage": "https://dsl.safeds.com",
"qna": "https://github.com/orgs/Safe-DS/discussions",
"bugs": {
"url": "https://github.com/Safe-DS/DSL/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/Safe-DS/DSL.git"
},
"badges": [
{
"url": "https://github.com/Safe-DS/DSL/actions/workflows/main.yml/badge.svg",
"href": "https://github.com/Safe-DS/DSL/actions/workflows/main.yml",
"description": "Main"
},
{
"url": "https://codecov.io/gh/Safe-DS/DSL/branch/main/graph/badge.svg?token=ma0ytglhO1",
"href": "https://codecov.io/gh/Safe-DS/DSL",
"description": "codecov"
}
],
"engines": {
"vscode": "^1.82.0"
},
"categories": [
"Programming Languages"
],
"contributes": {
"languages": [
{
"id": "safe-ds",
"aliases": [
"Safe-DS",
"safe-ds"
"safe-ds",
"SafeDS",
"safeds",
"SDS",
"sds"
],
"extensions": [
".sdspipe",
Expand All @@ -33,14 +71,14 @@
}
]
},
"type": "module",
"main": "out/extension/main.cjs",
"files": [
"bin"
],
"type": "module",
"bin": {
"safe-ds-cli": "./bin/cli"
},
"main": "out/extension/main.cjs",
"scripts": {
"vscode:prepublish": "npm run build && npm run lint",
"build": "tsc -b tsconfig.json && node esbuild.mjs",
Expand Down
32 changes: 31 additions & 1 deletion src/language/validation/other/declarations/placeholders.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,41 @@
import { isSdsBlock, isSdsStatement, SdsPlaceholder } from '../../../generated/ast.js';
import {
isSdsAssignment,
isSdsBlock,
isSdsParameter,
isSdsPlaceholder,
isSdsReference,
isSdsStatement,
SdsPlaceholder,
} from '../../../generated/ast.js';
import { getContainerOfType, ValidationAcceptor } from 'langium';
import { SafeDsServices } from '../../../safe-ds-module.js';
import { statementsOrEmpty } from '../../../helpers/nodeProperties.js';
import { last } from 'radash';

export const CODE_PLACEHOLDER_ALIAS = 'placeholder/alias';
export const CODE_PLACEHOLDER_UNUSED = 'placeholder/unused';

export const placeholdersMustNotBeAnAlias = (node: SdsPlaceholder, accept: ValidationAcceptor): void => {
if (node.$containerIndex ?? 0 > 0) {
return;
}

const containingAssignment = getContainerOfType(node, isSdsAssignment);
const rhs = containingAssignment?.expression;
if (!isSdsReference(rhs)) {
return;
}

const referenceTarget = rhs.target.ref;
if (isSdsParameter(referenceTarget) || isSdsPlaceholder(referenceTarget)) {
accept('error', 'Aliases are not allowed to provide a cleaner graphical view.', {
node,
property: 'name',
code: CODE_PLACEHOLDER_ALIAS,
});
}
};

export const placeholderShouldBeUsed =
(services: SafeDsServices) => (node: SdsPlaceholder, accept: ValidationAcceptor) => {
const usages = services.helpers.NodeMapper.placeholderToReferences(node);
Expand Down
4 changes: 2 additions & 2 deletions src/language/validation/safe-ds-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import {
namedTypeDeclarationShouldNotBeExperimental,
referenceTargetShouldNotExperimental,
} from './builtins/experimental.js';
import { placeholderShouldBeUsed } from './other/declarations/placeholders.js';
import { placeholderShouldBeUsed, placeholdersMustNotBeAnAlias } from './other/declarations/placeholders.js';
import { segmentParameterShouldBeUsed, segmentResultMustBeAssignedExactlyOnce } from './other/declarations/segments.js';
import { lambdaParameterMustNotHaveConstModifier } from './other/expressions/lambdas.js';
import { indexedAccessesShouldBeUsedWithCaution } from './experimentalLanguageFeature.js';
Expand Down Expand Up @@ -139,7 +139,7 @@ export const registerValidationChecks = function (services: SafeDsServices) {
],
SdsParameterList: [parameterListMustNotHaveRequiredParametersAfterOptionalParameters],
SdsPipeline: [pipelineMustContainUniqueNames],
SdsPlaceholder: [placeholderShouldBeUsed(services)],
SdsPlaceholder: [placeholdersMustNotBeAnAlias, placeholderShouldBeUsed(services)],
SdsReference: [
referenceTargetMustNotBeAnnotationPipelineOrSchema,
referenceTargetShouldNotBeDeprecated(services),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package tests.validation.other.placeholder.alias

annotation MyAnnotation

class MyClass {
static attr myAttribute: Int

static fun myMethod()
}

enum MyEnum {
MyEnumVariant
}

fun myFunction()

pipeline myPipeline {}

schema MySchema {}

segment mySegment1() {}

segment mySegment2(myParameter: Int) {
// $TEST$ no error "Aliases are not allowed to provide a cleaner graphical view."
val »myPlaceholder« = 1;
// $TEST$ no error "Aliases are not allowed to provide a cleaner graphical view."
val »a« = MyAnnotation;
// $TEST$ no error "Aliases are not allowed to provide a cleaner graphical view."
val »b« = MyClass.myAttribute;
// $TEST$ no error "Aliases are not allowed to provide a cleaner graphical view."
val »c« = MyClass;
// $TEST$ no error "Aliases are not allowed to provide a cleaner graphical view."
val »d« = MyEnum;
// $TEST$ no error "Aliases are not allowed to provide a cleaner graphical view."
val »e« = MyEnum.MyEnumVariant;
// $TEST$ no error "Aliases are not allowed to provide a cleaner graphical view."
val »f« = myFunction;
// $TEST$ error "Aliases are not allowed to provide a cleaner graphical view."
// $TEST$ no error "Aliases are not allowed to provide a cleaner graphical view."
val »g1«, val »g2« = myParameter;
// $TEST$ no error "Aliases are not allowed to provide a cleaner graphical view."
val »h« = myPipeline;
// $TEST$ error "Aliases are not allowed to provide a cleaner graphical view."
// $TEST$ no error "Aliases are not allowed to provide a cleaner graphical view."
val »i1«, val »i2« = myPlaceholder;
// $TEST$ no error "Aliases are not allowed to provide a cleaner graphical view."
val »j« = MySchema;
// $TEST$ no error "Aliases are not allowed to provide a cleaner graphical view."
val »k« = mySegment1;
// $TEST$ no error "Aliases are not allowed to provide a cleaner graphical view."
val »l« = unresolved;
}

0 comments on commit b99ab25

Please sign in to comment.