Helper function to annotate AST nodes with #__PURE__ comment for tree-shaking optimization
npx @tessl/cli install tessl/npm-babel--helper-annotate-as-pure@7.27.0Helper function to annotate AST nodes with #PURE comment for tree-shaking optimization. This utility enables bundlers to identify side-effect-free function calls and expressions, allowing unused code to be safely removed during the build process.
npm install @babel/helper-annotate-as-pure>=6.9.0import annotateAsPure from "@babel/helper-annotate-as-pure";Note: This package is published as an ES module with TypeScript definitions included. The package exports both the main function and TypeScript types through its exports field. It only provides a default export - there are no named exports available.
For CommonJS environments (with proper module loading):
const annotateAsPure = require("@babel/helper-annotate-as-pure").default;import annotateAsPure from "@babel/helper-annotate-as-pure";
import { Node } from "@babel/types";
// Annotating a direct AST node
const callExpression: Node = {
type: "CallExpression",
callee: { type: "Identifier", name: "myFunction" },
arguments: []
};
annotateAsPure(callExpression);
// Result: callExpression now has leadingComments: [{ type: "CommentBlock", value: "#__PURE__" }]
// Annotating via Babel traverse path
import traverse from "@babel/traverse";
traverse(ast, {
CallExpression(path) {
annotateAsPure(path); // Works with path objects too
}
});Adds a #__PURE__ leading comment to AST nodes to indicate they are side-effect-free for tree-shaking optimization.
/**
* Annotates a Babel AST node or path with a #__PURE__ comment
* @param pathOrNode - Either a @babel/types Node or a Babel traverse path containing a node
* @returns void
*/
export default function annotateAsPure(
pathOrNode: Node | { node: Node }
): void;Parameters:
pathOrNode: Can be either:
@babel/types Node object directlynode propertyBehavior:
#__PURE__ comments to the same node@__PURE__ and #__PURE__ annotation formats when checking for existing annotations@babel/types.addComment() internally to add the leading commentleadingComments property directlyUsage Examples:
import annotateAsPure from "@babel/helper-annotate-as-pure";
import * as t from "@babel/types";
// Example 1: Annotating a function call
const functionCall = t.callExpression(
t.identifier("calculate"),
[t.numericLiteral(42)]
);
annotateAsPure(functionCall);
console.log(functionCall.leadingComments);
// Output: [{ type: "CommentBlock", value: "#__PURE__" }]
// Example 2: Using with Babel traverse
import traverse from "@babel/traverse";
traverse(ast, {
CallExpression(path) {
// Only annotate specific function calls
if (t.isIdentifier(path.node.callee, { name: "safeFunction" })) {
annotateAsPure(path);
}
}
});
// Example 3: Preventing duplicates
const alreadyAnnotated = t.callExpression(t.identifier("test"), []);
annotateAsPure(alreadyAnnotated); // Adds comment
annotateAsPure(alreadyAnnotated); // Does nothing - prevents duplicate// From @babel/types - base interface for all AST nodes
interface Node {
type: string;
leadingComments?: Comment[] | null;
trailingComments?: Comment[] | null;
innerComments?: Comment[] | null;
// ... other AST node properties
}
// Comment structure added by annotation
interface Comment {
type: "CommentBlock" | "CommentLine";
value: string;
}
// Path object structure (from @babel/traverse)
interface TraversePath {
node: Node;
// ... other traverse path properties
}This package has the following runtime dependency:
@babel/types: For the addComment function and Node type definitionsDevelopment dependency (used in tests):
@babel/traverse: For path traversal in test scenarios#__PURE__ annotations on truly side-effect-free expressions