Skip to content

Commit

Permalink
Merge branch 'release-1.0.0' into conditional_compile_v1
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron committed May 10, 2024
2 parents 1421065 + d4479b4 commit cc6f762
Show file tree
Hide file tree
Showing 26 changed files with 5,756 additions and 3,381 deletions.
55 changes: 33 additions & 22 deletions docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -492,17 +492,19 @@ export default function () {
event.program.diagnosticManager.clearByContext({file: file, tag: 'no-underscores-plugin'});

// visit function statements and validate their name
file.parser.references.functionStatements.forEach((fun) => {
if (fun.name.text.includes('_')) {
event.program.diagnosticManager.register({
code: 9000,
message: 'Do not use underscores in function names',
range: fun.name.range,
file
}, {
tag: ['no-underscores-plugin']
});
event.file.ast.walk(createVisitor({
FunctionStatement: (funcStmt) => {
if (funcStmt.tokens.name.text.includes('_')) {
event.program.diagnostics.register([{
code: 9000,
message: 'Do not use underscores in function names',
range: funcStmt.tokens.name.range,
file
}]);
}
}
}, {
walkMode: WalkMode.visitStatements
});
}
}
Expand Down Expand Up @@ -555,6 +557,9 @@ This plugin will search through every LiteralExpression in the entire project, a
## Remove Comment and Print Statements
Another common use case is to remove print statements and comments. Here's a plugin to do that:
Note: Comments are not regular nodes in the AST. They're considered "trivia". To access them, you need to ask each AstNode for its trivia. to help with this, we've included the `AstNode` visitor method. Here's how you'd do that:
```typescript
import { isBrsFile, createVisitor, WalkMode, BeforeFileTranspileEvent, CompilerPlugin } from 'brighterscript';

Expand All @@ -563,19 +568,25 @@ export default function plugin() {
name: 'removeCommentAndPrintStatements',
beforeFileTranspile: (event: BeforeFileTranspileEvent) => {
if (isBrsFile(event.file)) {
// visit functions bodies and replace `PrintStatement` nodes with `EmptyStatement`
for (const func of event.file.parser.references.functionExpressions) {
func.body.walk(createVisitor({
PrintStatement: (statement) => {
event.editor.overrideTranspileResult(statement, '');
},
CommentStatement: (statement) => {
event.editor.overrideTranspileResult(statement, '');
// visit functions bodies
event.file.ast.walk(createVisitor({
PrintStatement: (statement) => {
//replace `PrintStatement` transpilation with empty string
event.editor.overrideTranspileResult(statement, '');
},
AstNode: (node: AstNode, _parent, owner, key) => {
const trivia = node.getLeadingTrivia();
for(let i = 0; i < trivia.length; i++) {
let triviaItem = trivia[i].
if (triviaItem.kind === TokenKind.Comment) {
//remove comment tokens
event.editor.removeProperty(trivia, i);
}
}
}), {
walkMode: WalkMode.visitStatements
});
}
}
}), {
walkMode: WalkMode.visitStatements | WalkMode.visitComments
});
}
}
} as CompilerPlugin;
Expand Down
34 changes: 21 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"docs": "ts-node scripts/compile-doc-examples.ts",
"benchmark": "cd ./benchmarks && ts-node ./index.ts",
"scrape-roku-docs": "ts-node scripts/scrape-roku-docs.ts",
"rescrape-roku-docs": "rm scripts/.cache.json && ts-node scripts/scrape-roku-docs.ts",
"rescrape-roku-docs": "rm scripts/.cache.json && npm run scrape-roku-docs",
"null-check": "tsc -p tsconfig-null-safe.json",
"create-test-package": "ts-node scripts/create-test-package.ts"
},
Expand Down
2 changes: 1 addition & 1 deletion scripts/.cache.json

Large diffs are not rendered by default.

0 comments on commit cc6f762

Please sign in to comment.