Skip to content

Commit

Permalink
feat: info if import alias can be removed (#637)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
lars-reimann and megalinter-bot committed Oct 12, 2023
1 parent 7ec746a commit 83936b8
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/language/formatting/safe-ds-formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export class SafeDsFormatter extends AbstractFormatter {
this.formatSdsImportedDeclarationList(node);
} else if (ast.isSdsImportedDeclaration(node)) {
this.formatSdsImportedDeclaration(node);
} else if (ast.isSdsImportedDeclarationAlias(node)) {
this.formatSdsImportedDeclarationAlias(node);
}

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

formatter.keyword('as').surround(Formatting.oneSpace());
formatter.property('alias').prepend(oneSpace());
}

private formatSdsImportedDeclarationAlias(node: ast.SdsImportedDeclarationAlias): void {
const formatter = this.getNodeFormatter(node);

formatter.keyword('as').append(oneSpace());
}

// -----------------------------------------------------------------------------
Expand Down
12 changes: 10 additions & 2 deletions src/language/grammar/safe-ds.langium
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,19 @@ SdsImportedDeclarationList returns SdsImportedDeclarationList:

interface SdsImportedDeclaration extends SdsObject {
declaration: @SdsModuleMember;
alias?: string;
alias?: SdsImportedDeclarationAlias;
}

SdsImportedDeclaration returns SdsImportedDeclaration:
declaration=[SdsModuleMember:ID] ('as' alias=ID)?
declaration=[SdsModuleMember:ID] alias=SdsImportedDeclarationAlias?
;

interface SdsImportedDeclarationAlias extends SdsObject {
alias: string;
}

SdsImportedDeclarationAlias returns SdsImportedDeclarationAlias:
{SdsImportedDeclarationAlias} 'as' alias=ID
;

interface SdsWildcardImport extends SdsImport {}
Expand Down
2 changes: 1 addition & 1 deletion src/language/scoping/safe-ds-scope-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ export class SafeDsScopeProvider extends DefaultScopeProvider {
}

if (importedDeclaration.alias) {
result.push({ ...description, name: importedDeclaration.alias });
result.push({ ...description, name: importedDeclaration.alias.alias });
} else {
result.push(description);
}
Expand Down
2 changes: 2 additions & 0 deletions src/language/validation/safe-ds-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
enumBodyShouldNotBeEmpty,
enumVariantParameterListShouldNotBeEmpty,
functionResultListShouldNotBeEmpty,
importedDeclarationAliasShouldDifferFromDeclarationName,
memberAccessNullSafetyShouldBeNeeded,
namedTypeTypeArgumentListShouldBeNeeded,
segmentResultListShouldNotBeEmpty,
Expand Down Expand Up @@ -139,6 +140,7 @@ export const registerValidationChecks = function (services: SafeDsServices) {
SdsExpressionLambda: [expressionLambdaMustContainUniqueNames],
SdsFunction: [functionMustContainUniqueNames, functionResultListShouldNotBeEmpty],
SdsImport: [importPackageMustExist(services), importPackageShouldNotBeEmpty(services)],
SdsImportedDeclaration: [importedDeclarationAliasShouldDifferFromDeclarationName],
SdsIndexedAccess: [indexedAccessesShouldBeUsedWithCaution],
SdsLambda: [lambdaParametersMustNotBeAnnotated, lambdaParameterMustNotHaveConstModifier],
SdsMemberAccess: [
Expand Down
19 changes: 19 additions & 0 deletions src/language/validation/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
SdsEnumBody,
SdsEnumVariant,
SdsFunction,
SdsImportedDeclaration,
SdsMemberAccess,
SdsNamedType,
SdsSegment,
Expand All @@ -28,6 +29,7 @@ export const CODE_STYLE_UNNECESSARY_BODY = 'style/unnecessary-body';
export const CODE_STYLE_UNNECESSARY_CONST_MODIFIER = 'style/unnecessary-const-modifier';
export const CODE_STYLE_UNNECESSARY_CONSTRAINT_LIST = 'style/unnecessary-constraint-list';
export const CODE_STYLE_UNNECESSARY_ELVIS_OPERATOR = 'style/unnecessary-elvis-operator';
export const CODE_STYLE_UNNECESSARY_IMPORT_ALIAS = 'style/unnecessary-import-alias';
export const CODE_STYLE_UNNECESSARY_PARAMETER_LIST = 'style/unnecessary-parameter-list';
export const CODE_STYLE_UNNECESSARY_RESULT_LIST = 'style/unnecessary-result-list';
export const CODE_STYLE_UNNECESSARY_SAFE_ACCESS = 'style/unnecessary-safe-access';
Expand Down Expand Up @@ -150,6 +152,23 @@ export const constraintListShouldNotBeEmpty = (node: SdsConstraintList, accept:
}
};

// -----------------------------------------------------------------------------
// Unnecessary import alias
// -----------------------------------------------------------------------------

export const importedDeclarationAliasShouldDifferFromDeclarationName = (
node: SdsImportedDeclaration,
accept: ValidationAcceptor,
) => {
if (node.alias && node.alias.alias === node.declaration.$refText) {
accept('info', 'This alias can be removed.', {
node,
property: 'alias',
code: CODE_STYLE_UNNECESSARY_IMPORT_ALIAS,
});
}
};

// -----------------------------------------------------------------------------
// Unnecessary parameter lists
// -----------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package tests.validation.style.unnecessaryImportAlias

// $TEST$ no info "This alias can be removed."
from tests.validation.style.unnecessaryImportAlias.other import C »as D«
// $TEST$ info "This alias can be removed."
// $TEST$ info "This alias can be removed."
from tests.validation.style.unnecessaryImportAlias.other import C »as C«, C »as C«
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package tests.validation.style.unnecessaryImportAlias.other

class C

0 comments on commit 83936b8

Please sign in to comment.