Skip to content

Commit 4195f9d

Browse files
Merge pull request #549 from DinoChiesa/pd006-hpc-fix
fix: correct PD006 to check for existence of HTTPProxyConnection
2 parents 0343a51 + 1103c9a commit 4195f9d

11 files changed

+177
-170
lines changed

lib/package/plugins/BN012-unreferencedTargets.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const plugin = {
2626
fatal: false,
2727
severity: 1, //1=warn, 2=error
2828
nodeType: "Bundle",
29-
enabled: true
29+
enabled: true,
3030
};
3131

3232
const onBundle = function (bundle, cb) {
@@ -52,7 +52,7 @@ const onBundle = function (bundle, cb) {
5252
});
5353
});
5454
const unreferenced = Object.keys(targets).filter(
55-
(key) => targets[key].count == 0
55+
(key) => targets[key].count == 0,
5656
);
5757
if (unreferenced.length) {
5858
try {
@@ -63,11 +63,11 @@ const onBundle = function (bundle, cb) {
6363
plugin,
6464
//entity: targets[tepname].ep,
6565
message: `Unreferenced TargetEndpoint ${tepname}. There are no RouteRules that reference this TargetEndpoint.`,
66-
line: 0
66+
line: 0,
6767
});
6868
});
6969
} catch (e) {
70-
console.log("Exception: " + e);
70+
console.log("exception in BN012: " + e);
7171
}
7272
}
7373
}
@@ -78,5 +78,5 @@ const onBundle = function (bundle, cb) {
7878

7979
module.exports = {
8080
plugin,
81-
onBundle
81+
onBundle,
8282
};

lib/package/plugins/PD001-missingTargets.js

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -15,74 +15,72 @@
1515
*/
1616

1717
const ruleId = require("../lintUtil.js").getRuleId(),
18-
debug = require("debug")("apigeelint:" + ruleId);
18+
debug = require("debug")("apigeelint:" + ruleId);
1919

2020
const plugin = {
21-
ruleId,
22-
name: "Missing targets",
23-
message: "Check RouteRules in a ProxyEndpoint to ensure that the targets are known.",
24-
fatal: false,
25-
severity: 2, // 1 == warn, 2 == error
26-
nodeType: "RouteRule",
27-
enabled: true
28-
};
21+
ruleId,
22+
name: "Missing targets",
23+
message:
24+
"Check RouteRules in a ProxyEndpoint to ensure that the targets are known.",
25+
fatal: false,
26+
severity: 2, // 1 == warn, 2 == error
27+
nodeType: "RouteRule",
28+
enabled: true,
29+
};
2930

3031
let thisBundle = null;
31-
const onBundle = function(bundle, cb) {
32-
thisBundle = bundle;
33-
if (typeof(cb) == 'function') {
34-
cb(null, false);
35-
}
36-
};
32+
const onBundle = function (bundle, cb) {
33+
thisBundle = bundle;
34+
if (typeof cb == "function") {
35+
cb(null, false);
36+
}
37+
};
3738

