From b21c38edef322b5a40c9b96df757fb09af507493 Mon Sep 17 00:00:00 2001 From: Bronley Plumb Date: Thu, 28 May 2020 15:55:52 -0400 Subject: [PATCH] transpile namespaced function var assignments Fixes #91 --- CHANGELOG.md | 6 ++++++ src/files/BrsFile.spec.ts | 24 ++++++++++++++++++++++++ src/parser/Expression.ts | 24 ++++++++++++------------ 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 55d6b8a67..e3fc00edd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 +## [Unreleased] +### Fixed + - bug where assigning a namespaced function to a variable wasn't properly transpiling the dots to underscores (fixes [#91](https://github.com/rokucommunity/brighterscript/issues/91)) + + + ## [0.10.3] - 2020-05-27 ### Changed - tokenizing a string with no closing quote will now include all of the text until the end of the line. diff --git a/src/files/BrsFile.spec.ts b/src/files/BrsFile.spec.ts index af01dd689..e705b98c3 100644 --- a/src/files/BrsFile.spec.ts +++ b/src/files/BrsFile.spec.ts @@ -1469,6 +1469,30 @@ describe('BrsFile', () => { }); describe('transpile', () => { + describe('namespaces', () => { + it('properly transpiles namespace functions for assignments', async () => { + await testTranspile(` + namespace NameA.NameB + sub Speak() + end sub + end namespace + sub main() + sayHello = NameA.NameB.Speak + sayHello() + someOtherObject = some.other.object + end sub + `, ` + sub NameA_NameB_Speak() + end sub + + sub main() + sayHello = NameA_NameB_Speak + sayHello() + someOtherObject = some.other.object + end sub + `); + }); + }); it('includes all text to end of line for a non-terminated string', async () => { await testTranspile(` sub main() diff --git a/src/parser/Expression.ts b/src/parser/Expression.ts index 784c33391..d5df69844 100644 --- a/src/parser/Expression.ts +++ b/src/parser/Expression.ts @@ -56,13 +56,8 @@ export class CallExpression implements Expression { transpile(state: TranspileState) { let result = []; - //if the callee starts with a namespace name, transpile the name - if (state.file.calleeStartsWithNamespace(this.callee)) { - result.push( - ...new NamespacedVariableNameExpression(this.callee as DottedGetExpression | VariableExpression).transpile(state) - ); - //if the callee is the name of a known namespace function - } else if (state.file.calleeIsKnownNamespaceFunction(this.callee, this.namespaceName?.getName(ParseMode.BrighterScript))) { + //if the callee is the name of a known namespace function + if (state.file.calleeIsKnownNamespaceFunction(this.callee, this.namespaceName?.getName(ParseMode.BrighterScript))) { result.push( new SourceNode( this.callee.range.start.line + 1, @@ -243,11 +238,16 @@ export class DottedGetExpression implements Expression { public readonly range: Range; transpile(state: TranspileState) { - return [ - ...this.obj.transpile(state), - '.', - new SourceNode(this.name.range.start.line + 1, this.name.range.start.character, state.pathAbsolute, this.name.text) - ]; + //if the callee starts with a namespace name, transpile the name + if (state.file.calleeStartsWithNamespace(this)) { + return new NamespacedVariableNameExpression(this as DottedGetExpression | VariableExpression).transpile(state); + } else { + return [ + ...this.obj.transpile(state), + '.', + new SourceNode(this.name.range.start.line + 1, this.name.range.start.character, state.pathAbsolute, this.name.text) + ]; + } } }