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

Correct regex for function source cleaning #2212

Merged
merged 1 commit into from
Jun 11, 2016
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
4 changes: 2 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,8 @@ exports.slug = function(str) {
exports.clean = function(str) {
str = str
.replace(/\r\n?|[\n\u2028\u2029]/g, '\n').replace(/^\uFEFF/, '')
.replace(/^function *\(.*\)\s*\{|\(.*\) *=> *\{?/, '')
.replace(/\s+\}$/, '');
// (traditional)-> space/name parameters body (lambda)-> parameters body multi-statement/single keep body content
.replace(/^function(?:\s*|\s+[^(]*)\([^)]*\)\s*\{((?:.|\n)*?)\s*\}$|^\([^)]*\)\s*=>\s*(?:\{((?:.|\n)*?)\s*\}|((?:.|\n)*))$/, '$1$2$3');
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there anyway this can be documented a bit, for legibility?
As in, have a comment in the previous line, and when you reach certain section you write what it's for.

//                           fname   args        code      etc
.replace(/^function(?:\s*|\s+[^(]*)\([^)]*\)\s*\{((?:.|\n)*?)\s*\}$|^\([^)]*\)\s*=>\s*(?:\{((?:.|\n)*?)\s*\}|((?:.|\n)*))$/, '$1$2$3');

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea. I had half a mind to break the regex up into multiple variables, but wasn't sure if it would clutter the function as a whole... A comment should be a good compromise I think.


var spaces = str.match(/^\n?( *)/)[1].length;
var tabs = str.match(/^\n?(\t*)/)[1].length;
Expand Down
24 changes: 24 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,30 @@ describe('utils', function() {
it('should remove tab character indentation from the function body', function() {
clean('\t//line1\n\t\t//line2').should.equal('//line1\n\t//line2');
});

it('should handle functions with tabs in their declarations', function() {
clean('function\t(\t)\t{\n//code\n}').should.equal('//code');
});

it('should handle named functions without space after name', function() {
clean('function withName() {\n//code\n}').should.equal('//code');
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add an it for when the function's name is followed by a space?

Copy link
Contributor

Choose a reason for hiding this comment

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

It's a quite common coding style.


it('should handle named functions with space after name', function() {
clean('function withName () {\n//code\n}').should.equal('//code');
});

it('should handle functions with no space between the end and the closing brace', function() {
clean('function() {/*code*/}').should.equal('/*code*/');
});

it('should handle functions with parentheses in the same line', function() {
clean('function() { if (true) { /* code */ } }').should.equal('if (true) { /* code */ }');
});

it('should handle empty functions', function() {
clean('function() {}').should.equal('');
});
});

describe('.isBuffer()', function() {
Expand Down