Skip to content

Commit

Permalink
Fix bslib prefix transpile (#1317)
Browse files Browse the repository at this point in the history
* Fix bug in bslib prefix transpiling

* Fix lint issues.
  • Loading branch information
TwitchBronBron authored Oct 3, 2024
1 parent abf8e13 commit cfe558d
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 7 deletions.
28 changes: 28 additions & 0 deletions src/bscPlugin/serialize/BslibInjector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { createSandbox } from 'sinon';
import * as fsExtra from 'fs-extra';
import { Program } from '../../Program';
import { tempDir, rootDir } from '../../testHelpers.spec';
import { BslibManager } from './BslibManager';
import { expect } from 'chai';
const sinon = createSandbox();

describe('BslibInjector', () => {
Expand All @@ -16,4 +18,30 @@ describe('BslibInjector', () => {
sinon.restore();
program.dispose();
});

describe('isBslibPkgPath', () => {
it('works for valid paths', () => {
expect(
BslibManager.isBslibPkgPath('source/bslib.brs')
).to.be.true;
expect(
BslibManager.isBslibPkgPath('source/roku_modules/bslib/bslib.brs')
).to.be.true;
expect(
BslibManager.isBslibPkgPath('source/roku_modules/rokucommunity_bslib/bslib.brs')
).to.be.true;
});

it('works for invalid paths', () => {
expect(
BslibManager.isBslibPkgPath('source/bslib2.brs')
).to.be.false;
expect(
BslibManager.isBslibPkgPath('source/roku_modules/1bslib/bslib.brs')
).to.be.false;
expect(
BslibManager.isBslibPkgPath('source/roku_modules/rokucommunity_bslib/3bslib.brs')
).to.be.false;
});
});
});
11 changes: 10 additions & 1 deletion src/bscPlugin/serialize/BslibManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { standardizePath as s } from '../../util';
import { source as bslibSource } from '@rokucommunity/bslib';
import { Cache } from '../../Cache';
import { BrsFile } from '../../files/BrsFile';
import type { FunctionStatement } from '../../parser/Statement';
import type { Editor } from '../../astUtils/Editor';
const bslibSrcPath = s`${require.resolve('@rokucommunity/bslib')}/dist/source/bslib.brs`;
export class BslibManager {

Expand All @@ -28,10 +30,17 @@ export class BslibManager {
}
}

public static applyPrefixIfMissing(statement: FunctionStatement, editor: Editor, prefix: string) {
//add the bslib prefix if the function does not start with bslib_ or rokucommunity_bslib_
if (!/^(rokucommunity_)?bslib_/i.test(statement.tokens.name.text)) {
editor.setProperty(statement.tokens.name, 'text', `${prefix}_${statement.tokens.name.text}`);
}
}

/**
* Is the pkgPath a support path to bslib?
*/
public static isBslibPkgPath(pkgPath: string) {
return /(source[\\\/]bslib.brs)|(source[\\\/]roku_modules[\\\/]bslib[\\\/]bslib.brs)$/i.test(pkgPath);
return /(source[\\\/]bslib.brs)|(source[\\\/]roku_modules[\\\/](rokucommunity_)?bslib)[\\\/]bslib.brs$/i.test(pkgPath);
}
}
47 changes: 47 additions & 0 deletions src/bscPlugin/transpile/BrsFileTranspileProcessor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { Program } from '../../Program';
import { standardizePath as s } from '../../util';
import { tempDir, rootDir } from '../../testHelpers.spec';
import { expect } from 'chai';
import { Parser } from '../../parser/Parser';
import { isFunctionStatement } from '../../astUtils/reflection';
import type { FunctionStatement } from '../../parser/Statement';
const sinon = createSandbox();

describe('BrsFileTranspileProcessor', () => {
Expand Down Expand Up @@ -42,4 +45,48 @@ describe('BrsFileTranspileProcessor', () => {
fsExtra.readFileSync(`${program.options.stagingDir}/source/bslib.brs`).toString()
).to.include('bslib_toString');
});

it('properly prefixes functions from bslib when found in roku_modules as `bslib`', async () => {
program.options.stagingDir = s`${tempDir}/staging`;
program.setFile('source/main.bs', `
sub main()
print true ? true : false
end sub
`);
program.setFile('source/roku_modules/bslib/bslib.brs', `
function bslib_toString()
end function
`);
program.validate();
await program.build();

const parser = Parser.parse(
fsExtra.readFileSync(s`${program.options.stagingDir}/source/roku_modules/bslib/bslib.brs`).toString()
);
expect(
parser.ast.findChildren<FunctionStatement>(isFunctionStatement).map(x => x.tokens.name.text)
).to.include('bslib_toString');
});

it('properly prefixes functions from bslib when found in roku_modules as `rokucommunity_bslib`', async () => {
program.options.stagingDir = s`${tempDir}/staging`;
program.setFile('source/main.bs', `
sub main()
print true ? true : false
end sub
`);
program.setFile('source/roku_modules/rokucommunity_bslib/bslib.brs', `
function rokucommunity_bslib_toString()
end function
`);
program.validate();
await program.build();

const parser = Parser.parse(
fsExtra.readFileSync(s`${program.options.stagingDir}/source/roku_modules/rokucommunity_bslib/bslib.brs`).toString()
);
expect(
parser.ast.findChildren<FunctionStatement>(isFunctionStatement).map(x => x.tokens.name.text)
).to.include('rokucommunity_bslib_toString');
});
});
9 changes: 3 additions & 6 deletions src/bscPlugin/transpile/BrsFileTranspileProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,14 @@ export class BrsFilePreTranspileProcessor {
this.iterateExpressions();
//apply prefixes to bslib
if (BslibManager.isBslibPkgPath(this.event.file.pkgPath)) {
this.applyPrefixesIfMissing(this.event.file, this.event.editor);
this.applyBslibPrefixesIfMissing(this.event.file, this.event.editor);
}
}

public applyPrefixesIfMissing(file: BrsFile, editor: Editor) {
public applyBslibPrefixesIfMissing(file: BrsFile, editor: Editor) {
file.ast.walk(createVisitor({
FunctionStatement: (statement) => {
//add the bslib prefix
if (!statement.tokens.name.text.startsWith('bslib_')) {
editor.setProperty(statement.tokens.name, 'text', `bslib_${statement.tokens.name.text}`);
}
BslibManager.applyPrefixIfMissing(statement, editor, this.event.program.bslibPrefix);
}
}), {
walkMode: WalkMode.visitAllRecursive
Expand Down

0 comments on commit cfe558d

Please sign in to comment.