Skip to content

Commit

Permalink
Fixes transpiles of Typecasts wrapped in parens
Browse files Browse the repository at this point in the history
  • Loading branch information
markwpearce committed Jan 3, 2024
1 parent 77ebef3 commit 5e2bcc8
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
61 changes: 61 additions & 0 deletions src/files/BrsFile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2437,6 +2437,67 @@ describe('BrsFile', () => {
`);
});

it('allows typecasts wrapped in parens', async () => {
program.setFile('source/SomeKlass.bs', `
class SomeKlass
end class
`);
await testTranspile(`
sub foo(obj as SomeKlass)
(obj as roAssociativeArray).append({key:"value"})
print 3 + (obj as roAssociativeArray).count()
end sub
`, `
sub foo(obj as dynamic)
obj.append({
key: "value"
})
print 3 + obj.count()
end sub
`);
});

it('allows multiple typecasts wrapped in parens', async () => {
program.setFile('source/SomeKlass.bs', `
class SomeKlass
function value()
return 0.123
end function
end class
`);
await testTranspile(`
sub foo(obj)
print val( sin( (0.707 + (obj as SomeKlass).value()) as float ).toStr() as string)
end sub
`, `
sub foo(obj)
print val(sin((0.707 + obj.value())).toStr())
end sub
`);
});

it('allows a string of typecasts wrapped in parens', async () => {
program.setFile('source/SomeKlass.bs', `
class SomeKlass
function data()
return {key: "value"}
end function
end class
interface SomeIFace
key
end interface
`);
await testTranspile(`
sub foo(obj)
print (((obj as SomeKlass).data() as SomeIFace).key as string).len() as integer
end sub
`, `
sub foo(obj)
print obj.data().key.len()
end sub
`);
});
});

describe('callfunc operator', () => {
Expand Down
5 changes: 4 additions & 1 deletion src/parser/Expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as fileUrl from 'file-url';
import type { WalkOptions, WalkVisitor } from '../astUtils/visitors';
import { WalkMode } from '../astUtils/visitors';
import { walk, InternalWalkMode, walkArray } from '../astUtils/visitors';
import { isAALiteralExpression, isAAMemberExpression, isArrayLiteralExpression, isArrayType, isCallExpression, isCallableType, isCallfuncExpression, isCommentStatement, isComponentType, isDottedGetExpression, isEscapedCharCodeLiteralExpression, isFunctionExpression, isFunctionStatement, isIntegerType, isInterfaceMethodStatement, isLiteralBoolean, isLiteralExpression, isLiteralNumber, isLiteralString, isLongIntegerType, isMethodStatement, isNamespaceStatement, isNewExpression, isReferenceType, isStringType, isUnaryExpression, isVariableExpression } from '../astUtils/reflection';
import { isAALiteralExpression, isAAMemberExpression, isArrayLiteralExpression, isArrayType, isCallExpression, isCallableType, isCallfuncExpression, isCommentStatement, isComponentType, isDottedGetExpression, isEscapedCharCodeLiteralExpression, isFunctionExpression, isFunctionStatement, isIntegerType, isInterfaceMethodStatement, isLiteralBoolean, isLiteralExpression, isLiteralNumber, isLiteralString, isLongIntegerType, isMethodStatement, isNamespaceStatement, isNewExpression, isReferenceType, isStringType, isTypeCastExpression, isUnaryExpression, isVariableExpression } from '../astUtils/reflection';
import type { GetTypeOptions, TranspileResult, TypedefProvider } from '../interfaces';
import { TypeChainEntry } from '../interfaces';
import type { BscType } from '../types/BscType';
Expand Down Expand Up @@ -577,6 +577,9 @@ export class GroupingExpression extends Expression {
public readonly range: Range;

transpile(state: BrsTranspileState) {
if (isTypeCastExpression(this.expression)) {
return this.expression.transpile(state);
}
return [
state.transpileToken(this.tokens.left),
...this.expression.transpile(state),
Expand Down

0 comments on commit 5e2bcc8

Please sign in to comment.