Skip to content

Commit

Permalink
Add remark-lint support
Browse files Browse the repository at this point in the history
  • Loading branch information
thorncp authored and Greg Lazarev committed Sep 14, 2017
1 parent 858addb commit 0536290
Show file tree
Hide file tree
Showing 9 changed files with 765 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This service uses the following linters:
* [haml-lint](https://github.com/brigade/haml-lint) for HAML
* [jshint](http://jshint.com) for JavaScript
* [reek](https://github.com/troessner/reek) for Ruby code smells
* [remark-lint](https://github.com/wooorm/remark-lint) for Markdown
* [rubocop](https://github.com/bbatsov/rubocop) for Ruby
* [sass-lint](https://github.com/sasstools/sass-lint) for SASS
* [scss-lint](https://github.com/brigade/scss-lint) for SCSS
Expand Down
3 changes: 3 additions & 0 deletions config/remarkrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["remark-preset-lint-recommended"]
}
1 change: 1 addition & 0 deletions jobs/linters_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require "linters/flog/options"
require "linters/haml_lint/options"
require "linters/jshint/options"
require "linters/remark/options"
require "linters/reek/options"
require "linters/rubocop/options"
require "linters/sass_lint/options"
Expand Down
43 changes: 43 additions & 0 deletions lib/linters/remark/options.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require "linters/base/options"
require "linters/remark/tokenizer"

module Linters
module Remark
class Options
def command(filename)
cmd = "remark-cli/cli.js #{filename}"
"NODE_PATH=#{node_modules_path} #{File.join(node_modules_path, cmd)}"
end

def config_filename
".remarkrc"
end

def tokenizer
Tokenizer.new
end

def config_content(content)
if JSON.parse(content).any?
content
else
config(content).to_json
end
end

private

def node_modules_path
File.join(current_path, "node_modules")
end

def current_path
File.expand_path("../../..", __dir__)
end

def config(content)
Config.new(content: content, default_config_path: "config/remarkrc")
end
end
end
end
25 changes: 25 additions & 0 deletions lib/linters/remark/tokenizer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Linters
module Remark
class Tokenizer
VIOLATION_REGEX = /\A
\s+
(?<line_number>\d+):(?<start_column>\d+)
(-(?<end_line_number>\d+):(?<end_column>\d+))?
\s{2,}
(?:\e\[\d+m)?
(?<violation_level>\w+)?
(?:\e\[\d+m)?
\s{2,}
(?<message>.+?)
\s{2,}
(?<rule-name>.+?)
\s{2,}
(?<source>.+?)
\z/x

def parse(text)
Linters::Tokenizer.new(text, VIOLATION_REGEX).parse
end
end
end
end
8 changes: 3 additions & 5 deletions lib/linters/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ def source_file

def config_file
if config_content
SourceFile.new(
linter_options.config_filename,
config_content,
)
SourceFile.new(linter_options.config_filename, config_content)
else
NoopFile.new
end
Expand All @@ -81,7 +78,8 @@ def filename
end

def config_content
linter_options.config_content(attributes.fetch("config"))
@_config_congtent ||= linter_options.
config_content(attributes.fetch("config"))
end
end
end
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"description": "Use Node for JS based linters",
"engines": {
"node": "6.6.0"
"node": "8.4.0"
},
"dependencies": {
"babel-eslint": "^7.2.3",
Expand Down Expand Up @@ -35,6 +35,9 @@
"esprima": "^3.1.3",
"jshint": "^2.9.2",
"prettier": "^1.3.1",
"remark-cli": "^3.0.1",
"remark-lint": "^6.0.0",
"remark-preset-lint-recommended": "^2.0.0",
"sass-lint": "^1.10.2",
"stylelint": "^7.9.0",
"stylelint-config-css-modules": "^1.1.0",
Expand Down
87 changes: 87 additions & 0 deletions spec/integrations/remark_lint_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
require "jobs/linters_job"

RSpec.describe LintersJob, "for remark-lint" do
include LintersHelper

context "when file with .md extention contains violations" do
it "reports violations" do
expect_violations_in_file(
content: content,
filename: "foo/test.md",
linter_name: "remark",
violations: [
{
line: 1,
message: "Incorrect list-item indent: add 2 spaces",
},
{
line: 3,
message: "Found reference to undefined definition",
},
],
)
end
end

context "when custom configuraton is provided" do
context "and plugins are an object" do
it "respects the custom configuration" do
config = <<~JSON
{
"plugins": {
"remark-preset-lint-recommended": true,
"lint-list-item-indent": false
}
}
JSON

expect_violations_in_file(
config: config,
content: content,
filename: "foo/test.md",
linter_name: "remark",
violations: [
{
line: 3,
message: "Found reference to undefined definition",
},
],
)
end
end

context "and plugins are an array" do
it "respects the custom configuration" do
config = <<~JSON
{
"plugins": [
"remark-preset-lint-recommended",
["lint-list-item-indent", false]
]
}
JSON

expect_violations_in_file(
config: config,
content: content,
filename: "foo/test.md",
linter_name: "remark",
violations: [
{
line: 3,
message: "Found reference to undefined definition",
},
],
)
end
end
end

def content
<<~MD
* Hello
[World][]
MD
end
end
Loading

0 comments on commit 0536290

Please sign in to comment.