lib/scss_lint/engine.rb in scss_lint-0.46.0 vs lib/scss_lint/engine.rb in scss_lint-0.47.0

- old
+ new

@@ -1,7 +1,9 @@ require 'sass' +require 'open3' + module SCSSLint class FileEncodingError < StandardError; end # Contains all information for a parsed SCSS file, including its name, # contents, and parse tree. @@ -13,17 +15,18 @@ # Creates a parsed representation of an SCSS document from the given string # or file. # # @param options [Hash] # @option options [String] :file The file to load + # @option options [String] :path The path of the file to load # @option options [String] :code The code to parse + # @option options [String] :preprocess_command A preprocessing command + # @option options [List<String>] :preprocess_files A list of files that should be preprocessed def initialize(options = {}) - if options[:path] - build_from_file(options) - elsif options[:code] - build_from_string(options[:code]) - end + @preprocess_command = options[:preprocess_command] + @preprocess_files = options[:preprocess_files] + build(options) # Need to force encoding to avoid Windows-related bugs. # Need to encode with universal newline to avoid other Windows-related bugs. # Need `to_a` for Ruby 1.9.3. encoding = 'UTF-8' @@ -43,27 +46,47 @@ end end private + def build(options) + if options[:path] + build_from_file(options) + elsif options[:code] + build_from_string(options[:code]) + end + end + # @param options [Hash] # @option file [IO] if provided, us this as the file object # @option path [String] path of file, loading from this if `file` object not # given def build_from_file(options) @filename = options[:path] @contents = options[:file] ? options[:file].read : File.read(@filename) + preprocess_contents @engine = Sass::Engine.new(@contents, ENGINE_OPTIONS.merge(filename: @filename)) end # @param scss [String] def build_from_string(scss) - @engine = Sass::Engine.new(scss, ENGINE_OPTIONS) @contents = scss + preprocess_contents + @engine = Sass::Engine.new(@contents, ENGINE_OPTIONS) end def find_any_control_commands @any_control_commands = @lines.any? { |line| line['scss-lint:disable'] || line['scss-line:enable'] } + end + + def preprocess_contents # rubocop:disable CyclomaticComplexity + return unless @preprocess_command + # Never preprocess :code scss if @preprocess_files is specified. + return if @preprocess_files && @filename.nil? + return if @preprocess_files && + @preprocess_files.none? { |pattern| File.fnmatch(pattern, @filename) } + @contents, status = Open3.capture2(@preprocess_command, stdin_data: @contents) + raise SCSSLint::Exceptions::PreprocessorError if status != 0 end end end