"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const utils_1 = require("@typescript-eslint/utils"); const load_1 = require("../utils/load"); const utils_2 = require("../utils/utils"); exports.default = utils_1.ESLintUtils.RuleCreator(() => "https://docs.microsoft.com/office/dev/add-ins/develop/application-specific-api-model#load")({ name: "call-sync-after-load", meta: { type: "suggestion", messages: { callSyncAfterLoad: "Call context.sync() after calling load on '{{name}}' for the property '{{loadValue}}' and before reading the property.", }, docs: { description: "Always call context.sync() between loading one or more properties on objects and reading any of those properties.", }, schema: [], }, create: function (context) { var _a; const sourceCode = (_a = context.sourceCode) !== null && _a !== void 0 ? _a : context.getSourceCode(); class VariablePropertySet extends Set { add(variableProperty) { return super.add(JSON.stringify(variableProperty)); } has(variableProperty) { return super.has(JSON.stringify(variableProperty)); } } let apiReferences = []; function findLoadBeforeSync() { const needSync = new VariablePropertySet(); apiReferences.forEach((apiReference) => { const operation = apiReference.operation; const reference = apiReference.reference; const identifier = reference.identifier; const variable = reference.resolved; if (operation === "Load" && variable) { const propertiesArgument = getPropertiesArgument(identifier); const propertyNames = propertiesArgument ? (0, load_1.parsePropertiesArgument)(propertiesArgument) : ["*"]; propertyNames.forEach((propertyName) => { needSync.add({ variable: variable.name, property: propertyName, }); }); } else if (operation === "Sync") { needSync.clear(); } else if (operation === "Read" && variable) { const propertyName = (0, utils_2.findPropertiesRead)(reference.identifier.parent); if (needSync.has({ variable: variable.name, property: propertyName }) || needSync.has({ variable: variable.name, property: "*" })) { const node = reference.identifier; context.report({ node: node, messageId: "callSyncAfterLoad", data: { name: node.name, loadValue: propertyName }, }); } } }); } function getPropertiesArgument(identifier) { var _a, _b; let propertiesArgument; if (((_a = identifier.parent) === null || _a === void 0 ? void 0 : _a.type) === utils_1.TSESTree.AST_NODE_TYPES.MemberExpression) { // Look for .load(...) call const methodCall = (0, utils_2.findCallExpression)(identifier.parent); if (methodCall && (0, load_1.isLoadCall)(methodCall)) { propertiesArgument = methodCall.arguments[0]; } } else if (((_b = identifier.parent) === null || _b === void 0 ? void 0 : _b.type) === utils_1.TSESTree.AST_NODE_TYPES.CallExpression) { // Look for context.load(, "...") call const args = identifier.parent.arguments; if ((0, load_1.isLoadCall)(identifier.parent) && args[0] == identifier && args.length < 3) { propertiesArgument = args[1]; } } return propertiesArgument; } return { Program(node) { const scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope(); apiReferences = (0, utils_2.findOfficeApiReferences)(scope); apiReferences.sort((left, right) => { return (left.reference.identifier.range[1] - right.reference.identifier.range[1]); }); findLoadBeforeSync(); }, }; }, defaultOptions: [], }); //# sourceMappingURL=call-sync-after-load.js.map