Coder Social home page Coder Social logo

neuroo / json-ast Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vtrushin/json-to-ast

20.0 20.0 7.0 2.8 MB

A tolerant JSON parser for Node

Home Page: https://www.npmjs.com/package/json-ast

License: MIT License

JavaScript 100.00%
error-recovery javascript json json-parser node tolerant-parser

json-ast's People

Contributors

ecutdavid avatar lahmatiy avatar neuroo avatar vtrushin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

json-ast's Issues

Tests are failing

Repro steps:

  1. git clone https://github.com/neuroo/json-ast/issues/new
  2. npm install
  3. npm run coverage

Expected:
Pass

Actual:

  ..............................

  29 passing (78ms)
  1 failing

  1) Test cases multi-line-comments-in-object:

      AssertionError [ERR_ASSERTION]: asts are not equal
      + expected - actual

Double-counts lines under certain (Windows) line-ending conditions

I discovered while traversing a produced AST that the node positions extended beyond the length of the file, nearly doubling the line length. After poking around and testing a hypothesis, I discovered that by normalizing lines endings to "\n", the line count returned to normal.

Operating System: Windows (works as expected on Mac)
Line ending: Windows standard (\r\n)

JSON source in question:

{
    "Snippet snippet": {
        "prefix": "snippet",
        "body": [
            "\"$1\": {",
            "\t\"prefix\": \"$2\",",
            "\t\"body\": [",
            "\t\t$3",
            "\t],",
            "\t\"description\": \"$4\"",
            "},"
        ],
        "description": "A snippet for creating new snippets"
    }
}

The normalization fix I applied in my parser abstraction is as follows:

function normalizeSourceLineEndings(source) {
    return source.split(/\r?\n/).join('\n');
}

I know this project is old, and potentially unmaintained, but I figured this issue would, at least, help someone else if they encountered a similar issue.

Thank you for building this.

Cheers!

AST Printer with comments?

I would like to use this package to pars and then pretty print JSON with comments. How can I do that? Using AST.JsonDocument.toJSON(ast) and similar methods does not sustain comments

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.