ws_lint 0.1.0
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.
- checksums.yaml +7 -0
- data/bin/ws_lint +57 -0
- data/lib/detector.rb +21 -0
- data/lib/formatter.rb +42 -0
- metadata +47 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 65c6e55baf71c48f555e69f38c78e185d48df92eca50c73d59f73ad8a957869e
|
4
|
+
data.tar.gz: 2b4cc002e7c620f2c1098ccb6c1bd72a0613e8a65ccebdf9716f419efba836c0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e916fceb11d59df61d1a16832bc5d927e15eb726429c59ed69f3077c7928b889e4faca830c7818af4c29bc9f0f3ad9916513541e14d18a9cf7bd58ab195a3b44
|
7
|
+
data.tar.gz: 90b171683301aaa753f9ec71af4727e6fa5d18a43998eba164bbbc40b28a3288818f9144234badf78632ad6a8ddd347a06d559e63a8d6e73f39d6f717dab1ec1
|
data/bin/ws_lint
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
require 'colorize'
|
3
|
+
require_relative '../lib/detector'
|
4
|
+
require_relative '../lib/formatter'
|
5
|
+
|
6
|
+
begin
|
7
|
+
file_lint = Detector.new(ARGV[0])
|
8
|
+
|
9
|
+
extra_whitespace = file_lint.detect_whitespace
|
10
|
+
extra_empty_lines = file_lint.detect_empty
|
11
|
+
|
12
|
+
unless extra_whitespace.empty?
|
13
|
+
puts
|
14
|
+
puts '....................................................'
|
15
|
+
puts
|
16
|
+
puts 'The following lines have extra trailing whitespace.'
|
17
|
+
puts
|
18
|
+
extra_whitespace.each do |num, line|
|
19
|
+
print "#{num} #{line}".red
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
unless extra_empty_lines.empty?
|
24
|
+
puts
|
25
|
+
puts '....................................................'
|
26
|
+
puts
|
27
|
+
puts 'The following empty lines are duplicate.'
|
28
|
+
puts
|
29
|
+
extra_empty_lines.each do |num, line|
|
30
|
+
print "#{num} #{line}".red
|
31
|
+
end
|
32
|
+
puts '....................................................'
|
33
|
+
puts
|
34
|
+
end
|
35
|
+
|
36
|
+
unless extra_whitespace.empty? && extra_empty_lines.empty?
|
37
|
+
loop do
|
38
|
+
puts 'Do you want to autoformat your file? y/n'
|
39
|
+
format_prompt = $stdin.gets.chomp.downcase
|
40
|
+
if format_prompt == 'y'
|
41
|
+
file_format = Formatter.new(ARGV[0])
|
42
|
+
file_format.format
|
43
|
+
puts 'Thanks for using whitespace linter'
|
44
|
+
break
|
45
|
+
elsif format_prompt == 'n'
|
46
|
+
puts 'Thanks for using whitespace linter'
|
47
|
+
break
|
48
|
+
else
|
49
|
+
puts 'Please input y or n'
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
puts 'No whitespace errors'.green if extra_empty_lines.empty? && extra_whitespace.empty?
|
55
|
+
rescue StandardError
|
56
|
+
puts 'Please input a valid file'.red
|
57
|
+
end
|
data/lib/detector.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
class Detector
|
2
|
+
def initialize(file)
|
3
|
+
@file = File.open(file).to_a
|
4
|
+
end
|
5
|
+
|
6
|
+
def detect_whitespace
|
7
|
+
result = {}
|
8
|
+
@file.each_with_index do |line, index|
|
9
|
+
result[index + 1] = line if line.end_with?(" \n") || line.end_with?(' ')
|
10
|
+
end
|
11
|
+
result
|
12
|
+
end
|
13
|
+
|
14
|
+
def detect_empty
|
15
|
+
result = {}
|
16
|
+
@file.each_with_index do |line, index|
|
17
|
+
result[index + 1] = line if @file[index] == "\n" && @file[index - 1] == "\n"
|
18
|
+
end
|
19
|
+
result
|
20
|
+
end
|
21
|
+
end
|
data/lib/formatter.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
class Formatter
|
2
|
+
def initialize(file)
|
3
|
+
@file = file
|
4
|
+
@temp = []
|
5
|
+
temp_file
|
6
|
+
end
|
7
|
+
|
8
|
+
def format
|
9
|
+
whitespace_remover
|
10
|
+
empty_line_remover
|
11
|
+
@new_file = File.open(@file, 'w')
|
12
|
+
@new_file.write(@temp.join)
|
13
|
+
@temp
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def temp_file
|
19
|
+
@read_file = File.open(@file, 'r')
|
20
|
+
@read_file.each do |line|
|
21
|
+
@temp << line
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def whitespace_remover
|
26
|
+
@temp.each_with_index do |line, index|
|
27
|
+
@temp[index] = if line.end_with?("\n")
|
28
|
+
line.gsub(/\s+\n$/, "\n")
|
29
|
+
else
|
30
|
+
line.gsub(/\s+$/, '')
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def empty_line_remover
|
36
|
+
result = []
|
37
|
+
@temp.each_with_index do |line, index|
|
38
|
+
result << line unless @temp[index - 1] == "\n" && line == "\n"
|
39
|
+
end
|
40
|
+
@temp = result
|
41
|
+
end
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ws_lint
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Meron Ogbai
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-02-13 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Basic linter that detects and formats trailing whitespace and extra empty
|
14
|
+
lines in your code and config files
|
15
|
+
email: okbaymeron@gmail.com
|
16
|
+
executables:
|
17
|
+
- ws_lint
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- bin/ws_lint
|
22
|
+
- lib/detector.rb
|
23
|
+
- lib/formatter.rb
|
24
|
+
homepage: https://github.com/meronokbay/whitespace-linter
|
25
|
+
licenses:
|
26
|
+
- MIT
|
27
|
+
metadata: {}
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubygems_version: 3.2.3
|
44
|
+
signing_key:
|
45
|
+
specification_version: 4
|
46
|
+
summary: Whitespace Linter
|
47
|
+
test_files: []
|