Skip to content

Commit

Permalink
Tweak and add tests to babel-helper-annotate-as-pure (babel#7245)
Browse files Browse the repository at this point in the history
  • Loading branch information
existentialism authored and aminmarashi committed Mar 17, 2018
1 parent 1ee0cf6 commit dce31b4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
10 changes: 3 additions & 7 deletions packages/babel-helper-annotate-as-pure/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ import * as t from "@babel/types";

const PURE_ANNOTATION = "#__PURE__";

const isPureAnnotated = node => {
const { leadingComments } = node;
if (leadingComments === undefined) {
return false;
}
return leadingComments.some(comment => /[@#]__PURE__/.test(comment.value));
};
const isPureAnnotated = ({ leadingComments }) =>
leadingComments &&
leadingComments.some(comment => /[@#]__PURE__/.test(comment.value));

export default function annotateAsPure(pathOrNode) {
const node = pathOrNode.node || pathOrNode;
Expand Down
36 changes: 36 additions & 0 deletions packages/babel-helper-annotate-as-pure/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import annotateAsPure from "../";
import assert from "assert";

describe("@babel/helper-annotate-as-pure", () => {
it("will add leading comment", () => {
const node = {};
annotateAsPure(node);

assert.deepEqual(node.leadingComments, [
{
type: "CommentBlock",
value: "#__PURE__",
},
]);
});

it("will not add an extra leading comment", () => {
const node = {
leadingComments: [
{
type: "CommentBlock",
value: "#__PURE__",
},
],
};

annotateAsPure(node);

assert.deepEqual(node.leadingComments, [
{
type: "CommentBlock",
value: "#__PURE__",
},
]);
});
});

0 comments on commit dce31b4

Please sign in to comment.