Skip to content

Commit 936b046

Browse files
committed
Remove Prettier XML formatting
This caused a ton of churn in the Pathfinder data files, making it hard to validate that there aren't unexpected changes. On reflection, I also don't actually like the Prettier XML formatting very much. Especially the way it breaks inside of markup, and breaks between every HTML attribute. Since it's simple to do reasonable indentation ourselves, I'm just reverting to that approach.
1 parent a97c3d8 commit 936b046

File tree

9 files changed

+89
-157
lines changed

9 files changed

+89
-157
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8-
## Unreleased
8+
<!-- ## Unreleased -->
99

1010
- Fix missing `<xliff>` element in XLIFF output.
11+
- Formatting change to XML output (fewer line breaks).
1112

1213
## [0.2.2] - 2020-05-13
1314

package-lock.json

Lines changed: 5 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@
2828
"prepack": "npm run build"
2929
},
3030
"dependencies": {
31-
"@prettier/plugin-xml": "^0.7.2",
3231
"fs-extra": "^9.0.0",
3332
"glob": "^7.1.6",
3433
"jsonschema": "^1.2.6",
3534
"minimist": "^1.2.5",
3635
"parse5": "^6.0.0",
37-
"prettier": "^2.0.5",
3836
"source-map-support": "^0.5.19",
3937
"typescript": "^3.8.3",
4038
"xmldom": "^0.3.0"
@@ -47,7 +45,6 @@
4745
"@types/minimist": "^1.2.0",
4846
"@types/node": "^14.0.1",
4947
"@types/parse5": "^5.0.2",
50-
"@types/prettier": "^2.0.0",
5148
"@types/xmldom": "^0.1.29",
5249
"@typescript-eslint/eslint-plugin": "^2.30.0",
5350
"@typescript-eslint/parser": "^2.30.0",
@@ -57,6 +54,7 @@
5754
"dir-compare": "^2.3.0",
5855
"eslint": "^7.0.0",
5956
"lit-html": "^1.2.1",
57+
"prettier": "^2.0.5",
6058
"rimraf": "^3.0.2",
6159
"typescript-json-schema": "^0.42.0"
6260
}

src/formatters/xlb.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {ProgramMessage, Message, Bundle, Placeholder} from '../messages';
2020
import {
2121
getOneElementByTagNameOrThrow,
2222
getNonEmptyAttributeOrThrow,
23-
formatXml,
2423
} from './xml-utils';
2524

