Skip to content

Commit

Permalink
Merge pull request #388 from DinoChiesa/issue387
Browse files Browse the repository at this point in the history
Allow spaces in template function arguments
  • Loading branch information
ssvaidyanathan authored Sep 27, 2023
2 parents 6cde92c + 2a2719c commit 03b2e22
Show file tree
Hide file tree
Showing 5 changed files with 571 additions and 424 deletions.
275 changes: 149 additions & 126 deletions lib/package/plugins/PO026-assignVariableUsage.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,153 +17,176 @@
//|   |:white_medium_square:| PO026 | AssignVariable Usage | With AssignVariable, check various usage issues. The Name element must be present. The Ref element, if any, should not be surrounded in curlies. |

const xpath = require("xpath"),
util = require('util'),
ruleId = require("../myUtil.js").getRuleId(),
pluginUtil = require('./_pluginUtil.js'),
debug = require("debug")("apigeelint:" + ruleId);
util = require("util"),
ruleId = require("../myUtil.js").getRuleId(),
pluginUtil = require("./_pluginUtil.js"),
debug = require("debug")("apigeelint:" + ruleId);

const plugin = {
ruleId,
name: "AssignVariableUsage",
fatal: false,
severity: 2, //error
nodeType: "AssignMessage",
enabled: true
};
ruleId,
name: "AssignVariableUsage",
fatal: false,
severity: 2, //error
nodeType: "AssignMessage",
enabled: true,
};

