зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1774287 - Part 2: Add helpers.getIsTopLevelScript. r=Standard8
Depends on D149606 Differential Revision: https://phabricator.services.mozilla.com/D149607
This commit is contained in:
Родитель
7766271309
Коммит
9cd39b4c3d
|
@ -489,26 +489,7 @@ module.exports = {
|
|||
},
|
||||
|
||||
/**
|
||||
* Check whether a node is a function.
|
||||
*
|
||||
* @param {Object} node
|
||||
* The AST node to check
|
||||
*
|
||||
* @return {Boolean}
|
||||
* True or false
|
||||
*/
|
||||
getIsFunctionNode(node) {
|
||||
switch (node.type) {
|
||||
case "ArrowFunctionExpression":
|
||||
case "FunctionDeclaration":
|
||||
case "FunctionExpression":
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
|
||||
/**
|
||||
* Check whether the context is the global scope.
|
||||
* Check whether it's inside top-level script.
|
||||
*
|
||||
* @param {Array} ancestors
|
||||
* The parents of the current node.
|
||||
|
@ -516,10 +497,15 @@ module.exports = {
|
|||
* @return {Boolean}
|
||||
* True or false
|
||||
*/
|
||||
getIsGlobalScope(ancestors) {
|
||||
getIsTopLevelScript(ancestors) {
|
||||
for (let parent of ancestors) {
|
||||
if (this.getIsFunctionNode(parent)) {
|
||||
return false;
|
||||
switch (parent.type) {
|
||||
case "ArrowFunctionExpression":
|
||||
case "FunctionDeclaration":
|
||||
case "FunctionExpression":
|
||||
case "PropertyDefinition":
|
||||
case "StaticBlock":
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
|
|
@ -26,13 +26,16 @@ module.exports = {
|
|||
create(context) {
|
||||
return {
|
||||
AwaitExpression(node) {
|
||||
if (!helpers.getIsGlobalScope(context.getAncestors())) {
|
||||
if (!helpers.getIsTopLevelScript(context.getAncestors())) {
|
||||
return;
|
||||
}
|
||||
context.report({ node, messageId: "rejectTopLevelAwait" });
|
||||
},
|
||||
ForOfStatement(node) {
|
||||
if (!node.await || !helpers.getIsGlobalScope(context.getAncestors())) {
|
||||
if (
|
||||
!node.await ||
|
||||
!helpers.getIsTopLevelScript(context.getAncestors())
|
||||
) {
|
||||
return;
|
||||
}
|
||||
context.report({ node, messageId: "rejectTopLevelAwait" });
|
||||
|
|
|
@ -24,7 +24,7 @@ module.exports = {
|
|||
return {
|
||||
VariableDeclaration(node) {
|
||||
if (node.kind === "var") {
|
||||
if (helpers.getIsGlobalScope(context.getAncestors())) {
|
||||
if (helpers.getIsTopLevelScript(context.getAncestors())) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче