eslint-autocorrect 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 39c7909285b8fa2d8fd9c244d90f88a98a03021f
4
+ data.tar.gz: fa785a9c88c4422e1b62fbffd0af25b442ca3b44
5
+ SHA512:
6
+ metadata.gz: 3c7fb3f97f79ce36b4de8fd789e3a200bb95b0fe5a09302f84e2b1c78d5bed2463811fb0481fccc54dc92cf9c4437dac58b2edefbe8db42bb18a9cfa24c89bcc
7
+ data.tar.gz: 28b1db40870ee4dd691207099e087a58d5146e9cf75e1e1a0d876317c5c12ad36cda2747676e6f20e6a21662ec6f35ce35171d3cc7cca2707454dd1d0e9291f0
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in eslint-autocorrect.gemspec
4
+ gemspec
@@ -0,0 +1,17 @@
1
+ # eslint-autocorrect
2
+
3
+ an autocorrecter for eslint
4
+
5
+ **NOTE** This library is still being developed. Please double check output and report issues for bugs you might find.
6
+
7
+ ## Installation
8
+
9
+ ```rb
10
+ gem install eslint-autocorrect
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```sh
16
+ `npm bin`/eslint . -f unix | eslint-autocorrect
17
+ ```
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'eslint/autocorrect/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "eslint-autocorrect"
8
+ spec.version = Eslint::Autocorrect::VERSION
9
+ spec.authors = ["Josh Bodah"]
10
+ spec.email = ["jb3689@yahoo.com"]
11
+
12
+ spec.summary = %q{an autocorrecter for eslint}
13
+ spec.homepage = "https://github.com/jbodah/eslint-autocorrect"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.12"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "minitest", "~> 5.0"
24
+ end
@@ -0,0 +1,106 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ class String
4
+ def delete_at(n)
5
+ buffer = each_char.to_a
6
+ buffer.delete_at(n)
7
+ buffer.join
8
+ end
9
+
10
+ def delete_range(range)
11
+ buffer = each_char.to_a
12
+ buffer = buffer[0..range.min - 1] + buffer[range.max + 1..-1]
13
+ buffer.join
14
+ end
15
+ end
16
+
17
+ class FileEditor
18
+ def self.edit(path)
19
+ editor = FileEditor.new(path)
20
+ yield editor
21
+ editor.write
22
+ end
23
+
24
+ attr_accessor :lines
25
+
26
+ def initialize(path)
27
+ @path = path
28
+ @lines = File.read(path).each_line.to_a
29
+ end
30
+
31
+ def write
32
+ File.open(@path, 'w') { |f| f.write @lines.join }
33
+ end
34
+ end
35
+
36
+ module Handler
37
+ class << self
38
+ def handle(err, path, line, col)
39
+ msg = err.downcase.gsub(/[\/-]/, '_')
40
+ if respond_to?(msg)
41
+ puts "Fixing #{err} @ #{path}:#{line}:#{col}"
42
+ send(msg, path, line - 1, col - 1)
43
+ else
44
+ puts "Don't know how to handle #{err} for #{path}:#{line}:#{col}"
45
+ end
46
+ end
47
+
48
+ def error_no_extra_semi(path, line, col)
49
+ # TODO: @jbodah 2016-08-25: use stringio
50
+ FileEditor.edit(path) do |f|
51
+ line_buffer = f.lines[line]
52
+
53
+ if line_buffer[col] == ';'
54
+ semi_idx = col
55
+ else
56
+ # NOTE: @jbodah 2016-08-25: eslint sometimes justifies columns based on your indentation!
57
+ first_char_idx = line_buffer.length - line_buffer.lstrip.length
58
+ semi_idx = first_char_idx + col
59
+
60
+ raise unless semi_idx == ';'
61
+ end
62
+
63
+ line_buffer = line_buffer.delete_at(semi_idx)
64
+ f.lines[line] = line_buffer
65
+ end
66
+ end
67
+
68
+ def error_no_unused_vars(path, line, col)
69
+ FileEditor.edit(path) do |f|
70
+ line_buffer = f.lines[line]
71
+ prev_char = line_buffer[col - 1]
72
+ if prev_char[/\W/]
73
+ # find word
74
+ start_idx = col
75
+ end_idx = start_idx
76
+ end_idx += 1 until line_buffer[end_idx + 1][/\W/]
77
+
78
+ # clean up any additional comma
79
+ start_idx -= 1 while line_buffer[start_idx - 1][/[, ]/]
80
+
81
+ line_buffer = line_buffer.delete_range(start_idx..end_idx)
82
+ f.lines[line] = line_buffer
83
+ else
84
+ raise
85
+ end
86
+ end
87
+ end
88
+
89
+ def error_keyword_spacing(path, line, col)
90
+ FileEditor.edit(path) do |f|
91
+ line_buffer = f.lines[line]
92
+ idx = col
93
+ idx += 1 until line_buffer[idx][/\W/]
94
+ line_buffer = line_buffer.insert(idx, ' ')
95
+ f.lines[line] = line_buffer
96
+ end
97
+ end
98
+ end
99
+ end
100
+
101
+ $stdin.each_line do |line|
102
+ path, line, col, rest = line.split(':', 4)
103
+ next if rest.nil?
104
+ err = rest[/(?<=\[).*(?=\])/]
105
+ Handler.handle(err, path, line.to_i, col.to_i)
106
+ end
@@ -0,0 +1,7 @@
1
+ require "eslint/autocorrect/version"
2
+
3
+ module Eslint
4
+ module Autocorrect
5
+ # Your code goes here...
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ module Eslint
2
+ module Autocorrect
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eslint-autocorrect
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Josh Bodah
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-08-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description:
56
+ email:
57
+ - jb3689@yahoo.com
58
+ executables:
59
+ - eslint-autocorrect
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - README.md
66
+ - Rakefile
67
+ - eslint-autocorrect.gemspec
68
+ - exe/eslint-autocorrect
69
+ - lib/eslint/autocorrect.rb
70
+ - lib/eslint/autocorrect/version.rb
71
+ homepage: https://github.com/jbodah/eslint-autocorrect
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.2.3
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: an autocorrecter for eslint
95
+ test_files: []