Files
powerpoint-toolbox/node_modules/eslint-plugin-office-addins/lib/rules/test-for-null-using-isNullObject.js
T
2025-03-07 19:22:02 +01:00

119 lines
5.6 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("@typescript-eslint/utils");
const getFunction_1 = require("../utils/getFunction");
exports.default = utils_1.ESLintUtils.RuleCreator(() => "https://docs.microsoft.com/office/dev/add-ins/develop/application-specific-api-model#ornullobject-methods-and-properties")({
name: "test-for-null-using-isNullObject",
meta: {
type: "problem",
messages: {
useIsNullObject: "Test the isNullObject property of '{{name}}'.",
},
docs: {
description: "Do not test the truthiness of an object returned by an OrNullObject method or property. Test it's isNullObject property.",
},
schema: [],
fixable: "code",
},
create: function (context) {
var _a;
const sourceCode = (_a = context.sourceCode) !== null && _a !== void 0 ? _a : context.getSourceCode();
function isConditionalTestExpression(node) {
return (node.parent != undefined &&
(node.parent.type === utils_1.AST_NODE_TYPES.IfStatement ||
node.parent.type === utils_1.AST_NODE_TYPES.WhileStatement ||
node.parent.type === utils_1.AST_NODE_TYPES.DoWhileStatement ||
node.parent.type === utils_1.AST_NODE_TYPES.ForStatement ||
node.parent.type === utils_1.AST_NODE_TYPES.ConditionalExpression) &&
node === node.parent.test);
}
function isInUnaryNullTest(node) {
return (node.parent != undefined &&
node.parent.type === utils_1.AST_NODE_TYPES.UnaryExpression &&
node.parent.operator === "!" &&
node.parent.argument === node);
}
function isInBinaryNullTest(node) {
return (node.parent != undefined &&
node.parent.type === utils_1.AST_NODE_TYPES.BinaryExpression &&
((node.parent.left === node &&
node.parent.right.type === utils_1.AST_NODE_TYPES.Literal &&
node.parent.right.raw === "null") ||
(node.parent.right === node &&
node.parent.left.type === utils_1.AST_NODE_TYPES.Literal &&
node.parent.left.raw === "null")));
}
function isInNullTest(node) {
var _a;
return (isConditionalTestExpression(node) ||
((_a = node.parent) === null || _a === void 0 ? void 0 : _a.type) === utils_1.AST_NODE_TYPES.LogicalExpression ||
isInUnaryNullTest(node) ||
isInBinaryNullTest(node));
}
function isNullObjectNode(node) {
if (node &&
((node.type === utils_1.AST_NODE_TYPES.VariableDeclarator &&
node.init &&
(0, getFunction_1.isGetOrNullObjectFunction)(node.init) &&
node.id.type === utils_1.AST_NODE_TYPES.Identifier) ||
(node.type === utils_1.AST_NODE_TYPES.AssignmentExpression &&
(0, getFunction_1.isGetOrNullObjectFunction)(node.right) &&
node.left.type === utils_1.AST_NODE_TYPES.Identifier))) {
return true;
}
return false;
}
function findNullObjectNullTests(scope) {
const variables = scope.variables;
const childScopes = scope.childScopes;
for (let i = 0; i < variables.length; i++) {
const variable = variables[i];
const references = variable.references;
let nullObjectCall = false;
const nullTests = [];
for (let ref = 0; ref < references.length; ref++) {
const identifier = references[ref].identifier;
if (isNullObjectNode(identifier.parent)) {
nullObjectCall = true;
}
if (isInNullTest(identifier)) {
nullTests.push(identifier);
}
}
if (nullObjectCall === true && nullTests.length > 0) {
nullTests.forEach((identifier) => {
context.report({
node: identifier,
messageId: "useIsNullObject",
data: { name: identifier.name },
fix: function (fixer) {
let ruleFix;
if (isInBinaryNullTest(identifier) && identifier.parent) {
const newTest = identifier.name + ".isNullObject";
ruleFix = fixer.replaceText(identifier.parent, newTest);
}
else {
ruleFix = fixer.insertTextAfter(identifier, ".isNullObject");
}
return ruleFix;
},
});
});
}
}
for (let i = 0; i < childScopes.length; ++i) {
findNullObjectNullTests(childScopes[i]);
}
}
return {
"Program:exit"(node) {
const scope = sourceCode.getScope
? sourceCode.getScope(node)
: context.getScope();
findNullObjectNullTests(scope);
},
};
},
defaultOptions: [],
});
//# sourceMappingURL=test-for-null-using-isNullObject.js.map