Skip to content

Commit a13a581

Browse files
Undo comment visitor logic
1 parent f5f4763 commit a13a581

File tree

3 files changed

+13
-116
lines changed

3 files changed

+13
-116
lines changed

docs/plugins.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,7 @@ This plugin will search through every LiteralExpression in the entire project, a
558558
559559
Another common use case is to remove print statements and comments. Here's a plugin to do that:
560560
561-
Note: Comments are not regular nodes in the AST, so to visit comments, there is an additional requirement to specify
562-
that the AST walk should also `visitComments`. In the case of comments, the first argument of the visitor function is a `Token`, not an `AstNode`.
561+
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:
563562
564563
```typescript
565564
import { isBrsFile, createVisitor, WalkMode, BeforeFileTranspileEvent, CompilerPlugin } from 'brighterscript';
@@ -575,9 +574,15 @@ export default function plugin() {
575574
//replace `PrintStatement` transpilation with empty string
576575
event.editor.overrideTranspileResult(statement, '');
577576
},
578-
CommentToken: (_token, _parent, owner, key) => {
579-
//remove comment tokens
580-
event.editor.removeProperty(owner, key);
577+
AstNode: (node: AstNode, _parent, owner, key) => {
578+
const trivia = node.getLeadingTrivia();
579+
for(let i = 0; i < trivia.length; i++) {
580+
let triviaItem = trivia[i].
581+
if (triviaItem.kind === TokenKind.Comment) {
582+
//remove comment tokens
583+
event.editor.removeProperty(trivia, i);
584+
}
585+
}
581586
}
582587
}), {
583588
walkMode: WalkMode.visitStatements | WalkMode.visitComments

src/astUtils/visitors.spec.ts

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,76 +1164,5 @@ describe('astUtils visitors', () => {
11641164
'VariableExpression'
11651165
]);
11661166
});
1167-
1168-
describe('visitComments', () => {
1169-
1170-
it('visits a comment when walkmode includes visitComments', () => {
1171-
const items = [];
1172-
const { ast } = Parser.parse(`
1173-
'1
1174-
sub main()
1175-
'2
1176-
'3
1177-
print "hello"
1178-
end sub
1179-
'4
1180-
`);
1181-
ast.walk(createVisitor({
1182-
CommentToken: (comment, parent, owner, key) => {
1183-
items.push(comment.text);
1184-
}
1185-
}), {
1186-
// eslint-disable-next-line no-bitwise
1187-
walkMode: WalkMode.visitAllRecursive | WalkMode.visitComments
1188-
});
1189-
expect(items).to.be.length(4);
1190-
});
1191-
1192-
it('has access to the next statement', () => {
1193-
const { ast } = Parser.parse(`
1194-
sub main()
1195-
'comment
1196-
print "hello"
1197-
end sub
1198-
`);
1199-
ast.walk(createVisitor({
1200-
CommentToken: (comment, parent, owner, key) => {
1201-
expect(comment.text).to.eq('\'comment');
1202-
expect(isPrintStatement(parent)).to.true;
1203-
expect(parent.getLeadingTrivia()).to.eq(owner);
1204-
expect(key).to.eq(2); //0: newline, 1: whitespace, 2: COMMENT, 3: newline, 4:whitespace
1205-
}
1206-
}), {
1207-
// eslint-disable-next-line no-bitwise
1208-
walkMode: WalkMode.visitAllRecursive | WalkMode.visitComments
1209-
});
1210-
});
1211-
1212-
it('can change the value of teh comment', () => {
1213-
const { ast } = Parser.parse(`
1214-
sub main()
1215-
'comment
1216-
print "hello"
1217-
end sub
1218-
`);
1219-
ast.walk(createVisitor({
1220-
CommentToken: (comment, parent, owner, key) => {
1221-
return createToken(TokenKind.Comment, 'changed');
1222-
}
1223-
}), {
1224-
// eslint-disable-next-line no-bitwise
1225-
walkMode: WalkMode.visitAllRecursive | WalkMode.visitComments
1226-
});
1227-
ast.walk(createVisitor({
1228-
CommentToken: (comment, parent, owner, key) => {
1229-
expect(comment.text).to.eq('changed');
1230-
}
1231-
}), {
1232-
// eslint-disable-next-line no-bitwise
1233-
walkMode: WalkMode.visitAllRecursive | WalkMode.visitComments
1234-
});
1235-
});
1236-
1237-
});
12381167
});
12391168
});

src/astUtils/visitors.ts

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ import type { AALiteralExpression, AAMemberExpression, AnnotationExpression, Arr
55
import { isExpression, isStatement } from './reflection';
66
import type { Editor } from './Editor';
77
import type { Statement, Expression, AstNode } from '../parser/AstNode';
8-
import { TokenKind } from '../lexer/TokenKind';
9-
import { isToken, type Token } from '../lexer/Token';
10-
118

129
/**
1310
* Walks the statements of a block and descendent sub-blocks, and allow replacing statements
@@ -24,8 +21,6 @@ export function walkStatements(
2421
}
2522

2623
export type WalkVisitor = <T = AstNode>(node: AstNode, parent?: AstNode, owner?: any, key?: any) => void | T;
27-
export type WalkTokenVisitor = <K = TokenKind>(token: Token & { kind: K }, parent?: AstNode, owner?: any, key?: any) => void | (Token & { kind: K });
28-
2924

3025
/**
3126
* A helper function for Statement and Expression `walkAll` calls.
@@ -47,25 +42,6 @@ export function walk<T>(owner: T, key: keyof T, visitor: WalkVisitor, options: W
4742

4843
//notify the visitor of this element
4944
if (element.visitMode & options.walkMode) {
50-
51-
if (options.walkMode & WalkMode.visitComments) {
52-
const leadingTrivia = element.getLeadingTrivia();
53-
for (let i = 0; i < leadingTrivia.length; i++) {
54-
const trivia = leadingTrivia[i];
55-
if (trivia.kind === TokenKind.Comment) {
56-
const result = (visitor as unknown as WalkTokenVisitor)?.(trivia, element as any, leadingTrivia, i);
57-
if (result && isToken(result)) {
58-
if (options.editor) {
59-
options.editor.setProperty(leadingTrivia, i, result as any);
60-
} else {
61-
leadingTrivia[i] = result;
62-
63-
}
64-
}
65-
}
66-
}
67-
}
68-
6945
const result = visitor?.(element, element.parent as any, owner, key);
7046

7147
//replace the value on the parent if the visitor returned a Statement or Expression (this is how visitors can edit AST)
@@ -193,14 +169,9 @@ export function createVisitor(
193169
RegexLiteralExpression?: (expression: RegexLiteralExpression, parent?: AstNode, owner?: any, key?: any) => Expression | void;
194170
TypeExpression?: (expression: TypeExpression, parent?: AstNode, owner?: any, key?: any) => Expression | void;
195171
TypecastExpression?: (expression: TypecastExpression, parent?: AstNode, owner?: any, key?: any) => Expression | void;
196-
//Tokens
197-
CommentToken?: (token: Token & { kind: TokenKind.Comment }, parent?: AstNode, owner?: any, key?: any) => Token & { kind: TokenKind.Comment } | void;
198172
}
199173
) {
200-
return <WalkVisitor>((statement: Statement | Token, parent?: Statement, owner?: any, key?: any): Statement | void => {
201-
if (isToken(statement)) {
202-
return visitor[statement.kind + 'Token']?.(statement, parent, owner, key);
203-
}
174+
return <WalkVisitor>((statement: Statement, parent?: Statement, owner?: any, key?: any): Statement | void => {
204175
return visitor[statement.constructor.name]?.(statement, parent, owner, key);
205176
});
206177
}
@@ -266,11 +237,7 @@ export enum InternalWalkMode {
266237
/**
267238
* If child function expressions are encountered, this will allow the walker to step into them.
268239
*/
269-
recurseChildFunctions = 16,
270-
/**
271-
* If a visited node has comments in its leadingTrivia, visit those comments
272-
*/
273-
visitComments = 32
240+
recurseChildFunctions = 16
274241
}
275242

276243
/* eslint-disable @typescript-eslint/prefer-literal-enum-member */
@@ -315,9 +282,5 @@ export enum WalkMode {
315282
/**
316283
* Visit all descendent statements and expressions, and DOES step into child functions
317284
*/
318-
visitAllRecursive = InternalWalkMode.walkStatements | InternalWalkMode.visitStatements | InternalWalkMode.walkExpressions | InternalWalkMode.visitExpressions | InternalWalkMode.recurseChildFunctions,
319-
/**
320-
* Visit comment tokens in leading trivia
321-
*/
322-
visitComments = InternalWalkMode.visitComments
285+
visitAllRecursive = InternalWalkMode.walkStatements | InternalWalkMode.visitStatements | InternalWalkMode.walkExpressions | InternalWalkMode.visitExpressions | InternalWalkMode.recurseChildFunctions
323286
}

0 commit comments

Comments
 (0)