Skip to content

Commit

Permalink
Merge branch 'release-1.0.0' into location-tracking-with-uri
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron authored Jun 14, 2024
2 parents ee1f220 + dc36189 commit 91ff76e
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 123 deletions.
2 changes: 1 addition & 1 deletion src/astUtils/visitors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1242,7 +1242,7 @@ describe('astUtils visitors', () => {

file.ast.walk(createVisitor({
AstNode: (node) => {
const endNodeComments = node.getEndTrivia().filter(t => t.kind === TokenKind.Comment);
const endNodeComments = node.endTrivia.filter(t => t.kind === TokenKind.Comment);
comments.push(...endNodeComments);
}
}), {
Expand Down
51 changes: 51 additions & 0 deletions src/files/BrsFile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { DiagnosticMessages } from '../DiagnosticMessages';
import util, { standardizePath as s } from '../util';
import { expectDiagnostics, expectHasDiagnostics, expectTypeToBe, expectZeroDiagnostics, getTestGetTypedef, getTestTranspile, trim, trimMap } from '../testHelpers.spec';
import { ParseMode, Parser } from '../parser/Parser';
import type { FunctionStatement } from '../parser/Statement';
import { ImportStatement } from '../parser/Statement';
import { createToken, createVariableExpression } from '../astUtils/creators';
import * as fsExtra from 'fs-extra';
Expand Down Expand Up @@ -3335,6 +3336,56 @@ describe('BrsFile', () => {
`);
});
});

describe('leading and end trivia', () => {
it('transpiles leading/end trivia', async () => {
await testTranspile(`
'comment before function
sub test(x) 'comment end of line
'comment at start of function
print x 'comment at end of line
'comment at end of function
end sub
'comment after function
`);
});

it('allows editing leading trivia', async () => {
const mainFile = program.setFile<BrsFile>('source/amain.bs', `
sub test(x)
print x
end sub
`);
program.validate();
const funcStmt = mainFile.ast.statements[0] as FunctionStatement;
funcStmt.leadingTrivia.push(createToken(TokenKind.Comment, `'comment before function`), createToken(TokenKind.Newline));
funcStmt.func.body.statements[0].leadingTrivia.unshift(createToken(TokenKind.Comment, `'comment after func declaration`));

funcStmt.func.body.statements[0].leadingTrivia.push(
createToken(TokenKind.Comment, `'comment at start of function`),
createToken(TokenKind.Newline)
);

funcStmt.func.endTrivia.push(
createToken(TokenKind.Comment, `'comment at end of function`),
createToken(TokenKind.Newline)
);

mainFile.parser.eofToken.leadingTrivia.push(createToken(TokenKind.Comment, `'comment at end of file`));


await testTranspile(mainFile, `
'comment before function
sub test(x)
'comment after func declaration
'comment at start of function
print x
'comment at end of function
end sub
'comment at end of file
`);
});
});
});

