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

Remove inline comments during line preprocessing #161

Open
wants to merge 4 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
4 changes: 2 additions & 2 deletions src/parse/parse_parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ function parseYields(parameters: string[], body: string[]): Yields {
}

function parseReturnFromDefinition(parameters: string[]): Returns | null {
const pattern = /^->\s*([\w\[\], \.]*)/;
const pattern = /^->\s*(["']?)([\w\[\], \.]*)\1/;

for (const param of parameters) {
const match = param.trim().match(pattern);
Expand All @@ -125,7 +125,7 @@ function parseReturnFromDefinition(parameters: string[]): Returns | null {
}

// Skip "-> None" annotations
return match[1] === "None" ? null : { type: match[1] };
return match[1] === "None" ? null : { type: match[2] };
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion src/parse/tokenize_definition.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export function tokenizeDefinition(functionDefinition: string): string[] {
const definitionPattern = /(?:def|class)\s+\w+\s*\(([\s\S]*)\)\s*(->\s*[\w\[\], \.]*)?:\s*(?:#.*)?$/;
const definitionPattern = /(?:def|class)\s+\w+\s*\(([\s\S]*)\)\s*(->\s*(["']?)[\w\[\], \.]*\3)?:\s*(?:#.*)?$/;

const match = definitionPattern.exec(functionDefinition);
if (match == undefined || match[1] == undefined) {
Expand Down
4 changes: 2 additions & 2 deletions src/parse/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export function getIndentation(line: string): string {
*/
export function preprocessLines(lines: string[]): string[] {
return lines
.map(line => line.trim())
.filter((line) => !line.startsWith("#"));
.filter((line) => !line.trim().startsWith("#"))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't do this unfortunately because it will cut off strings with # in them. See previous discussion here: #144

.map(line => line.split("#")[0].trim());
}

export function indentationOf(line: string): number {
Expand Down
15 changes: 15 additions & 0 deletions src/test/parse/utilities.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,19 @@ describe("preprocessLines()", () => {
'foo'
]);
});

it('should discard inline comments.', () => {
const result = preprocessLines([
'foo',
'arg: int # hello',
'hello#world',
'hello\t\t#world'
]);
expect(result).to.be.deep.equal([
'foo',
'arg: int',
'hello',
'hello'
]);
})
});