2625
/**
@@ -147,12 +146,16 @@ class XlbFormatter implements Formatter {
147146
*/
148147
async writeOutput(sourceMessages: ProgramMessage[]): Promise<void> {
149148
const doc = new xmldom.DOMImplementation().createDocument('', '', null);
149+
const indent = (node: Element | Document, level = 0) =>
150+
node.appendChild(doc.createTextNode('\n' + Array(level + 1).join(' ')));
150151
doc.appendChild(
151152
doc.createProcessingInstruction('xml', 'version="1.0" encoding="UTF-8"')
152153
);
154+
indent(doc);
153155
const bundle = doc.createElement('localizationbundle');
154156
bundle.setAttribute('locale', this.config.sourceLocale);
155157
doc.appendChild(bundle);
158+
indent(bundle, 1);
156159
const messagesNode = doc.createElement('messages');
157160
bundle.appendChild(messagesNode);
158161
for (const {name, contents, descStack} of sourceMessages) {
@@ -161,6 +164,7 @@ class XlbFormatter implements Formatter {
161164
if (descStack.length > 0) {
162165
messageNode.setAttribute('desc', descStack.join(' / '));
163166
}
167+
indent(messagesNode, 2);
164168
messagesNode.appendChild(messageNode);
165169
for (const content of contents) {
166170
if (typeof content === 'string') {
@@ -173,11 +177,13 @@ class XlbFormatter implements Formatter {
173177
}
174178
}
175179
}
180+
indent(messagesNode, 1);
181+
indent(bundle);
182+
indent(doc);
176183
const serialized = new xmldom.XMLSerializer().serializeToString(doc);
177-
const formatted = formatXml(serialized);
178184
await fsExtra.writeFile(
179185
this.config.resolve(this.xlbConfig.outputFile),
180-
formatted,
186+
serialized,
181187
'utf8'
182188
);
183189
}

src/formatters/xliff.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {Bundle, Message, ProgramMessage, Placeholder} from '../messages';
2020
import {
2121
getOneElementByTagNameOrThrow,
2222
getNonEmptyAttributeOrThrow,
23-
formatXml,
2423
} from './xml-utils';
2524

2625
/**
@@ -187,10 +186,12 @@ export class XliffFormatter implements Formatter {
187186
}
188187

189188
const doc = new xmldom.DOMImplementation().createDocument('', '', null);
189+
const indent = (node: Element | Document, level = 0) =>
190+
node.appendChild(doc.createTextNode('\n' + Array(level + 1).join(' ')));
190191
doc.appendChild(
191192
doc.createProcessingInstruction('xml', 'version="1.0" encoding="UTF-8"')
192193
);
193-
doc.appendChild(doc.createTextNode('\n'));
194+
indent(doc);
194195

195196
// https://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html#xliff
196197
const xliff = doc.createElement('xliff');
@@ -201,27 +202,32 @@ export class XliffFormatter implements Formatter {
201202
'urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-strict.xsd'
202203
);
203204
doc.appendChild(xliff);
205+
indent(xliff);
204206

205207
// https://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html#file
206208
const file = doc.createElement('file');
207209
xliff.appendChild(file);
210+
file.setAttribute('target-language', targetLocale);
211+
file.setAttribute('source-language', this.config.sourceLocale);
208212
// TODO The spec requires the source filename in the "original" attribute,
209213
// but we don't currently track filenames.
210214
file.setAttribute('original', 'lit-localize-inputs');
211215
// Plaintext seems right, as opposed to HTML, since our translatable
212216
// message text is just text, and all HTML markup is encoded into <ph>
213217
// elements.
214218
file.setAttribute('datatype', 'plaintext');
215-
file.setAttribute('source-language', this.config.sourceLocale);
216-
file.setAttribute('target-language', targetLocale);
219+
indent(file);
217220

221+
// https://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html#body
218222
const body = doc.createElement('body');
219223
file.appendChild(body);
224+
indent(body);
220225

221226
for (const {name, contents: sourceContents, descStack} of sourceMessages) {
222227
// https://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html#trans-unit
223228
const transUnit = doc.createElement('trans-unit');
224229
body.appendChild(transUnit);
230+
indent(transUnit, 1);
225231
transUnit.setAttribute('id', name);
226232

227233
if (descStack.length > 0) {
@@ -245,12 +251,18 @@ export class XliffFormatter implements Formatter {
245251
for (const child of this.encodeContents(doc, translation.contents)) {
246252
target.appendChild(child);
247253
}
254+
indent(transUnit, 1);
248255
transUnit.appendChild(target);
249256
}
257+
indent(transUnit);
258+
indent(body);
250259
}
260+
indent(file);
261+
indent(xliff);
262+
indent(doc);
251263
const serializer = new xmldom.XMLSerializer();
252264
const xmlStr = serializer.serializeToString(doc);
253-
return formatXml(xmlStr);
265+
return xmlStr;
254266
}
255267

256268
/**

src/formatters/xml-utils.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
* rights grant found at http://polymer.github.io/PATENTS.txt
1010
*/
1111

12-
import * as prettier from 'prettier';
13-
1412
/**
1513
* Query the given parent element for a descendent with the given tag name and
1614
* return it, or throw if none or more than one are found.
@@ -46,15 +44,3 @@ export function getNonEmptyAttributeOrThrow(
4644
}
4745
return attribute;
4846
}
49-
50-
/**
51-
* Format the given serialized XML using Prettier.
52-
*/
53-
export function formatXml(xmlStr: string): string {
54-
// TODO(aomarks) Types for the xml-parser plugin.
55-
return prettier.format(xmlStr, ({
56-
plugins: ['@prettier/plugin-xml'],
57-
parser: 'xml',
58-
xmlWhitespaceSensitivity: 'ignore',
59-
} as unknown) as prettier.Options);
60-
}

testdata/xlb/goldens/xlb/en.xlb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
<?xml version="1.0" encoding="UTF-8" ?>
1+
<?xml version="1.0" encoding="UTF-8"?>
22
<localizationbundle locale="en">
33
<messages>
44
<msg name="string">Hello World!</msg>
5-
<msg name="lit">Hello <ph>&lt;b>&lt;i></ph>World!<ph
6-
>&lt;/i>&lt;/b></ph></msg>
5+
<msg name="lit">Hello <ph>&lt;b>&lt;i></ph>World!<ph>&lt;/i>&lt;/b></ph></msg>
76
<msg name="variables_1">Hello <ph>${name}</ph>!</msg>
8-
<msg name="lit_variables_1">Hello <ph>${name}</ph>, click <ph
9-
>&lt;a href="${url}"></ph>here<ph>&lt;/a></ph>!</msg>
7+
<msg name="lit_variables_1">Hello <ph>${name}</ph>, click <ph>&lt;a href="${url}"></ph>here<ph>&lt;/a></ph>!</msg>
108
<msg name="lit_variables_2"><ph>${x}</ph>y<ph>${x}</ph>y<ph>${x}</ph></msg>
119
<msg name="lit_variables_3"><ph>&lt;b>
1210
${x}
Lines changed: 28 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,28 @@
1-
<?xml version="1.0" encoding="UTF-8" ?>
2-
<xliff
3-
version="1.2"
4-
xmlns="http://www.w3.org/2001/XMLSchema-instance"
5-
xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-strict.xsd"
6-
>
7-
<file
8-
original="lit-localize-inputs"
9-
datatype="plaintext"
10-
source-language="en"
11-
target-language="es-419"
12-
>
13-
<body>
14-
<trans-unit id="string">
15-
<source>Hello World!</source>
16-
<target>Hola Mundo!</target>
17-
</trans-unit>
18-
<trans-unit id="lit">
19-
<source>Hello <ph id="0">&lt;b>&lt;i></ph>World!<ph
20-
id="1"
21-
>&lt;/i>&lt;/b></ph></source>
22-
<target>Hola <ph id="0">&lt;b>&lt;i></ph>Mundo!<ph
23-
id="1"
24-
>&lt;/i>&lt;/b></ph></target>
25-
</trans-unit>
26-
<trans-unit id="variables_1">
27-
<source>Hello <ph id="0">${name}</ph>!</source>
28-
<target>Hola <ph id="0">${name}</ph>!</target>
29-
</trans-unit>
30-
<trans-unit id="lit_variables_1">
31-
<source>Hello <ph id="0">${name}</ph>, click <ph
32-
id="1"
33-
>&lt;a href="${url}"></ph>here<ph id="2">&lt;/a></ph>!</source>
34-
<target>Hola <ph id="0">${name}</ph>, clic <ph
35-
id="1"
36-
>&lt;a href="${url}"></ph>aquí<ph id="2">&lt;/a></ph>!</target>
37-
</trans-unit>
38-
<trans-unit id="lit_variables_2">
39-
<source><ph id="0">${x}</ph>y<ph id="1">${x}</ph>y<ph
40-
id="2"
41-
>${x}</ph></source>
42-
</trans-unit>
43-
<trans-unit id="lit_variables_3">
44-
<source><ph id="0">&lt;b>
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<xliff version="1.2" xmlns="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-strict.xsd">
3+
<file target-language="es-419" source-language="en" original="lit-localize-inputs" datatype="plaintext">
4+
<body>
5+
<trans-unit id="string">
6+
<source>Hello World!</source>
7+
<target>Hola Mundo!</target>
8+
</trans-unit>
9+
<trans-unit id="lit">
10+
<source>Hello <ph id="0">&lt;b>&lt;i></ph>World!<ph id="1">&lt;/i>&lt;/b></ph></source>
11+
<target>Hola <ph id="0">&lt;b>&lt;i></ph>Mundo!<ph id="1">&lt;/i>&lt;/b></ph></target>
12+
</trans-unit>
13+
<trans-unit id="variables_1">
14+
<source>Hello <ph id="0">${name}</ph>!</source>
15+
<target>Hola <ph id="0">${name}</ph>!</target>
16+
</trans-unit>
17+
<trans-unit id="lit_variables_1">
18+
<source>Hello <ph id="0">${name}</ph>, click <ph id="1">&lt;a href="${url}"></ph>here<ph id="2">&lt;/a></ph>!</source>
19+
<target>Hola <ph id="0">${name}</ph>, clic <ph id="1">&lt;a href="${url}"></ph>aquí<ph id="2">&lt;/a></ph>!</target>
20+
</trans-unit>
21+
<trans-unit id="lit_variables_2">
22+
<source><ph id="0">${x}</ph>y<ph id="1">${x}</ph>y<ph id="2">${x}</ph></source>
23+
</trans-unit>
24+
<trans-unit id="lit_variables_3">
25+
<source><ph id="0">&lt;b>
4526
${x}
4627
&lt;/b>
4728
&lt;i></ph>
@@ -56,7 +37,7 @@
5637
&lt;b>
5738
${x}
5839
&lt;/b></ph></source>
59-
</trans-unit>
60-
</body>
61-
</file>
40+
</trans-unit>
41+
</body>
42+
</file>
6243
</xliff>

0 commit comments

Comments
 (0)