UNPKG

apigeelint

Version:

Node module to lint and Apigee Edge bundle.

146 lines (128 loc) 5.61 kB
/* Copyright 2019-2021 Google LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ //|   |: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(), debug = require("debug")("apigeelint:" + ruleId); const plugin = { ruleId, name: "AssignVariableUsage", fatal: false, severity: 2, //error nodeType: "AssignMessage", enabled: true }; let profile = "apigee"; const onBundle = function(bundle, cb) { profile = bundle.profile; if (typeof cb == "function") { cb(null, false); } }; const onPolicy = function(policy, cb) { debug( "configuration profile is: " + profile ); let flagged = false; let 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 ); if( profile === "apigeex") { var found = { Name:0, Ref:0, Value:0, Template:0, PropertySetRef:0 }; } else { var found = { Name:0, Ref:0, Value:0, Template:0 }; } // Get the keys after Name var str = Object.keys(found).toString(); var foundKeysStr = str.substring(str.indexOf(",")+1); 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 (found.hasOwnProperty(child.tagName)) { 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) { let textnode = xpath.select("Ref/text()", node), refText = textnode[0].data; if (refText && refText.startsWith('{') && refText.endsWith('}')) { addMessage(textnode[0].lineNumber, textnode[0].columnNumber, "The text of the Ref element must be a variable name, should not be wrapped in curlies."); } } if (found.PropertySetRef > 1) { addNodeMessage("There is more than one PropertySetRef element"); } else if (found.PropertySetRef == 1) { let textnode = xpath.select("PropertySetRef/text()", node), refText = textnode[0].data; if (refText && refText.startsWith('{') && refText.endsWith('}')) { addMessage(textnode[0].lineNumber, textnode[0].columnNumber, "The text of the PropertySetRef element must be a variable name, should not be wrapped in curlies."); } } if (found.Template > 1) { addNodeMessage("There is more than one Template element"); } if (found.Value > 1) { addNodeMessage("There is more than one Value element"); } var foundAny = 0; for( var key in found ) { if( found.hasOwnProperty(key) && key != 'Name') { foundAny += found[key]; } } if( foundAny === 0) { addNodeMessage( "You should have at least one of: {" + foundKeysStr + "}"); } } }); } else { debug(`${policy.fileName} found no AssignVariable elements`); } } if (typeof(cb) == 'function') { cb(null, flagged); } }; module.exports = { plugin, onBundle, onPolicy };