69 lines
2.8 KiB
JavaScript
69 lines
2.8 KiB
JavaScript
const utils_1 = require("@typescript-eslint/utils");
|
|
const utils_2 = require("../utils/utils");
|
|
const utils_3 = require("../utils/utils");
|
|
module.exports = {
|
|
name: "call-sync-before-read",
|
|
meta: {
|
|
type: "problem",
|
|
messages: {
|
|
callSync: "Call context.sync() before trying to read '{{name}}'.",
|
|
},
|
|
docs: {
|
|
description: "Always call load on the object's properties followed by a context.sync() before reading them.",
|
|
category: "Possible Errors",
|
|
recommended: false,
|
|
url: "https://docs.microsoft.com/office/dev/add-ins/develop/application-specific-api-model#sync",
|
|
},
|
|
schema: [],
|
|
},
|
|
create: function (context) {
|
|
let apiReferences = [];
|
|
function checkPropertyIsRead(node) {
|
|
var _a;
|
|
const topExpression = (0, utils_2.findTopMemberExpression)(node);
|
|
switch ((_a = topExpression.parent) === null || _a === void 0 ? void 0 : _a.type) {
|
|
case utils_1.TSESTree.AST_NODE_TYPES.AssignmentExpression:
|
|
return topExpression.parent.right === topExpression;
|
|
default:
|
|
return true;
|
|
}
|
|
}
|
|
function findReadBeforeSync() {
|
|
const needSync = new Set();
|
|
apiReferences.forEach((apiReference) => {
|
|
var _a;
|
|
const operation = apiReference.operation;
|
|
const reference = apiReference.reference;
|
|
const variable = reference.resolved;
|
|
if (operation === "Get" && variable) {
|
|
needSync.add(variable);
|
|
}
|
|
if (operation === "Sync") {
|
|
needSync.clear();
|
|
}
|
|
if (operation === "Read" && variable && needSync.has(variable)) {
|
|
const node = reference.identifier;
|
|
if (((_a = node.parent) === null || _a === void 0 ? void 0 : _a.type) === utils_1.TSESTree.AST_NODE_TYPES.MemberExpression &&
|
|
checkPropertyIsRead(node.parent)) {
|
|
context.report({
|
|
node: node,
|
|
messageId: "callSync",
|
|
data: { name: node.name },
|
|
});
|
|
}
|
|
}
|
|
});
|
|
}
|
|
return {
|
|
Program() {
|
|
apiReferences = (0, utils_3.findOfficeApiReferences)(context.getScope());
|
|
apiReferences.sort((left, right) => {
|
|
return (left.reference.identifier.range[1] -
|
|
right.reference.identifier.range[1]);
|
|
});
|
|
findReadBeforeSync();
|
|
},
|
|
};
|
|
},
|
|
};
|
|
//# sourceMappingURL=call-sync-before-read.js.map
|