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

fix[ClearComments]: add python comments handling #506

Open
wants to merge 1 commit 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
11 changes: 7 additions & 4 deletions gserver/src/steps.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
getOSPath,
getFileContent,
clearComments,
clearPythonComments,
getMD5Id,
escapeRegExp,
escaprRegExpForPureText,
Expand Down Expand Up @@ -527,7 +528,7 @@ export default class StepsHandler {
getFileSteps(filePath: string) {
const fileContent = getFileContent(filePath);
const fileComments = this.getMultiLineComments(fileContent);
const definitionFile = clearComments(fileContent);
const definitionFile = filePath.endsWith('.py') ? clearPythonComments(fileContent) : clearComments(fileContent);
return definitionFile
.split(/\r?\n/g)
.reduce((steps, line, lineIndex, lines) => {
Expand Down Expand Up @@ -559,9 +560,11 @@ export default class StepsHandler {
getOSPath(filePath),
Range.create(pos, pos)
);
steps = steps.concat(
this.getSteps(finalLine, stepPart, def, gherkin, fileComments)
);
if(stepPart != '') {
steps = steps.concat(
this.getSteps(finalLine, stepPart, def, gherkin, fileComments)
);
}
}
return steps;
}, new Array<Step>());
Expand Down
63 changes: 63 additions & 0 deletions gserver/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,69 @@ export function clearGherkinComments(text: string): string {
return strip(text, { preserveNewlines: true });
}


export function clearPythonComments(text: string): string {
// Clear all single and multiline comments
let commentsMode: 'none' | 'singleTriplet' | 'doubleTriplet' = 'none';
text = text.split(/\r?\n/g).map(l => {
switch (commentsMode) {
case 'singleTriplet': {
const match = l.match(/^.*(''')/);
if (match) {
commentsMode = 'none';
const offset = match[0].length - match[1].length;
return ' '.repeat(offset) + l.substring(offset);
}
}
break;

case 'doubleTriplet': {
const match = l.match(/^.*(""")/);
if (match) {
commentsMode = 'none';
const offset = match[0].length - match[1].length;
return ' '.repeat(offset) + l.substring(offset);
}
}
break;

case 'none':
default: {
const match = l.match(/^.*f?("""|''')/);
if (match) {
if (match[1] === "'''") {
commentsMode = 'singleTriplet';
} else if (match[1] === '"""') {
commentsMode = 'doubleTriplet';
}

// Check if it's a single-line triple-quote comment
const closingTripleQuote = l.match(/(''')|(""")/g);
if (closingTripleQuote && closingTripleQuote.length === 2) {
commentsMode = 'none';
return '';
}

// Strip the content after the match
return l.substring(0, match[0].length);
}
}
return l;
}
})
.join('\r\n');

// Clear all line comments
text = text
.split(/\r?\n/g)
.map(l => {
return l.replace(/#.*$/gm, '');
})
.join('\r\n');

return text;
}

export function getMD5Id(str: string): string {
return md5(str);
}
Expand Down