Skip to content

Commit 83936b8

Browse files
feat: info if import alias can be removed (#637)
Closes #636 ### Summary of Changes Show an info if the import alias is identical to the declaration name. --------- Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
1 parent 7ec746a commit 83936b8

File tree

7 files changed

+51
-4
lines changed

7 files changed

+51
-4
lines changed

src/language/formatting/safe-ds-formatter.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ export class SafeDsFormatter extends AbstractFormatter {
4242
this.formatSdsImportedDeclarationList(node);
4343
} else if (ast.isSdsImportedDeclaration(node)) {
4444
this.formatSdsImportedDeclaration(node);
45+
} else if (ast.isSdsImportedDeclarationAlias(node)) {
46+
this.formatSdsImportedDeclarationAlias(node);
4547
}
4648

4749
// -----------------------------------------------------------------------------
@@ -297,7 +299,13 @@ export class SafeDsFormatter extends AbstractFormatter {
297299
private formatSdsImportedDeclaration(node: ast.SdsImportedDeclaration): void {
298300
const formatter = this.getNodeFormatter(node);
299301

300-
formatter.keyword('as').surround(Formatting.oneSpace());
302+
formatter.property('alias').prepend(oneSpace());
303+
}
304+
305+
private formatSdsImportedDeclarationAlias(node: ast.SdsImportedDeclarationAlias): void {
306+
const formatter = this.getNodeFormatter(node);
307+
308+
formatter.keyword('as').append(oneSpace());
301309
}
302310

303311
// -----------------------------------------------------------------------------

src/language/grammar/safe-ds.langium

+10-2
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,19 @@ SdsImportedDeclarationList returns SdsImportedDeclarationList:
7878

7979
interface SdsImportedDeclaration extends SdsObject {
8080
declaration: @SdsModuleMember;
81-
alias?: string;
81+
alias?: SdsImportedDeclarationAlias;
8282
}
8383

8484
SdsImportedDeclaration returns SdsImportedDeclaration:
85-
declaration=[SdsModuleMember:ID] ('as' alias=ID)?
85+
declaration=[SdsModuleMember:ID] alias=SdsImportedDeclarationAlias?
86+
;
87+
88+
interface SdsImportedDeclarationAlias extends SdsObject {
89+
alias: string;
90+
}
91+
92+
SdsImportedDeclarationAlias returns SdsImportedDeclarationAlias:
93+
{SdsImportedDeclarationAlias} 'as' alias=ID
8694
;
8795

8896
interface SdsWildcardImport extends SdsImport {}

src/language/scoping/safe-ds-scope-provider.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ export class SafeDsScopeProvider extends DefaultScopeProvider {
409409
}
410410

411411
if (importedDeclaration.alias) {
412-
result.push({ ...description, name: importedDeclaration.alias });
412+
result.push({ ...description, name: importedDeclaration.alias.alias });
413413
} else {
414414
result.push(description);
415415
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
enumBodyShouldNotBeEmpty,
2727
enumVariantParameterListShouldNotBeEmpty,
2828
functionResultListShouldNotBeEmpty,
29+
importedDeclarationAliasShouldDifferFromDeclarationName,
2930
memberAccessNullSafetyShouldBeNeeded,
3031
namedTypeTypeArgumentListShouldBeNeeded,
3132
segmentResultListShouldNotBeEmpty,
@@ -139,6 +140,7 @@ export const registerValidationChecks = function (services: SafeDsServices) {
139140
SdsExpressionLambda: [expressionLambdaMustContainUniqueNames],
140141
SdsFunction: [functionMustContainUniqueNames, functionResultListShouldNotBeEmpty],
141142
SdsImport: [importPackageMustExist(services), importPackageShouldNotBeEmpty(services)],
143+
SdsImportedDeclaration: [importedDeclarationAliasShouldDifferFromDeclarationName],
142144
SdsIndexedAccess: [indexedAccessesShouldBeUsedWithCaution],
143145
SdsLambda: [lambdaParametersMustNotBeAnnotated, lambdaParameterMustNotHaveConstModifier],
144146
SdsMemberAccess: [

src/language/validation/style.ts

+19
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
SdsEnumBody,
1111
SdsEnumVariant,
1212
SdsFunction,
13+
SdsImportedDeclaration,
1314
SdsMemberAccess,
1415
SdsNamedType,
1516
SdsSegment,
@@ -28,6 +29,7 @@ export const CODE_STYLE_UNNECESSARY_BODY = 'style/unnecessary-body';
2829
export const CODE_STYLE_UNNECESSARY_CONST_MODIFIER = 'style/unnecessary-const-modifier';
2930
export const CODE_STYLE_UNNECESSARY_CONSTRAINT_LIST = 'style/unnecessary-constraint-list';
3031
export const CODE_STYLE_UNNECESSARY_ELVIS_OPERATOR = 'style/unnecessary-elvis-operator';
32+
export const CODE_STYLE_UNNECESSARY_IMPORT_ALIAS = 'style/unnecessary-import-alias';
3133
export const CODE_STYLE_UNNECESSARY_PARAMETER_LIST = 'style/unnecessary-parameter-list';
3234
export const CODE_STYLE_UNNECESSARY_RESULT_LIST = 'style/unnecessary-result-list';
3335
export const CODE_STYLE_UNNECESSARY_SAFE_ACCESS = 'style/unnecessary-safe-access';
@@ -150,6 +152,23 @@ export const constraintListShouldNotBeEmpty = (node: SdsConstraintList, accept:
150152
}
151153
};
152154

155+
// -----------------------------------------------------------------------------
156+
// Unnecessary import alias
157+
// -----------------------------------------------------------------------------
158+
159+
export const importedDeclarationAliasShouldDifferFromDeclarationName = (
160+
node: SdsImportedDeclaration,
161+
accept: ValidationAcceptor,
162+
) => {
163+
if (node.alias && node.alias.alias === node.declaration.$refText) {
164+
accept('info', 'This alias can be removed.', {
165+
node,
166+
property: 'alias',
167+
code: CODE_STYLE_UNNECESSARY_IMPORT_ALIAS,
168+
});
169+
}
170+
};
171+
153172
// -----------------------------------------------------------------------------
154173
// Unnecessary parameter lists
155174
// -----------------------------------------------------------------------------
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package tests.validation.style.unnecessaryImportAlias
2+
3+
// $TEST$ no info "This alias can be removed."
4+
from tests.validation.style.unnecessaryImportAlias.other import C »as D«
5+
// $TEST$ info "This alias can be removed."
6+
// $TEST$ info "This alias can be removed."
7+
from tests.validation.style.unnecessaryImportAlias.other import C »as C«, C »as C«
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package tests.validation.style.unnecessaryImportAlias.other
2+
3+
class C

0 commit comments

Comments
 (0)