的FxCop:化合物字应被视为离散的术语 [英] FxCop: Compound word should be treated as discrete term

查看:269
本文介绍了的FxCop:化合物字应被视为离散的术语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的FxCop要我拼用户名以一个大写N(即用户名),因为它是一个复合词。但是,由于一致性的原因,我们需要有一个小写的n拼了 - 所以无论是用户名或用户名



我已经尝试通过添加以下扭捏CodeAnalysisDictionary.xml。节段:

 < DiscreteExceptions> 
<条款>用户名< /期限>
< / DiscreteExceptions>



据我了解如何自定义词典的工作,这应该告诉FxCop的对待用户名作为离散项,防止CompoundWordsShouldBeCasedCorrectly(CA1702)检查火错误。



可惜,这是行不通的。没有任何人有一个想法,这是为什么,以及如何解决这个问题?我不希望添加压抑,因为这会严重扰乱了GlobalSuppressions文件,因为有相当多的出现。



编辑补充:暂且我以GlobalSuppressions解决了这个,但鉴于问题的性质这似乎并不像解决这一理想的方式。任何人都可以给到哪里寻找有关的FxCop如何应用在字典中定义的规则进一步的信息提示吗?


解决方案

我是3年的的FxCop /托管代码分析团队的开发人员,我有你的答案。因为我的时候事情发生了变化,我已经忘记了辞典的处理究竟是如何工作的定制,因此,我花了相当多的时间来弄清楚这一点。 :)



摘要



简短的回答是,你需要删除,以用户名的所有引用,用户名,用户名,用户名以及从C:\Program文件(x86)\Microsoft的FxCop 1.36\CustomDictionary.xml



通常情况下,我不会推荐这个,因为它应该不是必需的,但你发现我相信是一个错误,这是我能找到的唯一的解决方法。



全文



好了,现在为的的答案......



规则有两个不同的检查,它会工作如下



一个。检查复合词,应该是离散




  1. 分割标识为标记:如文件名 - > {文件,姓名}

  2. 拼写检查每对相邻的令牌。

  3. 如果拼写检查成功(如文件名被认为是一个有效的字),结果
    ,则我们发现,因为一个字不应该被表示为$一个潜在的问题b $ b两标记

  4. 然而,如果有一个<期限CompoundAlternate =文件名>文件名< /期限>
    <化合物> 自定义词典的部分,那么它被认为是指
    虽然文件名是一个字,设计原则(这主要是由于点头一致性
    与该早于规则的存在框架现有技术)坚持认为,这
    应该写成文件名,所以我们要抑制警告

  5. 此外,如果有一个<期限>文件名< /期限> <进入; DiscreteExceptions> 自定义词典中的
    节,那么它被认为是指虽然文件名'是
    A的话,也可能是两个单词在不同的上下文文件'和'名'。例如
    发病是一个字,但要求用户更改 DoSomethingOnSet
    DoSomethingOnset 会噪音,所以我们要抑制警告。



乙。检查离散的话,应该是化合物




  1. 从A.1服用令牌,逐个检查与设定复合$ B中的每一个$ b项中的自定义词典。

  2. 如果有匹配,那么我们就必须提醒在与步骤A.4的解释是一致的。



请注意,你的警告:用户名应检测用户名在B部分,不咨询DiscreteExceptions部分,这就是为什么你不能够通过修改部分抑制警告。问题是,默认的自定义词典有一个条目,表明对于用户名正确的外壳总是用户名。它需要被拆除或以某种方式覆盖。



的错误



现在,提供了理想的解决方案是离开默认的自定义词典独自一人,在你的项目文件中指定 SearchFxCopDir = FALSE ,然后在只有你那是CustomDictionary.xml想要的默认的自定义词典的部分合并用于您的项目。可悲的是,这不起作用为1.36的FxCop忽略SearchFxCopDir指令,并始终将其视为真实的。我相信这是一个错误,但它也有可能,这是一个有意的变化,因为该指令未记录,并没有相应的UI。老实说,我不知道......



结论



