Skip to content

Commit cf72d4c

Browse files
Merge pull request #543 from DinoChiesa/class-refactor
Class refactor - no new function
2 parents 917ead7 + 40b4f24 commit cf72d4c

13 files changed

+1651
-1892
lines changed

lib/package/Bundle.js

Lines changed: 362 additions & 367 deletions
Large diffs are not rendered by default.

lib/package/Condition.js

Lines changed: 31 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2019, 2023 Google LLC
2+
Copyright 2019, 2023, 2025 Google LLC
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -14,78 +14,46 @@
1414
limitations under the License.
1515
*/
1616

17-
const TruthTable = require("./TruthTable.js");
17+
const TruthTable = require("./TruthTable.js"),
18+
ConfigElement = require("./ConfigElement.js");
1819

19-
function Condition(element, parent) {
20-
this.parent = parent;
21-
this.element = element;
22-
}
20+
class Condition extends ConfigElement {
21+
constructor(element, parent) {
22+
super(element, parent);
23+
}
2324

24-
Condition.prototype.getExpression = function () {
25-
return (
25+
getExpression = () =>
2626
(this.element.childNodes &&
2727
this.element.childNodes[0] &&
2828
this.element.childNodes[0].nodeValue) ||
29-
""
30-
);
31-
};
32-
33-
Condition.prototype.getMessages = function () {
34-
return this.parent.getMessages();
35-
};
36-
37-
Condition.prototype.getElement = function () {
38-
return this.element;
39-
};
40-
41-
Condition.prototype.getParent = function () {
42-
return this.parent;
43-
};
44-
45-
Condition.prototype.getLines = function (start, stop) {
46-
return this.parent.getLines(start, stop);
47-
};
48-
49-
Condition.prototype.getSource = function () {
50-
if (!this.source) {
51-
const start = this.element.lineNumber - 1,
52-
stop = this.element.nextSibling.lineNumber - 1;
53-
this.source = this.getLines(start, stop);
29+
"";
30+
31+
getTruthTable() {
32+
if (!this.truthTable) {
33+
this.truthTable = new TruthTable(
34+
this.getExpression().replace(/(\n| +)/g, " "),
35+
);
36+
}
37+
return this.truthTable;
5438
}
55-
return this.source;
56-
};
5739

58-
Condition.prototype.getTruthTable = function () {
59-
if (!this.truthTable) {
60-
this.truthTable = new TruthTable(
61-
this.getExpression().replace(/(\n| +)/g, " ")
62-
);
40+
onConditions(pluginFunction, cb) {
41+
pluginFunction(this, cb);
6342
}
64-
return this.truthTable;
65-
};
6643

67-
Condition.prototype.addMessage = function (msg) {
68-
if (!msg.hasOwnProperty("entity")) {
69-
msg.entity = this;
44+
summarize() {
45+
const summary = {
46+
type: "Condition",
47+
condition: this.getExpression(),
48+
};
49+
try {
50+
summary.truthTable = this.getTruthTable();
51+
} catch (e) {
52+
//just swallow it
53+
}
54+
return summary;
7055
}
71-
this.parent.addMessage(msg);
72-
};
73-
74-
Condition.prototype.onConditions = function (pluginFunction, cb) {
75-
pluginFunction(this, cb);
76-
};
77-
78-
Condition.prototype.summarize = function () {
79-
const summary = {};
80-
summary.type = "Condition";
81-
summary.condition = this.getExpression();
82-
try {
83-
summary.truthTable = this.getTruthTable();
84-
} catch (e) {
85-
//just swallow it
86-
}
87-
return summary;
88-
};
56+
}
8957

9058
//Public
9159
module.exports = Condition;

lib/package/ConfigElement.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
Copyright © 2025 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
const xpath = require("xpath"),
18+
debug = require("debug")("apigeelint:ConfigElement");
19+
20+
class ConfigElement {
21+
constructor(element, parent) {
22+
this.parent = parent;
23+
this.element = element;
24+
this.bundle = false;
25+
}
26+
27+
select(xs) {
28+
return xpath.select(xs, this.element);
29+
}
30+
31+
getElement() {
32+
return this.element;
33+
}
34+
getName() {
35+
return "unset";
36+
}
37+
38+
getParent() {
39+
debug(`> getParent name=${this.getName()}`);
40+
return this.parent;
41+
}
42+
43+
getSource() {
44+
if (!this.source) {
45+
var start = this.element.lineNumber - 1,
46+
stop =
47+
(this.element.nextSibling &&
48+
this.element.nextSibling.lineNumber - 1) ||
49+
this.element.lastChild.lineNumber;
50+
this.source = this.getLines(start, stop);
51+
}
52+
return this.source;
53+
}
54+
55+
getLines(start, stop) {
56+
return this.parent.getLines(start, stop);
57+
}
58+
59+
addMessage(msg) {
60+
if (!msg.hasOwnProperty("entity")) {
61+
msg.entity = this;
62+
}
63+
this.parent.addMessage(msg);
64+
}
65+
66+
// // TODO: determine if this is necessary
67+
// getMessages() {
68+
// return this.parent.getMessages();
69+
// }
70+
71+
getBundle() {
72+
if (this.bundle === false) {
73+
let antecedent = this.parent;
74+
while (antecedent && antecedent.constructor.name != "Bundle") {
75+
antecedent = antecedent.getParent();
76+
}
77+
this.bundle = antecedent;
78+
}
79+
return this.bundle;
80+
}
81+
}
82+
83+
//Public
84+
module.exports = ConfigElement;

0 commit comments

Comments
 (0)