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

fix: prevent references to following parameters from default values #707

Merged
merged 1 commit into from
Oct 30, 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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
isSdsModule,
isSdsNamedType,
isSdsNamedTypeDeclaration,
isSdsParameter,
isSdsPlaceholder,
isSdsQualifiedImport,
isSdsReference,
Expand All @@ -35,12 +36,14 @@ import {
isSdsWildcardImport,
isSdsYield,
SdsArgument,
type SdsCallable,
SdsDeclaration,
SdsExpression,
SdsImportedDeclaration,
SdsMemberAccess,
SdsMemberType,
SdsNamedTypeDeclaration,
type SdsParameter,
SdsPlaceholder,
SdsReference,
SdsStatement,
Expand Down Expand Up @@ -285,9 +288,17 @@ export class SafeDsScopeProvider extends DefaultScopeProvider {
}

private localDeclarations(node: AstNode, outerScope: Scope): Scope {
// Parameters
const containingCallable = getContainerOfType(node.$container, isSdsCallable);
const parameters = getParameters(containingCallable);

// Parameters (up to the containing parameter)
const containingParameter = getContainerOfType(node, isSdsParameter);

let parameters: Iterable<SdsParameter>;
if (containingCallable && containingParameter) {
parameters = this.parametersUpToParameter(containingCallable, containingParameter);
} else {
parameters = getParameters(containingCallable);
}

// Placeholders up to the containing statement
const containingStatement = getContainerOfType(node.$container, isSdsStatement);
Expand All @@ -311,6 +322,19 @@ export class SafeDsScopeProvider extends DefaultScopeProvider {
}
}

private *parametersUpToParameter(
callable: SdsCallable,
parameter: SdsParameter,
): Generator<SdsParameter, void, undefined> {
for (const current of getParameters(callable)) {
if (current === parameter) {
return;
}

yield current;
}
}

private *placeholdersUpToStatement(
statement: SdsStatement | undefined,
): Generator<SdsPlaceholder, void, undefined> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package tests.scoping.references.inSameFile.toParameters.fromDefaultValueOfParameter

annotation MyAnnotation(
// $TEST$ target annotation_param
// $TEST$ unresolved
»p«: String = »q«,
// $TEST$ references annotation_param
q: String = »p«,
// $TEST$ unresolved
r: String = »r«,
)

class MyClass(
// $TEST$ target class_param
// $TEST$ unresolved
»p«: String = »q«,
// $TEST$ references class_param
q: String = »p«,
// $TEST$ unresolved
r: String = »r«,
)

enum MyEnum {
MyEnumVariant(
// $TEST$ target enum_variant_param
// $TEST$ unresolved
»p«: String = »q«,
// $TEST$ references enum_variant_param
q: String = »p«,
// $TEST$ unresolved
r: String = »r«,
)
}

fun myFunction(
// $TEST$ target function_param
// $TEST$ unresolved
»p«: String = »q«,
// $TEST$ references function_param
q: String = »p«,
// $TEST$ unresolved
r: String = »r«,
)

segment mySegment1(
// $TEST$ target segment_param
// $TEST$ unresolved
»p«: String = »q«,
// $TEST$ references segment_param
q: String = »p«,
// $TEST$ unresolved
r: String = »r«,
) {}

segment mySegment2(
p: (
// $TEST$ target callable_type_param
// $TEST$ unresolved
»p«: String = »q«,
// $TEST$ references callable_type_param
q: String = »p«,
// $TEST$ unresolved
r: String = »r«,
) -> ()
) {
(
// $TEST$ target block_lambda_param
// $TEST$ unresolved
»p«: String = »q«,
// $TEST$ references block_lambda_param
q: String = »p«,
// $TEST$ unresolved
r: String = »r«,
) {};
(
// $TEST$ target block_function_param
// $TEST$ unresolved
»p«: String = »q«,
// $TEST$ references block_function_param
q: String = »p«,
// $TEST$ unresolved
r: String = »r«,
) -> 1;
}