diff --git a/CHANGELOG.md b/CHANGELOG.md index 7dc72b8..c0bebe6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased - Added doxygen format support ([#120](https://github.com/NilsJPWerner/autoDocstring/issues/120)) (@daluar @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) diff --git a/src/parse/tokenize_definition.ts b/src/parse/tokenize_definition.ts index e8d9d96..e9f9be3 100644 --- a/src/parse/tokenize_definition.ts +++ b/src/parse/tokenize_definition.ts @@ -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]); @@ -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[] = []; diff --git a/src/test/parse/tokenize_definition.spec.ts b/src/test/parse/tokenize_definition.spec.ts index c38f0c6..512a100 100644 --- a/src/test/parse/tokenize_definition.spec.ts +++ b/src/test/parse/tokenize_definition.spec.ts @@ -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"]); + }); });