-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactor XML AST * Add findLastIndex util function. * Refactor names of certain nodes * fix some names * Rename other elements * Remove benchmarking log
- Loading branch information
1 parent
ae8afa5
commit 57455f6
Showing
19 changed files
with
1,249 additions
and
606 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,26 @@ | ||
import type { SGChildren, SGComponent, SGField, SGFunction, SGInterface, SGNode, SGScript, SGTag } from '../parser/SGTypes'; | ||
import type { SGChildren, SGComponent, SGInterfaceField, SGInterfaceFunction, SGInterface, SGNode, SGScript, SGElement } from '../parser/SGTypes'; | ||
|
||
export function isSGComponent(tag: SGTag): tag is SGComponent { | ||
export function isSGComponent(tag: SGElement): tag is SGComponent { | ||
return tag?.constructor.name === 'SGComponent'; | ||
} | ||
export function isSGInterface(tag: SGTag): tag is SGInterface { | ||
export function isSGInterface(tag: SGElement): tag is SGInterface { | ||
return tag?.constructor.name === 'SGInterface'; | ||
} | ||
export function isSGScript(tag: SGTag): tag is SGScript { | ||
export function isSGScript(tag: SGElement): tag is SGScript { | ||
return tag?.constructor.name === 'SGScript'; | ||
} | ||
export function isSGChildren(tag: SGTag): tag is SGChildren { | ||
export function isSGChildren(tag: SGElement): tag is SGChildren { | ||
return tag?.constructor.name === 'SGChildren'; | ||
} | ||
export function isSGField(tag: SGTag): tag is SGField { | ||
export function isSGInterfaceField(tag: SGElement): tag is SGInterfaceField { | ||
return tag?.constructor.name === 'SGField'; | ||
} | ||
export function isSGFunction(tag: SGTag): tag is SGFunction { | ||
export function isSGInterfaceFunction(tag: SGElement): tag is SGInterfaceFunction { | ||
return tag?.constructor.name === 'SGFunction'; | ||
} | ||
export function isSGNode(tag: SGTag): tag is SGNode { | ||
export function isSGNode(tag: SGElement): tag is SGNode { | ||
return tag?.constructor.name === 'SGNode'; | ||
} | ||
export function isSGCustomization(tag: SGTag): tag is SGNode { | ||
return isSGNode(tag) && tag.tag?.text?.toLowerCase() === 'customization'; | ||
export function isSGCustomization(tag: SGElement): tag is SGNode { | ||
return isSGNode(tag) && tag.tokens.startTagName?.text?.toLowerCase() === 'customization'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { createSGScript } from '../../astUtils/creators'; | ||
import { isXmlFile } from '../../astUtils/reflection'; | ||
import type { XmlFile } from '../../files/XmlFile'; | ||
import type { BeforeFileTranspileEvent } from '../../interfaces'; | ||
import util from '../../util'; | ||
|
||
export class XmlFilePreTranspileProcessor { | ||
public constructor( | ||
private event: BeforeFileTranspileEvent<XmlFile> | ||
) { | ||
} | ||
|
||
public process() { | ||
if (!isXmlFile(this.event.file)) { | ||
return; | ||
} | ||
|
||
//insert any missing script imports | ||
this.injectScriptImports(); | ||
|
||
//transform any brighterscript `type` attributes to `brightscript` | ||
for (const script of this.event.file.ast?.componentElement?.scriptElements ?? []) { | ||
const type = script.getAttribute('type'); | ||
if (/text\/brighterscript/i.test(type?.value)) { | ||
this.event.editor.setProperty( | ||
type, | ||
'value', | ||
type.value.replace(/text\/brighterscript/i, 'text/brightscript') | ||
); | ||
} | ||
|
||
//replace `.bs` extensions with `.brs` | ||
const uri = script.getAttribute('uri'); | ||
if (/\.bs/i.test(uri?.value)) { | ||
this.event.editor.setProperty( | ||
uri, | ||
'value', | ||
uri.value.replace(/\.bs/i, '.brs') | ||
); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Inject any missing scripts into the xml file | ||
*/ | ||
private injectScriptImports() { | ||
// eslint-disable-next-line @typescript-eslint/dot-notation | ||
const extraImportScripts = this.event.file['getMissingImportsForTranspile']().map(uri => { | ||
return createSGScript({ | ||
type: 'text/brightscript', | ||
uri: util.sanitizePkgPath(uri.replace(/\.bs$/, '.brs')) | ||
}); | ||
}); | ||
|
||
if (extraImportScripts) { | ||
//add new scripts after the LAST `<script>` tag that was created explicitly by the user, or at the top of the component if it has no scripts | ||
let lastScriptIndex = util.findLastIndex(this.event.file.ast.componentElement.elements, x => x.tokens.startTagName.text.toLowerCase() === 'script'); | ||
lastScriptIndex = lastScriptIndex >= 0 | ||
? lastScriptIndex + 1 | ||
: 0; | ||
|
||
this.event.editor.arraySplice(this.event.file.ast.componentElement.elements, lastScriptIndex, 0, ...extraImportScripts); | ||
} | ||
} | ||
} |
Oops, something went wrong.