Skip to content

Commit dbe7134

Browse files
committed
Review comments. Mostly camelCases. And Object.create
This is different to python-fluent, as .equals doesn't ignore ordering of Attributes and Variants anymore. Issue on python-fluent is filed. Added an explicit test to ensure that SomeNode.clone creates the same class.
1 parent 387bc7e commit dbe7134

File tree

4 files changed

+24
-46
lines changed

4 files changed

+24
-46
lines changed

fluent-syntax/src/ast.js

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,12 @@ export class BaseNode {
3333
if (thisVal.length !== otherVal.length) {
3434
return false;
3535
}
36-
// Sort elements of order-agnostic fields to ensure the
37-
// comparison is order-agnostic as well. Annotations should be
38-
// here too but they don't have sorting keys.
39-
if (["attributes", "variants"].indexOf(fieldName) >= 0) {
40-
thisVal.sort(sorting_key_compare);
41-
otherVal.sort(sorting_key_compare);
42-
}
43-
for (let i = 0, ii = thisVal.length; i < ii; ++i) {
44-
if (!scalars_equal(thisVal[i], otherVal[i], ignoredFields)) {
36+
for (let i = 0; i < thisVal.length; ++i) {
37+
if (!scalarsEqual(thisVal[i], otherVal[i], ignoredFields)) {
4538
return false;
4639
}
4740
}
48-
} else if (!scalars_equal(thisVal, otherVal, ignoredFields)) {
41+
} else if (!scalarsEqual(thisVal, otherVal, ignoredFields)) {
4942
return false;
5043
}
5144
}
@@ -62,31 +55,21 @@ export class BaseNode {
6255
}
6356
return value;
6457
}
65-
const clone = Object.create(this);
58+
const clone = Object.create(this.constructor.prototype);
6659
for (const prop of Object.keys(this)) {
6760
clone[prop] = visit(this[prop]);
6861
}
6962
return clone;
7063
}
7164
}
7265