38-
const onProxyEndpoint =
39-
function(ep, cb) {
40-
const routeRules = ep.getRouteRules(),
41-
targets = thisBundle.getTargetEndpoints();
42-
let flagged = false;
39+
const onProxyEndpoint = function (ep, cb) {
40+
const routeRules = ep.getRouteRules(),
41+
targets = thisBundle.getTargetEndpoints();
42+
let flagged = false;
4343

44-
if (routeRules) {
45-
//let util = require('util');
44+
if (routeRules) {
45+
//let util = require('util');
4646
debug(`rr.length = ${routeRules.length}`);
47-
routeRules.forEach( (rr, ix) => {
48-
try {
49-
let targetName = rr.getTargetEndpoint();
50-
debug(`rr${ix} = "${targetName}"`);
51-
if (targetName == null) {
52-
// no-op
53-
}
54-
else if (targetName.trim()=== '') {
55-
ep.addMessage({
56-
plugin,
57-
source: rr.getSource(),
58-
line: rr.getElement().lineNumber,
59-
column: rr.getElement().columnNumber,
60-
message: `RouteRule specifies an empty TargetEndpoint.`
61-
});
62-
flagged = true;
63-
}
64-
else if ( ! targets.find( t => t.getName() == targetName)) {
65-
ep.addMessage({
66-
plugin,
67-
source: rr.getSource(),
68-
line: rr.getElement().lineNumber,
69-
column: rr.getElement().columnNumber,
70-
message: `RouteRule refers to an unknown TargetEndpoint (${targetName}).`
71-
});
72-
flagged = true;
73-
}
74-
} catch(exc1) {
75-
console.log('exception: ' + exc1.stack);
47+
routeRules.forEach((rr, ix) => {
48+
try {
49+
let targetName = rr.getTargetEndpoint();
50+
debug(`rr${ix} = "${targetName}"`);
51+
if (targetName == null) {
52+
// no-op
53+
} else if (targetName.trim() === "") {
54+
ep.addMessage({
55+
plugin,
56+
source: rr.getSource(),
57+
line: rr.getElement().lineNumber,
58+
column: rr.getElement().columnNumber,
59+
message: `RouteRule specifies an empty TargetEndpoint.`,
60+
});
61+
flagged = true;
62+
} else if (!targets.find((t) => t.getName() == targetName)) {
63+
ep.addMessage({
64+
plugin,
65+
source: rr.getSource(),
66+
line: rr.getElement().lineNumber,
67+
column: rr.getElement().columnNumber,
68+
message: `RouteRule refers to an unknown TargetEndpoint (${targetName}).`,
69+
});
70+
flagged = true;
7671
}
72+
} catch (exc1) {
73+
console.log("exception in PD001: " + exc1.stack);
74+
}
7775
});
7876
}
79-
if (typeof(cb) == 'function') {
77+
if (typeof cb == "function") {
8078
cb(null, flagged);
8179
}
8280
};
8381

8482
module.exports = {
8583
plugin,
8684
onBundle,
87-
onProxyEndpoint
85+
onProxyEndpoint,
8886
};

lib/package/plugins/PD004-proxyNameAttr.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const plugin = {
2525
fatal: false,
2626
severity: 2, // 1 = warn, 2 = error
2727
nodeType: "Endpoint",
28-
enabled: true
28+
enabled: true,
2929
};
3030

3131
const onProxyEndpoint = function (endpoint, cb) {
@@ -36,15 +36,15 @@ const onProxyEndpoint = function (endpoint, cb) {
3636

3737
try {
3838
debug(
39-
`onProxyEndpoint shortfile(${shortFilename}) nameFromAttr(${nameFromAttribute})`
39+
`onProxyEndpoint shortfile(${shortFilename}) nameFromAttr(${nameFromAttribute})`,
4040
);
4141
if (!nameFromAttribute) {
4242
const rootElement = endpoint.getElement();
4343
endpoint.addMessage({
4444
plugin,
4545
line: rootElement.lineNumber,
4646
column: rootElement.columnNumber,
47-
message: `ProxyEndpoint has no name attribute.`
47+
message: `ProxyEndpoint has no name attribute.`,
4848
});
4949
flagged = true;
5050
} else if (basename !== nameFromAttribute) {
@@ -53,20 +53,20 @@ const onProxyEndpoint = function (endpoint, cb) {
5353
plugin,
5454
line: nameAttr[0].lineNumber,
5555
column: nameAttr[0].columnNumber,
56-
message: `File basename (${basename}) does not match endpoint name (${nameFromAttribute}).`
56+
message: `File basename (${basename}) does not match endpoint name (${nameFromAttribute}).`,
5757
});
5858
flagged = true;
5959
}
6060
if (typeof cb == "function") {
6161
cb(null, flagged);
6262
}
6363
} catch (exc1) {
64-
console.error("exception: " + exc1);
64+
console.error("exception in PD004: " + exc1);
6565
debug(`onProxyEndpoint exc(${exc1})`);
6666
}
6767
};
6868

6969
module.exports = {
7070
plugin,
71-
onProxyEndpoint
71+
onProxyEndpoint,
7272
};

lib/package/plugins/PD006-proxy-endpoint-hygiene.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
limitations under the License.
1515
*/
1616

17-
const path = require("path"),
17+
const path = require("node:path"),
18+
util = require("node:util"),
1819
ruleId = require("../lintUtil.js").getRuleId(),
1920
debug = require("debug")("apigeelint:" + ruleId);
2021

@@ -45,19 +46,22 @@ const onProxyEndpoint = function (endpoint, cb) {
4546
debug(`onProxyEndpoint name(${name})`);
4647
// At the moment, this plugin checks only for BasePath
4748
const hpc = endpoint.getHTTPProxyConnection();
48-
const basepathElts = hpc.select("./BasePath");
49-
if (!basepathElts) {
50-
mark(hpc.getElement(), "Error performing selection.");
51-
} else if (basepathElts.length == 0) {
52-
mark(hpc.getElement(), "Missing required BasePath element.");
53-
} else if (basepathElts.length != 1) {
54-
mark(hpc.getElement(), "More than one BasePath element found.");
49+
if (hpc) {
50+
// a sharedflowbundle will not have an HTTPProxyConnection
51+
const basepathElts = hpc.select("./BasePath");
52+
if (!basepathElts) {
53+
mark(hpc.getElement(), "Error performing selection.");
54+
} else if (basepathElts.length == 0) {
55+
mark(hpc.getElement(), "Missing required BasePath element.");
56+
} else if (basepathElts.length != 1) {
57+
mark(hpc.getElement(), "More than one BasePath element found.");
58+
}
5559
}
5660
if (typeof cb == "function") {
5761
cb(null, flagged);
5862
}
5963
} catch (exc1) {
60-
console.error("exception: " + exc1);
64+
console.error("exception in PD006: " + exc1);
6165
debug(`onProxyEndpoint exc(${exc1})`);
6266
}
6367
};

lib/package/plugins/TD003-targetNameAttr.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const plugin = {
2323
fatal: false,
2424
severity: 2, // 1 = warn, 2 = error
2525
nodeType: "Endpoint",
26-
enabled: true
26+
enabled: true,
2727
};
2828

2929
const path = require("path"),
@@ -37,15 +37,15 @@ const onTargetEndpoint = function (endpoint, cb) {
3737

3838
try {
3939
debug(
40-
`onTargetEndpoint shortfile(${shortFilename}) nameFromAttr(${nameFromAttribute})`
40+
`onTargetEndpoint shortfile(${shortFilename}) nameFromAttr(${nameFromAttribute})`,
4141
);
4242
if (!nameFromAttribute) {
4343
const rootElement = endpoint.getElement();
4444
endpoint.addMessage({
4545
plugin,
4646
line: rootElement.lineNumber,
4747
column: rootElement.columnNumber,
48-
message: `TargetEndpoint has no name attribute.`
48+
message: `TargetEndpoint has no name attribute.`,
4949
});
5050
flagged = true;
5151
} else if (basename !== nameFromAttribute) {
@@ -54,20 +54,20 @@ const onTargetEndpoint = function (endpoint, cb) {
5454
plugin,
5555
line: nameAttr[0].lineNumber,
5656
column: nameAttr[0].columnNumber,
57-
message: `File basename (${basename}) does not match endpoint name (${nameFromAttribute}).`
57+
message: `File basename (${basename}) does not match endpoint name (${nameFromAttribute}).`,
5858
});
5959
flagged = true;
6060
}
6161
if (typeof cb == "function") {
6262
cb(null, flagged);
6363
}
6464
} catch (exc1) {
65-
console.error("exception: " + exc1);
65+
console.error("exception in TD003: " + exc1);
6666
debug(`onTargetEndpoint exc(${exc1})`);
6767
}
6868
};
6969

7070
module.exports = {
7171
plugin,
72-
onTargetEndpoint
72+
onTargetEndpoint,
7373
};

0 commit comments

Comments
 (0)