Skip to content

Commit

Permalink
Add --stdin-file-path option
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Matveyev committed Apr 8, 2021
1 parent f4d868c commit a24b9c5
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -62,6 +62,7 @@ Command Line Flag | Description
`-e`/`--exclude` | Exclude one or more files from being linted
`-i`/`--include-linter` | Specify which linters you specifically want to run
`-x`/`--exclude-linter` | Specify which linters you _don't_ want to run
`--stdin-file-path` | Pipe source from STDIN, using file in offense reports
`--[no-]color` | Whether to output in color
`--reporter [reporter]` | Specify which output formatter to use
`--show-linters` | Show all registered linters
Expand Down
5 changes: 5 additions & 0 deletions lib/slim_lint/options.rb
Expand Up @@ -73,6 +73,11 @@ def add_file_options(parser)
'List of file names to exclude') do |files|
@options[:excluded_files] = files
end

parser.on('--stdin-file-path file', String,
'Pipe source from STDIN, using file in offense reports.') do |file|
@options[:stdin_file_path] = file
end
end

# Register informational flags.
Expand Down
17 changes: 11 additions & 6 deletions lib/slim_lint/runner.rb
Expand Up @@ -19,9 +19,14 @@ def run(options = {})

linter_selector = SlimLint::LinterSelector.new(config, options)

lints = files.map do |file|
collect_lints(file, linter_selector, config)
end.flatten
lints =
if options[:stdin_file_path].nil?
files.map do |file|
collect_lints(File.read(file), file, linter_selector, config)
end.flatten
else
collect_lints($stdin.read, options[:stdin_file_path], linter_selector, config)
end

SlimLint::Report.new(lints, files)
end
Expand Down Expand Up @@ -49,14 +54,14 @@ def load_applicable_config(options)
# @param file [String] path to file to lint
# @param linter_selector [SlimLint::LinterSelector]
# @param config [SlimLint::Configuration]
def collect_lints(file, linter_selector, config)
def collect_lints(file_content, file_name, linter_selector, config)
begin
document = SlimLint::Document.new(File.read(file), file: file, config: config)
document = SlimLint::Document.new(file_content, file: file_name, config: config)
rescue SlimLint::Exceptions::ParseError => e
return [SlimLint::Lint.new(nil, file, e.lineno, e.error, :error)]
end

linter_selector.linters_for_file(file).map do |linter|
linter_selector.linters_for_file(file_name).map do |linter|
linter.run(document)
end.flatten
end
Expand Down
8 changes: 8 additions & 0 deletions spec/slim_lint/options_spec.rb
Expand Up @@ -41,6 +41,14 @@
end
end

context 'with input from stdin' do
let(:args) { %w[--stdin-file-path file1.slim] }

it 'sets the `stdin_file_path` option to passed file name' do
subject.should include stdin_file_path: 'file1.slim'
end
end

context 'with a reporter option' do
context 'for a reporter that exists' do
let(:args) { %w[--reporter Json] }
Expand Down
14 changes: 14 additions & 0 deletions spec/slim_lint/runner_spec.rb
Expand Up @@ -24,6 +24,7 @@

before do
runner.stub(:collect_lints).and_return([])
File.stub(:read).and_return('.myclass')
end

it 'searches for lints in each file' do
Expand Down Expand Up @@ -62,5 +63,18 @@
subject
end
end

context 'when `--stdin-file-path` option specified' do
let(:options) { { stdin_file_path: 'file1.slim' } }

before do
$stdin.stub(:read).and_return('.myclass')
end

it 'searches for lints from STDIN' do
runner.should_receive(:collect_lints).exactly(1).times
subject
end
end
end
end

0 comments on commit a24b9c5

Please sign in to comment.