[英]Warn when code throws a string instead of an error
有时在JavaScript中,人们会写出throw 'Something went wrong';
而不是throw Error('something went wrong');
第一个例子是完全有效的JS,但并不理想,因为字符串没有堆栈跟踪,但错误确实如此。 Closure Compiler可以在代码抛出字符串而不是抛出错误时发出警告吗?
您可以使用JS Conformance文件执行此操作。 完整的详细信息在这里 ,但基本上你创建一个文件js_conformance.txt
:
requirement: {
type: BANNED_CODE_PATTERN
error_message: 'The use of throw with a string is not allowed. Throw an Error object instead.'
value: '/** @param {string|String} str */ function template(str) { throw str }'
}
并使用--conformance_configs=js_conformance.txt
将其传递给编译器
如果你的代码库中有很多现有的throw 'string'
,你可以使用RefasterJS清理它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.