unparser 0.4.6 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -2
- data/bin/unparser +1 -1
- data/lib/unparser.rb +117 -62
- data/lib/unparser/ast.rb +0 -1
- data/lib/unparser/ast/local_variable_scope.rb +6 -76
- data/lib/unparser/buffer.rb +19 -16
- data/lib/unparser/cli.rb +84 -77
- data/lib/unparser/{cli/color.rb → color.rb} +0 -13
- data/lib/unparser/comments.rb +0 -26
- data/lib/unparser/constants.rb +4 -53
- data/lib/unparser/diff.rb +98 -0
- data/lib/unparser/dsl.rb +0 -32
- data/lib/unparser/emitter.rb +24 -425
- data/lib/unparser/emitter/alias.rb +2 -8
- data/lib/unparser/emitter/args.rb +45 -0
- data/lib/unparser/emitter/argument.rb +8 -166
- data/lib/unparser/emitter/array.rb +27 -0
- data/lib/unparser/emitter/array_pattern.rb +29 -0
- data/lib/unparser/emitter/assignment.rb +36 -127
- data/lib/unparser/emitter/begin.rb +9 -84
- data/lib/unparser/emitter/binary.rb +7 -20
- data/lib/unparser/emitter/block.rb +57 -41
- data/lib/unparser/emitter/case.rb +6 -48
- data/lib/unparser/emitter/case_guard.rb +27 -0
- data/lib/unparser/emitter/case_match.rb +40 -0
- data/lib/unparser/emitter/cbase.rb +1 -3
- data/lib/unparser/emitter/class.rb +6 -26
- data/lib/unparser/emitter/const_pattern.rb +24 -0
- data/lib/unparser/emitter/def.rb +7 -51
- data/lib/unparser/emitter/defined.rb +2 -12
- data/lib/unparser/emitter/dstr.rb +22 -0
- data/lib/unparser/emitter/dsym.rb +41 -0
- data/lib/unparser/emitter/flipflop.rb +11 -10
- data/lib/unparser/emitter/float.rb +29 -0
- data/lib/unparser/emitter/flow_modifier.rb +8 -55
- data/lib/unparser/emitter/for.rb +5 -19
- data/lib/unparser/emitter/hash.rb +74 -0
- data/lib/unparser/emitter/hash_pattern.rb +67 -0
- data/lib/unparser/emitter/hookexe.rb +5 -11
- data/lib/unparser/emitter/if.rb +9 -73
- data/lib/unparser/emitter/in_match.rb +21 -0
- data/lib/unparser/emitter/in_pattern.rb +34 -0
- data/lib/unparser/emitter/index.rb +21 -88
- data/lib/unparser/emitter/kwbegin.rb +31 -0
- data/lib/unparser/emitter/lambda.rb +0 -8
- data/lib/unparser/emitter/masgn.rb +20 -0
- data/lib/unparser/emitter/match.rb +3 -17
- data/lib/unparser/emitter/match_alt.rb +23 -0
- data/lib/unparser/emitter/match_as.rb +21 -0
- data/lib/unparser/emitter/match_rest.rb +26 -0
- data/lib/unparser/emitter/match_var.rb +19 -0
- data/lib/unparser/emitter/mlhs.rb +40 -0
- data/lib/unparser/emitter/module.rb +3 -9
- data/lib/unparser/emitter/op_assign.rb +12 -27
- data/lib/unparser/emitter/pin.rb +19 -0
- data/lib/unparser/emitter/primitive.rb +93 -0
- data/lib/unparser/emitter/range.rb +35 -0
- data/lib/unparser/emitter/regexp.rb +35 -0
- data/lib/unparser/emitter/repetition.rb +17 -57
- data/lib/unparser/emitter/rescue.rb +1 -97
- data/lib/unparser/emitter/root.rb +17 -1
- data/lib/unparser/emitter/send.rb +10 -219
- data/lib/unparser/emitter/simple.rb +33 -0
- data/lib/unparser/emitter/splat.rb +2 -18
- data/lib/unparser/emitter/super.rb +1 -29
- data/lib/unparser/emitter/undef.rb +1 -9
- data/lib/unparser/emitter/variable.rb +1 -31
- data/lib/unparser/emitter/xstr.rb +72 -0
- data/lib/unparser/emitter/yield.rb +1 -9
- data/lib/unparser/generation.rb +250 -0
- data/lib/unparser/node_details.rb +21 -0
- data/lib/unparser/node_details/send.rb +62 -0
- data/lib/unparser/node_helpers.rb +45 -6
- data/lib/unparser/validation.rb +151 -0
- data/lib/unparser/writer.rb +15 -0
- data/lib/unparser/writer/binary.rb +99 -0
- data/lib/unparser/writer/dynamic_string.rb +233 -0
- data/lib/unparser/writer/resbody.rb +40 -0
- data/lib/unparser/writer/rescue.rb +39 -0
- data/lib/unparser/writer/send.rb +124 -0
- data/lib/unparser/{emitter → writer}/send/attribute_assignment.rb +11 -26
- data/lib/unparser/writer/send/binary.rb +27 -0
- data/lib/unparser/writer/send/conditional.rb +25 -0
- data/lib/unparser/writer/send/regular.rb +33 -0
- data/lib/unparser/{emitter → writer}/send/unary.rb +10 -17
- metadata +151 -100
- data/.circleci/config.yml +0 -49
- data/.gitignore +0 -37
- data/.rspec +0 -4
- data/.rubocop.yml +0 -9
- data/Changelog.md +0 -150
- data/Gemfile +0 -11
- data/Gemfile.lock +0 -176
- data/LICENSE +0 -20
- data/Rakefile +0 -22
- data/config/devtools.yml +0 -2
- data/config/flay.yml +0 -3
- data/config/flog.yml +0 -2
- data/config/mutant.yml +0 -6
- data/config/reek.yml +0 -98
- data/config/rubocop.yml +0 -122
- data/config/yardstick.yml +0 -2
- data/lib/unparser/cli/differ.rb +0 -152
- data/lib/unparser/cli/source.rb +0 -267
- data/lib/unparser/emitter/empty.rb +0 -23
- data/lib/unparser/emitter/ensure.rb +0 -37
- data/lib/unparser/emitter/literal.rb +0 -10
- data/lib/unparser/emitter/literal/array.rb +0 -29
- data/lib/unparser/emitter/literal/dynamic.rb +0 -53
- data/lib/unparser/emitter/literal/dynamic_body.rb +0 -132
- data/lib/unparser/emitter/literal/execute_string.rb +0 -38
- data/lib/unparser/emitter/literal/hash.rb +0 -156
- data/lib/unparser/emitter/literal/primitive.rb +0 -145
- data/lib/unparser/emitter/literal/range.rb +0 -36
- data/lib/unparser/emitter/literal/regexp.rb +0 -114
- data/lib/unparser/emitter/literal/singleton.rb +0 -26
- data/lib/unparser/emitter/meta.rb +0 -16
- data/lib/unparser/emitter/redo.rb +0 -25
- data/lib/unparser/emitter/resbody.rb +0 -76
- data/lib/unparser/emitter/retry.rb +0 -25
- data/lib/unparser/emitter/send/binary.rb +0 -57
- data/lib/unparser/emitter/send/conditional.rb +0 -40
- data/lib/unparser/emitter/send/regular.rb +0 -40
- data/lib/unparser/preprocessor.rb +0 -159
- data/spec/integration/unparser/corpus_spec.rb +0 -111
- data/spec/integrations.yml +0 -92
- data/spec/spec_helper.rb +0 -20
- data/spec/unit/unparser/buffer/append_spec.rb +0 -24
- data/spec/unit/unparser/buffer/append_without_prefix_spec.rb +0 -23
- data/spec/unit/unparser/buffer/capture_content_spec.rb +0 -17
- data/spec/unit/unparser/buffer/content_spec.rb +0 -38
- data/spec/unit/unparser/buffer/fresh_line_spec.rb +0 -20
- data/spec/unit/unparser/buffer/indent_spec.rb +0 -20
- data/spec/unit/unparser/buffer/nl_spec.rb +0 -16
- data/spec/unit/unparser/buffer/unindent_spec.rb +0 -20
- data/spec/unit/unparser/comments/consume_spec.rb +0 -22
- data/spec/unit/unparser/comments/take_all_spec.rb +0 -19
- data/spec/unit/unparser/comments/take_before_spec.rb +0 -46
- data/spec/unit/unparser/comments/take_eol_comments_spec.rb +0 -32
- data/spec/unit/unparser/emitter/class_methods/handle_spec.rb +0 -17
- data/spec/unit/unparser_spec.rb +0 -1847
- data/unparser.gemspec +0 -30
data/Rakefile
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
require 'devtools'
|
2
|
-
Devtools.init_rake_tasks
|
3
|
-
|
4
|
-
Rake.application.load_imports
|
5
|
-
task('metrics:mutant').clear
|
6
|
-
|
7
|
-
namespace :metrics do
|
8
|
-
task mutant: :coverage do
|
9
|
-
args = %w[
|
10
|
-
bundle exec mutant
|
11
|
-
--ignore-subject Unparser::Buffer#initialize
|
12
|
-
--include lib
|
13
|
-
--require unparser
|
14
|
-
--use rspec
|
15
|
-
--zombie
|
16
|
-
--since HEAD~1
|
17
|
-
]
|
18
|
-
args.concat(%w[--jobs 4]) if ENV.key?('CIRCLECI')
|
19
|
-
|
20
|
-
system(*args.concat(%w[-- Unparser*])) or fail "Mutant task failed"
|
21
|
-
end
|
22
|
-
end
|
data/config/devtools.yml
DELETED
data/config/flay.yml
DELETED
data/config/flog.yml
DELETED
data/config/mutant.yml
DELETED
data/config/reek.yml
DELETED
@@ -1,98 +0,0 @@
|
|
1
|
-
---
|
2
|
-
detectors:
|
3
|
-
Attribute:
|
4
|
-
enabled: false
|
5
|
-
exclude: []
|
6
|
-
BooleanParameter:
|
7
|
-
enabled: true
|
8
|
-
exclude: []
|
9
|
-
ClassVariable:
|
10
|
-
enabled: true
|
11
|
-
exclude: []
|
12
|
-
ControlParameter:
|
13
|
-
enabled: true
|
14
|
-
exclude: []
|
15
|
-
DataClump:
|
16
|
-
enabled: true
|
17
|
-
exclude: []
|
18
|
-
max_copies: 2
|
19
|
-
min_clump_size: 2
|
20
|
-
DuplicateMethodCall:
|
21
|
-
enabled: false
|
22
|
-
exclude: []
|
23
|
-
max_calls: 1
|
24
|
-
allow_calls: []
|
25
|
-
FeatureEnvy:
|
26
|
-
enabled: false
|
27
|
-
# Buggy smell detector
|
28
|
-
IrresponsibleModule:
|
29
|
-
enabled: false
|
30
|
-
exclude: []
|
31
|
-
LongParameterList:
|
32
|
-
enabled: true
|
33
|
-
exclude: []
|
34
|
-
max_params: 2
|
35
|
-
LongYieldList:
|
36
|
-
enabled: true
|
37
|
-
exclude: []
|
38
|
-
max_params: 2
|
39
|
-
NestedIterators:
|
40
|
-
enabled: true
|
41
|
-
exclude: []
|
42
|
-
max_allowed_nesting: 1
|
43
|
-
ignore_iterators: []
|
44
|
-
NilCheck:
|
45
|
-
enabled: false
|
46
|
-
RepeatedConditional:
|
47
|
-
enabled: true
|
48
|
-
exclude: []
|
49
|
-
max_ifs: 1
|
50
|
-
TooManyInstanceVariables:
|
51
|
-
enabled: true
|
52
|
-
exclude: []
|
53
|
-
max_instance_variables: 3
|
54
|
-
TooManyMethods:
|
55
|
-
enabled: true
|
56
|
-
exclude: []
|
57
|
-
max_methods: 10
|
58
|
-
TooManyStatements:
|
59
|
-
enabled: true
|
60
|
-
exclude: []
|
61
|
-
max_statements: 7
|
62
|
-
UncommunicativeMethodName:
|
63
|
-
enabled: true
|
64
|
-
exclude: []
|
65
|
-
reject:
|
66
|
-
- '/^[a-z]$/'
|
67
|
-
- '/[0-9]$/'
|
68
|
-
- '/[A-Z]/'
|
69
|
-
accept: []
|
70
|
-
UncommunicativeModuleName:
|
71
|
-
enabled: true
|
72
|
-
exclude: []
|
73
|
-
reject:
|
74
|
-
- '/^.$/'
|
75
|
-
- '/[0-9]$/'
|
76
|
-
accept: []
|
77
|
-
UncommunicativeParameterName:
|
78
|
-
enabled: true
|
79
|
-
exclude: []
|
80
|
-
reject:
|
81
|
-
- '/^.$/'
|
82
|
-
- '/[0-9]$/'
|
83
|
-
- '/[A-Z]/'
|
84
|
-
accept: []
|
85
|
-
UncommunicativeVariableName:
|
86
|
-
enabled: true
|
87
|
-
exclude: []
|
88
|
-
reject:
|
89
|
-
- '/^.$/'
|
90
|
-
- '/[0-9]$/'
|
91
|
-
- '/[A-Z]/'
|
92
|
-
accept: ['force_utf32']
|
93
|
-
UnusedParameters:
|
94
|
-
enabled: true
|
95
|
-
exclude: []
|
96
|
-
UtilityFunction:
|
97
|
-
enabled: true
|
98
|
-
exclude: []
|
data/config/rubocop.yml
DELETED
@@ -1,122 +0,0 @@
|
|
1
|
-
inherit_from: ../.rubocop.yml
|
2
|
-
|
3
|
-
AllCops:
|
4
|
-
Include:
|
5
|
-
- 'lib/unparser.rb'
|
6
|
-
- 'lib/unparser/**/*.rb'
|
7
|
-
- '**/*.rake'
|
8
|
-
- 'Gemfile'
|
9
|
-
- 'Gemfile.triage'
|
10
|
-
|
11
|
-
# Avoid parameter lists longer than five parameters.
|
12
|
-
ParameterLists:
|
13
|
-
Max: 3
|
14
|
-
CountKeywordArgs: true
|
15
|
-
|
16
|
-
MethodLength:
|
17
|
-
CountComments: false
|
18
|
-
Max: 17
|
19
|
-
|
20
|
-
AbcSize:
|
21
|
-
Max: 18
|
22
|
-
|
23
|
-
# Avoid more than `Max` levels of nesting.
|
24
|
-
BlockNesting:
|
25
|
-
Max: 3
|
26
|
-
|
27
|
-
# Align with the style guide.
|
28
|
-
CollectionMethods:
|
29
|
-
PreferredMethods:
|
30
|
-
collect: 'map'
|
31
|
-
inject: 'reduce'
|
32
|
-
find: 'detect'
|
33
|
-
find_all: 'select'
|
34
|
-
|
35
|
-
# Limit line length
|
36
|
-
LineLength:
|
37
|
-
Max: 113 # TODO: lower to 79 once the rubocop branch in shared/Gemfile is removed
|
38
|
-
|
39
|
-
ClassLength:
|
40
|
-
Max: 204
|
41
|
-
|
42
|
-
# Prefer modifiers and explicit if statements over returning early for small methods
|
43
|
-
GuardClause:
|
44
|
-
Enabled: false
|
45
|
-
|
46
|
-
Metrics/BlockLength:
|
47
|
-
Exclude:
|
48
|
-
# Ignore RSpec DSL
|
49
|
-
- spec/**/*
|
50
|
-
|
51
|
-
# Flags freezes for singletons that could still be mutated like Regexps
|
52
|
-
RedundantFreeze:
|
53
|
-
Enabled: false
|
54
|
-
|
55
|
-
# Allow Fixnum and Bignum. This Gem supports versions before 2.4
|
56
|
-
UnifiedInteger:
|
57
|
-
Enabled: false
|
58
|
-
|
59
|
-
# Disabled because of indenting with private keyword in class bodies.
|
60
|
-
IndentationWidth:
|
61
|
-
Enabled: false
|
62
|
-
|
63
|
-
# I like raise more
|
64
|
-
SignalException:
|
65
|
-
Enabled: false
|
66
|
-
|
67
|
-
# False positive in unparser source
|
68
|
-
OneLineConditional:
|
69
|
-
Enabled: false
|
70
|
-
|
71
|
-
Documentation:
|
72
|
-
Enabled: false
|
73
|
-
|
74
|
-
# Disable documentation checking until a class needs to be documented once
|
75
|
-
Documentation:
|
76
|
-
Enabled: false
|
77
|
-
|
78
|
-
# Do not favor modifier if/unless usage when you have a single-line body
|
79
|
-
IfUnlessModifier:
|
80
|
-
Enabled: false
|
81
|
-
|
82
|
-
# Allow case equality operator (in limited use within the specs)
|
83
|
-
CaseEquality:
|
84
|
-
Enabled: false
|
85
|
-
|
86
|
-
# Constants do not always have to use SCREAMING_SNAKE_CASE
|
87
|
-
ConstantName:
|
88
|
-
Enabled: false
|
89
|
-
|
90
|
-
# Not all trivial readers/writers can be defined with attr_* methods
|
91
|
-
TrivialAccessors:
|
92
|
-
Enabled: false
|
93
|
-
|
94
|
-
# I like to have an empty line before closing the currently opened body
|
95
|
-
EmptyLinesAroundBlockBody:
|
96
|
-
Enabled: false
|
97
|
-
|
98
|
-
EmptyLinesAroundClassBody:
|
99
|
-
Enabled: false
|
100
|
-
|
101
|
-
EmptyLinesAroundModuleBody:
|
102
|
-
Enabled: false
|
103
|
-
|
104
|
-
# I like my style more
|
105
|
-
AccessModifierIndentation:
|
106
|
-
Enabled: false
|
107
|
-
|
108
|
-
Style/CommentedKeyword:
|
109
|
-
Enabled: false
|
110
|
-
|
111
|
-
Style/MixinGrouping:
|
112
|
-
Enabled: false
|
113
|
-
|
114
|
-
Lint/BooleanSymbol:
|
115
|
-
Enabled: false
|
116
|
-
|
117
|
-
Style/AccessModifierDeclarations:
|
118
|
-
Enabled: false
|
119
|
-
|
120
|
-
Layout/AlignHash:
|
121
|
-
EnforcedColonStyle: table
|
122
|
-
EnforcedHashRocketStyle: table
|
data/config/yardstick.yml
DELETED
data/lib/unparser/cli/differ.rb
DELETED
@@ -1,152 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Unparser
|
4
|
-
class CLI
|
5
|
-
# Class to create diffs from source code
|
6
|
-
class Differ
|
7
|
-
include Adamantium::Flat, Concord.new(:old, :new), Procto.call(:colorized_diff)
|
8
|
-
|
9
|
-
CONTEXT_LINES = 5
|
10
|
-
|
11
|
-
# Return new object
|
12
|
-
#
|
13
|
-
# @param [String] old
|
14
|
-
# @param [String] new
|
15
|
-
#
|
16
|
-
# @return [Differ]
|
17
|
-
#
|
18
|
-
# @api private
|
19
|
-
#
|
20
|
-
def self.build(old, new)
|
21
|
-
new(lines(old), lines(new))
|
22
|
-
end
|
23
|
-
|
24
|
-
# Return colorized diff line
|
25
|
-
#
|
26
|
-
# @param [String] line
|
27
|
-
#
|
28
|
-
# @return [String]
|
29
|
-
#
|
30
|
-
# @api private
|
31
|
-
#
|
32
|
-
def self.colorize_line(line)
|
33
|
-
case line[0]
|
34
|
-
when '+'
|
35
|
-
Color::GREEN
|
36
|
-
when '-'
|
37
|
-
Color::RED
|
38
|
-
else
|
39
|
-
Color::NONE
|
40
|
-
end.format(line)
|
41
|
-
end
|
42
|
-
|
43
|
-
# Break up source into lines
|
44
|
-
#
|
45
|
-
# @param [String] source
|
46
|
-
#
|
47
|
-
# @return [Array<String>]
|
48
|
-
#
|
49
|
-
# @api private
|
50
|
-
#
|
51
|
-
def self.lines(source)
|
52
|
-
source.lines.map(&:chomp)
|
53
|
-
end
|
54
|
-
private_class_method :lines
|
55
|
-
|
56
|
-
# Return hunks
|
57
|
-
#
|
58
|
-
# @return [Array<Diff::LCS::Hunk>]
|
59
|
-
#
|
60
|
-
# @api private
|
61
|
-
#
|
62
|
-
def hunks
|
63
|
-
file_length_difference = new.length - old.length
|
64
|
-
diffs.map do |piece|
|
65
|
-
hunk = Diff::LCS::Hunk.new(old, new, piece, CONTEXT_LINES, file_length_difference)
|
66
|
-
file_length_difference = hunk.file_length_difference
|
67
|
-
hunk
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
# Return collapsed hunks
|
72
|
-
#
|
73
|
-
# @return [Enumerable<Diff::LCS::Hunk>]
|
74
|
-
#
|
75
|
-
# @api private
|
76
|
-
#
|
77
|
-
def collapsed_hunks
|
78
|
-
hunks.each_with_object([]) do |hunk, output|
|
79
|
-
last = output.last
|
80
|
-
|
81
|
-
if last && hunk.merge(last)
|
82
|
-
output.pop
|
83
|
-
end
|
84
|
-
|
85
|
-
output << hunk
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
# Return source diff
|
90
|
-
#
|
91
|
-
# @return [String]
|
92
|
-
# if there is a diff
|
93
|
-
#
|
94
|
-
# @return [nil]
|
95
|
-
# otherwise
|
96
|
-
#
|
97
|
-
# @api private
|
98
|
-
#
|
99
|
-
def diff
|
100
|
-
output = +''
|
101
|
-
|
102
|
-
collapsed_hunks.each do |hunk|
|
103
|
-
output << hunk.diff(:unified) << "\n"
|
104
|
-
end
|
105
|
-
|
106
|
-
output
|
107
|
-
end
|
108
|
-
memoize :diff
|
109
|
-
|
110
|
-
# Return colorized source diff
|
111
|
-
#
|
112
|
-
# @return [String]
|
113
|
-
# if there is a diff
|
114
|
-
#
|
115
|
-
# @return [nil]
|
116
|
-
# otherwise
|
117
|
-
#
|
118
|
-
# @api private
|
119
|
-
#
|
120
|
-
def colorized_diff
|
121
|
-
diff.lines.map do |line|
|
122
|
-
self.class.colorize_line(line)
|
123
|
-
end.join
|
124
|
-
end
|
125
|
-
memoize :colorized_diff
|
126
|
-
|
127
|
-
private
|
128
|
-
|
129
|
-
# Return diffs
|
130
|
-
#
|
131
|
-
# @return [Array<Array>]
|
132
|
-
#
|
133
|
-
# @api private
|
134
|
-
#
|
135
|
-
def diffs
|
136
|
-
Diff::LCS.diff(old, new)
|
137
|
-
end
|
138
|
-
memoize :diffs
|
139
|
-
|
140
|
-
# Return max length
|
141
|
-
#
|
142
|
-
# @return [Fixnum]
|
143
|
-
#
|
144
|
-
# @api private
|
145
|
-
#
|
146
|
-
def max_length
|
147
|
-
[old, new].map(&:length).max
|
148
|
-
end
|
149
|
-
|
150
|
-
end # CLI
|
151
|
-
end # Differ
|
152
|
-
end # Unparser
|
data/lib/unparser/cli/source.rb
DELETED
@@ -1,267 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Unparser
|
4
|
-
class CLI
|
5
|
-
# Source representation for CLI sources
|
6
|
-
#
|
7
|
-
# ignore :reek:TooManyMethods
|
8
|
-
class Source
|
9
|
-
include AbstractType, Adamantium::Flat, NodeHelpers
|
10
|
-
|
11
|
-
# Source state generated after first unparse
|
12
|
-
class Generated
|
13
|
-
include Concord::Public.new(:source, :ast, :error)
|
14
|
-
|
15
|
-
# Test if source was generated successfully
|
16
|
-
#
|
17
|
-
# @return [Boolean]
|
18
|
-
#
|
19
|
-
# @api private
|
20
|
-
#
|
21
|
-
def success?
|
22
|
-
!error
|
23
|
-
end
|
24
|
-
|
25
|
-
# Build generated source
|
26
|
-
#
|
27
|
-
# @param [Parser::AST::Node] ast
|
28
|
-
#
|
29
|
-
# @api private
|
30
|
-
#
|
31
|
-
def self.build(ast)
|
32
|
-
source = Unparser.unparse(ast)
|
33
|
-
new(source, ast, nil)
|
34
|
-
rescue StandardError => exception
|
35
|
-
new(nil, ast, exception)
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
# Test if source could be unparsed successfully
|
40
|
-
#
|
41
|
-
# @return [Boolean]
|
42
|
-
#
|
43
|
-
# @api private
|
44
|
-
#
|
45
|
-
def success?
|
46
|
-
generated.success? && original_ast && generated_ast && original_ast.eql?(generated_ast)
|
47
|
-
end
|
48
|
-
|
49
|
-
# Return error report
|
50
|
-
#
|
51
|
-
# @return [String]
|
52
|
-
#
|
53
|
-
# @api private
|
54
|
-
#
|
55
|
-
def report
|
56
|
-
if original_ast && generated_ast
|
57
|
-
report_with_ast_diff
|
58
|
-
elsif !original_ast
|
59
|
-
report_original
|
60
|
-
elsif !generated.success?
|
61
|
-
report_unparser
|
62
|
-
elsif !generated_ast
|
63
|
-
report_generated
|
64
|
-
else
|
65
|
-
raise
|
66
|
-
end
|
67
|
-
end
|
68
|
-
memoize :report
|
69
|
-
|
70
|
-
private
|
71
|
-
|
72
|
-
# Return generated source
|
73
|
-
#
|
74
|
-
# @return [String]
|
75
|
-
#
|
76
|
-
# @api private
|
77
|
-
#
|
78
|
-
def generated
|
79
|
-
Source::Generated.build(original_ast)
|
80
|
-
end
|
81
|
-
memoize :generated
|
82
|
-
|
83
|
-
# Return stripped source
|
84
|
-
#
|
85
|
-
# @param [String] source
|
86
|
-
#
|
87
|
-
# @return [String]
|
88
|
-
#
|
89
|
-
# @api private
|
90
|
-
#
|
91
|
-
# ignore :reek:UtilityFunction
|
92
|
-
def strip(source)
|
93
|
-
source = source.rstrip
|
94
|
-
indent = source.scan(/^\s*/).first
|
95
|
-
source.gsub(/^#{indent}/, '')
|
96
|
-
end
|
97
|
-
|
98
|
-
# Return error report for parsing original
|
99
|
-
#
|
100
|
-
# @return [String]
|
101
|
-
#
|
102
|
-
# @api private
|
103
|
-
#
|
104
|
-
def report_original
|
105
|
-
strip(<<-MESSAGE)
|
106
|
-
Parsing of original source failed:
|
107
|
-
#{original_source}
|
108
|
-
MESSAGE
|
109
|
-
end
|
110
|
-
|
111
|
-
# Report unparser bug
|
112
|
-
#
|
113
|
-
# @return [String]
|
114
|
-
#
|
115
|
-
# @api private
|
116
|
-
#
|
117
|
-
def report_unparser
|
118
|
-
message = ['Unparsing parsed AST failed']
|
119
|
-
error = generated.error
|
120
|
-
message << error
|
121
|
-
error.backtrace.take(20).each(&message.method(:<<))
|
122
|
-
message << 'Original-AST:'
|
123
|
-
message << original_ast.inspect
|
124
|
-
message.join("\n")
|
125
|
-
end
|
126
|
-
|
127
|
-
# Return error report for parsing generated
|
128
|
-
#
|
129
|
-
# @return [String]
|
130
|
-
#
|
131
|
-
# @api private
|
132
|
-
#
|
133
|
-
def report_generated
|
134
|
-
strip(<<-MESSAGE)
|
135
|
-
Parsing of generated source failed:
|
136
|
-
Original-source:
|
137
|
-
#{original_source}
|
138
|
-
Original-AST:
|
139
|
-
#{original_ast.inspect}
|
140
|
-
Source:
|
141
|
-
#{generated.source}
|
142
|
-
MESSAGE
|
143
|
-
end
|
144
|
-
|
145
|
-
# Return error report with AST difference
|
146
|
-
#
|
147
|
-
# @return [String]
|
148
|
-
#
|
149
|
-
# @api private
|
150
|
-
#
|
151
|
-
def report_with_ast_diff
|
152
|
-
strip(<<-MESSAGE)
|
153
|
-
#{ast_diff}
|
154
|
-
Original-Source:\n#{original_source}
|
155
|
-
Original-AST:\n#{original_ast.inspect}
|
156
|
-
Generated-Source:\n#{generated.source}
|
157
|
-
Generated-AST:\n#{generated_ast.inspect}
|
158
|
-
MESSAGE
|
159
|
-
end
|
160
|
-
|
161
|
-
# Return ast diff
|
162
|
-
#
|
163
|
-
# @return [String]
|
164
|
-
#
|
165
|
-
# @api private
|
166
|
-
#
|
167
|
-
def ast_diff
|
168
|
-
Differ.call(
|
169
|
-
original_ast.inspect.lines.map(&:chomp),
|
170
|
-
generated_ast.inspect.lines.map(&:chomp)
|
171
|
-
)
|
172
|
-
end
|
173
|
-
|
174
|
-
# Return generated AST
|
175
|
-
#
|
176
|
-
# @return [Parser::AST::Node]
|
177
|
-
# if parser was sucessful for generated ast
|
178
|
-
#
|
179
|
-
# @return [nil]
|
180
|
-
# otherwise
|
181
|
-
#
|
182
|
-
# @api private
|
183
|
-
#
|
184
|
-
def generated_ast
|
185
|
-
generated.success? && Preprocessor.run(Unparser.parse(generated.source))
|
186
|
-
rescue Parser::SyntaxError
|
187
|
-
nil
|
188
|
-
end
|
189
|
-
memoize :generated_ast
|
190
|
-
|
191
|
-
# Return original AST
|
192
|
-
#
|
193
|
-
# @return [Parser::AST::Node]
|
194
|
-
#
|
195
|
-
# @api private
|
196
|
-
#
|
197
|
-
def original_ast
|
198
|
-
Preprocessor.run(Unparser.parse(original_source))
|
199
|
-
rescue Parser::SyntaxError
|
200
|
-
nil
|
201
|
-
end
|
202
|
-
memoize :original_ast
|
203
|
-
|
204
|
-
# CLI source from string
|
205
|
-
class String < self
|
206
|
-
include Concord.new(:original_source)
|
207
|
-
|
208
|
-
# Return identification
|
209
|
-
#
|
210
|
-
# @return [String]
|
211
|
-
#
|
212
|
-
# @api private
|
213
|
-
#
|
214
|
-
def identification
|
215
|
-
'(string)'
|
216
|
-
end
|
217
|
-
|
218
|
-
end # String
|
219
|
-
|
220
|
-
# CLI source from file
|
221
|
-
class File < self
|
222
|
-
include Concord.new(:file_name)
|
223
|
-
|
224
|
-
# Return identification
|
225
|
-
#
|
226
|
-
# @return [String]
|
227
|
-
#
|
228
|
-
# @api private
|
229
|
-
#
|
230
|
-
def identification
|
231
|
-
"(#{file_name})"
|
232
|
-
end
|
233
|
-
|
234
|
-
private
|
235
|
-
|
236
|
-
# Return original source
|
237
|
-
#
|
238
|
-
# @return [String]
|
239
|
-
#
|
240
|
-
# @api private
|
241
|
-
#
|
242
|
-
def original_source
|
243
|
-
::File.read(file_name)
|
244
|
-
end
|
245
|
-
memoize :original_source
|
246
|
-
|
247
|
-
end # File
|
248
|
-
|
249
|
-
# Source passed in as node
|
250
|
-
class Node < self
|
251
|
-
include Concord.new(:original_ast)
|
252
|
-
|
253
|
-
# Return original source
|
254
|
-
#
|
255
|
-
# @return [String]
|
256
|
-
#
|
257
|
-
# @api private
|
258
|
-
#
|
259
|
-
def original_source
|
260
|
-
Unparser.unparse(original_ast)
|
261
|
-
end
|
262
|
-
memoize :original_source
|
263
|
-
end # Node
|
264
|
-
|
265
|
-
end # Source
|
266
|
-
end # CLI
|
267
|
-
end # Unparser
|