asker-tool 2.2.0 → 2.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/asker/checker.rb DELETED
@@ -1,453 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rainbow'
4
-
5
- ##
6
- # Check HAML file syntax
7
- module Checker
8
- ##
9
- # Check:
10
- # * file exist
11
- # * filename extension
12
- # * and HAML syntax
13
- # @param filepath (String)
14
- def self.check(filepath)
15
- unless File.exist? filepath
16
- puts Rainbow('File not found!').red.bright
17
- return false
18
- end
19
- unless File.extname(filepath) == '.haml'
20
- puts Rainbow('Only check HAML files!').yellow.bright
21
- return false
22
- end
23
- check_filepath(filepath)
24
- end
25
-
26
- ##
27
- # Check HAML syntax
28
- # @param filepath (String)
29
- def self.check_filepath(filepath)
30
- data = Data.new(filepath)
31
- data.check
32
- data.show_errors
33
- data.ok?
34
- end
35
-
36
- ##
37
- # Internal class that revise syntax
38
- # rubocop:disable Metrics/ClassLength
39
- class Data
40
- attr_reader :inputs
41
- attr_reader :outputs
42
-
43
- # rubocop:disable Metrics/MethodLength
44
- def initialize(filepath)
45
- @inputs = File.read(filepath).split("\n")
46
- @outputs = []
47
- @inputs.each_with_index do |line, index|
48
- output = { id: index,
49
- level: 0,
50
- state: :none,
51
- type: :none,
52
- source: line,
53
- msg: '' }
54
- @outputs << output
55
- end
56
- @ok = false
57
- end
58
- # rubocop:enable Metrics/MethodLength
59
-
60
- def ok?
61
- @ok
62
- end
63
-
64
- def show
65
- @outputs.each do |i|
66
- puts "#{i[:id]}: #{i[:state]} [#{i[:type]}] #{i[:msg]}"
67
- end
68
- end
69
-
70
- # rubocop:disable Metrics/AbcSize
71
- # rubocop:disable Metrics/MethodLength
72
- def show_errors
73
- errors = 0
74
- # puts "Line : Error description"
75
- @outputs.each do |i|
76
- next if i[:state] == :ok
77
-
78
- errors += 1
79
- if errors < 11
80
- data = { id: i[:id], msg: i[:msg], source: i[:source][0, 40] }
81
- puts format(' %<id>03d : %<msg>s. => %<source>s', data)
82
- end
83
- puts '...' if errors == 11
84
- end
85
-
86
- if errors.positive?
87
- puts Rainbow("[ERROR] #{errors} errors " \
88
- "from #{@inputs.size} lines!").red.bright
89
- end
90
- puts Rainbow('Syntax OK!').green if errors.zero?
91
- end
92
- # rubocop:enable Metrics/AbcSize
93
- # rubocop:enable Metrics/MethodLength
94
-
95
- # rubocop:disable Metrics/MethodLength
96
- # rubocop:disable Metrics/AbcSize
97
- def check
98
- @ok = true
99
- @inputs.each_with_index do |line, index|
100
- check_empty_lines(line, index)
101
- check_map(line, index)
102
- check_concept(line, index)
103
- check_names(line, index)
104
- check_tags(line, index)
105
- check_def(line, index)
106
- check_table(line, index)
107
- check_row(line, index)
108
- check_col(line, index)
109
- check_template(line, index)
110
- check_code(line, index)
111
- check_type(line, index)
112
- check_path(line, index)
113
- check_features(line, index)
114
- check_unknown(line, index)
115
- @ok = false unless @outputs[index][:state] == :ok
116
- @ok = false if @outputs[index][:type] == :unkown
117
- end
118
- @ok
119
- end
120
- # rubocop:enable Metrics/MethodLength
121
- # rubocop:enable Metrics/AbcSize
122
-
123
- private
124
-
125
- def check_empty_lines(line, index)
126
- return unless line.strip.size.zero? || line.start_with?('#')
127
-
128
- @outputs[index][:type] = :empty
129
- @outputs[index][:level] = -1
130
- @outputs[index][:state] = :ok
131
- end
132
-
133
- # rubocop:disable Metrics/MethodLength
134
- # rubocop:disable Metrics/AbcSize
135
- def check_map(line, index)
136
- if index.zero?
137
- @outputs[index][:type] = :map
138
- if line.start_with?('%map{')
139
- @outputs[index][:state] = :ok
140
- else
141
- @outputs[index][:state] = :err
142
- @outputs[index][:msg] = 'Start with %map{'
143
- end
144
- elsif index.positive? && line.include?('%map{')
145
- @outputs[index][:state] = :err
146
- @outputs[index][:type] = :map
147
- @outputs[index][:msg] = 'Write %map on line 0'
148
- end
149
- end
150
- # rubocop:enable Metrics/MethodLength
151
- # rubocop:enable Metrics/AbcSize
152
-
153
- # rubocop:disable Metrics/MethodLength
154
- # rubocop:disable Metrics/AbcSize
155
- def check_concept(line, index)
156
- return unless @outputs[index][:state] == :none
157
- return unless line.include? '%concept'
158
-
159
- @outputs[index][:type] = :concept
160
- @outputs[index][:level] = 1
161
- @outputs[index][:state] = :ok
162
- if find_parent(index) != :map
163
- @outputs[index][:state] = :err
164
- @outputs[index][:msg] = 'Parent(map) not found!'
165
- elsif line != ' %concept'
166
- @outputs[index][:state] = :err
167
- @outputs[index][:msg] = 'Write 2 spaces before %concept'
168
- end
169
- end
170
- # rubocop:enable Metrics/MethodLength
171
- # rubocop:enable Metrics/AbcSize
172
-
173
- # rubocop:disable Metrics/AbcSize
174
- # rubocop:disable Metrics/MethodLength
175
- def check_names(line, index)
176
- return unless @outputs[index][:state] == :none
177
- return unless line.include? '%names'
178
-
179
- @outputs[index][:type] = :names
180
- @outputs[index][:level] = 2
181
- @outputs[index][:state] = :ok
182
- if find_parent(index) != :concept
183
- @outputs[index][:state] = :err
184
- @outputs[index][:msg] = 'Parent(concept) not found!'
185
- elsif !line.start_with? ' %names'
186
- @outputs[index][:state] = :err
187
- @outputs[index][:msg] = 'Write 4 spaces before %names'
188
- end
189
- end
190
- # rubocop:enable Metrics/AbcSize
191
- # rubocop:enable Metrics/MethodLength
192
-
193
- # rubocop:disable Metrics/AbcSize
194
- # rubocop:disable Metrics/MethodLength
195
- def check_tags(line, index)
196
- return unless @outputs[index][:state] == :none
197
- return unless line.include? '%tags'
198
-
199
- @outputs[index][:type] = :tags
200
- @outputs[index][:level] = 2
201
- @outputs[index][:state] = :ok
202
- if find_parent(index) != :concept
203
- @outputs[index][:state] = :err
204
- @outputs[index][:msg] = 'Parent(concept) not found!'
205
- elsif !line.start_with? ' %tags'
206
- @outputs[index][:state] = :err
207
- @outputs[index][:msg] = 'Write 4 spaces before %tags'
208
- end
209
- end
210
- # rubocop:enable Metrics/AbcSize
211
- # rubocop:enable Metrics/MethodLength
212
-
213
- # rubocop:disable Metrics/AbcSize
214
- # rubocop:disable Metrics/MethodLength
215
- def check_def(line, index)
216
- return unless @outputs[index][:state] == :none
217
- return unless line.include? '%def'
218
-
219
- @outputs[index][:type] = :def
220
- @outputs[index][:level] = 2
221
- @outputs[index][:state] = :ok
222
- if find_parent(index) != :concept
223
- @outputs[index][:state] = :err
224
- @outputs[index][:msg] = 'Parent(concept) not found!'
225
- elsif !line.start_with? ' %def'
226
- @outputs[index][:state] = :err
227
- @outputs[index][:msg] = 'Write 4 spaces before %def'
228
- end
229
- end
230
- # rubocop:enable Metrics/AbcSize
231
- # rubocop:enable Metrics/MethodLength
232
-
233
- # rubocop:disable Metrics/AbcSize
234
- # rubocop:disable Metrics/MethodLength
235
- def check_table(line, index)
236
- return unless @outputs[index][:state] == :none
237
- return unless line.include? '%table'
238
-
239
- @outputs[index][:type] = :table
240
- @outputs[index][:level] = 2
241
- @outputs[index][:state] = :ok
242
- if find_parent(index) != :concept
243
- @outputs[index][:state] = :err
244
- @outputs[index][:msg] = 'Parent(concept) not found!'
245
- elsif !line.start_with? ' %table'
246
- @outputs[index][:state] = :err
247
- @outputs[index][:msg] = 'Write 4 spaces before %table'
248
- end
249
- end
250
- # rubocop:enable Metrics/AbcSize
251
- # rubocop:enable Metrics/MethodLength
252
-
253
- # rubocop:disable Metrics/AbcSize
254
- # rubocop:disable Metrics/MethodLength
255
- def check_row(line, index)
256
- return unless @outputs[index][:state] == :none
257
- return unless line.include? '%row'
258
-
259
- @outputs[index][:type] = :row
260
- @outputs[index][:state] = :ok
261
-
262
- if count_spaces(line) == 6
263
- @outputs[index][:level] = 3
264
- parent = find_parent(index)
265
- unless %i[table features].include? parent
266
- @outputs[index][:state] = :err
267
- @outputs[index][:msg] = 'Parent(table/features) not found!'
268
- end
269
- elsif count_spaces(line) == 8
270
- @outputs[index][:level] = 4
271
- if find_parent(index) != :template
272
- @outputs[index][:state] = :err
273
- @outputs[index][:msg] = 'Parent(template) not found!'
274
- end
275
- else
276
- @outputs[index][:state] = :err
277
- @outputs[index][:msg] = 'Write 6 or 8 spaces before %row'
278
- end
279
- end
280
- # rubocop:enable Metrics/AbcSize
281
- # rubocop:enable Metrics/MethodLength
282
-
283
- # rubocop:disable Metrics/AbcSize
284
- # rubocop:disable Metrics/MethodLength
285
- def check_col(line, index)
286
- return unless @outputs[index][:state] == :none
287
- return unless line.include? '%col'
288
-
289
- @outputs[index][:type] = :col
290
- @outputs[index][:state] = :ok
291
- if count_spaces(line) == 8
292
- @outputs[index][:level] = 4
293
- if find_parent(index) != :row
294
- @outputs[index][:state] = :err
295
- @outputs[index][:msg] = 'Parent(row) not found!'
296
- end
297
- elsif count_spaces(line) == 10
298
- @outputs[index][:level] = 5
299
- if find_parent(index) != :row
300
- @outputs[index][:state] = :err
301
- @outputs[index][:msg] = 'Parent(row) not found!'
302
- end
303
- else
304
- @outputs[index][:state] = :err
305
- @outputs[index][:msg] = 'Write 8 or 10 spaces before %col'
306
- end
307
- check_text(line, index)
308
- end
309
- # rubocop:enable Metrics/AbcSize
310
- # rubocop:enable Metrics/MethodLength
311
-
312
- def check_text(line, index)
313
- return unless @outputs[index][:state] == :ok
314
-
315
- ok = ''
316
- %w[< >].each do |char|
317
- ok = char if line.include? char
318
- end
319
- return if ok == ''
320
-
321
- @outputs[index][:state] = :err
322
- @outputs[index][:msg] = "Char #{ok} not allow!"
323
- end
324
-
325
- # rubocop:disable Metrics/MethodLength
326
- # rubocop:disable Metrics/AbcSize
327
- def check_template(line, index)
328
- return unless @outputs[index][:state] == :none
329
- return unless line.include? '%template'
330
-
331
- @outputs[index][:type] = :template
332
- @outputs[index][:level] = 3
333
- @outputs[index][:state] = :ok
334
- if find_parent(index) != :table
335
- @outputs[index][:state] = :err
336
- @outputs[index][:msg] = 'Parent(concept) not found!'
337
- elsif !line.start_with? ' %template'
338
- @outputs[index][:state] = :err
339
- @outputs[index][:msg] = 'Write 6 spaces before %template'
340
- end
341
- end
342
- # rubocop:enable Metrics/AbcSize
343
- # rubocop:enable Metrics/MethodLength
344
-
345
- # rubocop:disable Metrics/MethodLength
346
- # rubocop:disable Metrics/AbcSize
347
- def check_code(line, index)
348
- return unless @outputs[index][:state] == :none
349
- return unless line.include? '%code'
350
-
351
- @outputs[index][:type] = :code
352
- @outputs[index][:level] = 1
353
- @outputs[index][:state] = :ok
354
- if find_parent(index) != :map
355
- @outputs[index][:state] = :err
356
- @outputs[index][:msg] = 'Parent(map) not found!'
357
- elsif line != ' %code'
358
- @outputs[index][:state] = :err
359
- @outputs[index][:msg] = 'Write 2 spaces before %code'
360
- end
361
- end
362
- # rubocop:enable Metrics/MethodLength
363
- # rubocop:enable Metrics/AbcSize
364
-
365
- # rubocop:disable Metrics/MethodLength
366
- # rubocop:disable Metrics/AbcSize
367
- def check_type(line, index)
368
- return unless @outputs[index][:state] == :none
369
- return unless line.include? '%type'
370
-
371
- @outputs[index][:type] = :type
372
- @outputs[index][:level] = 2
373
- @outputs[index][:state] = :ok
374
- if find_parent(index) != :code
375
- @outputs[index][:state] = :err
376
- @outputs[index][:msg] = 'Parent(code) not found!'
377
- elsif !line.start_with? ' %type'
378
- @outputs[index][:state] = :err
379
- @outputs[index][:msg] = 'Write 4 spaces before %type'
380
- end
381
- end
382
- # rubocop:enable Metrics/AbcSize
383
- # rubocop:enable Metrics/MethodLength
384
-
385
- # rubocop:disable Metrics/MethodLength
386
- # rubocop:disable Metrics/AbcSize
387
- def check_path(line, index)
388
- return unless @outputs[index][:state] == :none
389
- return unless line.include? '%path'
390
-
391
- @outputs[index][:type] = :path
392
- @outputs[index][:level] = 2
393
- @outputs[index][:state] = :ok
394
- if find_parent(index) != :code
395
- @outputs[index][:state] = :err
396
- @outputs[index][:msg] = 'Parent(code) not found!'
397
- elsif !line.start_with? ' %path'
398
- @outputs[index][:state] = :err
399
- @outputs[index][:msg] = 'Write 4 spaces before %type'
400
- end
401
- end
402
- # rubocop:enable Metrics/AbcSize
403
- # rubocop:enable Metrics/MethodLength
404
-
405
- # rubocop:disable Metrics/MethodLength
406
- # rubocop:disable Metrics/AbcSize
407
- def check_features(line, index)
408
- return unless @outputs[index][:state] == :none
409
- return unless line.include? '%features'
410
-
411
- @outputs[index][:type] = :features
412
- @outputs[index][:level] = 2
413
- @outputs[index][:state] = :ok
414
- if find_parent(index) != :code
415
- @outputs[index][:state] = :err
416
- @outputs[index][:msg] = 'Parent(code) not found!'
417
- elsif !line.start_with? ' %features'
418
- @outputs[index][:state] = :err
419
- @outputs[index][:msg] = 'Write 4 spaces before %features'
420
- end
421
- end
422
- # rubocop:enable Metrics/AbcSize
423
- # rubocop:enable Metrics/MethodLength
424
-
425
- def check_unknown(line, index)
426
- return unless @outputs[index][:state] == :none
427
-
428
- @outputs[index][:type] = :unknown
429
- @outputs[index][:level] = count_spaces(line) / 2
430
- @outputs[index][:state] = :err
431
- @outputs[index][:msg] = "Unknown tag with parent(#{find_parent(index)})!"
432
- end
433
-
434
- def find_parent(index)
435
- current_level = @outputs[index][:level]
436
- return :noparent if current_level.zero?
437
-
438
- i = index - 1
439
- while i >= 0
440
- return @outputs[i][:type] if @outputs[i][:level] == current_level - 1
441
-
442
- i -= 1
443
- end
444
- :noparent
445
- end
446
-
447
- def count_spaces(line)
448
- a = line.split('%')
449
- a[0].count(' ')
450
- end
451
- end
452
- # rubocop:enable Metrics/ClassLength
453
- end