JSFiddle - React, Tailwind, and code Playground

js docs

by jcubed111

CSS

body{
    font-family: 'Consolas', monospace;
}

h2{
    font-weight: normal;
}

JavaScript

class TestClass{
	constructor(a, b, c) {
    	// doc string
        // more doc string
        hello;
        // not doc string
    }
    
    hello(arg1, arg2) {
    	// doc string
        /* still doc string */
        // more doc string
        hello;
        // not doc string
    }
    
    ello(h) {
    	// doc string
        /* still doc string */ stuff;
        // not doc string
    }
    
    static bye(one, two, three=7) {
    	h; // not doc string
    }
}

class TestClass2 extends TestClass{
	constructor() {
    	super();
    	// test class 2 constructor
    }
}


function extractParamsFromMethodText(text) {
	let match = text.match(/^[a-zA-Z0-9]*\((.*?)\)\s*(\{|=>)/s);
    if(match) return match[1];
    return '';
}

function extractDocsFromMethodText(text) {
	let match = text.match(/\)\s*\{((\s+|\/\/[^\n]*\n|\/\*.*?\*\/)*)/s);
    if(!match) return '';
    let commentText = '\n' + match[1];
    commentText = commentText.replace(/\n[ \t]*\/\//gs, '\n');
    commentText = commentText.replace(/\/\*|\*\//gs, '');
    commentText = commentText.replace(/(^\s*\n+)|(\n+\s*$)/gs, ''); // trim newlines
    //commentText = commentText.split('\n').map(l => l.trim()).filter(l => l.length).join('<br/>');
    
    return commentText;
}

function documentMethod(docs, className, c, methodName, method, isStatic) {
	if(!c.hasOwnProperty(methodName)) return;
    if(!(method instanceof Function)) return;
    let methodText = method.toString();
    let name;
    if(isStatic) {
    	name = className + '::' + methodName;
    }else{
    	if(methodName == 'constructor') {
        	name = className;
            isStatic = true;
            methodName = '';
            if(methodText.indexOf('class') == 0) {
                let match = methodText.match(/^class\s+[a-zA-Z0-9]+(\s+extends\s+[a-zA-Z0-9]+)?\s*\{\s*(constructor\s*\(.*)$/s);
                if(match) {
                    methodText = match[2];
                }else{
                    methodText = 'constructor() {}';
 ...