由于FxCop的始终使用其默认的自定义词典中除了项目自定义词典,你唯一的办法是删除从默认的自定义词典问题的条目。



如果我有机会,我会联系当前的代码分析团队,看看这其实是一个错误,并报告在这里...


FxCop wants me to spell Username with a capital N (i.e. UserName), due to it being a compound word. However, due to consistency reasons we need to spell it with a lowercase n - so either username or Username.

I've tried tweaking the CodeAnalysisDictionary.xml by adding the following section to the section:

<DiscreteExceptions>
  <Term>username</Term>
</DiscreteExceptions>

From what I understand how custom dictionaries work, this should tell FxCop to treat username as a discrete term and prevent the CompoundWordsShouldBeCasedCorrectly (CA1702) check to fire an error.

Unfortunately this doesn't work. Does anybody have an idea why that is and how to solve this? I don't want to add suppressions, because this would seriously clutter the GlobalSuppressions file as there are quite a lot of occurrences.

Edited to add: For the time being I have solved this by using GlobalSuppressions, but given the nature of the issue this doesn't seem like the ideal way to solve this. Can anybody give a hint on where to look for further information on how FxCop applies the rules defined in a dictionary?

解决方案

I was a developer on the FxCop / Managed Code Analysis Team for 3 years and I have your answer. Things have changed since my time, and I had forgotten exactly how custom dictionary handling worked and so it took me quite a bit of time to figure this out. :)

Executive Summary

The short answer is that you need to remove all references to username, usernames, UserName, and UserNames from C:\Program Files (x86)\Microsoft FxCop 1.36\CustomDictionary.xml.

Normally, I would not recommend this as it should not be required, but you have found what I believe is a bug, and this is the only workaround that I could find.

Full Story

OK, now for the long answer...

The rule has two distinct checks which work as follows:

A. Check for compound words that should be discrete

  1. Split identifier into tokens: e.g. FileName --> { "file", "name" }
  2. Spell check each adjacent pair of tokens.
  3. If the spell check succeeds (e.g. filename is deemed to be a valid word),
    then we have found a potential problem since a single word should not be expressed as two tokens.
  4. However, if there is a <Term CompoundAlternate="FileName">filename</Term> in the <Compound> section of the custom dictionary, then it is taken to mean that although filename is a word, the design guidelines (largely as a nod to consistency with prior art in the Framework that predates the existence of the rule) insist it should be written as FileName, and so we must suppress the warning.
  5. Also, if there is a <Term>filename</Term> entry in the <DiscreteExceptions> section of the custom dictionary, then it is taken to mean that although 'filename' is a word, it might also be two words 'file' and 'name' in a different context. e.g. Onset is a word, but asking the user to change DoSomethingOnSet to DoSomethingOnset would be noise, and so we must suppress the warning.

B. Check for discrete words that should be compound:

  1. Taking the tokens from A.1, check each one individually against the set of compound terms in the custom dictionary.
  2. If there is a match, then we must warn in keeping with the interpretation in step A.4.

Notice that your warning: Username should be UserName is detected in part B, which does not consult the DiscreteExceptions section, which is why you are not able to suppress the warning by modifying that section. The problem is that the default custom dictionary has an entry stating that the correct casing for username is always UserName. It needs to be removed or overridden somehow.

The Bug

Now, the ideal solution would be to leave the default custom dictionary alone, specify SearchFxCopDir=false in your project file, and then merge in only the parts of the default custom dictionary that you want in the CustomDictionary.xml that is used for your project. Sadly, this does not work as FxCop 1.36 ignores the SearchFxCopDir directive and always treats it as true. I believe this is a bug, but it is also possible that this was an intentional change since the directive is not documented and has no corresponding UI. I honestly don't know...

Conclusion

Given that FxCop always uses its default custom dictionary in addition to the project custom dictionary, your only recourse is to remove the entries in question from the default custom dictionary.

If I have a chance, I will contact the current code analysis team to see if this in fact a bug, and report back here...

这篇关于的FxCop:化合物字应被视为离散的术语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