json-estree-ast

1.0.1 • Public • Published

ESTree-compatible JSON AST parser

A small wrapper around json-to-ast that translates its output to the ESTree Spec, which makes it usable for example as a jscodeshift parser.

Installation

npm install json-estree-ast

Usage

const { parse } = require('json-estree-ast');

const options = {
  loc: true,  // include source location information. Default `true`
  source: 'data.json'  // include source information. Default `null`
};

parse('{"a": 1}', options);

Output

{
  type: 'Program',
  body: [
    {
      type: 'ObjectExpression',
      properties: [
        {
          type: 'Property',
          key: {
            type: 'Identifier',
            name: 'a',
            raw: '"a"',
            loc: {
              start: { line: 1, column: 1 },
              end: { line: 1, column: 4 },
              source: 'data.json'
            }
          },
          kind: 'init',
          value: {
            type: 'Literal',
            value: 1,
            raw: '1',
            loc: {
              start: { line: 1, column: 6 },
              end: { line: 1, column: 7 },
              source: 'data.json'
            }
          },
          loc: {
            start: { line: 1, column: 1 },
            end: { line: 1, column: 7 },
            source: 'data.json'
          }
        }
      ],
      loc: {
        start: { line: 1, column: 0 },
        end: { line: 1, column: 8 },
        source: 'data.json'
      }
    }
  ],
  loc: {
    start: { line: 1, column: 0 },
    end: { line: 1, column: 8 },
    source: 'data.json'
  }
}

Node types

ObjectExpression

{
  type: 'ObjectExpression',
  properties: Property[],
  loc?: SourceLocation
}

Property

{
  type: 'Property',
  key: Identifier,
  kind: 'init',
  value: ObjectExpression | ArrayExpression | Literal,
  loc?: SourceLocation
}

Identifier

{
  type: 'Identifier',
  name: string,
  raw: string,
  loc?: SourceLocation
}

ArrayExpression

{
  type: 'ArrayExpression',
  children: (ObjectExpression | ArrayExpression | Literal)[],
  loc?: SourceLocation
}

Literal

{
  type: 'Literal',
  value: string | number | boolean | null,
  raw: string,
  loc?: SourceLocation
}

SourceLocation

{
  source?: string,
  start: SourcePosition,
  end: SourcePosition
}

SourcePosition

{
  line: number,
  column: number
}

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i json-estree-ast

Weekly Downloads

1,456

Version

1.0.1

License

MIT

Unpacked Size

4.98 kB

Total Files

4

Last publish

Collaborators

  • eemeli