|
| 1 | +const initialSource = `# frozen_string_literal: true |
| 2 | +
|
| 3 | +require "json" |
| 4 | +require "pp" |
| 5 | +require "prettyprint" |
| 6 | +require "ripper" |
| 7 | +require "stringio" |
| 8 | +
|
| 9 | +require_relative "syntax_tree/formatter" |
| 10 | +require_relative "syntax_tree/node" |
| 11 | +require_relative "syntax_tree/parser" |
| 12 | +require_relative "syntax_tree/prettyprint" |
| 13 | +require_relative "syntax_tree/version" |
| 14 | +require_relative "syntax_tree/visitor" |
| 15 | +require_relative "syntax_tree/visitor/json_visitor" |
| 16 | +require_relative "syntax_tree/visitor/pretty_print_visitor" |
| 17 | +
|
| 18 | +module SyntaxTree |
| 19 | + # This holds references to objects that respond to both #parse and #format |
| 20 | + # so that we can use them in the CLI. |
| 21 | + HANDLERS = {} |
| 22 | + HANDLERS.default = SyntaxTree |
| 23 | +
|
| 24 | + # This is a hook provided so that plugins can register themselves as the |
| 25 | + # handler for a particular file type. |
| 26 | + def self.register_handler(extension, handler) |
| 27 | + HANDLERS[extension] = handler |
| 28 | + end |
| 29 | +
|
| 30 | + # Parses the given source and returns the syntax tree. |
| 31 | + def self.parse(source) |
| 32 | + parser = Parser.new(source) |
| 33 | + response = parser.parse |
| 34 | + response unless parser.error? |
| 35 | + end |
| 36 | +
|
| 37 | + # Parses the given source and returns the formatted source. |
| 38 | + def self.format(source) |
| 39 | + formatter = Formatter.new(source, []) |
| 40 | + parse(source).format(formatter) |
| 41 | +
|
| 42 | + formatter.flush |
| 43 | + formatter.output.join |
| 44 | + end |
| 45 | +
|
| 46 | + # Returns the source from the given filepath taking into account any potential |
| 47 | + # magic encoding comments. |
| 48 | + def self.read(filepath) |
| 49 | + encoding = |
| 50 | + File.open(filepath, "r") do |file| |
| 51 | + header = file.readline |
| 52 | + header += file.readline if header.start_with?("#!") |
| 53 | + Ripper.new(header).tap(&:parse).encoding |
| 54 | + end |
| 55 | +
|
| 56 | + File.read(filepath, encoding: encoding) |
| 57 | + end |
| 58 | +end |
| 59 | +`; |
| 60 | + |
| 61 | +export default initialSource; |
0 commit comments