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

Fixes transpiles of Typecasts wrapped in parens #998

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
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