Skip to content

Commit f2047a7

Browse files
Use AstEditor for all transpile modifications (#159)
* adds comments indicating ASTEdits * handle addLaunchHook with ast editor * fix lint issues * Remove unused `isFrameworkAdded` property * small FileFactory cleanup * Use `Program.getFile` instead of deprecated func * move testRunnerMetadata to beforeProgramTranspile * remove two ast comments as they don't need changed. * prettier RawCodeStatement output * Fix some AST edits. Add new unit test to validate * More AstEditor changes * use current package.json version in unit tests * Use AstEditor for callfunc transformations * add back default plugin export * Use AstEditor for 'getTestSuiteData' injection * remove already-fixed comment * remove unused util functions related to AST editing * Remove many unused util functions * Remove the "removeCode" method as the AstEditor already handles this * remove unused function * [email protected] fixes `super` transpile in watch mode * wip fix for updateClassLookupFunction bug * set plugin name * Run .ts version of plugin for `tests` instead of pre-compiling. * Fix transpiled code formatting issues. * fix lint issues. * run bsc build for tests in CI * fix tests build * fix framework path when running in ts-node * adds watchers * ignores slow test * adds some useful snippetS * Fixes issue where test suite counts never reset on subsequent launches, meaning if something had an @only on it, then @only rules would apply for all subsequent runs * adds watch task to launch tasks Co-authored-by: Bronley Plumb <[email protected]>
1 parent af32a65 commit f2047a7

23 files changed

+1907
-355
lines changed

.github/workflows/build.yml

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ jobs:
1919
- run: cd bsc-plugin && npm i
2020
- run: cd bsc-plugin && npm run preversion
2121
- run: cd bsc-plugin && npm run publish-coverage
22+
- run: cd tests && npm i && npm run build
2223
npm-release:
2324
#only run this task if a tag starting with 'v' was used to trigger this (i.e. a tagged release)
2425
if: startsWith(github.ref, 'refs/tags/v')

bsc-plugin/.vscode/launch.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"configurations": [
44
{
55
"name": "Run tests",
6-
"type": "node",
6+
"type": "pwa-node",
77
"request": "launch",
88
"smartStep": false,
99
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
@@ -17,7 +17,6 @@
1717
"<node_internals>/**/*.js"
1818
],
1919
"cwd": "${workspaceRoot}",
20-
"protocol": "inspector",
2120
"internalConsoleOptions": "openOnSessionStart"
2221
}
2322
]

