简体   繁体   English

检查/验证javascript语法的简单方法

[英]Simple way to check/validate javascript syntax

I have some big set of different javascript-snippets (several thousands), and some of them have some stupid errors in syntax (like unmatching braces/quotes, HTML inside javascript, typos in variable names).我有很多不同的 javascript 片段(数千个),其中一些在语法上有一些愚蠢的错误(例如不匹配的大括号/引号、javascript 中的 HTML、变量名称中的拼写错误)。

I need a simple way to check JS syntax.我需要一种简单的方法来检查 JS 语法。 I've tried JSLint but it send too many warnings about style, way of variable definitions, etc. (even if i turn off all flags).我试过 JSLint,但它发送了太多关于样式、变量定义方式等的警告(即使我关闭了所有标志)。 I don't need to find out style problems, or improve javascript quality, i just need to find obvious syntax errors.我不需要找出样式问题,或提高 javascript 质量,我只需要找到明显的语法错误。 Of course i can simply check it in browser/browser console, but i need to do it automatically as the number of that snippets is big.当然,我可以简单地在浏览器/浏览器控制台中检查它,但我需要自动进行,因为这些片段的数量很大。

Add:添加:
JSLint/JSHint reports a lot of problems in the lines that are not 'beauty' but working (ie have some potential problems), and can't see the real problems, where the normal compiler will simply report syntax error and stop execution. JSLint/JSHint 报告很多不是'美'而是工作(即有一些潜在问题)的行中的问题,并且看不到真正的问题,正常编译器只会报告语法错误并停止执行。 For example, try to JSLint that code, which has syntax errors on line 4 (unmatched quotes), line 6 (comma required), and line 9 (unexpected <script>).例如,尝试 JSLint 该代码,该代码在第 4 行(不匹配的引号)、第 6 行(需要逗号)和第 9 行(意外的 <script>)有语法错误。

document.write('something');
a = 0;
if (window.location == 'http://google.com')  a = 1;
document.write("aaa='andh"+a+"eded"');
a = {
  something: ['a']
  something2: ['a']
};
<script>
a = 1;

你可以尝试JSHint ,它不那么冗长。

I've found that SpiderMonkey has ability to compile script without executing it, and if compilation failed - it prints error. 我发现SpiderMonkey能够编译脚本而不执行它,如果编译失败 - 它会输出错误。

So i just created small wrapper for SpiderMonkey 所以我刚刚为SpiderMonkey创建了一个小包装器

sub checkjs {
    my $js = shift;
    my ( $js_fh, $js_tmpfile ) = File::Temp::tempfile( 'XXXXXXXXXXXX', EXLOCK => 0, UNLINK => 1, TMPDIR => 1 );
    $| = 1;
    print $js_fh $js;
    close $js_fh;
    return qx(js -C -f $js_tmpfile 2>&1);
}

And javascriptlint.com also deals very good in my case. 而javascriptlint.com在我的情况下也非常好。 (Thanks to @rajeshkakawat). (感谢@rajeshkakawat)。

Just in case anyone is still looking you could try Esprima , 万一有人还在寻找你可以尝试Esprima

It only checks syntax, nothing else. 它只检查语法,没有别的。

Lots of options if you have an exhaustive list of the JSLint errors you do want to capture. 的选项很多,如果你有想捕捉的JSLint详尽的错误列表。

JSLint's code is actually quite good and fairly easy to understand (I'm assuming you already know JavaScript fairly well from your question). JSLint的代码实际上非常好并且相当容易理解(我假设你已经从你的问题中很好地了解了JavaScript)。 You could hack it to only check what you want and to continue no matter how many errors it finds. 您可以破解它只检查您想要的内容并继续,无论它发现多少错误。

You could also write something quickly in Node.js to use JSLint as-is to check every file/snippet quickly and output only those errors you care about. 您还可以在Node.js中快速编写内容,以便按原样使用JSLint快速检查每个文件/片段,并仅输出您关心的错误。

只需使用node --check filename

Semantic Designs' (my company) JavaScript formatter read JS files and formats them. Semantic Designs'(我的公司)JavaScript格式化程序读取JS文件并格式化它们。 You don't want the formatting part. 您不需要格式化部分。

To read the files it will format, it uses a full JavaScript parser, which does a complete syntax check (even inside regular expressions). 要读取它将格式化的文件,它使用完整的JavaScript解析器,它执行完整的语法检查(甚至在正则表达式中)。 If you run it and simply ignore the formatted result, you get a syntax checker. 如果您运行它并且只是忽略格式化结果,您将获得语法检查器。

You can give it big list of files and it will format all of them. 你可以给它大的文件列表,它将格式化所有文件。 You could use this to batch-check your large set. 您可以使用它来批量检查您的大型集。 (If there are any syntax errors, it returns a nonzero error status to a shell). (如果存在任何语法错误,则会向shell返回非零错误状态)。

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

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