recast

  • Version 0.23.9
  • Published
  • 245 kB
  • 5 dependencies
  • MIT license

Install

npm i recast
yarn add recast
pnpm add recast

Overview

JavaScript syntax tree transformer, nondestructive pretty-printer, and automatic source map generator

Index

Functions

function parse

parse: (source: string, options?: Partial<Options>) => any;

    function prettyPrint

    prettyPrint: (
    node: types.ASTNode,
    options?: Options
    ) => import('./lib/printer').PrintResultType;
    • Print without attempting to reuse any original source code.

    function print

    print: (
    node: types.ASTNode,
    options?: Options
    ) => import('./lib/printer').PrintResultType;
    • Reprint a modified syntax tree using as much of the original source code as possible.

    function run

    run: (transformer: Transformer, options?: RunOptions) => void;
    • Convenient command-line interface (see e.g. example/add-braces).

    Interfaces

    interface Options

    interface Options extends DeprecatedOptions {}
    • All Recast API functions take second parameter with configuration options, documented in options.js

    property arrayBracketSpacing

    arrayBracketSpacing?: boolean;
    • Controls the printing of spaces inside array brackets. See: http://eslint.org/docs/rules/array-bracket-spacing false

    property arrowParensAlways

    arrowParensAlways?: boolean;
    • If you want parenthesis to wrap single-argument arrow function parameter lists, pass true for this option. false

    property flowObjectCommas

    flowObjectCommas?: boolean;
    • There are 2 supported syntaxes (, and ;) in Flow Object Types; The use of commas is in line with the more popular style and matches how objects are defined in JS, making it a bit more natural to write. true

    property inputSourceMap

    inputSourceMap?: string | null;
    • If you provide a source map that was generated from a previous call to recast.print as options.inputSourceMap, the old source map will be composed with the new source map. null

    property lineTerminator

    lineTerminator?: string;
    • Override this option to use a different line terminator, e.g. \r\n. require("os").EOL || "\n"

    property objectCurlySpacing

    objectCurlySpacing?: boolean;
    • Controls the printing of spaces inside object literals, destructuring assignments, and import/export specifiers. See: http://eslint.org/docs/rules/object-curly-spacing true

    property parser

    parser?: any;
    • If you want to use a different branch of esprima, or any other module that supports a .parse function, pass that module object to recast.parse as options.parser (legacy synonym: options.esprima). require("recast/parsers/esprima")

    property quote

    quote?: 'single' | 'double' | 'auto' | null;
    • If you want to override the quotes used in string literals, specify either "single", "double", or "auto" here ("auto" will select the one which results in the shorter literal) Otherwise, use double quotes. null

    property range

    range?: boolean;
    • If you want esprima to generate .range information (recast only uses .loc internally), pass true for this option. false

    property reuseWhitespace

    reuseWhitespace?: boolean;
    • The reprinting code leaves leading whitespace untouched unless it has to reindent a line, or you pass false for this option. true

    property sourceFileName

    sourceFileName?: string | null;
    • Pass a string as options.sourceFileName to recast.parse to tell the reprinter to keep track of reused code so that it can construct a source map automatically. null

    property sourceMapName

    sourceMapName?: string | null;
    • Pass a string as options.sourceMapName to recast.print, and (provided you passed options.sourceFileName earlier) the PrintResult of recast.print will have a .map property for the generated source map. null

    property sourceRoot

    sourceRoot?: string | null;
    • If provided, this option will be passed along to the source map generator as a root directory for relative source file paths. null

    property tabWidth

    tabWidth?: number;
    • Number of spaces the pretty-printer should use per tab for indentation. If you do not pass this option explicitly, it will be (quite reliably!) inferred from the original code. 4

    property tokens

    tokens?: boolean;
    • Whether to return an array of .tokens on the root AST node. true

    property tolerant

    tolerant?: boolean;
    • If you want esprima not to throw exceptions when it encounters non-fatal errors, keep this option true. true

    property trailingComma

    trailingComma?: boolean;
    • Controls the printing of trailing commas in object literals, array expressions and function parameters.

      This option could either be: * Boolean - enable/disable in all contexts (objects, arrays and function params). * Object - enable/disable per context.

      Example: trailingComma: { objects: true, arrays: true, parameters: false, }

      false

    property useTabs

    useTabs?: boolean;
    • If you really want the pretty-printer to use tabs instead of spaces, make this option true. false

    property wrapColumn

    wrapColumn?: number;
    • Some of the pretty-printer code (such as that for printing function parameter lists) makes a valiant attempt to prevent really long lines. You can adjust the limit by changing this option; however, there is no guarantee that line length will fit inside this limit. 74

    interface RunOptions

    interface RunOptions extends Options {}

      method writeback

      writeback: (code: string) => void;

        interface Transformer

        interface Transformer {}

          call signature

          (ast: types.ASTNode, callback: (ast: types.ASTNode) => void): void;

            Package Files (3)

            Dependencies (5)

            Dev Dependencies (17)

            Peer Dependencies (0)

            No peer dependencies.

            Badge

            To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

            You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/recast.

            • Markdown
              [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/recast)
            • HTML
              <a href="https://www.jsdocs.io/package/recast"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>