我想编写一个测试套件,以确保某些给定的函数使用严格模式.其中有很多,手动检查它们似乎是一件苦差事.
类似问题中的答案使用函数定义的正则表达式进行检查.但是,我认为这将错误地检测正在测试的函数是在具有"use strict"或文件级"use strict"声明的函数内的情况.答案说"使用严格"是前置的,但在我的环境(Mozilla Rhino)中,情况并非如此:
$ cat strict_sub.js
"use strict";
var strict_function = function() {
not_a_real_global = "foo";
};
print(strict_function);
$ rhino strict_sub.js
function () {
not_a_real_global = "foo";
}
Run Code Online (Sandbox Code Playgroud)
我觉得答案是"不",但有没有办法反省一个函数,看它是否被解析并发现是严格模式?
更新:@Amy建议的一种方法是解析函数的源代码来解决它.如果函数具有use-strict声明(虽然它很繁琐),但是如果严格模式是从程序级传播,则不行; 在这种情况下,我们必须走了AST的计划水平,并检查该对use strict.为了使这个健壮,我们必须实现use strict-propagation的所有规则,解释器已经在某处.
(在SpiderMonkey中测试:
function f() {
"use strict";
}
var fast1 = Reflect.parse(f.toString());
var first_line = fast1.body[0].body.body[0].expression;
print(first_line.type === 'Literal' && first_line.value === 'use strict'); //true
Run Code Online (Sandbox Code Playgroud)
)
严格模式功能确实有"中毒" .caller和.arguments属性(也是ES5,额外),所以你可以测试:
function isStrict(fn) {
if (typeof fn != "function")
throw new TypeError("expected function");
try {
fn.caller; // expected to throw
return false;
} catch(e) {
return true;
}
}
Run Code Online (Sandbox Code Playgroud)