Coder Social home page Coder Social logo

diffparser's Introduction

Build Status

Unified diff parser for Node and the browser

This project is a ES2015 version of https://github.com/sergeyt/parse-diff.

It comes with a full test suite and in addition to line numbers also provides position information as required by the GitHub Comments API.

import parse from 'diffparser';

const diff = `
diff --git a/file b/file
index 123..456 789
--- a/file
+++ b/file
@@ -1,2 +1,2 @@
- line1
+ line2
`;

parse(diff);

This will return an array (one entry per file) with the following structure:

[
  {
    "from": "file",
    "to": "file",
    "chunks": [
      {
        "content": "@@ -1,2 +1,2 @@",
        "changes": [
          {
            "type": "del",
            "del": true,
            "oldLine": 1,
            "position": 1,
            "content": "- line1"
          },
          {
            "type": "add",
            "add": true,
            "newLine": 1,
            "position": 2,
            "content": "+ line2"
          }
        ],
        "oldStart": 1,
        "oldLines": 2,
        "newStart": 1,
        "newLines": 2
      }
    ],
    "deletions": 1,
    "additions": 1,
    "index": [
      "123..456",
      "789"
    ]
  }
]

Optional Rename Detection

You can pass { findRenames: true } as option in order to detect renamed files:

const diff = `
diff --git a/bar b/bar
new file mode 100644
index 0000000..4e4b354
--- /dev/null
+++ b/bar
@@ -0,0 +1,2 @@
+this is a
+sample file
diff --git a/foo b/foo
deleted file mode 100644
index 4e4b354..0000000
--- a/foo
+++ /dev/null
@@ -1,2 +0,0 @@
-this is a
-sample file
`;

parse(diff, { findRenames: true });

This will return the following array:

[
  {
    "renamed": true,
    "from": "foo",
    "to": "bar"
  }
]

License

MIT

diffparser's People

Contributors

danthareja avatar fgnass avatar macklinu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

diffparser's Issues

Update ESLint

I'd like to update ESLint to the latest version (from v2 to v5) and track that in this issue. Some questions before tackling that:

  • ESLint isn't currently enforced for PRs (on Travis). Cool to add an npm script to run this on PRs? (npm run lint)
  • The airbnb ESLint plugin is also out of date. Do you have a strong opinion of what ESLint restrictions you'd like to enforce? Perhaps using a community standard like standard could be an option?
  • Do you have a preference on using something like prettier to enforce a consistent code style (which can also be integrated with ESLint)?

Line number does not consider context

Input:

@@ -36,6 +36,7 @@ export type SomeContext = {
   foo: bar,
 };

+import newdep from 'newdep';
 import {bla} from 'bla';
 import {qwe} from 'qwe';
 import {ertyu} from 'ertyu';

Output:

...
{
      "content": "@@ -36,6 +36,7 @@ export type SomeContext = {",
      "changes": [
        {
          "type": "normal",
          "normal": true,
          "position": 1,
          "content": "   foo: bar,"
        },
        {
          "type": "normal",
          "normal": true,
          "position": 2,
          "content": " };"
        },
        {
          "type": "add",
          "add": true,
          "ln": 36,
          "position": 3,
          "content": "+import newdep from 'newdep';"
        },
        {
          "type": "normal",
          "normal": true,
          "position": 4,
          "content": " import {bla} from 'bla';"
        },
        {
          "type": "normal",
          "normal": true,
          "position": 5,
          "content": " import {qwe} from 'qwe';"
        },
        {
          "type": "normal",
          "normal": true,
          "position": 6,
          "content": " import {ertyu} from 'ertyu';"
        }
      ],
      "oldStart": 36,
      "oldLines": 6,
      "newStart": 36,
      "newLines": 7
    },
...

There are 2 issues here:

  1. The ln property is only present for changed lines (type === add/del), but not in lines of type normal.
  2. When the ln property is present, it does not consider the number of context lines preceding it, rendering its value inaccurate. In the above example, the line marked with ln: 36 should really be ln: 38.

Ideally, parse would return a consistent format for each changed line, including the ln number, the calculation for which should also consider the extent of the surrounding context.

Parsing this diff fails

Hello,
I've been trying to get this diff parsed but I believe there is something in it that makes it fail.

What I see in my console is the following:

Uncaught (in promise) Error: Objects are not valid as a React child (found: TypeError: Cannot read property 'changes' of null). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of `DiffViewerMeta`.
    at invariant (invariant.js:44)
    at traverseAllChildrenImpl (traverseAllChildren.js:144)
    at traverseAllChildren (traverseAllChildren.js:172)
    at flattenChildren (flattenChildren.js:66)
    at ReactDOMComponent._reconcilerUpdateChildren (ReactMultiChild.js:202)
    at ReactDOMComponent._updateChildren (ReactMultiChild.js:310)
    at ReactDOMComponent.updateChildren (ReactMultiChild.js:297)
    at ReactDOMComponent._updateDOMChildren (ReactDOMComponent.js:942)
    at ReactDOMComponent.updateComponent (ReactDOMComponent.js:760)
    at ReactDOMComponent.receiveComponent (ReactDOMComponent.js:722)

Do you have documentation on how to contribute? Thanks.

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.