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

Upgrading to Brighterscript v1 #289

Merged
merged 15 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
466 changes: 430 additions & 36 deletions bsc-plugin/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion bsc-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"@types/node": "^14.18.41",
"@typescript-eslint/eslint-plugin": "^5.27.0",
"@typescript-eslint/parser": "^5.27.0",
"brighterscript": "^0.65.22",
"brighterscript": "^1.0.0-alpha.35",
"chai": "^4.2.0",
"chai-subset": "^1.6.0",
"coveralls": "^3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion bsc-plugin/src/lib/rooibos/Annotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export class RooibosAnnotation {
}
try {
if (rawParams) {
this.params.push(new AnnotationParams(annotation, rawParams, annotation.range.start.line, annotation.getArguments() as any, isIgnore, isSolo, noCatch));
this.params.push(new AnnotationParams(annotation, rawParams, annotation.location.range.start.line, annotation.getArguments() as any, isIgnore, isSolo, noCatch));
} else {
diagnosticIllegalParams(file, annotation);
}
Expand Down
4 changes: 2 additions & 2 deletions bsc-plugin/src/lib/rooibos/CodeCoverageProcessor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ describe('RooibosPlugin', () => {
builder.plugins = new PluginInterface([plugin], { logger: builder.logger });
program.plugins = new PluginInterface([plugin], { logger: builder.logger });
program.createSourceScope(); //ensure source scope is created
plugin.beforeProgramCreate(builder);
plugin.beforeProgramCreate({ builder: builder });

});
afterEach(() => {
plugin.afterProgramCreate(program);
plugin.afterProgramCreate({ builder: builder, program: program });
fsExtra.ensureDirSync(tmpPath);
fsExtra.emptyDirSync(tmpPath);
builder.dispose();
Expand Down
29 changes: 15 additions & 14 deletions bsc-plugin/src/lib/rooibos/CodeCoverageProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,29 +79,30 @@ export class CodeCoverageProcessor {
file.ast.walk(createVisitor({
ForStatement: (ds, parent, owner, key) => {
this.addStatement(ds);
ds.forToken.text = `${this.getFuncCallText(ds.range.start.line, CodeCoverageLineType.code)}: for`;
ds.tokens.for.text = `${this.getFuncCallText(ds.location.range.start.line, CodeCoverageLineType.code)}: for`;
},
IfStatement: (ifStatement, parent, owner, key) => {
this.addStatement(ifStatement);
(ifStatement as any).condition = new BinaryExpression(
new RawCodeExpression(this.getFuncCallText(ifStatement.condition.range.start.line, CodeCoverageLineType.condition)),
createToken(TokenKind.And),
new GroupingExpression({
left: createToken(TokenKind.LeftParen),
right: createToken(TokenKind.RightParen)
}, ifStatement.condition)
);
(ifStatement as any).condition = new BinaryExpression({
left: new RawCodeExpression(this.getFuncCallText(ifStatement.condition.location.range.start.line, CodeCoverageLineType.condition)),
operator: createToken(TokenKind.And),
right: new GroupingExpression({
leftParen: createToken(TokenKind.LeftParen),
rightParen: createToken(TokenKind.RightParen),
expression: ifStatement.condition
})
});

let blockStatements = ifStatement?.thenBranch?.statements;
if (blockStatements) {
let coverageStatement = new RawCodeStatement(this.getFuncCallText(ifStatement.range.start.line, CodeCoverageLineType.branch));
let coverageStatement = new RawCodeStatement(this.getFuncCallText(ifStatement.location.range.start.line, CodeCoverageLineType.branch));
blockStatements.splice(0, 0, coverageStatement);
}

// Handle the else blocks
let elseBlock = ifStatement.elseBranch;
if (isBlock(elseBlock) && elseBlock.statements) {
let coverageStatement = new RawCodeStatement(this.getFuncCallText(elseBlock.range.start.line - 1, CodeCoverageLineType.branch));
let coverageStatement = new RawCodeStatement(this.getFuncCallText(elseBlock.location.range.start.line - 1, CodeCoverageLineType.branch));
elseBlock.statements.splice(0, 0, coverageStatement);
}

Expand All @@ -112,15 +113,15 @@ export class CodeCoverageProcessor {

},
WhileStatement: (ds, parent, owner, key) => {
ds.tokens.while.text = `${this.getFuncCallText(ds.range.start.line, CodeCoverageLineType.code)}: while`;
ds.tokens.while.text = `${this.getFuncCallText(ds.location.range.start.line, CodeCoverageLineType.code)}: while`;
},
ReturnStatement: (ds, parent, owner, key) => {
this.addStatement(ds);
this.convertStatementToCoverageStatement(ds, CodeCoverageLineType.code, owner, key);
},
ForEachStatement: (ds, parent, owner, key) => {
this.addStatement(ds);
ds.tokens.forEach.text = `${this.getFuncCallText(ds.range.start.line, CodeCoverageLineType.code)}: for each`;
ds.tokens.forEach.text = `${this.getFuncCallText(ds.location.range.start.line, CodeCoverageLineType.code)}: for each`;
},
ExitWhileStatement: (ds, parent, owner, key) => {

Expand Down Expand Up @@ -171,7 +172,7 @@ export class CodeCoverageProcessor {
return;
}

const lineNumber = statement.range.start.line;
const lineNumber = statement.location.range.start.line;
this.coverageMap.set(lineNumber, coverageType);
const parsed = Parser.parse(this.getFuncCallText(lineNumber, coverageType)).ast.statements[0] as ExpressionStatement;
this.astEditor.arraySplice(owner, key, 0, parsed);
Expand Down
2 changes: 1 addition & 1 deletion bsc-plugin/src/lib/rooibos/FileFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export class FileFactory {
public addFileToRootDir(program: Program, filePath: string, contents: string) {
try {
fse.outputFileSync(
path.join(program.options.stagingFolderPath ?? program.options.stagingDir ?? program.options.sourceRoot, filePath),
path.join(program.options.stagingDir ?? program.options.sourceRoot, filePath),
contents
);
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions bsc-plugin/src/lib/rooibos/MockUtil.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ describe('MockUtil', () => {
builder.plugins = new PluginInterface([plugin], { logger: builder.logger });
program.plugins = new PluginInterface([plugin], { logger: builder.logger });
program.createSourceScope(); //ensure source scope is created
plugin.beforeProgramCreate(builder);
plugin.beforeProgramCreate({ builder: builder });

});
afterEach(() => {
plugin.afterProgramCreate(program);
plugin.afterProgramCreate({ program: program, builder: builder });
fsExtra.ensureDirSync(tmpPath);
fsExtra.emptyDirSync(tmpPath);
builder.dispose();
Expand Down
21 changes: 11 additions & 10 deletions bsc-plugin/src/lib/rooibos/MockUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { RooibosSession } from './RooibosSession';
import { diagnosticErrorProcessingFile } from '../utils/Diagnostics';
import type { TestCase } from './TestCase';
import type { TestSuite } from './TestSuite';
import { functionRequiresReturnValue, getAllDottedGetParts, getScopeForSuite } from './Utils';
import { functionRequiresReturnValue, getAllDottedGetParts, getFileLookups, getScopeForSuite } from './Utils';

export class MockUtil {

Expand Down Expand Up @@ -50,7 +50,7 @@ export class MockUtil {
this.processedStatements = new Set<brighterscript.FunctionStatement>();
this.astEditor = astEditor;
// console.log('processing global methods on ', file.pkgPath);
for (let functionStatement of file.parser.references.functionStatements) {
for (let functionStatement of getFileLookups(file).functionStatements) {
this.enableMockOnFunction(file, functionStatement);
}

Expand Down Expand Up @@ -94,10 +94,11 @@ export class MockUtil {
// TODO check if the user has actually mocked or stubbed this function, otherwise leave it alone!

for (let param of functionStatement.func.parameters) {
param.asToken = null;
// WHY ?
// param.tokens.as = null;
}

const paramNames = functionStatement.func.parameters.map((param) => param.name.text).join(',');
const paramNames = functionStatement.func.parameters.map((param) => param.tokens.name.text).join(',');
const requiresReturnValue = functionRequiresReturnValue(functionStatement);
const globalAaName = '__stubs_globalAa';
const resultName = '__stubOrMockResult';
Expand Down Expand Up @@ -141,21 +142,21 @@ export class MockUtil {
}
private gatherMockedGlobalMethods(testSuite: TestSuite, testCase: TestCase) {
try {
let func = testSuite.classStatement.methods.find((m) => m.name.text.toLowerCase() === testCase.funcName.toLowerCase());
let func = testSuite.classStatement.methods.find((m) => m.tokens.name.text.toLowerCase() === testCase.funcName.toLowerCase());
func.walk(brighterscript.createVisitor({
ExpressionStatement: (expressionStatement, parent, owner) => {
let callExpression = expressionStatement.expression as brighterscript.CallExpression;
if (brighterscript.isCallExpression(callExpression) && brighterscript.isDottedGetExpression(callExpression.callee)) {
let dge = callExpression.callee;
let assertRegex = /(?:fail|assert(?:[a-z0-9]*)|expect(?:[a-z0-9]*)|stubCall)/i;
if (dge && assertRegex.test(dge.name.text)) {
if (dge.name.text === 'stubCall') {
if (dge && assertRegex.test(dge.tokens.name.text)) {
if (dge.tokens.name.text === 'stubCall') {
this.processGlobalStubbedMethod(callExpression, testSuite);
return expressionStatement;

} else {

if (dge.name.text === 'expectCalled' || dge.name.text === 'expectNotCalled') {
if (dge.tokens.name.text === 'expectCalled' || dge.tokens.name.text === 'expectNotCalled') {
this.processGlobalStubbedMethod(callExpression, testSuite);
}
}
Expand All @@ -178,7 +179,7 @@ export class MockUtil {
const scope = getScopeForSuite(testSuite);
const namespaceLookup = this.session.namespaceLookup;
if (brighterscript.isDottedGetExpression(callExpression.callee)) {
const nameText = callExpression.callee.name.text;
const nameText = callExpression.callee.tokens.name.text;
isNotCalled = nameText === 'expectNotCalled';
isStubCall = nameText === 'stubCall';
}
Expand Down Expand Up @@ -214,7 +215,7 @@ export class MockUtil {
}
} else if (brighterscript.isVariableExpression(expression)) {
let functionName = expression.getName(ParseMode.BrightScript);
if (scope.symbolTable.hasSymbol(functionName)) {
if (scope.symbolTable.hasSymbol(functionName, brighterscript.SymbolTypeFlag.runtime)) {
result = functionName;
}

Expand Down
8 changes: 7 additions & 1 deletion bsc-plugin/src/lib/rooibos/RawCodeExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ export class RawCodeExpression extends Expression {
super();
}

readonly kind = 'RawCodeExpression' as brighterscript.AstNodeKind;

get location() {
return brighterscript.util.createLocationFromFileRange(this.sourceFile, this.range);
}

public transpile(state: BrsTranspileState) {
return [new SourceNode(
this.range.start.line + 1,
this.range.start.character,
this.sourceFile ? this.sourceFile.pathAbsolute : state.srcPath,
this.sourceFile ? this.sourceFile.srcPath : state.srcPath,
this.source
)];
}
Expand Down
13 changes: 11 additions & 2 deletions bsc-plugin/src/lib/rooibos/RawCodeStatement.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type {
AstNodeKind,
BscFile,
WalkOptions,
WalkVisitor
} from 'brighterscript';
import {
Range,
Statement
Statement,
util
} from 'brighterscript';

import { SourceNode } from 'source-map';
Expand All @@ -21,6 +23,13 @@ export class RawCodeStatement extends Statement {
super();
}


readonly kind = 'RawCodeExpression' as AstNodeKind;

get location() {
return util.createLocationFromFileRange(this.sourceFile, this.range);
}

public transpile(state: BrsTranspileState) {
//indent every line with the current transpile indent level (except the first line, because that's pre-indented by bsc)
let source = this.source.replace(/\r?\n/g, (match, newline) => {
Expand All @@ -30,7 +39,7 @@ export class RawCodeStatement extends Statement {
return [new SourceNode(
this.range.start.line + 1,
this.range.start.character,
this.sourceFile ? this.sourceFile.pathAbsolute : state.srcPath,
this.sourceFile ? this.sourceFile.srcPath : state.srcPath,
source
)];
}
Expand Down
Loading