Skip to content

Commit b99ab25

Browse files
feat: error if placeholder is alias for parameter or placeholder (#628)
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>
1 parent 18641de commit b99ab25

File tree

6 files changed

+140
-18
lines changed

6 files changed

+140
-18
lines changed

img/safe-ds_logo_rounded.png

9.7 KB
Loading

language-configuration.json

+9-7
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,28 @@
44
"blockComment": ["/*", "*/"]
55
},
66
"brackets": [
7+
["(", ")"],
78
["{", "}"],
89
["[", "]"],
9-
["(", ")"],
10+
["<", ">"],
1011
["»", "«"]
1112
],
1213
"autoClosingPairs": [
14+
["(", ")"],
1315
["{", "}"],
1416
["[", "]"],
15-
["(", ")"],
16-
["»", "«"],
17+
["<", ">"],
1718
["\"", "\""],
18-
["`", "`"]
19+
["`", "`"],
20+
["»", "«"]
1921
],
2022
"surroundingPairs": [
23+
["(", ")"],
2124
["{", "}"],
2225
["[", "]"],
23-
["(", ")"],
2426
["<", ">"],
25-
["»", "«"],
2627
["\"", "\""],
27-
["`", "`"]
28+
["`", "`"],
29+
["»", "«"]
2830
]
2931
}

package.json

