Skip to content

Commit

Permalink
Fix bug where profileEnd() gets added even if no comment was found
Browse files Browse the repository at this point in the history
This is due to a bug in logic. If we receive a `BlockStatement`, we were
adding `console.profileEnd()` even there was no `// profile` comment by
checking `!state.gotReturn` assuming this condition meant we reached the
end of the block but didn't get a return (`if` condition, for instance).
Since `gotReturn` is by default `false`, we were adding the closing
comment always. This introduces a perf issue in Safari because that
browser will try to run the profile by seeing this comment.
  • Loading branch information
kgrz committed Feb 23, 2018
1 parent cdee827 commit 8a3124e
Showing 4 changed files with 71 additions and 5 deletions.
30 changes: 29 additions & 1 deletion __snapshots__/test.js.snap
Original file line number Diff line number Diff line change
@@ -201,7 +201,6 @@ class A {
newState: {}
});
callSomeMethod();
console.profileEnd();
};
console.profileEnd();
@@ -212,6 +211,35 @@ class A {
}"
`;

exports[`usage inside if conditions 1`] = `
"
const b = () => {
if (true) {
console.profile('b');
console.profileEnd();
// profile
return 12;
}
switch (true) {
case 'case1':
return 'what';
case 'case 2':
{
return 'where';
}
case 'case 3':
return 'who';
default:
return 'default';
}
return 42;
};"
`;

exports[`wraps a function in profile block 1`] = `
"
const b = () => {
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -162,6 +162,7 @@ const ConsoleProfileVisitor = function (babel) {
BlockStatement: function (path, args) {
let state = {
gotProfileComment: false,
addedProfileComment: false,
gotReturn: false,
blockDepth: 0
};
@@ -173,11 +174,13 @@ const ConsoleProfileVisitor = function (babel) {
'body',
generateProfileStart(getName(path))
);

state.addedProfileComment = true;
}

path.traverse(ReturnVisitor, { state });

if (!state.gotReturn) {
if (state.addedProfileComment && !state.gotReturn) {
path.pushContainer('body', generateProfileEnd());
}
}
13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "babel-plugin-console-perf",
"version": "0.0.2",
"version": "0.0.3",
"description": "A plugin that makes it easy to profile functions using console.profile and console.time functions",
"main": "index.js",
"author": "Kashyap Kondamudi",
@@ -16,6 +16,13 @@
"babel-plugin-transform-class-properties": "^6.24.1",
"jest": "^21.2.1"
},
"keywords": [ "babel-plugin", "console.profile", "console.time", "performance" ],
"files": [ "index.js" ]
"keywords": [
"babel-plugin",
"console.profile",
"console.time",
"performance"
],
"files": [
"index.js"
]
}
28 changes: 28 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -187,6 +187,34 @@ it('maintains indentation when for if blocks', () => {
expect(code).toMatchSnapshot();
});

fit('usage inside if conditions', () => {
const example = `
const b = () => {
if (true) {
// profile
return 12;
}
switch (true) {
case 'case1':
return 'what';
case 'case 2': {
return 'where';
}
case 'case 3':
return 'who';
default:
return 'default'
}
return 42;
};
`;

const code = babel.transform(example, { plugins: [ plugin ] }).code;
expect(code).toMatchSnapshot();
});

describe('usage inside classes', () => {
it('works inside function properties', () => {

0 comments on commit 8a3124e

Please sign in to comment.