|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require 'unparser' |
| 5 | +require 'prism' |
| 6 | +require 'fileutils' |
| 7 | +require 'pathname' |
| 8 | + |
| 9 | +module PrismParser |
| 10 | + PARSER = 'ruby/prism' |
| 11 | + PARSER_VERSION = Prism::VERSION |
| 12 | + PARSER_PATH = Pathname('tmp/parser-prism') |
| 13 | + |
| 14 | + PRISM_INVALID = Set[ |
| 15 | + 'def __ENCODING__.a', |
| 16 | + 'def __FILE__.a', |
| 17 | + 'def __LINE__.a', |
| 18 | + '\777', |
| 19 | + 'ち\xE3\x81\xFF', |
| 20 | + '\x8E\x01', |
| 21 | + 'a\xE9b', |
| 22 | + 'a\247b', |
| 23 | + '\xE3\xD3\x8B\xE3\x83\xBC\x83\xE3\x83\xE3\x82\xB3\xA3\x82\x99', |
| 24 | + 'hello \u{fc}', |
| 25 | + '# encoding: sjis' |
| 26 | + ].freeze |
| 27 | + |
| 28 | + PRISM_TODO = Set[ |
| 29 | + '0..', |
| 30 | + 'def a(...); "foo#{b(...)}"; end', |
| 31 | + '(a,), = []', |
| 32 | + 'foo = 1, 2 rescue nil', |
| 33 | + '` |
| 34 | +foo\ |
| 35 | +b\nar |
| 36 | +`', |
| 37 | + ].freeze |
| 38 | + |
| 39 | + PRISM_NO_ROUND_TRIP = (PRISM_INVALID + PRISM_TODO).to_set.freeze |
| 40 | + |
| 41 | + private_constant :PRISM_INVALID, :PRISM_TODO |
| 42 | + |
| 43 | + class << self |
| 44 | + def prepare # rubocop:disable Metrics |
| 45 | + FileUtils.rm_rf("#{PARSER_PATH}/test/prism/fixtures-tmp") |
| 46 | + Dir.mkdir("#{PARSER_PATH}/test/prism/fixtures-tmp") |
| 47 | + Dir.glob("#{PARSER_PATH}/test/prism/fixtures/**/*.txt") |
| 48 | + .reject { _1.include?('unparser/') } |
| 49 | + .each do |path| |
| 50 | + examples = File.read(path).split(/(?<=\n\n)/).reject do |example| |
| 51 | + PRISM_NO_ROUND_TRIP.any? { |snippet| example.include?(snippet) } |
| 52 | + end |
| 53 | + path = Pathname(path).relative_path_from("#{PARSER_PATH}/test/prism/fixtures").to_s |
| 54 | + path = "#{PARSER_PATH}/test/prism/fixtures-tmp/#{path}" |
| 55 | + |
| 56 | + dirname = File.dirname(path) |
| 57 | + unless File.directory?(dirname) |
| 58 | + FileUtils.mkdir_p(dirname) |
| 59 | + end |
| 60 | + File.write(path, examples.join("\n\n")) |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + def target_glob |
| 65 | + "#{PARSER_PATH}/test/prism/fixtures-tmp/**/*.txt" |
| 66 | + end |
| 67 | + |
| 68 | + def excludes |
| 69 | + %w[ |
| 70 | + spanning_heredoc |
| 71 | + heredocs_with_fake_newlines |
| 72 | + heredocs_nested |
| 73 | + ].to_set { |file| "#{PARSER_PATH}/test/prism/fixtures-tmp/#{file}.txt" } |
| 74 | + end |
| 75 | + |
| 76 | + end |
| 77 | +end |
| 78 | + |
| 79 | +unless PrismParser::PARSER_PATH.exist? |
| 80 | + Kernel.system( |
| 81 | + *%W[ |
| 82 | + git |
| 83 | + clone |
| 84 | + https://github.com/#{PrismParser::PARSER} |
| 85 | + #{PrismParser::PARSER_PATH} |
| 86 | + ], |
| 87 | + exception: true |
| 88 | + ) |
| 89 | +end |
| 90 | + |
| 91 | +Dir.chdir(PrismParser::PARSER_PATH) do |
| 92 | + Kernel.system( |
| 93 | + *%W[ |
| 94 | + git |
| 95 | + checkout |
| 96 | + v#{PrismParser::PARSER_VERSION} |
| 97 | + ], |
| 98 | + exception: true |
| 99 | + ) |
| 100 | + Kernel.system(*%w[git clean --force -d -X], exception: true) |
| 101 | +end |
| 102 | + |
| 103 | +ignores_cli_option = PrismParser.excludes.flat_map { |file| ['--ignore', file] } |
| 104 | + |
| 105 | +PrismParser.prepare |
| 106 | +exit Unparser::CLI.run([PrismParser.target_glob, *ignores_cli_option]) |
0 commit comments