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: address edge case in comment stripping #1780

Merged
merged 1 commit into from Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 29 additions & 3 deletions lib/autoInject.js
Expand Up @@ -6,11 +6,37 @@ var FN_ARGS = /^(?:async\s+)?(?:function)?\s*\w*\s*\(\s*([^)]+)\s*\)(?:\s*{)/;
var ARROW_FN_ARGS = /^(?:async\s+)?\(?\s*([^)=]+)\s*\)?(?:\s*=>)/;
var FN_ARG_SPLIT = /,/;
var FN_ARG = /(=.+)?(\s*)$/;
var STRIP_COMMENTS = /(\/\*(?:[^/]|\/(?!\*))*\*\/)|\/\/.*$/mg;

function stripComments(string) {
let stripped = '';
let index = 0;
let endBlockComment = string.indexOf('*/');
while (index < string.length) {
if (string[index] === '/' && string[index+1] === '/') {
// inline comment
let endIndex = string.indexOf('\n', index);
index = (endIndex === -1) ? string.length : endIndex;
} else if ((endBlockComment !== -1) && (string[index] === '/') && (string[index+1] === '*')) {
// block comment
let endIndex = string.indexOf('*/', index);
if (endIndex !== -1) {
index = endIndex + 2;
endBlockComment = string.indexOf('*/', index);
} else {
stripped += string[index];
index++;
}
} else {
stripped += string[index];
index++;
}
}
return stripped;
}

function parseParams(func) {
const src = func.toString().replace(STRIP_COMMENTS, '');
let match = src.match(FN_ARGS)
const src = stripComments(func.toString());
let match = src.match(FN_ARGS);
if (!match) {
match = src.match(ARROW_FN_ARGS);
}
Expand Down
10 changes: 7 additions & 3 deletions test/autoInject.js
Expand Up @@ -243,13 +243,17 @@ describe('autoInject', () => {
,callback) {
callback(null, true);
},
task3: function task3(callback) {
task3: function task3(task4 /* /* )
*/, callback) {
callback(null, true);
}
},
task4: function task4(callback) {
callback(null, true);
},
},
(err, result) => {
expect(err).to.eql(null);
expect(result).to.deep.eql({task1: true, task2: true, task3: true});
expect(result).to.deep.eql({task1: true, task2: true, task3: true, task4: true});
done();
});
});
Expand Down