github-pages-deploy-action/node_modules/eslint/lib/rules/no-labels.js

150 lines
4.4 KiB
JavaScript
Raw Permalink Normal View History

2020-03-07 11:45:40 +08:00
/**
* @fileoverview Disallow Labeled Statements
* @author Nicholas C. Zakas
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
2020-03-31 20:40:00 +08:00
const astUtils = require("./utils/ast-utils");
2020-03-07 11:45:40 +08:00
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
type: "suggestion",
docs: {
description: "disallow labeled statements",
category: "Best Practices",
recommended: false,
url: "https://eslint.org/docs/rules/no-labels"
},
schema: [
{
type: "object",
properties: {
allowLoop: {
type: "boolean",
default: false
},
allowSwitch: {
type: "boolean",
default: false
}
},
additionalProperties: false
}
2020-05-15 05:33:08 +08:00
],
messages: {
unexpectedLabel: "Unexpected labeled statement.",
unexpectedLabelInBreak: "Unexpected label in break statement.",
unexpectedLabelInContinue: "Unexpected label in continue statement."
}
2020-03-07 11:45:40 +08:00
},
create(context) {
const options = context.options[0];
const allowLoop = options && options.allowLoop;
const allowSwitch = options && options.allowSwitch;
let scopeInfo = null;
/**
* Gets the kind of a given node.
2020-03-31 20:40:00 +08:00
* @param {ASTNode} node A node to get.
2020-03-07 11:45:40 +08:00
* @returns {string} The kind of the node.
*/
function getBodyKind(node) {
if (astUtils.isLoop(node)) {
return "loop";
}
if (node.type === "SwitchStatement") {
return "switch";
}
return "other";
}
/**
* Checks whether the label of a given kind is allowed or not.
2020-03-31 20:40:00 +08:00
* @param {string} kind A kind to check.
2020-03-07 11:45:40 +08:00
* @returns {boolean} `true` if the kind is allowed.
*/
function isAllowed(kind) {
switch (kind) {
case "loop": return allowLoop;
case "switch": return allowSwitch;
default: return false;
}
}
/**
* Checks whether a given name is a label of a loop or not.
2020-03-31 20:40:00 +08:00
* @param {string} label A name of a label to check.
2020-03-07 11:45:40 +08:00
* @returns {boolean} `true` if the name is a label of a loop.
*/
function getKind(label) {
let info = scopeInfo;
while (info) {
if (info.label === label) {
return info.kind;
}
info = info.upper;
}
/* istanbul ignore next: syntax error */
return "other";
}
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
return {
LabeledStatement(node) {
scopeInfo = {
label: node.label.name,
kind: getBodyKind(node.body),
upper: scopeInfo
};
},
"LabeledStatement:exit"(node) {
if (!isAllowed(scopeInfo.kind)) {
context.report({
node,
2020-05-15 05:33:08 +08:00
messageId: "unexpectedLabel"
2020-03-07 11:45:40 +08:00
});
}
scopeInfo = scopeInfo.upper;
},
BreakStatement(node) {
if (node.label && !isAllowed(getKind(node.label.name))) {
context.report({
node,
2020-05-15 05:33:08 +08:00
messageId: "unexpectedLabelInBreak"
2020-03-07 11:45:40 +08:00
});
}
},
ContinueStatement(node) {
if (node.label && !isAllowed(getKind(node.label.name))) {
context.report({
node,
2020-05-15 05:33:08 +08:00
messageId: "unexpectedLabelInContinue"
2020-03-07 11:45:40 +08:00
});
}
}
};
}
};