-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: warn if parameters or placeholders are unused (#612)
Closes partially #543 ### Summary of Changes Show a warning if a parameter or placeholder is unused and suggest removing it. --------- Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
- Loading branch information
1 parent
43b276f
commit 3a2e9cc
Showing
9 changed files
with
220 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/language/validation/other/declarations/placeholders.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { isSdsBlock, 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_UNUSED = 'placeholder/unused'; | ||
|
||
export const placeholderShouldBeUsed = | ||
(services: SafeDsServices) => (node: SdsPlaceholder, accept: ValidationAcceptor) => { | ||
const usages = services.helpers.NodeMapper.placeholderToReferences(node); | ||
if (!usages.isEmpty()) { | ||
return; | ||
} | ||
|
||
// Don't show a warning if the placeholder is declared in the last statement in the block | ||
const containingStatement = getContainerOfType(node, isSdsStatement); | ||
const containingBlock = getContainerOfType(containingStatement, isSdsBlock); | ||
const allStatementsInBlock = statementsOrEmpty(containingBlock); | ||
if (last(allStatementsInBlock) === containingStatement) { | ||
return; | ||
} | ||
|
||
accept('warning', 'This placeholder is unused and can be removed.', { | ||
node, | ||
property: 'name', | ||
code: CODE_PLACEHOLDER_UNUSED, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.