Skip to content

Commit 0fba69b

Browse files
authored
Added comments to statements in the Lua AST (#982)
* Added comments to statements in the Lua AST * Added support for arrays as multiline comments * Removed pushed indent from multiline comments * factored out printComment
1 parent 28d0a57 commit 0fba69b

5 files changed

Lines changed: 139 additions & 0 deletions

File tree

src/LuaAST.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ export function createBlock(statements: Statement[], tsOriginal?: ts.Node): Bloc
229229

230230
export interface Statement extends Node {
231231
_statementBrand: any;
232+
leadingComments?: Array<string | string[]>;
233+
trailingComments?: Array<string | string[]>;
232234
}
233235

234236
export interface DoStatement extends Statement {

src/LuaPrinter.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,42 @@ export class LuaPrinter {
297297
}
298298

299299
public printStatement(statement: lua.Statement): SourceNode {
300+
let resultNode = this.printStatementExcludingComments(statement);
301+
302+
if (statement.leadingComments) {
303+
resultNode = this.concatNodes(
304+
statement.leadingComments.map(c => this.printComment(c)).join("\n"),
305+
"\n",
306+
resultNode
307+
);
308+
}
309+
310+
if (statement.trailingComments) {
311+
resultNode = this.concatNodes(
312+
resultNode,
313+
"\n",
314+
statement.trailingComments.map(c => this.printComment(c)).join("\n")
315+
);
316+
}
317+
318+
return resultNode;
319+
}
320+
321+
public printComment(comment: string | string[]): SourceChunk {
322+
if (Array.isArray(comment)) {
323+
if (comment.length === 0) {
324+
return this.indent("--[[]]");
325+
} else {
326+
const [firstLine, ...restLines] = comment;
327+
const commentLines = this.concatNodes(...restLines.map(c => this.concatNodes("\n", this.indent(c))));
328+
return this.concatNodes(this.indent("--[["), firstLine, commentLines, "]]");
329+
}
330+
} else {
331+
return this.indent(`--${comment}`);
332+
}
333+
}
334+
335+
protected printStatementExcludingComments(statement: lua.Statement): SourceNode {
300336
switch (statement.kind) {
301337
case lua.SyntaxKind.DoStatement:
302338
return this.printDoStatement(statement as lua.DoStatement);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`statement comments 1`] = `
4+
"local ____exports = {}
5+
function ____exports.__main(self)
6+
--This comment on variable declaration was added by a plugin!
7+
--- This one too!
8+
local foo = \\"bar\\"
9+
-- This trailing comment was also added by a plugin!
10+
--- Example luadoc comment
11+
---@param paramName ParamClass
12+
foo = \\"baz\\"
13+
--[[ This plugin can also (kinda) create multiline comments.
14+
-- Line 2
15+
--]]
16+
--[[Multiline comments are supported as arrays
17+
This is the second line!]]
18+
while true do
19+
end
20+
--Single line comments and multiline comments can also be mixed
21+
--[[Like this
22+
Pretty cool!]]
23+
--Empty multiline comment below:
24+
--[[]]
25+
--Single line multiline comment:
26+
--[[Single line]]
27+
end
28+
return ____exports"
29+
`;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import * as ts from "typescript";
2+
import * as tstl from "../../../src";
3+
4+
const plugin: tstl.Plugin = {
5+
visitors: {
6+
[ts.SyntaxKind.VariableStatement](node, context) {
7+
const result = context.superTransformStatements(node);
8+
9+
const firstLuaStatement = result[0];
10+
const lastLuaStatement = result[result.length - 1];
11+
12+
firstLuaStatement.leadingComments = [
13+
"This comment on variable declaration was added by a plugin!",
14+
"- This one too!",
15+
];
16+
17+
lastLuaStatement.trailingComments = [" This trailing comment was also added by a plugin!"];
18+
19+
return result;
20+
},
21+
[ts.SyntaxKind.ExpressionStatement](node, context) {
22+
const result = context.superTransformStatements(node);
23+
24+
const firstLuaStatement = result[0];
25+
const lastLuaStatement = result[result.length - 1];
26+
27+
firstLuaStatement.leadingComments = ["- Example luadoc comment", "-@param paramName ParamClass"];
28+
29+
lastLuaStatement.trailingComments = [
30+
"[[ This plugin can also (kinda) create multiline comments.",
31+
" Line 2",
32+
"]]",
33+
];
34+
35+
return result;
36+
},
37+
[ts.SyntaxKind.WhileStatement](node, context) {
38+
const result = context.superTransformStatements(node);
39+
40+
const firstLuaStatement = result[0];
41+
const lastLuaStatement = result[result.length - 1];
42+
43+
firstLuaStatement.leadingComments = [
44+
["Multiline comments are supported as arrays", "This is the second line!"],
45+
];
46+
47+
lastLuaStatement.trailingComments = [
48+
"Single line comments and multiline comments can also be mixed",
49+
["Like this", "Pretty cool!"],
50+
"Empty multiline comment below:",
51+
[],
52+
"Single line multiline comment:",
53+
["Single line"],
54+
];
55+
56+
return result;
57+
},
58+
},
59+
};
60+
61+
// eslint-disable-next-line import/no-default-export
62+
export default plugin;

test/transpile/plugins/plugins.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,13 @@ test("passing arguments", () => {
3030
.setOptions({ luaPlugins: [{ name: path.join(__dirname, "arguments.ts"), option: true }] })
3131
.expectToEqual({ name: path.join(__dirname, "arguments.ts"), option: true });
3232
});
33+
34+
test("statement comments", () => {
35+
util.testFunction`
36+
let foo = "bar";
37+
foo = "baz";
38+
while (true) {}
39+
`
40+
.setOptions({ luaPlugins: [{ name: path.join(__dirname, "add-comments.ts") }] })
41+
.expectLuaToMatchSnapshot();
42+
});

0 commit comments

Comments
 (0)