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 placeholder is alias for parameter or placeholder #628

Merged
merged 3 commits into from
Oct 10, 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
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;
}