Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle comments in a multiline definition #280

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Added doxygen format support ([#120](https://github.com/NilsJPWerner/autoDocstring/issues/120)) (@daluar @PhilipNelson5)
- Disable HTML-escaping behavior globally ([#235](https://github.com/NilsJPWerner/autoDocstring/issues/235)) (@PhilipNelson5)
- Handle comments in multiline definitions ([#252](https://github.com/NilsJPWerner/autoDocstring/issues/252)) (@GabrielSchoenweiler @PhilipNelson5)

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

Expand Down
18 changes: 17 additions & 1 deletion src/parse/tokenize_definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function tokenizeDefinition(functionDefinition: string): string[] {
return [];
}

const tokens = tokenizeParameterString(match[1]);
const tokens = tokenizeParameterString(stripComments(match[1]));

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

function stripComments(parameterString: string): string {
let cleanString = "";
let position = 0;

while (position < parameterString.length) {
// When a comment is encountered, skip ahead to the end of the line
if (parameterString[position] === "#") {
position = parameterString.indexOf("\n", position);
}

cleanString += parameterString[position++];
}

return cleanString;
}

function tokenizeParameterString(parameterString: string): string[] {
const stack: string[] = [];
const parameters: string[] = [];
Expand Down
20 changes: 20 additions & 0 deletions src/test/parse/tokenize_definition.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,24 @@ describe("tokenizeDefinition()", () => {

expect(result).to.have.ordered.members(["arg", "arg_2"]);
});

it("should ignore comments in the middle of the function definition", () => {
const functionDefinition = `def abc_c(
arg, # comment
arg_2, # comment, comment
arg_3 # comment with special characters "'"({[]})
):`;
const result = tokenizeDefinition(functionDefinition);

expect(result).to.have.ordered.members(["arg", "arg_2", "arg_3"]);
});
it("should ignore comments in the middle of the class definition", () => {
const functionDefinition = `class abc_c(
arg, # comment,
arg_2 # comment with special characters "'"({[]})
):`;
const result = tokenizeDefinition(functionDefinition);

expect(result).to.have.ordered.members(["arg", "arg_2"]);
});
});
Loading