From ca8156966e6ca7f46252658f422abc6d881ef335 Mon Sep 17 00:00:00 2001 From: Kashyap Date: Mon, 30 Oct 2017 12:21:19 +0530 Subject: [PATCH] Check for exact match for profile --- __snapshots__/test.js.snap | 13 +++++++++++++ index.js | 13 ++++++++++--- test.js | 17 +++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/__snapshots__/test.js.snap b/__snapshots__/test.js.snap index d33b768..cf195bf 100644 --- a/__snapshots__/test.js.snap +++ b/__snapshots__/test.js.snap @@ -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 = () => { diff --git a/index.js b/index.js index 1ddb305..a95440c 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,7 @@ const t = require('babel-types'); +const SPACE_REGEX = /\s+/; + const BlockVisitor = function (path, args) { const body = path.node.body || []; @@ -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; } } diff --git a/test.js b/test.js index ead9fa8..6e07e25 100644 --- a/test.js +++ b/test.js @@ -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 = () => {