src/trace.js
/**
     * Determines where a function was defined within the source code.
     * @param {(Function|string)} func A function reference or serialized
     * function definition.
     * @return {?Object.<string, (string|number)>} An object containing
     * the url, line, and column number of the defined function.
     */
Determines where a function was defined within the source code.
@param {(Function|string)} func A function reference or serialized
function definition.
@return {?Object.} An object containing
the url, line, and column number of the defined function.
Determines where a function was defined within the source code . @param { ( Function|string ) } func A function reference or serialized function definition . @return { ?Object . } An object containing the url line and column number of the defined function .
function findSourceByFunctionBody(func) {
        var urls = [window.location.href],
            scripts = document.getElementsByTagName('script'),
            body,
            code = '' + func,
            codeRE = /^function(?:\s+([\w$]+))?\s*\(([\w\s,]*)\)\s*\{\s*(\S[\s\S]*\S)\s*\}\s*$/,
            eventRE = /^function on([\w$]+)\s*\(event\)\s*\{\s*(\S[\s\S]*\S)\s*\}\s*$/,
            re,
            parts,
            result;
        for (var i = 0; i < scripts.length; ++i) {
            var script = scripts[i];
            if (script.src) {
                urls.push(script.src);
            }
        }
        if (!(parts = codeRE.exec(code))) {
            re = new RegExp(escapeRegExp(code).replace(/\s+/g, '\\s+'));
        }
        else {
            var name = parts[1] ? '\\s+' + parts[1] : '',
                args = parts[2].split(',').join('\\s*,\\s*');
            body = escapeRegExp(parts[3]).replace(/;$/, ';?'); 
            re = new RegExp('function' + name + '\\s*\\(\\s*' + args + '\\s*\\)\\s*{\\s*' + body + '\\s*}');
        }
        if ((result = findSourceInUrls(re, urls))) {
            return result;
        }
        if ((parts = eventRE.exec(code))) {
            var event = parts[1];
            body = escapeCodeAsRegExpForMatchingInsideHTML(parts[2]);
            re = new RegExp('on' + event + '=[\\\'"]\\s*' + body + '\\s*[\\\'"]', 'i');
            if ((result = findSourceInUrls(re, urls[0]))) {
                return result;
            }
            re = new RegExp(body);
            if ((result = findSourceInUrls(re, urls))) {
                return result;
            }
        }
        return null;
    }
function findSourceByFunctionBody ( func ) { var urls = [ window . location . href ] , scripts = document . getElementsByTagName ( 'script' ) , body , code = '' + func , codeRE = / ^function(?:\s+([\w$]+))?\s*\(([\w\s,]*)\)\s*\{\s*(\S[\s\S]*\S)\s*\}\s*$ / , eventRE = / ^function on([\w$]+)\s*\(event\)\s*\{\s*(\S[\s\S]*\S)\s*\}\s*$ / , re , parts , result ; for ( var i = 0 ; i < scripts . length ; ++ i ) { var script = scripts [ i ] ; if ( script . src ) { urls . push ( script . src ) ; } } if ( ! ( parts = codeRE . exec ( code ) ) ) { re = new RegExp ( escapeRegExp ( code ) . replace ( / \s+ / g , '\\s+' ) ) ; } else { var name = parts [ 1 ] ? '\\s+' + parts [ 1 ] : '' , args = parts [ 2 ] . split ( ',' ) . join ( '\\s*,\\s*' ) ; body = escapeRegExp ( parts [ 3 ] ) . replace ( / ;$ / , ';?' ) ; re = new RegExp ( 'function' + name + '\\s*\\(\\s*' + args + '\\s*\\)\\s*{\\s*' + body + '\\s*}' ) ; } if ( ( result = findSourceInUrls ( re , urls ) ) ) { return result ; } if ( ( parts = eventRE . exec ( code ) ) ) { var event = parts [ 1 ] ; body = escapeCodeAsRegExpForMatchingInsideHTML ( parts [ 2 ] ) ; re = new RegExp ( 'on' + event + '=[\\\'"]\\s*' + body + '\\s*[\\\'"]' , 'i' ) ; if ( ( result = findSourceInUrls ( re , urls [ 0 ] ) ) ) { return result ; } re = new RegExp ( body ) ; if ( ( result = findSourceInUrls ( re , urls ) ) ) { return result ; } } return null ; }
Determines where a function was defined within the source code.
Determines where a function was defined within the source code .
// not sure if this is really necessary, but I don’t have a test // corpus large enough to confirm that and it was in the original. // semicolon is inserted if the function ends with a comment.replace(/\s+/g, '\\s+'); // look for a normal function definition // look for an old-school event handler function // look for a function defined in HTML as an onXXX handler // look for ???