generated from rokucommunity/bsc-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
210 additions
and
78 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,76 @@ | ||
import type { AstEditor, FunctionStatement, BrsFile, BscFile, Identifier, CallExpression, Expression, ReturnStatement, Statement, AssignmentStatement } from 'brighterscript'; | ||
import { isBrsFile, Parser, WalkMode, createVisitor, util as bscUtil, isExpressionStatement, GroupingExpression, createToken, TokenKind, isCommentStatement } from 'brighterscript'; | ||
|
||
import { BrsTranspileState } from 'brighterscript/dist/parser/BrsTranspileState'; | ||
import { SourceNode } from 'source-map'; | ||
|
||
export class FileTranspiler { | ||
public preprocess(file: BscFile, editor: AstEditor, annotatedFunctions: Map<string, FunctionStatement>) { | ||
if (isBrsFile(file)) { | ||
this.walkBscExpressions(file, editor, annotatedFunctions); | ||
} | ||
} | ||
private walkBscExpressions(file: BscFile, editor: AstEditor, annotatedFunctions: Map<string, FunctionStatement>) { | ||
if (isBrsFile(file)) { | ||
file.ast.walk(createVisitor({ | ||
CallExpression: (call) => { | ||
const parts = bscUtil.getAllDottedGetParts(call.callee); | ||
return ( | ||
this.inlineCall(file, editor, call, parts ?? [], annotatedFunctions) | ||
); | ||
} | ||
}), { | ||
walkMode: WalkMode.visitAllRecursive, | ||
editor: editor | ||
}); | ||
} | ||
} | ||
|
||
private inlineCall(file: BrsFile, editor: AstEditor, call: CallExpression, parts: Identifier[], annotatedFunctions: Map<string, FunctionStatement>) { | ||
const fullFunctionName = parts?.map(x => x.text).join('.').toLowerCase(); | ||
const annotatedFunction = annotatedFunctions.get(fullFunctionName); | ||
if (annotatedFunction) { | ||
const parameterMap = new Map<string, Expression>(); | ||
|
||
for (let index = 0; index < annotatedFunction.func.parameters.length; index++) { | ||
const annotatedParameter = annotatedFunction.func.parameters[index]; | ||
const callArgument = call.args[index] ?? annotatedParameter?.defaultValue; | ||
parameterMap.set(annotatedParameter.name.text, callArgument); | ||
} | ||
|
||
const clonedStatement = this.cloneStatement<ReturnStatement>(annotatedFunction.func.body.statements.filter(x => !isCommentStatement(x))[0], file); | ||
clonedStatement.walk(createVisitor({ | ||
VariableExpression: (variable, parent?, owner?, key?) => { | ||
const arg = parameterMap.get(variable.name.text); | ||
if (arg) { | ||
return this.cloneExpression(arg, file); | ||
} | ||
} | ||
}), { | ||
walkMode: WalkMode.visitAllRecursive, | ||
editor: editor | ||
}); | ||
|
||
if (isExpressionStatement(call.parent)) { | ||
return clonedStatement.value; | ||
} else { | ||
return new GroupingExpression({ | ||
left: createToken(TokenKind.LeftParen), | ||
right: createToken(TokenKind.RightParen) | ||
}, clonedStatement.value!); | ||
} | ||
} | ||
} | ||
|
||
private cloneStatement<T>(statement: Statement, file: BrsFile) { | ||
return statement.clone() as T; | ||
} | ||
|
||
private cloneExpression<T = Expression>(expression: Expression, file: BrsFile) { | ||
const newCode = new SourceNode(null, null, null, [ | ||
(expression.transpile(new BrsTranspileState(file)) as any) | ||
]).toString(); | ||
return (Parser.parse(`__thing = ${newCode}`).ast.statements[0] as AssignmentStatement).value as T; | ||
} | ||
} | ||
|
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,40 @@ | ||
import type { BscFile, FunctionStatement } from 'brighterscript'; | ||
import { isCommentStatement } from 'brighterscript'; | ||
import { isReturnStatement } from 'brighterscript'; | ||
import { BsDiagnostic, ParseMode } from 'brighterscript'; | ||
Check failure on line 4 in src/FileValidator.ts GitHub Actions / ci (ubuntu-latest)
|
||
import { AstEditor, createVisitor, isBrsFile, WalkMode } from 'brighterscript'; | ||
import { diagnostics } from './diagnostics'; | ||
|
||
export class FileValidator { | ||
private editor = new AstEditor(); | ||
public annotatedFunctions = new Map<string, FunctionStatement>(); | ||
|
||
public reset() { | ||
this.editor.undoAll(); | ||
this.editor = new AstEditor(); | ||
this.annotatedFunctions.clear(); | ||
} | ||
|
||
public findAnnotations(file: BscFile) { | ||
if (isBrsFile(file)) { | ||
file.ast.walk(createVisitor({ | ||
FunctionStatement: (statement, parent?, owner?, key?) => { | ||
if (statement.annotations?.find(annotation => annotation.name.toLowerCase() === 'inline')) { | ||
this.annotatedFunctions.set(statement.getName(ParseMode.BrighterScript).toLowerCase(), statement); | ||
const bodyStatements = statement.func.body.statements.filter(x => !isCommentStatement(x)); | ||
|
||
if (bodyStatements.length !== 1 || !isReturnStatement(bodyStatements[0])) { | ||
file.addDiagnostic( | ||
diagnostics.badAnnotationBody( | ||
bodyStatements[bodyStatements.length - 1]?.range ?? statement.func.range | ||
) | ||
); | ||
} | ||
} | ||
} | ||
}), { | ||
walkMode: WalkMode.visitStatements | ||
}); | ||
} | ||
} | ||
} |
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,45 +1,24 @@ | ||
import type { BeforeFileTranspileEvent, CompilerPlugin } from 'brighterscript'; | ||
import { Parser, WalkMode, createVisitor, isBrsFile, isXmlFile } from 'brighterscript'; | ||
import SGParser from 'brighterscript/dist/parser/SGParser'; | ||
import { SGChildren } from 'brighterscript/dist/parser/SGTypes'; | ||
import type { AstEditor, CompilerPlugin, Program, TranspileObj } from 'brighterscript'; | ||
import { FileValidator } from './FileValidator'; | ||
import { FileTranspiler } from './FileTranspilers'; | ||
|
||
export class Plugin implements CompilerPlugin { | ||
name = 'bsc-plugin-awesome'; | ||
name = 'bsc-plugin-inline-annotation'; | ||
|
||
beforeFileTranspile(event: BeforeFileTranspileEvent) { | ||
if (isBrsFile(event.file)) { | ||
event.file.ast.walk(createVisitor({ | ||
FunctionStatement: (functionStatement) => { | ||
const printStatement = Parser.parse(`print "hello from ${functionStatement.name.text}"`).statements[0]; | ||
private fileValidator = new FileValidator(); | ||
private fileTranspiler = new FileTranspiler(); | ||
afterProgramValidate(program: Program) { | ||
this.fileValidator.reset(); | ||
|
||
//prepend a print statement to the top of every function body | ||
event.editor.arrayUnshift(functionStatement.func.body.statements, printStatement); | ||
} | ||
}), { | ||
walkMode: WalkMode.visitAllRecursive | ||
}); | ||
|
||
} else if (isXmlFile(event.file)) { | ||
//prepend a label to every xml file (why? just for fun....) | ||
const parser = new SGParser(); | ||
parser.parse('generated.xml', ` | ||
<component name="Generated"> | ||
<children> | ||
<label text="Hello from ${event.file.ast.component!.name}" /> | ||
</children> | ||
</component> | ||
`); | ||
|
||
const label = parser.ast.component!.children.children[0]; | ||
// Get a map of all annotated functions | ||
for (const file of Object.values(program.files)) { | ||
this.fileValidator.findAnnotations(file); | ||
} | ||
} | ||
|
||
//ensure the <children> component exists | ||
if (!event.file.ast.component!.children) { | ||
event.file.ast.component!.children = new SGChildren(); | ||
} | ||
event.editor.arrayUnshift( | ||
event.file.ast.component!.children.children, | ||
label | ||
); | ||
beforeProgramTranspile(program: Program, entries: TranspileObj[], editor: AstEditor) { | ||
for (const entry of entries) { | ||
this.fileTranspiler.preprocess(entry.file, editor, this.fileValidator.annotatedFunctions); | ||
} | ||
} | ||
} | ||
} |
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,11 @@ | ||
import type { Range } from 'brighterscript'; | ||
import { DiagnosticSeverity } from 'brighterscript'; | ||
|
||
export const diagnostics = { | ||
badAnnotationBody: (range: Range) => ({ | ||
message: 'Inlineable function body must be a single return statement', | ||
code: 'bad-annotation-body', | ||
severity: DiagnosticSeverity.Error, | ||
range: range | ||
}) | ||
}; |