diff --git a/README.md b/README.md index afec290..53303a8 100644 --- a/README.md +++ b/README.md @@ -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 [file]`| 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 diff --git a/lib/slim_lint/options.rb b/lib/slim_lint/options.rb index 861f1b6..79d6af3 100644 --- a/lib/slim_lint/options.rb +++ b/lib/slim_lint/options.rb @@ -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. diff --git a/lib/slim_lint/runner.rb b/lib/slim_lint/runner.rb index b3e529f..19649d1 100644 --- a/lib/slim_lint/runner.rb +++ b/lib/slim_lint/runner.rb @@ -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 @@ -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 diff --git a/spec/slim_lint/options_spec.rb b/spec/slim_lint/options_spec.rb index 07d21bb..9285255 100644 --- a/spec/slim_lint/options_spec.rb +++ b/spec/slim_lint/options_spec.rb @@ -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] } diff --git a/spec/slim_lint/runner_spec.rb b/spec/slim_lint/runner_spec.rb index 0cfa0d7..fee562e 100644 --- a/spec/slim_lint/runner_spec.rb +++ b/spec/slim_lint/runner_spec.rb @@ -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 @@ -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