|
| 1 | +// Parse accuracy benchmark against real-world Ruby projects |
| 2 | +// Downloads files from GitHub and measures error-free line percentage |
| 3 | + |
| 4 | +import {rubyLanguage} from "../dist/index.js" |
| 5 | +const parser = rubyLanguage.parser |
| 6 | + |
| 7 | +const FILES = [ |
| 8 | + { |
| 9 | + project: "Fastlane", |
| 10 | + file: "runner.rb", |
| 11 | + url: "https://raw.githubusercontent.com/fastlane/fastlane/master/fastlane/lib/fastlane/runner.rb", |
| 12 | + }, |
| 13 | + { |
| 14 | + project: "Grape", |
| 15 | + file: "api.rb", |
| 16 | + url: "https://raw.githubusercontent.com/ruby-grape/grape/master/lib/grape/api.rb", |
| 17 | + }, |
| 18 | + { |
| 19 | + project: "Jekyll", |
| 20 | + file: "site.rb", |
| 21 | + url: "https://raw.githubusercontent.com/jekyll/jekyll/master/lib/jekyll/site.rb", |
| 22 | + }, |
| 23 | + { |
| 24 | + project: "Devise", |
| 25 | + file: "devise.rb", |
| 26 | + url: "https://raw.githubusercontent.com/heartcombo/devise/main/lib/devise.rb", |
| 27 | + }, |
| 28 | + { |
| 29 | + project: "Sidekiq", |
| 30 | + file: "config.rb", |
| 31 | + url: "https://raw.githubusercontent.com/sidekiq/sidekiq/main/lib/sidekiq/config.rb", |
| 32 | + }, |
| 33 | + { |
| 34 | + project: "Rails", |
| 35 | + file: "query_methods.rb", |
| 36 | + url: "https://raw.githubusercontent.com/rails/rails/main/activerecord/lib/active_record/relation/query_methods.rb", |
| 37 | + }, |
| 38 | + { |
| 39 | + project: "Faker", |
| 40 | + file: "internet.rb", |
| 41 | + url: "https://raw.githubusercontent.com/faker-ruby/faker/main/lib/faker/default/internet.rb", |
| 42 | + }, |
| 43 | +] |
| 44 | + |
| 45 | +async function fetchFile(url) { |
| 46 | + const res = await fetch(url) |
| 47 | + if (!res.ok) throw new Error(`Failed to fetch ${url}: ${res.status}`) |
| 48 | + return await res.text() |
| 49 | +} |
| 50 | + |
| 51 | +function measureAccuracy(code) { |
| 52 | + const tree = parser.parse(code) |
| 53 | + const lines = code.split("\n") |
| 54 | + const totalLines = lines.length |
| 55 | + |
| 56 | + // Find which lines contain error nodes |
| 57 | + const errorLines = new Set() |
| 58 | + tree.iterate({ |
| 59 | + enter(node) { |
| 60 | + if (node.type.isError) { |
| 61 | + // Mark all lines this error spans |
| 62 | + const startLine = code.slice(0, node.from).split("\n").length |
| 63 | + const endLine = code.slice(0, node.to).split("\n").length |
| 64 | + for (let i = startLine; i <= endLine; i++) { |
| 65 | + errorLines.add(i) |
| 66 | + } |
| 67 | + } |
| 68 | + }, |
| 69 | + }) |
| 70 | + |
| 71 | + const cleanLines = totalLines - errorLines.size |
| 72 | + const accuracy = (cleanLines / totalLines) * 100 |
| 73 | + return { totalLines, errorLines: errorLines.size, cleanLines, accuracy } |
| 74 | +} |
| 75 | + |
| 76 | +console.log("Fetching files and measuring parse accuracy...\n") |
| 77 | + |
| 78 | +const results = [] |
| 79 | +for (const entry of FILES) { |
| 80 | + try { |
| 81 | + const code = await fetchFile(entry.url) |
| 82 | + const stats = measureAccuracy(code) |
| 83 | + results.push({ ...entry, ...stats }) |
| 84 | + console.log( |
| 85 | + `${entry.project.padEnd(12)} ${entry.file.padEnd(22)} ${String(stats.totalLines).padStart(5)} lines ${stats.accuracy.toFixed(1)}%` |
| 86 | + ) |
| 87 | + } catch (e) { |
| 88 | + console.log(`${entry.project.padEnd(12)} FAILED: ${e.message}`) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// Sort by accuracy descending |
| 93 | +results.sort((a, b) => b.accuracy - a.accuracy) |
| 94 | + |
| 95 | +console.log("\n--- README table (sorted by accuracy) ---\n") |
| 96 | +console.log("| Project | File | Lines | Accuracy |") |
| 97 | +console.log("|---------|------|-------|----------|") |
| 98 | +for (const r of results) { |
| 99 | + console.log( |
| 100 | + `| ${r.project} | ${r.file} | ${r.totalLines} | **${r.accuracy.toFixed(1)}%** |` |
| 101 | + ) |
| 102 | +} |
0 commit comments