From c09d25b52023526db503bee18273d91f9134054a Mon Sep 17 00:00:00 2001 From: Adam Coster Date: Thu, 17 Aug 2023 12:11:16 -0500 Subject: [PATCH] fix: Resolved issue with the RHS of dot-accessor assignments not properly resetting to the outer scope. --- .../parser/src/visitor.identifierAccessor.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/parser/src/visitor.identifierAccessor.ts b/packages/parser/src/visitor.identifierAccessor.ts index 9ca7e978..d9b1c2b7 100644 --- a/packages/parser/src/visitor.identifierAccessor.ts +++ b/packages/parser/src/visitor.identifierAccessor.ts @@ -419,6 +419,13 @@ function processDotAccessor( visitor.PROCESSOR.pushSelfScope(dot, dottableType, true, { accessorScope: true, }); + let scopeIsPopped = false; + const popSelfScope = (on = propertyNameLocation) => { + if (scopeIsPopped) return; + visitor.PROCESSOR.scope.setEnd(on, true); + visitor.PROCESSOR.popSelfScope(on, true); + scopeIsPopped = true; + }; // While editing a user will dot into something // prior to actually adding the new identifier. @@ -426,8 +433,7 @@ function processDotAccessor( // still add a scopeRange for the dot. const hasIdentifier = !isEmpty(dotAccessor.identifier[0].children); if (!hasIdentifier) { - visitor.PROCESSOR.scope.setEnd(dot, true); - visitor.PROCESSOR.popSelfScope(dot, true); + popSelfScope(dot); nextAccessed.types = [visitor.ANY]; return nextAccessed; } @@ -452,6 +458,8 @@ function processDotAccessor( !isTypeOfKind(dottableType, 'Enum') && !property.def ) { + // Pop the self-scope so the RHS is in the right place! + popSelfScope(); property = assignVariable( visitor, @@ -481,6 +489,9 @@ function processDotAccessor( } else if (lastAccessed.rhs) { // Then this variable is not yet defined on this struct, // but we have an assignment operation to use to define it. + // We need to pop the scope first so that the RHS is evaluated + // in the correct scope! + popSelfScope(); property = assignVariable( visitor, { @@ -512,8 +523,7 @@ function processDotAccessor( ); } } - visitor.PROCESSOR.scope.setEnd(propertyNameLocation, true); - visitor.PROCESSOR.popSelfScope(propertyNameLocation, true); + popSelfScope(); nextAccessed.signifier = property; nextAccessed.types = arrayWrapped(property?.type || visitor.ANY); return nextAccessed;