ruby-on-rails – 为urlFormat的scss_lint gem添加排除选项

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 为urlFormat的scss_lint gem添加排除选项前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用gem scss_lint进行scss linting.我遇到的问题是我在网址的css中的某些字符串中有变量,因此linter报告错误

我认为应该有一种方法可以排除支票中的某些内容.我看到数据:已被列入白名单,但我们有时会在css中使用变量,因为它被放入了cms.

UrlFormat: Invalid URL '/response/${siteId}/artifact/${campaignAndToolPath}/assets/fonts/roboto-light-webfont.woff2': bad URI(is not URI?): /response/${siteId}/artifact/${campaignAndToolPath}/assets/fonts/roboto-light-webfont.woff2

错误代码是:

@font-face {
  font-family: 'roboto';
  src: url('/response/${siteId}/artifact/${campaignAndToolPath}/assets/fonts/roboto-italic-webfont.woff2') format('woff2'),url('/response/${siteId}/artifact/${campaignAndToolPath}/assets/fonts/roboto-italic-webfont.woff') format('woff');
  font-weight: $regular;
  font-style: italic;
}

也许还有办法

UrlFormat:
  enabled: true
  ignore or exclude: [
     - '${everythingbetween}'
     - '{{everything between}}'
  ]

我不熟悉Ruby,以便编写一些内容以允许检查这些变量,并忽略变量本身或整行.

有人有兴趣解决这个问题吗?

它的Ruby代码是.

require 'uri'

module SCSSLint
  # Checks the format of URLs for unnecessary protocols or domains.
  class Linter::UrlFormat < Linter
    include LinterRegistry

  def visit_script_funcall(node)
    return unless node.name == 'url'

    if url_string?(node.args[0])
      url = node.args[0].value.value.to_s
      check_url(url,node)
    end

    yield
  end

  def visit_prop(node)
    if url_literal?(node.value)
      url = node.value.to_sass.sub(/^url\((.*)\)$/,'\\1')
      check_url(url,node)
    end

    yield
  end

  private

    def url_literal?(prop_value)
      return unless prop_value.is_a?(Sass::Script::Tree::Literal)
      return unless prop_value.value.is_a?(Sass::Script::Value::String)
      return unless prop_value.value.type == :identifier

      prop_value.to_sass.start_with?('url(')
    end

    def url_string?(arg)
      return unless arg.is_a?(Sass::Script::Tree::Literal)
      return unless arg.value.is_a?(Sass::Script::Value::String)

      arg.value.type == :string
    end

    def check_url(url,node)
      return if url.start_with?('data:')
      uri = URI(url)

      if uri.scheme || uri.host
        add_lint(node,"URL `#{url}` should not contain protocol or domain")
      end
    rescue URI::Error => ex
      add_lint(node,"Invalid URL `#{url}`: #{ex}")
    end
  end
end

我认为解决方案可能只是忽略变量对数据的处理方式:而不是错误,除非UrlFormat实际上由于缺少引号或其他内容而无效.

任何帮助,将不胜感激.

解决方法

这应该工作:我做的改变是这行返回如果url.include?(‘${‘).它忽略包含字符串${的url,就像忽略以数据开头的url一样:
def check_url(url,node)
  return if url.start_with?('data:') || url.include?('${')
  uri = URI(url)

  if uri.scheme || uri.host
    add_lint(node,"URL `#{url}` should not contain protocol or domain")
  end
rescue URI::Error => ex
  add_lint(node,"Invalid URL `#{url}`: #{ex}")
end
原文链接:https://www.f2er.com/ruby/269773.html

猜你在找的Ruby相关文章