Coder Social home page Coder Social logo

common-comment-parser's Introduction

common-comment-parser

A universal comment parser (supports more than 30 languages)

Usage

npm install common-comment-parser
import parser from 'common-comment-parser';

const context = `// line1

const cc = 111;
/*
block1
*/
console.log(222);

console.log(11); /* block2 */ const c = 111; /* block3 */ console.log(111); // line2


const aaa = 111; // line3

const ddd = `//ddd//ddd//dddd`;
const eee = '//eee//eee//eee';
const fff = "//ddd//ddd//dddd";

const dd = `\`ddddd//dddd`;
const ee = '\'ddddd//dddd';
const ff = "\"ddddd//dddd";
`

const ext = 'js';

console.log(parser(context, ext));
// [
    {
        "type": "CommentLine",
        "value": " line1",
        "loc": {
            "start": {
                "line": 1,
                "column": 3
            },
            "end": {
                "line": 1,
                "column": 9
            }
        }
    },
    {
        "type": "CommentBlock",
        "value": "\nblock1\n",
        "loc": {
            "start": {
                "line": 4,
                "column": 2
            },
            "end": {
                "line": 6,
                "column": 1
            }
        }
    },
    {
        "type": "CommentBlock",
        "value": " block2 ",
        "loc": {
            "start": {
                "line": 9,
                "column": 19
            },
            "end": {
                "line": 9,
                "column": 27
            }
        }
    },
    {
        "type": "CommentBlock",
        "value": " block3 ",
        "loc": {
            "start": {
                "line": 9,
                "column": 47
            },
            "end": {
                "line": 9,
                "column": 55
            }
        }
    },
    {
        "type": "CommentLine",
        "value": " line2",
        "loc": {
            "start": {
                "line": 9,
                "column": 78
            },
            "end": {
                "line": 9,
                "column": 84
            }
        }
    },
    {
        "type": "CommentLine",
        "value": " line3",
        "loc": {
            "start": {
                "line": 12,
                "column": 19
            },
            "end": {
                "line": 12,
                "column": 25
            }
        }
    }
]

parser(context, ext)

  • context: What needs to be parsed

  • ext: File extension

AST

commentsAST {
  type: string;
  value: string;
  loc: {
    start: {
      line: number,
      column: number
    },
    end: {
      line: number,
      column: number
    }
  }
}

License

MIT

Copyright (c) 2019 蓝色的秋风

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.