Skip to content

Commit

Permalink
Check for exact match for profile
Browse files Browse the repository at this point in the history
  • Loading branch information
kgrz committed Oct 30, 2017
1 parent 933a0d6 commit ca81569
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
13 changes: 13 additions & 0 deletions __snapshots__/test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ const b = () => {
};"
`;

exports[`does not wrap a function if comment does not exactly match 1`] = `
"
const b = () => {
// wraps a profile
return 42;
};
const a = () => {
// bug in something else called profile
return b();
};"
`;

exports[`has multiple comments 1`] = `
"
const b = () => {
Expand Down
13 changes: 10 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const t = require('babel-types');

const SPACE_REGEX = /\s+/;

const BlockVisitor = function (path, args) {
const body = path.node.body || [];

Expand All @@ -16,9 +18,14 @@ const BlockVisitor = function (path, args) {
for (var j = 0, length = allComments.length; j < length; j++) {
const cNode = allComments[j];

if (cNode.type === 'CommentLine' && (cNode.value || '').indexOf('profile') > -1) {
args.state.gotProfileComment = true;
args.state.path = path;
if (cNode.type === 'CommentLine') {
const value = (cNode.value || '').replace(SPACE_REGEX, '');

if (value === 'profile') {
args.state.gotProfileComment = true;
args.state.path = path;
}

return;
}
}
Expand Down
17 changes: 17 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ it('wraps a function in profile block', () => {
expect(code).toMatchSnapshot();
});

it('does not wrap a function if comment does not exactly match', () => {
const example = `
const b = () => {
// wraps a profile
return 42;
}
const a = () => {
// bug in something else called profile
return b();
}
`;

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

it('caters to the case of multiple returns', () => {
const example = `
const a = () => {
Expand Down

0 comments on commit ca81569

Please sign in to comment.