Skip to content

Commit

Permalink
Use AstEditor for all transpile modifications (#159)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
georgejecook and TwitchBronBron authored May 21, 2022
1 parent af32a65 commit f2047a7
Show file tree
Hide file tree
Showing 23 changed files with 1,907 additions and 355 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
- run: cd bsc-plugin && npm i
- run: cd bsc-plugin && npm run preversion
- run: cd bsc-plugin && npm run publish-coverage
- run: cd tests && npm i && npm run build
npm-release:
#only run this task if a tag starting with 'v' was used to trigger this (i.e. a tagged release)
if: startsWith(github.ref, 'refs/tags/v')
Expand Down
3 changes: 1 addition & 2 deletions bsc-plugin/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"configurations": [
{
"name": "Run tests",
"type": "node",
"type": "pwa-node",
"request": "launch",
"smartStep": false,
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
Expand All @@ -17,7 +17,6 @@
"<node_internals>/**/*.js"
],
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"internalConsoleOptions": "openOnSessionStart"
}
]
Expand Down
4 changes: 2 additions & 2 deletions bsc-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@types/node": "^17.0.29",
"@typescript-eslint/eslint-plugin": "^4.4.1",
"@typescript-eslint/parser": "^4.4.1",
"brighterscript": "^0.49.0",
"brighterscript": "^0.50.1",
"chai": "^4.2.0",
"chai-subset": "^1.6.0",
"coveralls": "^3.0.0",
Expand All @@ -43,7 +43,7 @@
"undent": "^0.1.0"
},
"peerDependencies": {
"brighterscript": "^0.49.0"
"brighterscript": "^0.50.1"
},
"scripts": {
"preversion": "npm run build && npm run lint && npm run test",
Expand Down
92 changes: 48 additions & 44 deletions bsc-plugin/src/lib/rooibos/FileFactory.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import type { BrsFile, FileObj, Program, XmlFile } from 'brighterscript';

import type { BrsFile, Program, XmlFile } from 'brighterscript';
import { standardizePath as s } from 'brighterscript';
import * as path from 'path';
import * as fs from 'fs';

export class FileFactory {

constructor(options: any) {
if (options.frameworkSourcePath) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
this.sourcePath = options.frameworkSourcePath;
constructor(
private options?: {
frameworkSourcePath?: string;
}
) {
this.options = this.options ?? {};
if (!this.options.frameworkSourcePath) {
if (__filename.endsWith('.ts')) {
//load the files directly from their source location. (i.e. the plugin is running as a typescript file from within ts-node)
this.options.frameworkSourcePath = s`${__dirname}/../../../../framework/src/source`;
} else {
//load the framework files from the dist folder (i.e. the plugin is running as a node_module)
this.options.frameworkSourcePath = s`${__dirname}/../framework`;
}
}
}

Expand All @@ -29,36 +39,29 @@ export class FileFactory {
'Utils'
];

public sourcePath = path.join(__dirname, '../framework');
private targetPath = 'source/rooibos/';
private targetCompsPath = 'components/rooibos/';
public addedFrameworkFiles = [];

public getFrameworkFiles(): FileObj[] {
let files: FileObj[] = [];

for (let fileName of this.frameworkFileNames) {
files.push({ src: path.resolve(path.join(this.sourcePath, `${fileName}.bs`)), dest: path.join(this.targetPath, `${fileName}.bs`) });
}
files.push({ src: path.resolve(path.join(this.sourcePath, `RooibosScene.xml`)), dest: path.join(this.targetCompsPath, `RooibosScene.xml`) });
return files;
}

public addFrameworkFiles(program: Program) {
this.addedFrameworkFiles = [];
for (let fileName of this.frameworkFileNames) {
let sourcePath = path.resolve(path.join(this.sourcePath, `${fileName}.bs`));
let sourcePath = path.resolve(path.join(this.options.frameworkSourcePath, `${fileName}.bs`));
let fileContents = fs.readFileSync(sourcePath, 'utf8');
let destPath = path.join(this.targetPath, `${fileName}.bs`);
let entry = { src: sourcePath, dest: destPath };

this.addedFrameworkFiles.push(program.setFile(entry, fileContents));
this.addedFrameworkFiles.push(
program.setFile(entry, fileContents)
);
}

let sourcePath = path.resolve(path.join(this.sourcePath, `RooibosScene.xml`));
let destPath = path.join(this.targetCompsPath, `RooibosScene.xml`);
let entry = { src: sourcePath, dest: destPath };
this.addedFrameworkFiles.push(program.setFile(entry, this.createTestXML('TestsScene', 'Scene')));
let entry = {
src: s`${this.options.frameworkSourcePath}/RooibosScene.xml`,
dest: s`${this.targetCompsPath}/RooibosScene.xml`
};
this.addedFrameworkFiles.push(
program.setFile(entry, this.createTestXML('TestsScene', 'Scene'))
);
}

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

let contents = `<?xml version="1.0" encoding="UTF-8" ?>
<component
name="${name}"
extends="${baseName}">
${scriptImports.join('\n')}
<interface>
<field id="rooibosTestResult" type="assocarray"/>
<field id="testText" type="string" alias="statusLabel.text" />
<field id="failedText" type="string" alias="failedLabel.text" />
<field id="statusColor" type="string" alias="statusBackground.color" />
<function name='Rooibos_CreateTestNode' />
</interface>
<children>
<Rectangle id="statusBackground" color="#444444" width="1920" height="1080" />
<Label id="statusLabel" text='Rooibos is running tests' />
<Label id="failedLabel" text="" translation="[0, 100]" width="1800" wrap="true" maxLines="15"/>
</children>
</component>
`;
<component name="${name}" extends="${baseName}">
${scriptImports.join('\n')}
<interface>
<field id="rooibosTestResult" type="assocarray"/>
<field id="testText" type="string" alias="statusLabel.text" />
<field id="failedText" type="string" alias="failedLabel.text" />
<field id="statusColor" type="string" alias="statusBackground.color" />
<function name='Rooibos_CreateTestNode' />
</interface>
<children>
<Rectangle id="statusBackground" color="#444444" width="1920" height="1080" />
<Label id="statusLabel" text='Rooibos is running tests' />
<Label id="failedLabel" text="" translation="[0, 100]" width="1800" wrap="true" maxLines="15"/>
</children>
</component>
`;
return contents;
}

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

public addFile(program, projectPath: string, contents: string) {
try {
const file = program.setFile({ src: path.resolve(projectPath), dest: projectPath }, contents);
const file = program.setFile({
src: path.resolve(projectPath),
dest: projectPath
}, contents);
this.addedFrameworkFiles.push(file);
return file;
} catch (error) {
Expand Down
22 changes: 16 additions & 6 deletions bsc-plugin/src/lib/rooibos/RawCodeStatement.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import type { BscFile,
import type {
BscFile,
WalkOptions,
WalkVisitor } from 'brighterscript';
import { Range,
Statement } from 'brighterscript';
import type { BrsTranspileState } from 'brighterscript/dist/parser/BrsTranspileState';
WalkVisitor
} from 'brighterscript';
import {
Range,
Statement
} from 'brighterscript';

import { SourceNode } from 'source-map';

import type { BrsTranspileState } from 'brighterscript/dist/parser/BrsTranspileState';

export class RawCodeStatement extends Statement {
constructor(
public source: string,
Expand All @@ -17,11 +22,16 @@ export class RawCodeStatement extends Statement {
}

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) => {
return state.newline + state.indent();
});

return [new SourceNode(
this.range.start.line + 1,
this.range.start.character,
this.sourceFile ? this.sourceFile.pathAbsolute : state.srcPath,
this.source
source
)];
}
public walk(visitor: WalkVisitor, options: WalkOptions) {
Expand Down
5 changes: 5 additions & 0 deletions bsc-plugin/src/lib/rooibos/RooibosConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ export interface RooibosConfig {
tags?: string[];
catchCrashes?: boolean;
sendHomeOnFinish?: boolean;
/**
* The path to the folder where the rooibos framework roku files reside.
* @default `dist/lib/framework`
*/
frameworkSourcePath?: string;
}
Loading

0 comments on commit f2047a7

Please sign in to comment.