【问题标题】:Can you Lint parts of a file in different languages?你能用不同的语言对文件的一部分进行 Lint 吗?
【发布时间】:2021-01-05 07:36:24
【问题描述】:

BLUF:您能否使用 linter 以不同的规则/语言对同一文件的不同部分进行 lint?

我有一些 HTML 文件,其中包含正文中各种不同语言的代码块。为了测试这些并确保它们遵循正确的样式,我希望能够以各自的语言对文件的这些部分进行 lint。

是否有可以做到这一点的 linter 或者有什么方法可以做到这一点?如果我只是用其他语言进行 lint,文件中的 html 只会抛出错误。

我知道我可以整理这些样本,将它们的内容写入单独的文件中,然后分别对其进行 lint,但我希望避免在 linting 周期中创建和更新它们。

目前正在使用 eslint。

例子:

<h1>A Title</h1>
<p>
  Some text that is nice normal html that can be linted the usual way.
</p>
<pre class='javascript>
  // this is some javascript that needs to be linted
  const foo = bar;
  function myFunction() {
    somethingThatMightNotPassLinting
  }
  ...
</pre>

【问题讨论】:

    标签: eslint static-analysis


    【解决方案1】:

    您可以使用自定义插件(例如 eslint-plugin-html)对 HTML 文件中的 js 进行 lint。修改插件以查找 &lt;pre&gt; 标签而不是 &lt;script&gt; 标签。

    【讨论】:

    • eslint-plugin-html 仅对脚本标签内的代码进行 lints。我希望能够在 html 正文中 lint 代码(也许我的问题不够清楚)
    • 哦,我明白了,那么在这种情况下,您有两个选择,修改该插件以同时查找 &lt;pre&gt; 标签,或者将内容迁移到 Markdown 并使用 eslint-plugin-markdown。第一个选项应该非常简单
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-29
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    • 1970-01-01
    相关资源
    最近更新 更多