+46-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,59 @@
11
{
22
"name": "safe-ds",
3-
"displayName": "safe-ds",
4-
"description": "Statically checked Data Science programs.",
53
"version": "0.1.0",
4+
"preview": true,
5+
"publisher": "Safe-DS",
6+
"displayName": "Safe-DS",
7+
"description": "Statically checked Data Science programs.",
8+
"license": "MIT",
9+
"categories": [
10+
"Programming Languages",
11+
"Machine Learning",
12+
"Data Science"
13+
],
14+
"keywords": [
15+
"dsl",
16+
"static checking"
17+
],
18+
"galleryBanner": {
19+
"color": "#e3e9e9"
20+
},
21+
"icon": "img/safe-ds_logo_rounded.png",
22+
"homepage": "https://dsl.safeds.com",
23+
"qna": "https://github.com/orgs/Safe-DS/discussions",
24+
"bugs": {
25+
"url": "https://github.com/Safe-DS/DSL/issues"
26+
},
27+
"repository": {
28+
"type": "git",
29+
"url": "https://github.com/Safe-DS/DSL.git"
30+
},
31+
"badges": [
32+
{
33+
"url": "https://github.com/Safe-DS/DSL/actions/workflows/main.yml/badge.svg",
34+
"href": "https://github.com/Safe-DS/DSL/actions/workflows/main.yml",
35+
"description": "Main"
36+
},
37+
{
38+
"url": "https://codecov.io/gh/Safe-DS/DSL/branch/main/graph/badge.svg?token=ma0ytglhO1",
39+
"href": "https://codecov.io/gh/Safe-DS/DSL",
40+
"description": "codecov"
41+
}
42+
],
643
"engines": {
744
"vscode": "^1.82.0"
845
},
9-
"categories": [
10-
"Programming Languages"
11-
],
1246
"contributes": {
1347
"languages": [
1448
{
1549
"id": "safe-ds",
1650
"aliases": [
1751
"Safe-DS",
18-
"safe-ds"
52+
"safe-ds",
53+
"SafeDS",
54+
"safeds",
55+
"SDS",
56+
"sds"
1957
],
2058
"extensions": [
2159
".sdspipe",
@@ -33,14 +71,14 @@
3371
}
3472
]
3573
},
74+
"type": "module",
75+
"main": "out/extension/main.cjs",
3676
"files": [
3777
"bin"
3878
],
39-
"type": "module",
4079
"bin": {
4180
"safe-ds-cli": "./bin/cli"
4281
},
43-
"main": "out/extension/main.cjs",
4482
"scripts": {
4583
"vscode:prepublish": "npm run build && npm run lint",
4684
"build": "tsc -b tsconfig.json && node esbuild.mjs",

src/language/validation/other/declarations/placeholders.ts

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,41 @@
1-
import { isSdsBlock, isSdsStatement, SdsPlaceholder } from '../../../generated/ast.js';
1+
import {
2+
isSdsAssignment,
3+
isSdsBlock,
4+
isSdsParameter,
5+
isSdsPlaceholder,
6+
isSdsReference,
7+
isSdsStatement,
8+
SdsPlaceholder,
9+
} from '../../../generated/ast.js';
210
import { getContainerOfType, ValidationAcceptor } from 'langium';
311
import { SafeDsServices } from '../../../safe-ds-module.js';
412
import { statementsOrEmpty } from '../../../helpers/nodeProperties.js';
513
import { last } from 'radash';
614

15+
export const CODE_PLACEHOLDER_ALIAS = 'placeholder/alias';
716
export const CODE_PLACEHOLDER_UNUSED = 'placeholder/unused';
817

18+
export const placeholdersMustNotBeAnAlias = (node: SdsPlaceholder, accept: ValidationAcceptor): void => {
19+
if (node.$containerIndex ?? 0 > 0) {
20+
return;
21+
}
22+
23+
const containingAssignment = getContainerOfType(node, isSdsAssignment);
24+
const rhs = containingAssignment?.expression;
25+
if (!isSdsReference(rhs)) {
26+
return;
27+
}
28+
29+
const referenceTarget = rhs.target.ref;
30+
if (isSdsParameter(referenceTarget) || isSdsPlaceholder(referenceTarget)) {
31+
accept('error', 'Aliases are not allowed to provide a cleaner graphical view.', {
32+
node,
33+
property: 'name',
34+
code: CODE_PLACEHOLDER_ALIAS,
35+
});
36+
}
37+
};
38+
939
export const placeholderShouldBeUsed =
1040
(services: SafeDsServices) => (node: SdsPlaceholder, accept: ValidationAcceptor) => {
1141
const usages = services.helpers.NodeMapper.placeholderToReferences(node);

src/language/validation/safe-ds-validator.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ import {
6161
namedTypeDeclarationShouldNotBeExperimental,
6262
referenceTargetShouldNotExperimental,
6363
} from './builtins/experimental.js';
64-
import { placeholderShouldBeUsed } from './other/declarations/placeholders.js';
64+
import { placeholderShouldBeUsed, placeholdersMustNotBeAnAlias } from './other/declarations/placeholders.js';
6565
import { segmentParameterShouldBeUsed, segmentResultMustBeAssignedExactlyOnce } from './other/declarations/segments.js';
6666
import { lambdaParameterMustNotHaveConstModifier } from './other/expressions/lambdas.js';
6767
import { indexedAccessesShouldBeUsedWithCaution } from './experimentalLanguageFeature.js';
@@ -139,7 +139,7 @@ export const registerValidationChecks = function (services: SafeDsServices) {
139139
],
140140
SdsParameterList: [parameterListMustNotHaveRequiredParametersAfterOptionalParameters],
141141
SdsPipeline: [pipelineMustContainUniqueNames],
142-
SdsPlaceholder: [placeholderShouldBeUsed(services)],
142+
SdsPlaceholder: [placeholdersMustNotBeAnAlias, placeholderShouldBeUsed(services)],
143143
SdsReference: [
144144
referenceTargetMustNotBeAnnotationPipelineOrSchema,
145145
referenceTargetShouldNotBeDeprecated(services),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package tests.validation.other.placeholder.alias
2+
3+
annotation MyAnnotation
4+
5+
class MyClass {
6+
static attr myAttribute: Int
7+
8+
static fun myMethod()
9+
}
10+
11+
enum MyEnum {
12+
MyEnumVariant
13+
}
14+
15+
fun myFunction()
16+
17+
pipeline myPipeline {}
18+
19+
schema MySchema {}
20+
21+
segment mySegment1() {}
22+
23+
segment mySegment2(myParameter: Int) {
24+
// $TEST$ no error "Aliases are not allowed to provide a cleaner graphical view."
25+
val »myPlaceholder« = 1;
26+
// $TEST$ no error "Aliases are not allowed to provide a cleaner graphical view."
27+
val »a« = MyAnnotation;
28+
// $TEST$ no error "Aliases are not allowed to provide a cleaner graphical view."
29+
val »b« = MyClass.myAttribute;
30+
// $TEST$ no error "Aliases are not allowed to provide a cleaner graphical view."
31+
val »c« = MyClass;
32+
// $TEST$ no error "Aliases are not allowed to provide a cleaner graphical view."
33+
val »d« = MyEnum;
34+
// $TEST$ no error "Aliases are not allowed to provide a cleaner graphical view."
35+
val »e« = MyEnum.MyEnumVariant;
36+
// $TEST$ no error "Aliases are not allowed to provide a cleaner graphical view."
37+
val »f« = myFunction;
38+
// $TEST$ error "Aliases are not allowed to provide a cleaner graphical view."
39+
// $TEST$ no error "Aliases are not allowed to provide a cleaner graphical view."
40+
val »g1«, val »g2« = myParameter;
41+
// $TEST$ no error "Aliases are not allowed to provide a cleaner graphical view."
42+
val »h« = myPipeline;
43+
// $TEST$ error "Aliases are not allowed to provide a cleaner graphical view."
44+
// $TEST$ no error "Aliases are not allowed to provide a cleaner graphical view."
45+
val »i1«, val »i2« = myPlaceholder;
46+
// $TEST$ no error "Aliases are not allowed to provide a cleaner graphical view."
47+
val »j« = MySchema;
48+
// $TEST$ no error "Aliases are not allowed to provide a cleaner graphical view."
49+
val »k« = mySegment1;
50+
// $TEST$ no error "Aliases are not allowed to provide a cleaner graphical view."
51+
val »l« = unresolved;
52+
}

0 commit comments

Comments
 (0)