Skip to content

Commit

Permalink
Fix some missing transpiled comments for namespaced calls
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron committed Aug 13, 2024
1 parent 2a3dbae commit d2d227c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 16 deletions.
39 changes: 29 additions & 10 deletions src/files/BrsFile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2615,22 +2615,41 @@ describe('BrsFile', () => {
it('includes comments above namespaced method call', async () => {
await testTranspile(`
sub main()
'delay for a bit
utils.delay(sub()
end sub)
'do nothing
utils.noop()
end sub
namespace utils
function delay(callback as function)
end function
sub noop()
end sub
end namespace
`, `
sub main()
'delay for a bit
utils_delay(sub()
end sub)
'do nothing
utils_noop()
end sub
sub utils_noop()
end sub
`, undefined, 'source/main.bs');
});

it('includes comments above inferred namespace function call', async () => {
await testTranspile(`
namespace utils
sub test()
'do nothing
noop()
end sub
sub noop()
end sub
end namespace
`, `
sub utils_test()
'do nothing
utils_noop()
end sub
sub utils_noop()
end sub
function utils_delay(callback as function)
end function
`, undefined, 'source/main.bs');
});

Expand Down
10 changes: 8 additions & 2 deletions src/parser/Expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,14 @@ export class CallExpression extends Expression {

transpile(state: BrsTranspileState, nameOverride?: string) {
let result: TranspileResult = [];
throw new Error('crash');

//transpile the name
if (nameOverride) {
result.push(state.sourceNode(this.callee, nameOverride));
result.push(
//transpile leading comments since we're bypassing callee.transpile (which would normally do this)
...state.transpileLeadingCommentsForAstNode(this),
state.sourceNode(this.callee, nameOverride)
);
} else {
result.push(...this.callee.transpile(state));
}
Expand Down Expand Up @@ -554,6 +557,7 @@ export class DottedGetExpression extends Expression {
//if the callee starts with a namespace name, transpile the name
if (state.file.calleeStartsWithNamespace(this)) {
return [
...state.transpileLeadingCommentsForAstNode(this),
state.sourceNode(this, this.getName(ParseMode.BrightScript))
];
} else {
Expand Down Expand Up @@ -1185,6 +1189,8 @@ export class VariableExpression extends Expression {
//if the callee is the name of a known namespace function
if (namespace && state.file.calleeIsKnownNamespaceFunction(this, namespace.getName(ParseMode.BrighterScript))) {
result.push(
//transpile leading comments since the token isn't being transpiled directly
...state.transpileLeadingCommentsForAstNode(this),
state.sourceNode(this, [
namespace.getName(ParseMode.BrightScript),
'_',
Expand Down
5 changes: 4 additions & 1 deletion src/parser/Statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,10 @@ export class ExpressionStatement extends Statement {
public readonly location: Location | undefined;

transpile(state: BrsTranspileState) {
return this.expression.transpile(state);
return [
state.transpileAnnotations(this),
this.expression.transpile(state)
];
}

walk(visitor: WalkVisitor, options: WalkOptions) {
Expand Down
20 changes: 17 additions & 3 deletions src/parser/TranspileState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import { SourceNode } from 'source-map';
import type { Location } from 'vscode-languageserver';
import type { BsConfig } from '../BsConfig';
import { TokenKind } from '../lexer/TokenKind';
import type { Token } from '../lexer/Token';
import { isToken, type Token } from '../lexer/Token';

Check failure on line 5 in src/parser/TranspileState.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest)

'isToken' is declared but its value is never read.
import type { RangeLike } from '../util';
import { util } from '../util';
import type { TranspileResult } from '../interfaces';


interface TranspileToken {
location?: Location;
text: string;
Expand Down Expand Up @@ -113,6 +112,17 @@ export class TranspileState {
);
}

public transpileLeadingCommentsForAstNode(node: { leadingTrivia?: Token[] }) {
const leadingTrivia = node?.leadingTrivia ?? [];
const leadingCommentsSourceNodes = this.transpileComments(leadingTrivia);
if (leadingCommentsSourceNodes.length > 0) {
// indent in preparation for next text
leadingCommentsSourceNodes.push(this.indent());
}

return leadingCommentsSourceNodes;
}

public transpileLeadingComments(token: TranspileToken) {
const leadingTrivia = (token?.leadingTrivia ?? []);
const leadingCommentsSourceNodes = this.transpileComments(leadingTrivia);
Expand All @@ -124,7 +134,7 @@ export class TranspileState {
return leadingCommentsSourceNodes;
}

public transpileComments(tokens: TranspileToken[]) {
public transpileComments(tokens: TranspileToken[], prepNextLine = false): Array<string | SourceNode> {
const leadingCommentsSourceNodes = [];
const justComments = tokens.filter(t => t.kind === TokenKind.Comment || t.kind === TokenKind.Newline);
let newLinesSinceComment = 0;
Expand All @@ -150,6 +160,10 @@ export class TranspileState {
}
transpiledCommentAlready = true;
}
//if we should prepare for the next line, add an indent (only if applicable)
if (prepNextLine && transpiledCommentAlready) {
leadingCommentsSourceNodes.push(this.indent());
}
return leadingCommentsSourceNodes;
}

Expand Down

0 comments on commit d2d227c

Please sign in to comment.