eslint-plugin-json-schema-validator
Version:
ESLint plugin that validates data using JSON Schema Validator.
131 lines (130 loc) • 4.36 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.findInitNode = exports.getStaticValue = exports.getStringLiteralValue = exports.getStaticPropertyName = void 0;
const eslintUtils = __importStar(require("eslint-utils"));
function getStaticPropertyName(node, context) {
let key;
if (node.type === "Property") {
key = node.key;
if (!node.computed) {
if (key.type === "Identifier") {
return key.name;
}
}
}
else if (node.type === "MemberExpression") {
key = node.property;
if (!node.computed) {
if (key.type === "Identifier") {
return key.name;
}
return null;
}
}
else {
return null;
}
if (key.type === "Literal" || key.type === "TemplateLiteral") {
return getStringLiteralValue(key);
}
if (key.type === "Identifier") {
const init = findInitNode(context, key);
if (init) {
if (init.node.type === "Literal" ||
init.node.type === "TemplateLiteral") {
return getStringLiteralValue(init.node);
}
}
}
return null;
}
exports.getStaticPropertyName = getStaticPropertyName;
function getStringLiteralValue(node) {
if (node.type === "Literal") {
if (node.value == null) {
if (node.bigint != null) {
return String(node.bigint);
}
}
return String(node.value);
}
if (node.type === "TemplateLiteral") {
if (node.expressions.length === 0 && node.quasis.length === 1) {
return node.quasis[0].value.cooked;
}
}
return null;
}
exports.getStringLiteralValue = getStringLiteralValue;
function findVariable(context, node) {
return eslintUtils.findVariable(getScope(context, node), node);
}
function getStaticValue(context, node) {
return eslintUtils.getStaticValue(node, getScope(context, node));
}
exports.getStaticValue = getStaticValue;
function findInitNode(context, node) {
const variable = findVariable(context, node);
if (!variable) {
return null;
}
if (variable.defs.length === 1) {
const def = variable.defs[0];
if (def.type === "Variable" &&
def.parent.kind === "const" &&
def.node.init) {
let init = def.node.init;
const reads = variable.references
.filter((ref) => ref.isRead())
.map((ref) => ref.identifier);
if (init.type === "Identifier") {
const data = findInitNode(context, init);
if (!data) {
return null;
}
init = data.node;
reads.push(...data.reads);
}
return {
node: init,
reads,
};
}
}
return null;
}
exports.findInitNode = findInitNode;
function getScope(context, currentNode) {
const inner = currentNode.type !== "Program";
const scopeManager = context.getSourceCode().scopeManager;
let node = currentNode;
for (; node; node = node.parent || null) {
const scope = scopeManager.acquire(node, inner);
if (scope) {
if (scope.type === "function-expression-name") {
return scope.childScopes[0];
}
return scope;
}
}
return scopeManager.scopes[0];
}