UNPKG

1.47 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = annotateAsPure;
7
8var t = _interopRequireWildcard(require("@babel/types"));
9
10function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
11
12function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
13
14const PURE_ANNOTATION = "#__PURE__";
15
16const isPureAnnotated = ({
17 leadingComments
18}) => !!leadingComments && leadingComments.some(comment => /[@#]__PURE__/.test(comment.value));
19
20function annotateAsPure(pathOrNode) {
21 const node = pathOrNode["node"] || pathOrNode;
22
23 if (isPureAnnotated(node)) {
24 return;
25 }
26
27 t.addComment(node, "leading", PURE_ANNOTATION);
28}
\No newline at end of file