asker-tool 2.2.1 → 2.2.5

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