-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support annotating model fixture files (#110)
This PR fixes adding annotation to YML files, like fixtures. The Ruby parser introduced in #72 and #82 did not support YML files.
- Loading branch information
Showing
17 changed files
with
390 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# frozen_string_literal: true | ||
|
||
require "psych" | ||
|
||
module AnnotateRb | ||
module ModelAnnotator | ||
module FileParser | ||
class YmlParser | ||
class << self | ||
def parse(string) | ||
_parser = new(string).tap(&:parse) | ||
end | ||
end | ||
|
||
attr_reader :comments, :starts, :ends | ||
|
||
def initialize(input) | ||
@input = input | ||
@comments = [] | ||
@starts = [] | ||
@ends = [] | ||
end | ||
|
||
def parse | ||
parse_comments | ||
parse_yml | ||
end | ||
|
||
private | ||
|
||
def parse_comments | ||
# Adds 0-indexed line numbers | ||
@input.split($/).each_with_index do |line, line_no| | ||
if line.strip.starts_with?("#") | ||
@comments << [line, line_no] | ||
end | ||
end | ||
end | ||
|
||
def parse_yml | ||
# https://docs.ruby-lang.org/en/master/Psych.html#module-Psych-label-Reading+to+Psych-3A-3ANodes-3A-3AStream+structure | ||
parser = Psych.parser | ||
parser.parse(@input) | ||
|
||
stream = parser.handler.root | ||
|
||
if stream.children.any? | ||
doc = stream.children.first | ||
@starts << [nil, doc.start_line] | ||
@ends << [nil, doc.end_line] | ||
else | ||
# When parsing a yml file, streamer returns an instance of `Psych::Nodes::Stream` which is a subclass of | ||
# `Psych::Nodes::Node`. It along with children nodes, implement #start_line and #end_line. | ||
# | ||
# When parsing input that is only comments, the parser counts #start_line as the start of the file being | ||
# line 0. | ||
# | ||
# What we really want is where the "start" of the yml file would happen, which would be after comments. | ||
# This is stream#end_line. | ||
@starts << [nil, stream.end_line] | ||
@ends << [nil, stream.end_line] | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
module AnnotateRb | ||
module ModelAnnotator | ||
class FileToParserMapper | ||
class UnsupportedFileTypeError < StandardError; end | ||
|
||
MAP = { | ||
".rb" => FileParser::CustomParser, | ||
".yml" => FileParser::YmlParser | ||
}.freeze | ||
|
||
class << self | ||
def map(file_name) | ||
extension = File.extname(file_name).downcase | ||
parser = MAP[extension] | ||
|
||
raise UnsupportedFileTypeError, "File '#{file_name}' does not have a supported file type." if parser.nil? | ||
|
||
parser | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.