diff --git a/packages/babel-helper-annotate-as-pure/src/index.js b/packages/babel-helper-annotate-as-pure/src/index.js index 0148aa1cc0..f04cd31212 100644 --- a/packages/babel-helper-annotate-as-pure/src/index.js +++ b/packages/babel-helper-annotate-as-pure/src/index.js @@ -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; diff --git a/packages/babel-helper-annotate-as-pure/test/index.js b/packages/babel-helper-annotate-as-pure/test/index.js new file mode 100644 index 0000000000..d2c9a34f5d --- /dev/null +++ b/packages/babel-helper-annotate-as-pure/test/index.js @@ -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__", + }, + ]); + }); +});