简体   繁体   English

ESLint - 帮助识别缩进子规则

[英]ESLint - Help identifying indent sub-rule

How can I get ESLint indent to like this code:我怎样才能让 ESLint indent喜欢这个代码:

const a = `
  <li
    style=${styleMap({
      color: '#F00',      // bad line
      background: '#0F0'  // bad line
    })}                   // bad line
  >Something</li>
`;

It doesn't like the indenting on the lines I marked bad line .它不喜欢我标记为bad line的行上的缩进。 This is what it wants me to do:这就是它要我做的:

const a = `
  <li
    style=${styleMap({
  color: '#F00',
  background: '#0F0'
})}
  >
    <span>Something</span>
  </li>
`;

which looks ridiculous.这看起来很荒谬。

Here is my current rule regarding 'indent':这是我目前关于“缩进”的规则:

indent: ['error', 2, { SwitchCase: 1 }],

I looked through the indent documentation but didn't see anything that seemed to match this scenario.我查看了indent文档,但没有看到任何与这种情况相匹配的内容。


Edit: I went through all of the options for the indent rule, set them all to 0 then went through one-by-one setting them to 10 to see which one affects my expression.编辑:我浏览了indent规则的所有选项,将它们全部设置为0 ,然后将它们一一设置为10 ,看看哪一个会影响我的表达。 Looks like ObjectExpression is the culprit, but that value is normally good.看起来ObjectExpression是罪魁祸首,但该值通常很好。

It seems like there are some issue dealing with template literal multiple line placeholder object declarations.似乎在处理模板文字多行占位符 object 声明时存在一些问题。 I think my only recourse is going to be to add it to ignoreNodes .我认为我唯一的办法是将其添加到ignoreNodes

Note : See the bottom of this answer for the final version.注意:有关最终版本,请参阅此答案的底部。 Tweaked it further as I continued through linting everything.当我继续检查所有内容时,进一步调整了它。

Once I narrowed it down a little further, I was able to find some better Google results and found this answer which seemed very similar to my problem, but not quite: Ignore the indentation in a template literal, with the ESLint `indent` rule一旦我进一步缩小范围,我就能找到一些更好的谷歌结果并找到这个答案,这似乎与我的问题非常相似,但不完全是: Ignore the indentation in a template literal, with the ESLint `indent` rule

The AST pattern didn't work for my specific case (at least the first one). AST 模式不适用于我的特定情况(至少是第一个)。 After poking it with an AST explorer, I ended up going with these patterns:在用 AST explorer 戳它之后,我最终使用了这些模式:

indent: ['error', 2, { 
  ignoredNodes: [
    'TemplateLiteral > CallExpression > ObjectExpression',
    'TemplateLiteral *'
  ]
]

The first covers the exact scenario in my question.第一个涵盖了我的问题中的确切情况。 The second covers a similar scenario:第二个涵盖了类似的场景:

const a = `
  <li
    style=${someFunc(
      'arg1',
      'arg2'
    })}      
  >Something</li>
`;

Seems this is ultimately an issue that ESLint is going to have to fix somehow, because the ObjectExpression check alone gets really confused inside of a template literal placeholder.似乎这最终是 ESLint 必须以某种方式解决的问题,因为单独的ObjectExpression检查在模板文字占位符内部真的很混乱。


EDIT:编辑:

I changed the second expression from TemplateLiteral > CallExpression to TemplateLiteral > * because I found two more slightly different scenarios that weren't caught already.我将第二个表达式从TemplateLiteral > CallExpressionTemplateLiteral > * ,因为我发现了两个稍微不同的场景,它们还没有被捕获。 I'd prefer to be more specific, but I don't want to have to identify 80 different scenarios.我希望更具体一些,但我不想确定 80 种不同的场景。


EDIT 2:编辑2:

Changed TemplateLiteral > * to TemplateLiteral * instead because found even more that were nested and not caught by > * .TemplateLiteral > *更改为TemplateLiteral *而是因为发现更多嵌套且未被> *捕获的内容。

Now, the patterns are just one:现在,模式只是一个:

indent: ['error', 2, { 
  ignoredNodes: [
    'TemplateLiteral *'
  ]
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM