Package detail

esrap

sveltejs2.9mMIT1.4.9

Parse in reverse

readme

esrap

Parse in reverse. AST goes in, code comes out.

Usage

import { print } from 'esrap';

const { code, map } = print({
  type: 'Program',
  body: [
    {
      type: 'ExpressionStatement',
      expression: {
        callee: {
          type: 'Identifier',
          name: 'alert'
        },
        arguments: [
          {
            type: 'Literal',
            value: 'hello world!'
          }
        ]
      }
    }
  ]
});

console.log(code); // alert('hello world!');

If the nodes of the input AST have loc properties (e.g. the AST was generated with acorn with the locations option set), sourcemap mappings will be created.

Options

You can pass the following options:

const { code, map } = print(ast, {
  // Populate the `sources` field of the resulting sourcemap
  // (note that the AST is assumed to come from a single file)
  sourceMapSource: 'input.js',

  // Populate the `sourcesContent` field of the resulting sourcemap
  sourceMapContent: fs.readFileSync('input.js', 'utf-8'),

  // Whether to encode the `mappings` field of the resulting sourcemap
  // as a VLQ string, rather than an unencoded array. Defaults to `true`
  sourceMapEncodeMappings: false,

  // String to use for indentation — defaults to '\t'
  indent: '  ',

  // Whether to wrap strings in single or double quotes — defaults to 'single'.
  // This only applies to string literals with no `raw` value, which generally
  // means the AST node was generated programmatically, rather than parsed
  // from an original source
  quotes: 'single'
});

TypeScript

esrap can also print TypeScript nodes, assuming they match the ESTree-like @typescript-eslint/types.

Why not just use Prettier?

Because it's ginormous.

Developing

This repo uses pnpm. Once it's installed, do pnpm install to install dependencies, and pnpm test to run the tests.

License

MIT

changelog

esrap changelog

1.4.9

Patch Changes

  • bd045a3: fix: support function return type
  • bd045a3: fix: support decorator expression

1.4.8

Patch Changes

  • 61c9902: fix: only push with clause if attributes length is gt 0

1.4.7

Patch Changes

  • bab0228: fix: support import attributes

1.4.6

Patch Changes

  • 1caf836: fix: add missing spaces in interfaces
  • 49d433b: fix: correctly handle parenthesised FunctionExpressions
  • 76eabe8: fix: consistently escape escape characters
  • 9daf5dd: fix: correct indentation of TSModuleBlock

1.4.5

Patch Changes

  • 6a6eed9: fix: support TSImportType

1.4.4

Patch Changes

  • b683171: fix: support TSModuleDeclaration

1.4.3

Patch Changes

  • 030c3ec: fix: correctly escape \r

1.4.2

Patch Changes

  • faa865e: fix: including sourcemap mappings for block statement brackets

1.4.1

Patch Changes

  • 10c6156: fix: escape newlines when quoting strings

1.4.0

Minor Changes

  • d97e56f: feat: add option for quote and indentation styles

1.3.7

Patch Changes

  • 4c784f6: fix: move @changesets/cli to dev dependencies

1.3.6

Patch Changes

  • 333b32c: chore: verify changesets setup, again

1.3.5

Patch Changes

  • a6c0031: chore: verify changesets setup

1.3.4

  • Transfer repo to sveltejs

1.3.3

  • Support export * as (#16)
  • Support indexed access types (#18)

1.3.2

  • Loosen types (#15)

1.3.1

  • Fix types

1.3.0

  • Support TypeScript nodes (#13)

1.2.3

  • Wrap object expression statements in parentheses (#14)

1.2.2

  • Correctly handle arrow functions where body includes an object expression (#9)

1.2.1

  • Support default and namespace import in same declaration (#8)

1.2.0

  • Rewrite for better performance (#7)

1.1.1

  • Actually add the changes that were supposed to go in 1.1.0

1.1.0

  • Tweak output (#5)

1.0.3

  • Fix typo (#4)

1.0.2

  • Collapse empty bodies (#3)

1.0.1

  • Better types

1.0.0

  • First release