Skip to content

Commit 0bd9b9f

Browse files
committed
strip out comments before tokenizing
1 parent 9c6ed72 commit 0bd9b9f

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
- Added doxygen format support ([#120](https://github.com/NilsJPWerner/autoDocstring/issues/120)) (@daluar @PhilipNelson5)
6+
- Handle comments in multiline definitions ([#252](https://github.com/NilsJPWerner/autoDocstring/issues/252)) (@GabrielSchoenweiler @PhilipNelson5)
67

78
[All Changes](https://github.com/NilsJPWerner/autoDocstring/compare/v0.6.1...master)
89

src/parse/tokenize_definition.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export function tokenizeDefinition(functionDefinition: string): string[] {
77
return [];
88
}
99

10-
const tokens = tokenizeParameterString(match[1]);
10+
const tokens = tokenizeParameterString(stripComments(match[1]));
1111

1212
if (match[2] != undefined) {
1313
tokens.push(match[2]);
@@ -16,6 +16,22 @@ export function tokenizeDefinition(functionDefinition: string): string[] {
1616
return tokens;
1717
}
1818

19+
function stripComments(parameterString: string): string {
20+
let cleanString = "";
21+
let position = 0;
22+
23+
while (position < parameterString.length) {
24+
// When a comment is encountered, skip ahead to the end of the line
25+
if (parameterString[position] === "#") {
26+
position = parameterString.indexOf("\n", position);
27+
}
28+
29+
cleanString += parameterString[position++];
30+
}
31+
32+
return cleanString;
33+
}
34+
1935
function tokenizeParameterString(parameterString: string): string[] {
2036
const stack: string[] = [];
2137
const parameters: string[] = [];

src/test/parse/tokenize_definition.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,24 @@ describe("tokenizeDefinition()", () => {
113113

114114
expect(result).to.have.ordered.members(["arg", "arg_2"]);
115115
});
116+
117+
it("should ignore comments in the middle of the function definition", () => {
118+
const functionDefinition = `def abc_c(
119+
arg, # comment
120+
arg_2, # comment, comment
121+
arg_3 # comment with special characters "'"({[]})
122+
):`;
123+
const result = tokenizeDefinition(functionDefinition);
124+
125+
expect(result).to.have.ordered.members(["arg", "arg_2", "arg_3"]);
126+
});
127+
it("should ignore comments in the middle of the class definition", () => {
128+
const functionDefinition = `class abc_c(
129+
arg, # comment,
130+
arg_2 # comment with special characters "'"({[]})
131+
):`;
132+
const result = tokenizeDefinition(functionDefinition);
133+
134+
expect(result).to.have.ordered.members(["arg", "arg_2"]);
135+
});
116136
});

0 commit comments

Comments
 (0)