Skip to content
Browse files

Merge branch 'better_line_matcher'

  • Loading branch information...
2 parents 2eb8583 + 837269e commit 711864ffdcb3131d3f798df5b7fc393e9db773cc @xenomuta committed
Showing with 68 additions and 26 deletions.
  1. +18 −18 lib/block_finder.coffee
  2. +17 −8 lib/coffee-trace.coffee
  3. +29 −0 test/test.js
  4. +4 −0 test/test2.coffee
View
36 lib/block_finder.coffee
@@ -5,7 +5,7 @@ ScoredString = require("./quicksilver_score")
module.exports = (file, trace_lines=[]) ->
# Read coffee-script source code from file
- source = fs.readFileSync(file).toString()
+ source = fs.readFileSync(file).toString('utf8')
# Map lines - Taken from @showell's https://github.com/showell/CoffeeScriptLineMatcher
cs_lines = source.split '\n'
@@ -14,30 +14,30 @@ module.exports = (file, trace_lines=[]) ->
# Get exception line
{line, source} = trace_lines.map((l)->l if l.crash_line).filter((l)->l)[0]
+ js_crash_line = source.replace(/\W+/g, ' ')
- # Return matching line, matching block or 0
- s_index = 0
- d_index = 0
-
- # Find matching block and try to find matching line too
- for match, idx in mapping
- # Found block, now try lines...
- if line > d_index and line <= d_index + match[1]
- js_crash_line = source.replace(/\W/g, '')
- block = {lines: cs_lines.slice(s_index, s_index + match[0]), start: s_index + 1, end: s_index + match[0] + 1, crash_line: '?' }
+ # Find matching block and try to find matching line as well
+ [s_start, d_start] = [0, 0]
+ for match in mapping
+ [s_end, d_end] = match
+ if [d_start...d_end].indexOf(line) > -1
+ block = {lines: cs_lines.slice(s_start, s_end), start: s_start, end: s_end }
+ # Make a compare on javascript crash line and each coffee-script line in block
res = { score: 0, line: 0 }
-
- # Make a fuzzy compare on javascript crash line and each coffee-script line in block
for cline, idx2 in block.lines
- score = new ScoredString(js_crash_line).score cline.replace(/\W/g, ''), 0
+ score = 0
+ for word in cline.replace(/\W+/g, ' ').split ' '
+ ++score if js_crash_line.indexOf(word) > -1
+ # Set the strongest matching line as possible match line
if score > res.score
res.score = score
- res.line = idx2 - 1
+ res.line = idx2 + 1
# Pick the highest scoring line match as crash line
- block.crash_line = if res.line > 0 then res.line + s_index else undefined
+ block.crash_line = if res.line > 0 then res.line + s_start else undefined
return block
- s_index += match[0]
- d_index += match[1]
+ [s_start, d_start] = [s_end, d_end]
+ return null
+
# ...otherwise, return same old sad and boring null
return null
View
25 lib/coffee-trace.coffee
@@ -36,16 +36,25 @@ module.exports = coffee_trace = (options={ascii_art:true})->
draw_coffee_cup() if options.ascii_art
console.error "\x1b[1;41;37m ❛●•・Coffee-Trace \x1b[0m"
- # If coffee-script crash line found then print source-code
+ # If coffee-script crash line or block found then print source-code
if (block = block_finder(filename, trace_lines))?
{start,end,lines,crash_line} = block
- console.error "\n\x1b[0;33m CoffeeScript:\x1b[0m\x1b[1;37m #{filename}\x1b[33m:\x1b[32m#{crash_line}\x1b[0m"
- for line, idx in lines
- i = start + idx
- _line_num = new Array((crash_line + margin).toString().length - i.toString().length + 1).join(' ') + i.toString()
- if i is crash_line
- console.error " \x1b[1;31m✘\x1b[36m #{_line_num}: \x1b[1;37m#{line}"
- else
+ if crash_line?
+ console.error "\n\x1b[0;33m CoffeeScript:\x1b[0m\x1b[1;37m #{filename}\x1b[33m:\x1b[32m#{crash_line}\x1b[0m"
+ coffee_lines = coffee_source.split('\n')
+ for i in [(crash_line - margin)..(crash_line + margin)]
+ _line_num = new Array((crash_line + margin).toString().length - i.toString().length + 1).join(' ') + i.toString()
+ continue if crash_line - margin < 1
+ line = coffee_lines[i - 1]
+ if i is crash_line
+ console.error " \x1b[1;31m✘\x1b[36m #{_line_num}: \x1b[1;37m#{line}"
+ else
+ console.error " \x1b[0;36m #{_line_num}: \x1b[1;30m#{line}"
+ else
+ console.error "\n\x1b[0;33m CoffeeScript:\x1b[0m\x1b[1;37m #{filename}\x1b[0m"
+ for line, idx in lines
+ i = start + idx
+ _line_num = new Array(end.toString().length - i.toString().length + 1).join(' ') + i.toString()
console.error " \x1b[0;36m #{_line_num}: \x1b[1;30m#{line}"
console.error "\n\x1b[0;33m Javascript:\x1b[0m\x1b[1;37m #{filename}\x1b[33m:\x1b[32m#{line_num}\x1b[33m:\x1b[32m#{line_col}\x1b[0m"
View
29 test/test.js
@@ -0,0 +1,29 @@
+(function() {
+ var test;
+
+ require('../');
+
+ test = function() {
+ var p, people, _i, _len, _ref, _results;
+ people = {
+ john: {
+ first_name: 'john',
+ last_name: 'doe'
+ },
+ mary: {
+ first_name: 'mary',
+ last_name: 'jane'
+ }
+ };
+ _ref = ['john', 'mary', 'josh'];
+ _results = [];
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ p = _ref[_i];
+ _results.push(console.log("Welcome", people[p].first_name, people[p].last_name, "!!!"));
+ }
+ return _results;
+ };
+
+ process.nextTick(test);
+
+}).call(this);
View
4 test/test2.coffee
@@ -0,0 +1,4 @@
+require("../.")
+
+[1..100].forEach (i) ->
+ setTimeout (-> console.log(i/tt)), Math.random() * 100

0 comments on commit 711864f

Please sign in to comment.
Something went wrong with that request. Please try again.