Skip to content

Trim trailing whitespace in TextElements #247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion fluent-syntax/src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { FTLParserStream } from "./ftlstream";
import { ParseError } from "./errors";


const trailingWSRe = /[ \t\n\r]+$/;


function withSpan(fn) {
return function(ps, ...args) {
if (!this.withSpans) {
Expand Down Expand Up @@ -387,7 +390,7 @@ export default class FluentParser {
}
}

return new AST.VariantName(name.trimRight());
return new AST.VariantName(name.replace(trailingWSRe, ""));
}

getDigits(ps) {
Expand Down Expand Up @@ -467,6 +470,12 @@ export default class FluentParser {
}
}

// Trim trailing whitespace.
const lastElement = elements[elements.length - 1];
if (lastElement.type === "TextElement") {
lastElement.value = lastElement.value.replace(trailingWSRe, "");
}

return new AST.Pattern(elements);
}

Expand Down
8 changes: 8 additions & 0 deletions fluent-syntax/test/fixtures_structure/whitespace_leading.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# < whitespace >
key1 = Value

# ↓ nbsp
key2 =   Value

key3 = {""} Value
key4 = {" "}Value
218 changes: 218 additions & 0 deletions fluent-syntax/test/fixtures_structure/whitespace_leading.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
{
"type": "Resource",
"body": [
{
"type": "Message",
"annotations": [],
"id": {
"type": "Identifier",
"name": "key1",
"span": {
"type": "Span",
"start": 21,
"end": 25
}
},
"value": {
"type": "Pattern",
"elements": [
{
"type": "TextElement",
"value": "Value",
"span": {
"type": "Span",
"start": 41,
"end": 46
}
}
],
"span": {
"type": "Span",
"start": 41,
"end": 46
}
},
"attributes": [],
"comment": {
"type": "Comment",
"annotations": [],
"content": " < whitespace >\n",
"span": {
"type": "Span",
"start": 0,
"end": 20
}
},
"span": {
"type": "Span",
"start": 21,
"end": 46
}
},
{
"type": "Message",
"annotations": [],
"id": {
"type": "Identifier",
"name": "key2",
"span": {
"type": "Span",
"start": 65,
"end": 69
}
},
"value": {
"type": "Pattern",
"elements": [
{
"type": "TextElement",
"value": "  Value",
"span": {
"type": "Span",
"start": 75,
"end": 84
}
}
],
"span": {
"type": "Span",
"start": 75,
"end": 84
}
},
"attributes": [],
"comment": {
"type": "Comment",
"annotations": [],
"content": " ↓ nbsp\n",
"span": {
"type": "Span",
"start": 48,
"end": 64
}
},
"span": {
"type": "Span",
"start": 65,
"end": 84
}
},
{
"type": "Message",
"annotations": [],
"id": {
"type": "Identifier",
"name": "key3",
"span": {
"type": "Span",
"start": 86,
"end": 90
}
},
"value": {
"type": "Pattern",
"elements": [
{
"type": "Placeable",
"expression": {
"type": "StringLiteral",
"value": "",
"span": {
"type": "Span",
"start": 94,
"end": 96
}
},
"span": {
"type": "Span",
"start": 93,
"end": 97
}
},
{
"type": "TextElement",
"value": " Value",
"span": {
"type": "Span",
"start": 97,
"end": 111
}
}
],
"span": {
"type": "Span",
"start": 93,
"end": 111
}
},
"attributes": [],
"comment": null,
"span": {
"type": "Span",
"start": 86,
"end": 111
}
},
{
"type": "Message",
"annotations": [],
"id": {
"type": "Identifier",
"name": "key4",
"span": {
"type": "Span",
"start": 112,
"end": 116
}
},
"value": {
"type": "Pattern",
"elements": [
{
"type": "Placeable",
"expression": {
"type": "StringLiteral",
"value": " ",
"span": {
"type": "Span",
"start": 120,
"end": 131
}
},
"span": {
"type": "Span",
"start": 119,
"end": 132
}
},
{
"type": "TextElement",
"value": "Value",
"span": {
"type": "Span",
"start": 132,
"end": 137
}
}
],
"span": {
"type": "Span",
"start": 119,
"end": 137
}
},
"attributes": [],
"comment": null,
"span": {
"type": "Span",
"start": 112,
"end": 137
}
}
],
"span": {
"type": "Span",
"start": 0,
"end": 138
}
}
8 changes: 8 additions & 0 deletions fluent-syntax/test/fixtures_structure/whitespace_trailing.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# < whitespace >
key1 = Value

# ↓ nbsp
key2 = Value  

key3 = Value {placeable}.
key4 = Value{" "}
Loading