简体   繁体   English

Javascript代码解析及常量

[英]Javascript code analysis and constants

Given there is no cross browser const in Javascript and most of the work-arounds are more complex than I care for, I am just going to go with the naming convention of THIS_IS_A_CONSTANT.鉴于 Javascript 中没有跨浏览器 const,并且大多数解决方法比我关心的更复杂,我将使用命名约定 THIS_IS_A_CONSTANT 的 go。 All well and good, but what occurred to me is that if there was way to get my IDE (VS.NET 2010 with Resharper 6) to give me a warning on any Javascript code that makes an assignment to a variable with that naming convention except in the variable declaration this would handle most of the potential issues around the lack of real constants in Javascript (at least for my needs).一切都很好,但我想到的是,如果有办法让我的 IDE(带有 Resharper 6 的 VS.NET 2010)在任何 Javascript 代码上给我一个警告,该代码使用该命名约定对变量进行赋值,除了在变量声明中,这将处理 Javascript 中缺少实常数的大部分潜在问题(至少满足我的需要)。

So does anyone know of a good way to generate such warnings?那么有人知道生成此类警告的好方法吗? In-IDE would be the best thing but other solutions are fine as well. In-IDE 将是最好的选择,但其他解决方案也很好。 I have looked for something like FX-Cop for Javascript;我已经为 Javascript 寻找类似 FX-Cop 的东西; jslint doesn't seem to allow the creation of new rules but maybe I didn't look deep enough. jslint 似乎不允许创建新规则,但也许我看的不够深入。 I may also suggest this as a feature in Resharper (assuming I am not missing a way to make it do so already).我也可能建议将此作为 Resharper 中的一个功能(假设我没有错过让它这样做的方法)。

Thanks, Matthew谢谢,马修

So you want to find any assigment of the form:所以你想找到表格的任何分配:

id = exp ;

where id doesn't contain the substring CONSTANT and exp is a numeric constant?其中 id 不包含 substring CONSTANT 并且 exp 是数字常量?

Our Source Code Search Engine (SCSE) can do this pretty directly.我们的源代码搜索引擎 (SCSE)可以非常直接地做到这一点。 The SCSE reads source code for a large set of files for many languages (including JavaScript), breaks it into tokens ignoring whitespace , and indexes it all to enable fast search for token sequences. SCSE 读取用于多种语言(包括 JavaScript)的大量文件的源代码,将其分解为忽略空格的标记,并将其全部编入索引以实现快速搜索标记序列。 Any hits are displayed in a hit window and can be clicked to see the actual file text in context.任何命中都会显示在命中 window 中,并且可以单击以查看上下文中的实际文件文本。

Your particular query would be stated:将说明您的特定查询:

  (I - I=*CONSTANT*) '=' N ( ';' | O | K | I)

This hunts for any assignment in which the target identifier doesn't contain the string constant (see wildcard stars around the match string), assigned a constant * N *umber is not followed by a ';'这会寻找目标标识符不包含字符串常量的任何分配(请参阅匹配字符串周围的通配符星号),分配一个常量 * N *number 后不跟一个 ';' or an * O *perator, * K *word or * I *dentifier (all this extra stuff is because JavaScript might not have a semicolon to terminate the statement).或 * O * 运算符、 * K *word 或 * I * 标识符(所有这些额外的东西是因为 JavaScript 可能没有分号来终止语句)。 It probably picks up some cases it should not but these are easily inspected.它可能会捡起一些不应该的情况,但这些情况很容易检查。

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

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