describe('callfunc operator', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/parser/AstNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,15 @@ export abstract class AstNode {
* Gets all the trivia (comments, whitespace) that is directly before the start of this node
* Note: this includes all trivia that might start on the line of the previous node
*/
public getLeadingTrivia(): Token[] {
public get leadingTrivia(): Token[] {
return [];
}

/**
* Gets any trivia that is directly before the end of the node
* For example, this would return all trivia before a `end function` token of a FunctionExpression
*/
public getEndTrivia(): Token[] {
public get endTrivia(): Token[] {
return [];
}

Expand Down
64 changes: 32 additions & 32 deletions src/parser/Expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ export class BinaryExpression extends Expression {
return DynamicType.instance;
}

getLeadingTrivia(): Token[] {
return this.left.getLeadingTrivia();
get leadingTrivia(): Token[] {
return this.left.leadingTrivia;
}
}

Expand Down Expand Up @@ -198,8 +198,8 @@ export class CallExpression extends Expression {
return new TypePropertyReferenceType(calleeType, 'returnType');
}

getLeadingTrivia(): Token[] {
return this.callee.getLeadingTrivia();
get leadingTrivia(): Token[] {
return this.callee.leadingTrivia;
}
}

Expand Down Expand Up @@ -256,11 +256,11 @@ export class FunctionExpression extends Expression implements TypedefProvider {
*/
public functionStatement?: FunctionStatement;

public getLeadingTrivia(): Token[] {
public get leadingTrivia(): Token[] {
return this.tokens.functionType?.leadingTrivia;
}

public getEndTrivia(): Token[] {
public get endTrivia(): Token[] {
return this.tokens.endFunctionType?.leadingTrivia;
}

Expand Down Expand Up @@ -515,7 +515,7 @@ export class FunctionParameterExpression extends Expression {
}
}

getLeadingTrivia(): Token[] {
get leadingTrivia(): Token[] {
return this.tokens.name.leadingTrivia;
}
}
Expand Down Expand Up @@ -600,8 +600,8 @@ export class DottedGetExpression extends Expression {
return util.getAllDottedGetPartsAsString(this, parseMode);
}

getLeadingTrivia(): Token[] {
return this.obj.getLeadingTrivia();
get leadingTrivia(): Token[] {
return this.obj.leadingTrivia;
}

}
Expand Down Expand Up @@ -646,8 +646,8 @@ export class XmlAttributeGetExpression extends Expression {
}
}

getLeadingTrivia(): Token[] {
return this.obj.getLeadingTrivia();
get leadingTrivia(): Token[] {
return this.obj.leadingTrivia;
}
}

Expand Down Expand Up @@ -728,8 +728,8 @@ export class IndexedGetExpression extends Expression {
return super.getType(options);
}

getLeadingTrivia(): Token[] {
return this.obj.getLeadingTrivia();
get leadingTrivia(): Token[] {
return this.obj.leadingTrivia;
}
}

Expand Down Expand Up @@ -779,7 +779,7 @@ export class GroupingExpression extends Expression {
return this.expression.getType(options);
}

getLeadingTrivia(): Token[] {
get leadingTrivia(): Token[] {
return this.tokens.leftParen?.leadingTrivia;
}
}
Expand Down Expand Up @@ -833,7 +833,7 @@ export class LiteralExpression extends Expression {
//nothing to walk
}

getLeadingTrivia(): Token[] {
get leadingTrivia(): Token[] {
return this.tokens.value.leadingTrivia;
}
}
Expand Down Expand Up @@ -938,11 +938,11 @@ export class ArrayLiteralExpression extends Expression {
const innerTypes = this.elements.map(expr => expr.getType(options));
return new ArrayType(...innerTypes);
}
getLeadingTrivia(): Token[] {
get leadingTrivia(): Token[] {
return this.tokens.open?.leadingTrivia;
}

getEndTrivia(): Token[] {
get endTrivia(): Token[] {
return this.tokens.close?.leadingTrivia;
}
}
Expand Down Expand Up @@ -991,7 +991,7 @@ export class AAMemberExpression extends Expression {
return this.value.getType(options);
}

getLeadingTrivia(): Token[] {
get leadingTrivia(): Token[] {
return this.tokens.key.leadingTrivia;
}

Expand Down Expand Up @@ -1091,11 +1091,11 @@ export class AALiteralExpression extends Expression {
return resultType;
}

public getLeadingTrivia(): Token[] {
public get leadingTrivia(): Token[] {
return this.tokens.open?.leadingTrivia;
}

public getEndTrivia(): Token[] {
public get endTrivia(): Token[] {
return this.tokens.close?.leadingTrivia;
}

Expand Down Expand Up @@ -1150,7 +1150,7 @@ export class UnaryExpression extends Expression {
return util.unaryOperatorResultType(this.tokens.operator, this.right.getType(options));
}

public getLeadingTrivia(): Token[] {
public get leadingTrivia(): Token[] {
return this.tokens.operator.leadingTrivia;
}
}
Expand Down Expand Up @@ -1221,7 +1221,7 @@ export class VariableExpression extends Expression {
return resultType;
}

getLeadingTrivia(): Token[] {
get leadingTrivia(): Token[] {
return this.tokens.name.leadingTrivia;
}
}
Expand Down Expand Up @@ -1337,7 +1337,7 @@ export class SourceLiteralExpression extends Expression {
//nothing to walk
}

getLeadingTrivia(): Token[] {
get leadingTrivia(): Token[] {
return this.tokens.value.leadingTrivia;
}
}
Expand Down Expand Up @@ -1407,7 +1407,7 @@ export class NewExpression extends Expression {
return result;
}

getLeadingTrivia(): Token[] {
get leadingTrivia(): Token[] {
return this.tokens.new.leadingTrivia;
}
}
Expand Down Expand Up @@ -1527,8 +1527,8 @@ export class CallfuncExpression extends Expression {
return result;
}

getLeadingTrivia(): Token[] {
return this.callee.getLeadingTrivia();
get leadingTrivia(): Token[] {
return this.callee.leadingTrivia;
}
}

Expand Down Expand Up @@ -1823,7 +1823,7 @@ export class AnnotationExpression extends Expression {
return this.call.args.map(e => expressionToValue(e, strict));
}

public getLeadingTrivia(): Token[] {
public get leadingTrivia(): Token[] {
return this.tokens.at?.leadingTrivia;
}

Expand Down Expand Up @@ -1949,8 +1949,8 @@ export class TernaryExpression extends Expression {
}
}

getLeadingTrivia(): Token[] {
return this.test.getLeadingTrivia();
get leadingTrivia(): Token[] {
return this.test.leadingTrivia;
}
}

Expand Down Expand Up @@ -2051,8 +2051,8 @@ export class NullCoalescingExpression extends Expression {
}
}

getLeadingTrivia(): Token[] {
return this.consequent.getLeadingTrivia();
get leadingTrivia(): Token[] {
return this.consequent.leadingTrivia;
}
}

Expand Down Expand Up @@ -2104,7 +2104,7 @@ export class RegexLiteralExpression extends Expression {
//nothing to walk
}

getLeadingTrivia(): Token[] {
get leadingTrivia(): Token[] {
return this.tokens.regexLiteral?.leadingTrivia;
}
}
Expand Down
Loading

0 comments on commit 91ff76e

Please sign in to comment.