Skip to content

Commit 864634f

Browse files
committed
Backport MessageContext parser changes from fluent 0.6.2
- Correctly parse empty comment lines. (projectfluent#149) - Forbid null attribute and variant values. (part of projectfluent#150)
1 parent 1782ee1 commit 864634f

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

fluent/src/parser.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ class RuntimeParser {
7575

7676
// We don't care about comments or sections at runtime
7777
if (ch === '/' ||
78-
(ch === '#' && [' ', '#'].includes(this._source[this._index + 1]))) {
78+
(ch === '#' &&
79+
[' ', '#', '\n'].includes(this._source[this._index + 1]))) {
7980
this.skipComment();
8081
return;
8182
}
@@ -340,7 +341,9 @@ class RuntimeParser {
340341
this.skipBlankLines();
341342

342343
if (this._source[this._index] !== ' ') {
343-
// No indentation means we're done with this message.
344+
// No indentation means we're done with this message. Callers should check
345+
// if the return value here is null. It may be OK for messages, but not OK
346+
// for terms, attributes and variants.
344347
return firstLineContent;
345348
}
346349

@@ -759,6 +762,10 @@ class RuntimeParser {
759762

760763
const val = this.getPattern();
761764

765+
if (val === null) {
766+
throw this.error('Expected attribute to have a value');
767+
}
768+
762769
if (typeof val === 'string') {
763770
attrs[key] = val;
764771
} else {
@@ -806,11 +813,13 @@ class RuntimeParser {
806813

807814
this.skipInlineWS();
808815

809-
const variant = {
810-
key,
811-
val: this.getPattern()
812-
};
813-
variants[index++] = variant;
816+
const val = this.getPattern();
817+
818+
if (val === null) {
819+
throw this.error('Expected variant to have a value');
820+
}
821+
822+
variants[index++] = {key, val};
814823

815824
this.skipWS();
816825
}

0 commit comments

Comments
 (0)