let profile = "apigee";
const onBundle = function(bundle, cb) {
const onBundle = function (bundle, cb) {
profile = bundle.profile;
if (typeof cb == "function") {
cb(null, false);
}
};

const onPolicy = function(policy, cb) {
let flagged = false;
const ptype = policy.getType();
if (ptype === "AssignMessage" || ptype === "RaiseFault") {
const addMessage = (line, column, message) => {
policy.addMessage({plugin, message, line, column});
flagged = true;
};
const xpathExpr = (ptype === "AssignMessage")? "/AssignMessage/AssignVariable" : "/RaiseFault/FaultResponse/AssignVariable";
const avNodes = xpath.select(xpathExpr, policy.getElement());
if (avNodes && avNodes.length>0) {
debug(`${policy.fileName} found ${avNodes.length} AssignVariable elements`);
avNodes.forEach( (node, ix) => {
const addNodeMessage = (message) =>
addMessage(node.lineNumber, node.columnNumber, message);

debug(`Configuration profile is: ${profile}`);
let found = { Name:0, Ref:0, Value:0, Template:0 };
if( profile === "apigeex") {
found = { ...found, PropertySetRef:0, ResourceURL:0 };
}

// Get the keys after Name
const foundKeysStr = Object.keys(found).filter(k => k != 'Name').join(',');
const onPolicy = function (policy, cb) {
let flagged = false;
const ptype = policy.getType();
if (ptype === "AssignMessage" || ptype === "RaiseFault") {
const addMessage = (line, column, message) => {
policy.addMessage({ plugin, message, line, column });
flagged = true;
};
const xpathExpr =
ptype === "AssignMessage"
? "/AssignMessage/AssignVariable"
: "/RaiseFault/FaultResponse/AssignVariable";
const avNodes = xpath.select(xpathExpr, policy.getElement());
if (avNodes && avNodes.length > 0) {
debug(
`${policy.fileName} found ${avNodes.length} AssignVariable elements`,
);
avNodes.forEach((node, ix) => {
const addNodeMessage = (message) =>
addMessage(node.lineNumber, node.columnNumber, message);

debug(`Configuration profile is: ${profile}`);
let found = { Name: 0, Ref: 0, Value: 0, Template: 0 };
if (profile === "apigeex") {
found = { ...found, PropertySetRef: 0, ResourceURL: 0 };
}

const childNodes = xpath.select("*", node);
if (childNodes.length == 0) {
addNodeMessage(`empty AssignVariable. Should have a Name child, and at least one of {${foundKeysStr}}.` );
// Get the keys after Name
const foundKeysStr = Object.keys(found)
.filter((k) => k != "Name")
.join(",");

const childNodes = xpath.select("*", node);
if (childNodes.length == 0) {
addNodeMessage(
`empty AssignVariable. Should have a Name child, and at least one of {${foundKeysStr}}.`,
);
} else {
childNodes.forEach((child, _ix) => {
debug(util.format(child));
if (typeof found[child.tagName] !== "undefined") {
found[child.tagName]++;
} else {
addMessage(
child.lineNumber,
child.columnNumber,
`There is a stray element (${child.tagName})`,
);
}
else {
childNodes.forEach( (child, _ix) => {
debug(util.format(child));
if (typeof found[child.tagName] !== 'undefined') {
found[child.tagName]++;
}
else {
addMessage(child.lineNumber, child.columnNumber, `There is a stray element (${child.tagName})`);
}
});

if (found.Name > 1) {
addNodeMessage("There is more than one Name element");
}
else if (found.Name == 0) {
addNodeMessage("There is no Name element");
}

if (found.Ref > 1) {
addNodeMessage("There is more than one Ref element");
}
else if (found.Ref == 1) {
const textnode = xpath.select("Ref/text()", node),
refText = textnode[0] && textnode[0].data;
if (refText && (refText.includes('{') || refText.includes('}'))) {
addMessage(textnode[0].lineNumber, textnode[0].columnNumber, "The text of the Ref element must be a variable name, should not include curlies");
}
}

if (found.PropertySetRef > 1) {
addNodeMessage("There is more than one PropertySetRef element");
}
else if (found.PropertySetRef == 1) {
const textnode = xpath.select("PropertySetRef/text()", node),
psrText = textnode[0] && textnode[0].data;
if ( ! psrText) {
addMessage(textnode[0].lineNumber, textnode[0].columnNumber,
"The text of the PropertySetRef element must not be empty");
}
let r = pluginUtil.validateTemplate(psrText);
if ( r) {
addMessage(textnode[0].lineNumber, textnode[0].columnNumber,
`The text of the PropertySetRef element must be a valid message template (${r})`);
}
else {
r = pluginUtil.validatePropertySetRef(psrText);
if (r) {
addMessage(textnode[0].lineNumber, textnode[0].columnNumber,
`Error in the text of the PropertySetRef element: ${r}`);
}
}
}
});

if (found.Template > 1) {
addNodeMessage("There is more than one Template element");
if (found.Name > 1) {
addNodeMessage("There is more than one Name element");
} else if (found.Name == 0) {
addNodeMessage("There is no Name element");
}

if (found.Ref > 1) {
addNodeMessage("There is more than one Ref element");
} else if (found.Ref == 1) {
const textnode = xpath.select("Ref/text()", node),
refText = textnode[0] && textnode[0].data;
if (refText && (refText.includes("{") || refText.includes("}"))) {
addMessage(
textnode[0].lineNumber,
textnode[0].columnNumber,
"The text of the Ref element must be a variable name, should not include curlies",
);
}
}

if (found.PropertySetRef > 1) {
addNodeMessage("There is more than one PropertySetRef element");
} else if (found.PropertySetRef == 1) {
const textnode = xpath.select("PropertySetRef/text()", node),
psrText = textnode[0] && textnode[0].data;
if (!psrText) {
addMessage(
textnode[0].lineNumber,
textnode[0].columnNumber,
"The text of the PropertySetRef element must not be empty",
);
}
let r = pluginUtil.validateTemplate(psrText);
if (r) {
addMessage(
textnode[0].lineNumber,
textnode[0].columnNumber,
`The text of the PropertySetRef element must be a valid message template (${r})`,
);
} else {
r = pluginUtil.validatePropertySetRef(psrText);
if (r) {
addMessage(
textnode[0].lineNumber,
textnode[0].columnNumber,
`Error in the text of the PropertySetRef element: ${r}`,
);
}
else if (found.Template == 1) {
const textnode = xpath.select("Template/text()", node),
templateText = textnode[0] && textnode[0].data;
if (templateText) {
const r = pluginUtil.validateTemplate(templateText);
if ( r ) {
addMessage(textnode[0].lineNumber, textnode[0].columnNumber,
r.error);
}
}
}
}

if (found.Template > 1) {
addNodeMessage("There is more than one Template element");
} else if (found.Template == 1) {
const textnode = xpath.select("Template/text()", node),
templateText = textnode[0] && textnode[0].data;
if (templateText) {
const r = pluginUtil.validateTemplate(templateText);
if (r) {
addMessage(
textnode[0].lineNumber,
textnode[0].columnNumber,
"error: " + util.format(r),
);
}
}
}

if (found.Value > 1) {
addNodeMessage("There is more than one Value element");
}
if (found.Value > 1) {
addNodeMessage("There is more than one Value element");
}

let foundAny = 0;
let key;
for( key in found ) {
if( found[key] && key != 'Name') {
foundAny += found[key];
}
}
if( foundAny === 0) {
addNodeMessage( `You should have at least one of: {${foundKeysStr}}`);
}
let foundAny = 0;
let key;
for (key in found) {
if (found[key] && key != "Name") {
foundAny += found[key];
}
});
}
else {
debug(`${policy.fileName} found no AssignVariable elements`);
}
if (foundAny === 0) {
addNodeMessage(
`You should have at least one of: {${foundKeysStr}}`,
);
}
}
}
if (typeof(cb) == 'function') {
cb(null, flagged);
}
};
});
} else {
debug(`${policy.fileName} found no AssignVariable elements`);
}
}
if (typeof cb == "function") {
cb(null, flagged);
}
};

module.exports = {
plugin,
onBundle,
onPolicy
onPolicy,
};
Loading

0 comments on commit 03b2e22

Please sign in to comment.