bsc-plugin/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"@types/node": "^17.0.29",
2727
"@typescript-eslint/eslint-plugin": "^4.4.1",
2828
"@typescript-eslint/parser": "^4.4.1",
29-
"brighterscript": "^0.49.0",
29+
"brighterscript": "^0.50.1",
3030
"chai": "^4.2.0",
3131
"chai-subset": "^1.6.0",
3232
"coveralls": "^3.0.0",
@@ -43,7 +43,7 @@
4343
"undent": "^0.1.0"
4444
},
4545
"peerDependencies": {
46-
"brighterscript": "^0.49.0"
46+
"brighterscript": "^0.50.1"
4747
},
4848
"scripts": {
4949
"preversion": "npm run build && npm run lint && npm run test",

bsc-plugin/src/lib/rooibos/FileFactory.ts

+48-44
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1-
import type { BrsFile, FileObj, Program, XmlFile } from 'brighterscript';
2-
1+
import type { BrsFile, Program, XmlFile } from 'brighterscript';
2+
import { standardizePath as s } from 'brighterscript';
33
import * as path from 'path';
44
import * as fs from 'fs';
55

66
export class FileFactory {
77

8-
constructor(options: any) {
9-
if (options.frameworkSourcePath) {
10-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
11-
this.sourcePath = options.frameworkSourcePath;
8+
constructor(
9+
private options?: {
10+
frameworkSourcePath?: string;
11+
}
12+
) {
13+
this.options = this.options ?? {};
14+
if (!this.options.frameworkSourcePath) {
15+
if (__filename.endsWith('.ts')) {
16+
//load the files directly from their source location. (i.e. the plugin is running as a typescript file from within ts-node)
17+
this.options.frameworkSourcePath = s`${__dirname}/../../../../framework/src/source`;
18+
} else {
19+
//load the framework files from the dist folder (i.e. the plugin is running as a node_module)
20+
this.options.frameworkSourcePath = s`${__dirname}/../framework`;
21+
}
1222
}
1323
}
1424

@@ -29,36 +39,29 @@ export class FileFactory {
2939
'Utils'
3040
];
3141

32-
public sourcePath = path.join(__dirname, '../framework');
3342
private targetPath = 'source/rooibos/';
3443
private targetCompsPath = 'components/rooibos/';
3544
public addedFrameworkFiles = [];
3645

37-
public getFrameworkFiles(): FileObj[] {
38-
let files: FileObj[] = [];
39-
40-
for (let fileName of this.frameworkFileNames) {
41-
files.push({ src: path.resolve(path.join(this.sourcePath, `${fileName}.bs`)), dest: path.join(this.targetPath, `${fileName}.bs`) });
42-
}
43-
files.push({ src: path.resolve(path.join(this.sourcePath, `RooibosScene.xml`)), dest: path.join(this.targetCompsPath, `RooibosScene.xml`) });
44-
return files;
45-
}
46-
4746
public addFrameworkFiles(program: Program) {
4847
this.addedFrameworkFiles = [];
4948
for (let fileName of this.frameworkFileNames) {
50-
let sourcePath = path.resolve(path.join(this.sourcePath, `${fileName}.bs`));
49+
let sourcePath = path.resolve(path.join(this.options.frameworkSourcePath, `${fileName}.bs`));
5150
let fileContents = fs.readFileSync(sourcePath, 'utf8');
5251
let destPath = path.join(this.targetPath, `${fileName}.bs`);
5352
let entry = { src: sourcePath, dest: destPath };
54-
55-
this.addedFrameworkFiles.push(program.setFile(entry, fileContents));
53+
this.addedFrameworkFiles.push(
54+
program.setFile(entry, fileContents)
55+
);
5656
}
5757

58-
let sourcePath = path.resolve(path.join(this.sourcePath, `RooibosScene.xml`));
59-
let destPath = path.join(this.targetCompsPath, `RooibosScene.xml`);
60-
let entry = { src: sourcePath, dest: destPath };
61-
this.addedFrameworkFiles.push(program.setFile(entry, this.createTestXML('TestsScene', 'Scene')));
58+
let entry = {
59+
src: s`${this.options.frameworkSourcePath}/RooibosScene.xml`,
60+
dest: s`${this.targetCompsPath}/RooibosScene.xml`
61+
};
62+
this.addedFrameworkFiles.push(
63+
program.setFile(entry, this.createTestXML('TestsScene', 'Scene'))
64+
);
6265
}
6366

6467
public createTestXML(name: string, baseName: string, useBs = true): string {
@@ -68,25 +71,23 @@ export class FileFactory {
6871
}
6972

7073
let contents = `<?xml version="1.0" encoding="UTF-8" ?>
71-
<component
72-
name="${name}"
73-
extends="${baseName}">
74-
${scriptImports.join('\n')}
75-
<interface>
76-
<field id="rooibosTestResult" type="assocarray"/>
77-
<field id="testText" type="string" alias="statusLabel.text" />
78-
<field id="failedText" type="string" alias="failedLabel.text" />
79-
<field id="statusColor" type="string" alias="statusBackground.color" />
80-
<function name='Rooibos_CreateTestNode' />
81-
</interface>
82-
83-
<children>
84-
<Rectangle id="statusBackground" color="#444444" width="1920" height="1080" />
85-
<Label id="statusLabel" text='Rooibos is running tests' />
86-
<Label id="failedLabel" text="" translation="[0, 100]" width="1800" wrap="true" maxLines="15"/>
87-
</children>
88-
</component>
89-
`;
74+
<component name="${name}" extends="${baseName}">
75+
${scriptImports.join('\n')}
76+
<interface>
77+
<field id="rooibosTestResult" type="assocarray"/>
78+
<field id="testText" type="string" alias="statusLabel.text" />
79+
<field id="failedText" type="string" alias="failedLabel.text" />
80+
<field id="statusColor" type="string" alias="statusBackground.color" />
81+
<function name='Rooibos_CreateTestNode' />
82+
</interface>
83+
84+
<children>
85+
<Rectangle id="statusBackground" color="#444444" width="1920" height="1080" />
86+
<Label id="statusLabel" text='Rooibos is running tests' />
87+
<Label id="failedLabel" text="" translation="[0, 100]" width="1800" wrap="true" maxLines="15"/>
88+
</children>
89+
</component>
90+
`;
9091
return contents;
9192
}
9293

@@ -101,7 +102,10 @@ ${scriptImports.join('\n')}
101102

102103
public addFile(program, projectPath: string, contents: string) {
103104
try {
104-
const file = program.setFile({ src: path.resolve(projectPath), dest: projectPath }, contents);
105+
const file = program.setFile({
106+
src: path.resolve(projectPath),
107+
dest: projectPath
108+
}, contents);
105109
this.addedFrameworkFiles.push(file);
106110
return file;
107111
} catch (error) {

bsc-plugin/src/lib/rooibos/RawCodeStatement.ts

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
import type { BscFile,
1+
import type {
2+
BscFile,
23
WalkOptions,
3-
WalkVisitor } from 'brighterscript';
4-
import { Range,
5-
Statement } from 'brighterscript';
6-
import type { BrsTranspileState } from 'brighterscript/dist/parser/BrsTranspileState';
4+
WalkVisitor
5+
} from 'brighterscript';
6+
import {
7+
Range,
8+
Statement
9+
} from 'brighterscript';
710

811
import { SourceNode } from 'source-map';
912

13+
import type { BrsTranspileState } from 'brighterscript/dist/parser/BrsTranspileState';
14+
1015
export class RawCodeStatement extends Statement {
1116
constructor(
1217
public source: string,
@@ -17,11 +22,16 @@ export class RawCodeStatement extends Statement {
1722
}
1823

1924
public transpile(state: BrsTranspileState) {
25+
//indent every line with the current transpile indent level (except the first line, because that's pre-indented by bsc)
26+
let source = this.source.replace(/\r?\n/g, (match, newline) => {
27+
return state.newline + state.indent();
28+
});
29+
2030
return [new SourceNode(
2131
this.range.start.line + 1,
2232
this.range.start.character,
2333
this.sourceFile ? this.sourceFile.pathAbsolute : state.srcPath,
24-
this.source
34+
source
2535
)];
2636
}
2737
public walk(visitor: WalkVisitor, options: WalkOptions) {

bsc-plugin/src/lib/rooibos/RooibosConfig.ts

+5
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,9 @@ export interface RooibosConfig {
2020
tags?: string[];
2121
catchCrashes?: boolean;
2222
sendHomeOnFinish?: boolean;
23+
/**
24+
* The path to the folder where the rooibos framework roku files reside.
25+
* @default `dist/lib/framework`
26+
*/
27+
frameworkSourcePath?: string;
2328
}

0 commit comments

Comments
 (0)