Tweak and add tests to babel-helper-annotate-as-pure (#7245)

This commit is contained in:
Brian Ng 2018-01-20 12:29:38 -06:00 committed by Henry Zhu
parent 064c17e03f
commit 5ce54799ff
2 changed files with 39 additions and 7 deletions

View File

@ -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;

View File

@ -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__",
},
]);
});
});