Add Path#isInStrictMode (#7712)

* Add Path#isInStrictMode

* Fix undefined directives

* Explicitly return for arrow expressions
This commit is contained in:
Justin Ridgewell
2018-04-11 09:13:38 -04:00
committed by GitHub
parent 6597a472b3
commit 7ae724f553
3 changed files with 136 additions and 20 deletions

View File

@@ -439,3 +439,33 @@ export function isConstantExpression() {
return false;
}
export function isInStrictMode() {
const start = this.isProgram() ? this : this.parentPath;
const strictParent = start.find(path => {
if (path.isProgram({ sourceType: "module" })) return true;
if (path.isClass()) return true;
if (!path.isProgram() && !path.isFunction()) return false;
if (
path.isArrowFunctionExpression() &&
!path.get("body").isBlockStatement()
) {
return false;
}
let { node } = path;
if (path.isFunction()) node = node.body;
for (const directive of node.directives) {
if (directive.value.value === "use strict") {
return true;
}
}
});
return !!strictParent;
}