98 lines
4.4 KiB
JavaScript
98 lines
4.4 KiB
JavaScript
const utils_1 = require("@typescript-eslint/utils");
|
|
const load_1 = require("../utils/load");
|
|
const utils_2 = require("../utils/utils");
|
|
module.exports = {
|
|
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.",
|
|
category: "Best Practices",
|
|
recommended: false,
|
|
url: "https://docs.microsoft.com/office/dev/add-ins/develop/application-specific-api-model#load",
|
|
},
|
|
schema: [],
|
|
},
|
|
create: function (context) {
|
|
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 <obj>.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(<obj>, "...") 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() {
|
|
apiReferences = (0, utils_2.findOfficeApiReferences)(context.getScope());
|
|
apiReferences.sort((left, right) => {
|
|
return (left.reference.identifier.range[1] -
|
|
right.reference.identifier.range[1]);
|
|
});
|
|
findLoadBeforeSync();
|
|
},
|
|
};
|
|
},
|
|
};
|
|
//# sourceMappingURL=call-sync-after-load.js.map
|