/// <summary> /// Iterates over all the code in the JavaScript file to get a list of all the functions declared in that file. /// </summary> internal IReadOnlyList <FunctionMapEntry> ParseSourceCode(StreamReader sourceCodeStreamReader, SourceMap sourceMap) { if (sourceCodeStreamReader == null) { return(null); } string sourceCode = sourceCodeStreamReader.ReadToEnd(); sourceCodeStreamReader.Close(); JSParser jsParser = new JSParser(); // We just want the AST, don't let AjaxMin do any optimizations CodeSettings codeSettings = new CodeSettings { MinifyCode = false }; Block block = jsParser.Parse(sourceCode, codeSettings); FunctionFinderVisitor functionFinderVisitor = new FunctionFinderVisitor(sourceMap); functionFinderVisitor.Visit(block); // Sort in descending order by start position. This allows the first result found in a linear search to be the "closest function to the [consumer's] source position". // // ATTN: It may be possible to do this with an ascending order sort, followed by a series of binary searches on rows & columns. // Our current profiles show the memory pressure being a bigger issue than the stack lookup, so I'm leaving this for now. functionFinderVisitor.FunctionMap.Sort((x, y) => y.StartSourcePosition.CompareTo(x.StartSourcePosition)); return(functionFinderVisitor.FunctionMap); }
/// <summary> /// Iterates over all the code in the JavaScript file to get a list of all the functions declared in that file. /// </summary> internal List <FunctionMapEntry> ParseSourceCode(StreamReader sourceCodeStreamReader) { if (sourceCodeStreamReader == null) { return(null); } string sourceCode = sourceCodeStreamReader.ReadToEnd(); sourceCodeStreamReader.Close(); JSParser jsParser = new JSParser(); // We just want the AST, don't let AjaxMin do any optimizations CodeSettings codeSettings = new CodeSettings { MinifyCode = false }; Block block = jsParser.Parse(sourceCode, codeSettings); FunctionFinderVisitor functionFinderVisitor = new FunctionFinderVisitor(); functionFinderVisitor.Visit(block); // Sort in descending order by start position functionFinderVisitor.FunctionMap.Sort((x, y) => y.StartSourcePosition.CompareTo(x.StartSourcePosition)); return(functionFinderVisitor.FunctionMap); }