or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-babel-messages

Collection of debug messages used by Babel for consistent error reporting and debugging output across the JavaScript transpilation ecosystem.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/babel-messages@6.23.x

To install, run

npx @tessl/cli install tessl/npm-babel-messages@6.23.0

index.mddocs/

babel-messages

babel-messages provides a centralized collection of debug and error messages used throughout the Babel JavaScript transpilation ecosystem. It offers predefined message templates with placeholder support for dynamic content insertion, ensuring consistent error reporting and debugging output across all Babel tools and plugins.

Package Information

  • Package Name: babel-messages
  • Package Type: npm
  • Language: JavaScript (with Flow type annotations)
  • Installation: npm install babel-messages

Core Imports

import * as messages from 'babel-messages';
import { MESSAGES, get, parseArgs } from 'babel-messages';

For CommonJS:

const messages = require('babel-messages');
const { MESSAGES, get, parseArgs } = require('babel-messages');

Basic Usage

import { get, MESSAGES } from 'babel-messages';

// Get a simple message
const message = get('readOnly', 'someVariable');
// Result: "someVariable is read-only"

// Access all messages directly
console.log(MESSAGES.didYouMean);
// Result: "Did you mean $1?"

// Custom argument parsing
import { parseArgs } from 'babel-messages';
const args = parseArgs([{name: 'test'}, 42, null]);
// Result: ['{"name":"test"}', '42', 'null']

Capabilities

Message Retrieval

Retrieves formatted messages with placeholder replacement for consistent error reporting across Babel ecosystem.

/**
 * Get a message with $1, $2, etc. placeholders replaced by arguments
 * @param key - Message key from MESSAGES object
 * @param args - Variable arguments to replace placeholders
 * @returns Formatted message string with placeholders replaced
 * @throws ReferenceError if key is not found in MESSAGES object
 */
function get(key: string, ...args: Array<any>): string;

Message Templates

Collection of predefined debug and error message templates covering all Babel subsystems.

const MESSAGES: {
  // General/Core Messages
  tailCallReassignmentDeopt: string;
  readOnly: string;
  didYouMean: string;
  codeGeneratorDeopt: string;
  missingTemplatesDirectory: string;
  unsupportedOutputType: string;
  illegalMethodName: string;
  lostTrackNodePath: string;
  
  // Class-related Messages
  classesIllegalBareSuper: string;
  classesIllegalSuperCall: string;
  
  // Scope/Variable Messages
  scopeDuplicateDeclaration: string;
  undeclaredVariable: string;
  undeclaredVariableType: string;
  undeclaredVariableSuggestion: string;
  
  // Syntax/Parser Messages
  settersNoRest: string;
  noAssignmentsInForHead: string;
  expectedMemberExpressionOrIdentifier: string;
  invalidParentForThisNode: string;
  unknownForHead: string;
  
  // Module System Messages
  modulesIllegalExportName: string;
  modulesDuplicateDeclarations: string;
  
  // Traversal Messages
  traverseNeedsParent: string;
  traverseVerifyRootFunction: string;
  traverseVerifyVisitorProperty: string;
  traverseVerifyNodeType: string;
  
  // Plugin System Messages
  pluginNotObject: string;
  pluginNotFunction: string;
  pluginUnknown: string;
  pluginInvalidProperty: string;
};

Message Categories:

  • General/Core Messages (8): Function optimization, read-only variables, suggestions, code generation, template handling
  • Class-related Messages (2): Super call validation and bare super usage
  • Scope/Variable Messages (4): Declaration conflicts, undeclared variables, type references
  • Syntax/Parser Messages (5): Setter restrictions, for-loop constraints, expression validation, AST handling
  • Module System Messages (2): Export validation, module declaration conflicts
  • Traversal Messages (4): AST traversal requirements, visitor validation
  • Plugin System Messages (4): Plugin loading, validation, and property checks

Argument Processing

Converts various argument types to string representations for message formatting.

/**
 * Convert arguments to string representations for message formatting
 * @param args - Array of values to stringify  
 * @returns Array of string representations of the input values
 */
function parseArgs(args: Array<any>): Array<string>;

Conversion Strategy:

  1. Uses .inspect() method if available on the value
  2. Falls back to JSON.stringify() or string concatenation
  3. Uses Node.js util.inspect() as final fallback for complex objects

Usage Examples

Error Message Generation

import { get } from 'babel-messages';

// Generate specific error messages
const duplicateError = get('scopeDuplicateDeclaration', 'myVariable');
// "Duplicate declaration myVariable"

const pluginError = get('pluginNotFunction', '.babelrc', 'my-plugin', 'object');
// "Plugin my-plugin specified in .babelrc was expected to return a function but returned object"

const undeclaredWithSuggestion = get('undeclaredVariableSuggestion', 'varName', 'variableName');  
// "Reference to undeclared variable varName - did you mean variableName?"

Message Template Access

import { MESSAGES } from 'babel-messages';

// Access raw templates for custom processing
const template = MESSAGES.codeGeneratorDeopt;
// "Note: The code generator has deoptimised the styling of $1 as it exceeds the max of $2."

// Use with custom placeholder replacement
const customMessage = template.replace(/\$(\d+)/g, (match, num) => {
  return customArgs[parseInt(num) - 1];
});

Argument Stringification

import { parseArgs } from 'babel-messages';

// Handle complex objects
const complexArgs = [
  { ast: 'node', type: 'Identifier' },
  null,
  undefined,
  function myFunc() {},
  new Date()
];

const stringified = parseArgs(complexArgs);
// Results in appropriate string representations for each type

Error Handling

The get() function throws a ReferenceError when attempting to retrieve non-existent message keys:

import { get } from 'babel-messages';

try {
  get('nonExistentMessage');
} catch (error) {
  console.log(error instanceof ReferenceError); // true
  console.log(error.message); // 'Unknown message "nonExistentMessage"'
}