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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15679827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:41:18  来源:igfitidea点击:

Simple way to check/validate javascript syntax

javascriptsyntaxjslintjshint

提问by filimonov

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. I've tried JSLint but it send too many warnings about style, way of variable definitions, etc. (even if i turn off all flags). I don't need to find out style problems, or improve javascript quality, i just need to find obvious syntax errors. 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.

我需要一种简单的方法来检查 JS 语法。我试过 JSLint,但它发送了太多关于样式、变量定义方式等的警告(即使我关闭了所有标志)。我不需要找出样式问题,或提高 javascript 质量,我只需要找到明显的语法错误。当然,我可以简单地在浏览器/浏览器控制台中检查它,但我需要自动进行,因为这些片段的数量很大。

Add:
JSLint/JSHint reports a lot of problems in the lines that are not 'beauty' but working (i.e. have some potentialproblems), and can't see the realproblems, where the normal compiler will simply report syntax error and stop execution. 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/JSHint 报告很多不是'美'而是工作的行中的问题(即有一些潜在的问题),并看不到真正的问题,正常编译器会简单地报告语法错误并停止执行. 例如,尝试 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;

采纳答案by filimonov

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. (Thanks to @rajeshkakawat).

javascriptlint.com 对我来说也很划算。(感谢@rajeshkakawat)。

回答by Piet van Dongen

You could try JSHint, which is less verbose.

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

回答by Kevin Whitefoot

Just in case anyone is still looking you could try Esprima,

以防万一有人还在找你,你可以试试Esprima

It only checks syntax, nothing else.

它只检查语法,没有别的。

回答by ruffin

Lots of options if you have an exhaustive list of the JSLint errors you dowant 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). You could hack it to only check what you want and to continue no matter how many errors it finds.

JSLint 的代码实际上非常好并且相当容易理解(我假设您已经从您的问题中了解了 JavaScript)。您可以破解它以仅检查您想要的内容并继续,无论它发现多少错误。

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 来快速检查每个文件/代码段并仅输出您关心的那些错误。

回答by Ira Baxter

Semantic Designs' (my company) JavaScript formatter read JS files and formats them. You don't want the formatting part.

Semantic Designs(我的公司)的 JavaScript 格式化程序读取 JS 文件并对其进行格式化。你不想要格式部分。

To read the files it will format, it uses a full JavaScript parser, which does a complete syntax check (even inside regular expressions). If you run it and simply ignore the formatted result, you get a syntax checker.

为了读取它将格式化的文件,它使用了一个完整的 JavaScript 解析器,它会进行完整的语法检查(甚至在正则表达式中)。如果你运行它并简单地忽略格式化的结果,你会得到一个语法检查器。

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 返回一个非零错误状态)。