Babel Comment Extractor

1.0.0 · abandoned · verified Tue Apr 21

The `babel-extract-comments` library is a utility designed to parse JavaScript code and extract its associated comments, leveraging Babel's (specifically, its earlier parser Babylon) capabilities. It provides methods to process either a JavaScript string or a file path, returning an array of structured comment objects. Each object in the array details the comment's `type` (e.g., 'CommentBlock', 'CommentLine'), its raw `value`, and precise `start` and `end` character positions, along with `loc` (source location data including line and column numbers). The package's current and only major version is 1.0.0, last published in February 2018. Due to its age and lack of subsequent updates, it exclusively supports CommonJS module loading patterns and relies on older Babel parsing internals, making it unsuitable for projects requiring modern JavaScript syntax parsing or native ESM integration. Users should be aware of its abandoned status and potential compatibility issues with contemporary JavaScript language features.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates extracting comments from both a JavaScript string and a file using the `extract` function and its `.file` method. It showcases different comment types and the structure of the returned comment objects.

const extract = require('babel-extract-comments');
const fs = require('fs');

const jsString = `
// This is a line comment
/**
 * This is a block comment.
 * It can span multiple lines.
 */
const myVariable = 123; // Inline comment

/* Another block comment */
function myFunction() {
  /*
   * Multi-line
   * function comment
   */
  console.log('Hello');
}
`;

// Extract comments from a string
const stringComments = extract(jsString);
console.log('Comments from string:', JSON.stringify(stringComments, null, 2));

// Create a dummy file for .file method demonstration
const filePath = './temp-script.js';
fs.writeFileSync(filePath, jsString);

// Extract comments from a file
const fileComments = extract.file(filePath);
console.log('\nComments from file:', JSON.stringify(fileComments, null, 2));

// Clean up the dummy file
fs.unlinkSync(filePath);

view raw JSON →