73-
function scalars_equal(thisVal, otherVal, ignoredFields) {
66+
function scalarsEqual(thisVal, otherVal, ignoredFields) {
7467
if (thisVal instanceof BaseNode) {
7568
return thisVal.equals(otherVal, ignoredFields);
7669
}
7770
return thisVal === otherVal;
7871
}
7972

80-
function sorting_key_compare(left, right) {
81-
if (left.sorting_key < right.sorting_key) {
82-
return -1;
83-
}
84-
if (left.sorting_key === right.sorting_key) {
85-
return 0;
86-
}
87-
return 1;
88-
}
89-
9073
/*
9174
* Base class for AST nodes which can have Spans.
9275
*/
@@ -267,7 +250,7 @@ export class Attribute extends SyntaxNode {
267250
this.value = value;
268251
}
269252

270-
get sorting_key() {
253+
get sortingKey() {
271254
return this.id.name;
272255
}
273256
}
@@ -281,7 +264,7 @@ export class Variant extends SyntaxNode {
281264
this.default = def;
282265
}
283266

284-
get sorting_key() {
267+
get sortingKey() {
285268
if (this.key instanceof NumberLiteral) {
286269
return this.key.value;
287270
}

fluent-syntax/src/visitor.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ export class Visitor {
1212
if (!(node instanceof BaseNode)) {
1313
return;
1414
}
15-
const nodename = node.type;
16-
const visit = this[`visit_${nodename}`] || this.generic_visit;
15+
const visit = this[`visit${node.type}`] || this.genericVisit;
1716
visit.call(this, node);
1817
}
1918

20-
generic_visit(node) {
19+
genericVisit(node) {
2120
for (const propname of Object.keys(node)) {
2221
this.visit(node[propname]);
2322
}
@@ -32,12 +31,11 @@ export class Transformer extends Visitor {
3231
if (!(node instanceof BaseNode)) {
3332
return node;
3433
}
35-
const nodename = node.type;
36-
const visit = this[`visit_${nodename}`] || this.generic_visit;
34+
const visit = this[`visit${node.type}`] || this.genericVisit;
3735
return visit.call(this, node);
3836
}
3937

40-
generic_visit(node) {
38+
genericVisit(node) {
4139
for (const propname of Object.keys(node)) {
4240
const propvalue = node[propname];
4341
if (Array.isArray(propvalue)) {

fluent-syntax/test/ast_test.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ suite("BaseNode.equals", function() {
1212
test("Identifier.equals", function() {
1313
const thisNode = new AST.Identifier("name");
1414
const otherNode = new AST.Identifier("name");
15+
assert.ok(thisNode.clone() instanceof AST.Identifier);
1516
assert.strictEqual(thisNode.equals(otherNode), true);
1617
assert.strictEqual(thisNode.equals(thisNode.clone()), true);
1718
assert.notStrictEqual(thisNode, thisNode.clone());
@@ -34,7 +35,7 @@ suite("BaseNode.equals", function() {
3435
]);
3536
assert.strictEqual(thisNode.equals(otherNode), true);
3637
});
37-
test("Variants", function() {
38+
test("Variant order matters", function() {
3839
const thisRes = this.parser.parse(ftl`
3940
msg = { $val ->
4041
[few] things
@@ -43,7 +44,6 @@ suite("BaseNode.equals", function() {
4344
}
4445
`);
4546
const otherRes = this.parser.parse(ftl`
46-
# a comment
4747
msg = { $val ->
4848
[few] things
4949
*[other] default
@@ -53,13 +53,10 @@ suite("BaseNode.equals", function() {
5353
const thisNode = thisRes.body[0];
5454
const otherNode = otherRes.body[0];
5555
assert.strictEqual(thisNode.equals(otherNode), false);
56-
assert.strictEqual(thisNode.equals(otherNode, ['span', 'comment']), true);
57-
assert.strictEqual(thisNode.value.equals(otherNode.value), true);
58-
assert.strictEqual(thisNode.value.equals(otherNode.value, []), false);
5956
assert.strictEqual(thisRes.equals(thisRes.clone(), []), true);
6057
assert.notStrictEqual(thisRes, thisRes.clone());
6158
});
62-
test("Attributes without order", function() {
59+
test("Attribute order matters", function() {
6360
const thisRes = this.parser.parse(ftl`
6461
msg =
6562
.attr1 = one
@@ -72,6 +69,6 @@ suite("BaseNode.equals", function() {
7269
`);
7370
const thisNode = thisRes.body[0];
7471
const otherNode = otherRes.body[0];
75-
assert.strictEqual(thisNode.equals(otherNode), true);
72+
assert.strictEqual(thisNode.equals(otherNode), false);
7673
});
7774
});

fluent-syntax/test/visitor_test.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ suite("Visitor", function() {
2222
this.calls = {};
2323
this.pattern_calls = 0;
2424
}
25-
generic_visit(node) {
25+
genericVisit(node) {
2626
const nodename = node.type;
2727
if (nodename in this.calls) {
2828
this.calls[nodename]++;
2929
} else {
3030
this.calls[nodename] = 1;
3131
}
32-
super.generic_visit(node);
32+
super.genericVisit(node);
3333
}
34-
visit_Pattern(node) {
34+
visitPattern(node) {
3535
this.pattern_calls++;
3636
}
3737
}
@@ -56,16 +56,16 @@ suite("Visitor", function() {
5656
super();
5757
this.word_count = 0;
5858
}
59-
generic_visit(node) {
59+
genericVisit(node) {
6060
switch (node.type) {
6161
case 'Span':
6262
case 'Annotation':
6363
break;
6464
default:
65-
super.generic_visit(node);
65+
super.genericVisit(node);
6666
}
6767
}
68-
visit_TextElement(node) {
68+
visitTextElement(node) {
6969
this.word_count += node.value.split(/\s+/).length;
7070
}
7171
}
@@ -93,17 +93,17 @@ suite("Transformer", function() {
9393
this.before = before;
9494
this.after = after;
9595
}
96-
generic_visit(node) {
96+
genericVisit(node) {
9797
switch (node.type) {
9898
case 'Span':
9999
case 'Annotation':
100100
return node;
101101
break;
102102
default:
103-
return super.generic_visit(node);
103+
return super.genericVisit(node);
104104
}
105105
}
106-
visit_TextElement(node) {
106+
visitTextElement(node) {
107107
node.value = node.value.replace(this.before, this.after);
108108
return node;
109109
}

0 commit comments

Comments
 (0)