Skip to content

Commit

Permalink
Add transpile fix for instantresume components (#465)
Browse files Browse the repository at this point in the history
* Add transpile fix for instantresume components

* Fix bug
  • Loading branch information
TwitchBronBron committed Oct 28, 2021
1 parent 2612b19 commit 199e0ef
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/astUtils/xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ export function isSGFunction(tag: SGTag): tag is SGFunction {
export function isSGNode(tag: SGTag): tag is SGNode {
return tag?.constructor.name === 'SGNode';
}
export function isSGCustomization(tag: SGTag): tag is SGNode {
return isSGNode(tag) && tag.tag?.text?.toLowerCase() === 'customization';
}
37 changes: 37 additions & 0 deletions src/files/XmlFile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { BrsFile } from './BrsFile';
import { XmlFile } from './XmlFile';
import { standardizePath as s } from '../util';
import { expectZeroDiagnostics, getTestTranspile, trim, trimMap } from '../testHelpers.spec';
import { ProgramBuilder } from '../ProgramBuilder';
import { LogLevel } from '../Logger';

describe('XmlFile', () => {
const tempDir = s`${process.cwd()}/.tmp`;
Expand Down Expand Up @@ -624,6 +626,41 @@ describe('XmlFile', () => {
});

describe('transpile', () => {
it('supports instantresume <customization> elements', async () => {
fsExtra.outputFileSync(`${rootDir}/manifest`, '');
fsExtra.outputFileSync(`${rootDir}/source/main.brs`, `sub main()\nend sub`);
fsExtra.outputFileSync(`${rootDir}/components/MainScene.xml`, trim`
<?xml version="1.0" encoding="utf-8" ?>
<component name="MainScene" extends="Scene">
<customization resumehandler="customResume" />
<customization suspendhandler="customSuspend" />
<children>
<Rectangle width="1920" height="1080" />
</children>
</component>
`);
const builder = new ProgramBuilder();
await builder.run({
cwd: rootDir,
retainStagingFolder: true,
stagingFolderPath: stagingDir,
logLevel: LogLevel.off
});
expect(
fsExtra.readFileSync(`${stagingDir}/components/MainScene.xml`).toString()
).to.eql(trim`
<?xml version="1.0" encoding="utf-8" ?>
<component name="MainScene" extends="Scene">
<script type="text/brightscript" uri="pkg:/source/bslib.brs" />
<children>
<Rectangle width="1920" height="1080" />
</children>
<customization resumehandler="customResume" />
<customization suspendhandler="customSuspend" />
</component>
`);
});

it(`honors the 'needsTranspiled' flag when set in 'afterFileParse'`, () => {
program.plugins.add({
name: 'test',
Expand Down
2 changes: 1 addition & 1 deletion src/parser/SGParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ function mapElement({ children }: ElementCstNode, diagnostics: Diagnostic[]): SG
const content = children.content?.[0];
switch (name.text) {
case 'component':
const componentContent = mapElements(content, ['interface', 'script', 'children'], diagnostics);
const componentContent = mapElements(content, ['interface', 'script', 'children', 'customization'], diagnostics);
return new SGComponent(name, attributes, componentContent, range);
case 'interface':
const interfaceContent = mapElements(content, ['field', 'function'], diagnostics);
Expand Down
13 changes: 8 additions & 5 deletions src/parser/SGTypes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SourceNode } from 'source-map';
import type { Range } from 'vscode-languageserver';
import { createSGAttribute } from '../astUtils/creators';
import { isSGChildren, isSGField, isSGFunction, isSGInterface, isSGScript } from '../astUtils/xml';
import { isSGChildren, isSGCustomization, isSGField, isSGFunction, isSGInterface, isSGScript } from '../astUtils/xml';
import type { FileReference, TranspileResult } from '../interfaces';
import util from '../util';
import type { TranspileState } from './TranspileState';
Expand Down Expand Up @@ -296,8 +296,6 @@ export class SGInterface extends SGTag {
this.fields.push(tag);
} else if (isSGFunction(tag)) {
this.functions.push(tag);
} else {
throw new Error(`Unexpected tag ${tag.tag.text}`);
}
}
}
Expand Down Expand Up @@ -366,8 +364,8 @@ export class SGComponent extends SGTag {
this.scripts.push(tag);
} else if (isSGChildren(tag)) {
this.children = tag;
} else {
throw new Error(`Unexpected tag ${tag.tag.text}`);
} else if (isSGCustomization(tag)) {
this.customizations.push(tag);
}
}
}
Expand All @@ -379,6 +377,8 @@ export class SGComponent extends SGTag {

public children: SGChildren;

public customizations: SGNode[] = [];

get name() {
return this.getAttributeValue('name');
}
Expand All @@ -405,6 +405,9 @@ export class SGComponent extends SGTag {
if (this.children) {
body.push(this.children.transpile(state));
}
if (this.customizations.length > 0) {
body.push(...this.customizations.map(node => node.transpile(state)));
}
state.blockDepth--;
body.push(state.indentText, '</', this.tag.text, '>\n');
return body;
Expand Down

0 comments on commit 199e0ef

Please sign in to comment.