Skip to content

Commit

Permalink
Merge pull request #10 from rokucommunity/better-init-formatting
Browse files Browse the repository at this point in the history
Fix some formatting for the `init()` function in new files
  • Loading branch information
TwitchBronBron authored Jan 9, 2025
2 parents 32c1f79 + c7891a4 commit 2e08fd4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
44 changes: 31 additions & 13 deletions src/findNodes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,8 @@ describe('findnode', () => {
await program.transpile([], stagingDir);

expect(
undent(
fsExtra.readFileSync(s`${stagingDir}/components/ZombieKeyboard-findnode.brs`).toString()
)
).to.equal(undent`
sub init()
m.helloZombieText = m.top.findNode("helloZombieText")
end sub
`);
fsExtra.readFileSync(s`${stagingDir}/components/ZombieKeyboard-findnode.brs`).toString()
).to.equal(`sub init()\n m.helloZombieText = m.top.findNode("helloZombieText")\nend sub`);

//make sure the import to this new file is present in the xml file
expect(
Expand All @@ -181,7 +175,7 @@ describe('findnode', () => {
`);
});

it('it works when no init was found, but codebehind file doesbs file is present', async () => {
it('it works when no init was found', async () => {
program.setFile('components/ZombieKeyboard.xml', `
<component name="ZombieKeyboard" extends="group">
<children>
Expand Down Expand Up @@ -235,7 +229,7 @@ describe('findnode', () => {
await program.transpile([], stagingDir);

expect(
undent(fsExtra.readFileSync(s`${stagingDir}/components/ZombieKeyboard-findnode.brs`).toString())
fsExtra.readFileSync(s`${stagingDir}/components/ZombieKeyboard-findnode.brs`).toString()
).to.equal(undent`
sub init()
m.helloZombieText = m.top.findNode("helloZombieText")
Expand All @@ -261,10 +255,11 @@ describe('findnode', () => {
await program.transpile([], stagingDir);

expect(
undent(fsExtra.readFileSync(s`${stagingDir}/components/ZombieKeyboard-findnode.brs`).toString())
fsExtra.readFileSync(s`${stagingDir}/components/ZombieKeyboard-findnode.brs`).toString()
).to.equal(undent`'original contents`);

expect(
undent(fsExtra.readFileSync(s`${stagingDir}/components/ZombieKeyboard-findnode-2.brs`).toString())
fsExtra.readFileSync(s`${stagingDir}/components/ZombieKeyboard-findnode-2.brs`).toString()
).to.equal(undent`
sub init()
m.helloZombieText = m.top.findNode("helloZombieText")
Expand All @@ -290,7 +285,7 @@ describe('findnode', () => {
await program.transpile([], stagingDir);

expect(
undent(fsExtra.readFileSync(s`${stagingDir}/components/ZombieKeyboard-findnode.brs`).toString())
fsExtra.readFileSync(s`${stagingDir}/components/ZombieKeyboard-findnode.brs`).toString()
).to.equal(undent`'original contents`);
expect(
undent(fsExtra.readFileSync(s`${stagingDir}/components/ZombieKeyboard-findnode-2.brs`).toString())
Expand Down Expand Up @@ -420,6 +415,29 @@ describe('findnode', () => {
}]);
});


it('it does not warn for findnode IDs NOT in the current xml file', () => {
program.setFile('components/ZombieKeyboard.bs', `
sub init()
m.helloZombieText2 = m.top.findNode("notInMyXml")
end sub
`);

program.setFile('components/ZombieKeyboard.xml', `
<component name="ZombieKeyboard" extends="Group">
<script uri="ZombieKeyboard.bs" />
<children>
<label id="helloZombieText" />
</children>
</component>
`);

program.validate();
expect(
program.getDiagnostics().map(x => ({ message: x.message, range: x.range, relatedInformation: x.relatedInformation }))
).to.eql([]);
});

it('it works when you extend a component and found nodes are declared within their correct component', async () => {
program.setFile('components/BaseKeyboard.xml', `
<component name="BaseKeyboard" extends="group">
Expand Down
12 changes: 4 additions & 8 deletions src/findNodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,10 @@ export function findNodeWithIDInjection(program: Program, entries: TranspileObj[

//build the list of assignments
const assignments = Array.from(ids).map(([id, range]) => {
return `m.${id} = m.top.findNode("${id}")`;
return ` m.${id} = m.top.findNode("${id}")`;
}).join('\n');

const initFunctionText = `
sub init()
${assignments}
end sub
`;
const initFunctionText = `sub init()\n${assignments}\nend sub`;

const initFunctionInfo = findInitFunction(scope);

Expand Down Expand Up @@ -132,8 +128,8 @@ function findInitFunction(scope: Scope): { file: BscFile; initFunction: Function
/**
* Get a pkgPath for a new brs file that will sit next to the given xml file. This is deterministic,
* so if the file already exists, we'll append the next available number number to the end of the filename to make it unique.
* @param file
* @param program
* @param file the xml file that we want to make a new brs file for
* @param program the bsc program (used for file name collision detection)
*/
function getUniqueFilename(file: XmlFile, program: Program) {
let pkgPath = file.pkgPath.replace('.xml', '-findnode');
Expand Down

0 comments on commit 2e08fd4

Please sign in to comment.